|
"""datacollector controller.""" |
|
|
|
|
|
|
|
from controller import Supervisor |
|
import uuid |
|
import numpy as np |
|
import os |
|
|
|
|
|
|
|
robot = Supervisor() |
|
|
|
|
|
timestep = int(robot.getBasicTimeStep()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rf = robot.getDevice("realsenseD405") |
|
camera = robot.getDevice("camera") |
|
|
|
gripper = robot.getDevice("ROBOTIQ 2F-140 Gripper::left finger joint") |
|
|
|
|
|
|
|
|
|
gripper.setPosition(float('inf')) |
|
|
|
|
|
|
|
camera.enable(32) |
|
camera.recognitionEnable(32) |
|
camera.enableRecognitionSegmentation() |
|
rf.enable(32) |
|
|
|
settle_time = 90 |
|
elapsed_time = 0 |
|
|
|
|
|
|
|
|
|
|
|
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: |
|
name=node.getField("name").getSFString() |
|
if name.startswith("random"): |
|
v=np.random.rand(3) |
|
v = v / np.linalg.norm(v) |
|
v=[*v,np.random.uniform(-np.pi,+np.pi)] |
|
node.getField("rotation").setSFRotation(v) |
|
|
|
|
|
|
|
|
|
path = "../../data/" |
|
directories = ['d','rgb','mask','meta'] |
|
for directory in directories: |
|
directory=path+directory |
|
if not os.path.exists(directory): |
|
os.makedirs(directory) |
|
|
|
|
|
|
|
|
|
|
|
while robot.step(timestep) != -1: |
|
gripper.setVelocity(-0.3) |
|
|
|
|
|
|
|
if(elapsed_time>=settle_time): |
|
fname = str(uuid.uuid4()) |
|
|
|
|
|
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() |
|
|
|
|
|
elapsed_time+=1 |
|
pass |
|
|
|
|
|
|