In [1]:
import sys
sys.path.append('../../build')
import cv2 as cv
import numpy as np
import libry as ry
import time
print(cv.__version__)


4.2.0

In [2]:
#-- Add REAL WORLD configuration and camera
RealWorld = ry.Config()
RealWorld.addFile("../../scenarios/challenge.g")
S = RealWorld.simulation(ry.SimulatorEngine.physx, True)
S.addSensor("camera")

C = ry.Config()
C.addFile('../../scenarios/pandasTable.g')
V = ry.ConfigurationViewer()
V.setConfiguration(C)
cameraFrame = C.frame("camera")

In [3]:
#the focal length
f = 0.895
f = f * 360.
#the relative pose of the camera
# pcl.setRelativePose('d(-90 0 0 1) t(-.08 .205 .115) d(26 1 0 0) d(-1 0 1 0) d(6 0 0 1) ')
fxfypxpy = [f, f, 320., 180.]

In [4]:
points = []
tau = .01

for t in range(300):
    time.sleep(0.01)

    #grab sensor readings from the simulation
    q = S.get_q()
    if t%10 == 0:
        [rgb, depth] = S.getImageAndDepth()  #we don't need images with 100Hz, rendering is slow
        points = S.depthData2pointCloud(depth, fxfypxpy)
        cameraFrame.setPointCloud(points, rgb)
        V.recopyMeshes(C)
        V.setConfiguration(C)
            
        if len(rgb)>0: cv.imshow('OPENCV - rgb', rgb)
        if len(depth)>0: cv.imshow('OPENCV - depth', 0.5* depth)

        if cv.waitKey(1) & 0xFF == ord('q'):
            break
        
    S.step([], tau, ry.ControlMode.none)

In [5]:
cv.destroyAllWindows()

example to grap images from a webcam

(but this only gives you an rgb, not depth) hit 'q' in the window to stop


In [6]:
cap = cv.VideoCapture(0)
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    # Our operations on the frame come here
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    # Display the resulting frame
    if len(gray)>0:cv.imshow('frame',gray)
    if cv.waitKey(1) & 0xFF == ord('q'):
        break


---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-6-7e9d3e85a754> in <module>
      4     ret, frame = cap.read()
      5     # Our operations on the frame come here
----> 6     gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
      7     # Display the resulting frame
      8     if len(gray)>0:cv.imshow('frame',gray)

error: OpenCV(4.2.0) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

In [4]:
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

example to use multiple camera attached to different robot frames


In [ ]:
import os
os._exit(0)

In [1]:
import sys
sys.path.append('../../build')
import libry as ry
RealWorld = ry.Config()
RealWorld.addFile("../../scenarios/challenge.g")
# change the position of the central sensor
f = RealWorld.frame("camera")
f.setPosition(f.getPosition()+[0,0,.5])
# add a frame for the additional camera
f = RealWorld.addFrame("R_gripperCamera", "R_gripper")
f.setRelativePosition([0,.1, 0])
f.setShape(ry.ST.marker, [.5])
S = RealWorld.simulation(ry.SimulatorEngine.physx, True)
S.addSensor("camera") # camera is a pre-existing frame that specifies the intrinsic camera parameter
S.addSensor("Rcamera", "R_gripperCamera", 640, 360, 1.) # R_gripperCamera is a fresh frame - we have to specify intrinsic parameters explicitly
for k in range(5):
    # get images from the wrist
    S.selectSensor("camera")
    [rgb, depth] = S.getImageAndDepth()
    input()
    # get images from the main sensor
    S.selectSensor("Rcamera")
    [rgb, depth] = S.getImageAndDepth()
    input()






---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
~/.local/lib/python3.6/site-packages/ipykernel/kernelbase.py in _input_request(self, prompt, ident, parent, password)
    883             try:
--> 884                 ident, reply = self.session.recv(self.stdin_socket, 0)
    885             except Exception:

~/.local/lib/python3.6/site-packages/jupyter_client/session.py in recv(self, socket, mode, content, copy)
    802         try:
--> 803             msg_list = socket.recv_multipart(mode, copy=copy)
    804         except zmq.ZMQError as e:

~/.local/lib/python3.6/site-packages/zmq/sugar/socket.py in recv_multipart(self, flags, copy, track)
    474         """
--> 475         parts = [self.recv(flags, copy=copy, track=track)]
    476         # have first part already, only loop while more to receive

zmq/backend/cython/socket.pyx in zmq.backend.cython.socket.Socket.recv()

zmq/backend/cython/socket.pyx in zmq.backend.cython.socket.Socket.recv()

zmq/backend/cython/socket.pyx in zmq.backend.cython.socket._recv_copy()

~/.local/lib/python3.6/site-packages/zmq/backend/cython/checkrc.pxd in zmq.backend.cython.checkrc._check_rc()

KeyboardInterrupt: 

During handling of the above exception, another exception occurred:

KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-1-dcd2d246291e> in <module>
     22     S.selectSensor("Rcamera")
     23     [rgb, depth] = S.getImageAndDepth()
---> 24     input()

~/.local/lib/python3.6/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
    857             self._parent_ident,
    858             self._parent_header,
--> 859             password=False,
    860         )
    861 

~/.local/lib/python3.6/site-packages/ipykernel/kernelbase.py in _input_request(self, prompt, ident, parent, password)
    887             except KeyboardInterrupt:
    888                 # re-raise KeyboardInterrupt, to truncate traceback
--> 889                 raise KeyboardInterrupt
    890             else:
    891                 break

KeyboardInterrupt: 

In [ ]: