File size: 3,627 Bytes
4fd87e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""datacollector controller."""

# You may need to import some classes of the controller module. Ex:
#  from controller import Robot, Motor, DistanceSensor
from controller import Supervisor
import uuid
import numpy as np
import os


# create the Robot instance.
robot = Supervisor()

# get the time step of the current world.
timestep = int(robot.getBasicTimeStep())

# You should insert a getDevice-like function in order to get the
# instance of a device of the robot. Something like:
#  motor = robot.getDevice('motorname')
#  ds = robot.getDevice('dsname')
#  ds.enable(timestep)


rf = robot.getDevice("realsenseD405")
camera = robot.getDevice("camera")

gripper = robot.getDevice("ROBOTIQ 2F-140 Gripper::left finger joint")
     
#= {'left' : robot.getDevice("ROBOTIQ 2F-140 Gripper::left finger joint"),
        #   'right': robot.getDevice("ROBOTIQ 2F-140 Gripper::right finger joint")}
           
gripper.setPosition(float('inf'))



camera.enable(32)
camera.recognitionEnable(32)
camera.enableRecognitionSegmentation()
rf.enable(32)

settle_time = 90
elapsed_time = 0




# Randomize all the nodes that start with "random_"
rootNode = robot.getRoot()
rootChildren = rootNode.getField("children")
n = rootChildren.getCount()
print(n," nodes")

for i in range(n):
    node = rootChildren.getMFNode(i)
    if node.getType()==80: # only for solids
        name=node.getField("name").getSFString()
        if name.startswith("random"):
            v=np.random.rand(3) 
            v = v / np.linalg.norm(v) # create unit vector
            v=[*v,np.random.uniform(-np.pi,+np.pi)] # add random orientation
            node.getField("rotation").setSFRotation(v)
            

# Create paths if they don't exist

path = "../../data/"
directories = ['d','rgb','mask','meta']
for directory in directories:
    directory=path+directory
    if not os.path.exists(directory):
        os.makedirs(directory)
    


# Main loop:
# - perform simulation steps until Webots is stopping the controller
while robot.step(timestep) != -1:
    gripper.setVelocity(-0.3) # keep gripper open
    
    
    
    if(elapsed_time>=settle_time):
        fname = str(uuid.uuid4())
        #fname =""
        
        rf.saveImage(path + 'd/'+ fname + "_depth.png",quality=100)
        camera.saveImage(path + 'rgb/' + fname + "_rgb.png",quality=100)
        camera.saveRecognitionSegmentationImage(path + 'mask/' + fname + "_mask.png",quality=100)
        objects=camera.getRecognitionObjects()
        
        metadata=[]
        for object in objects:

            info = {'id': object.getId(),
                    'file' : fname,
                    'position' : np.array(object.getPosition()).tolist(),
                    'orientation' : np.array(object.getOrientation()).tolist(),
                    'size' : np.array(object.getSize()).tolist(),
                    'positionOnImage' : np.array(object.getPositionOnImage()).tolist(),
                    'sizeOnImage' : np.array(object.getSizeOnImage()).tolist(),
                    'numberOfColors' : object.getNumberOfColors(),
                    'colors' : np.ctypeslib.as_array(object.getColors(),(3,)).tolist(),
                    'model' : object.getModel()}
            metadata.append(info)    

        print(metadata)
        
        import json

        with open(path + 'meta/' +fname+'_meta.json', 'w') as fp:
            json.dump(metadata, fp)
        
        robot.getSelf().restartController()
        robot.simulationResetPhysics()
        robot.simulationReset()
  #      robot.worldReload()
        
    elapsed_time+=1
    pass

# Enter here exit cleanup code.