correll's picture
Upload 7 files
4fd87e4
"""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.