Search code examples
pythonformularealsense

Calculating distance from Intel Realsense R200 Depth Camera


I have been trying to calculate the distances of objects with values of R200 camera. I installed PyRealsense and librealsense(legacy). And examples of PyRealsense are working without any problem.

I have created a code for this purpose:

import pyrealsense as pyrs
from pyrealsense.constants import rs_option
depth_stream = pyrs.stream.DepthStream()
infrared_stream = pyrs.stream.InfraredStream()

with pyrs.Service() as serv:
    with serv.Device(streams=(depth_stream, infrared_stream, )) as dev:
        #dev.apply_ivcam_preset(0)
        while True:
            dev.wait_for_frames()

            print(dev.infrared) 

It returns a matrix that values changing with depending on the position of the object:

 [37 37 39 ... 20 20 21]
 [35 35 38 ... 17 18 19]
 [34 33 37 ... 19 20 20]]
[[40 36 30 ... 16 15 17]
 [40 37 28 ... 14 14 19]
 [42 39 28 ... 14 16 20]

Which column of this matrix is represent distance value or what should I do to calculate the distance.


Solution

  • While searching on Google, I have found an example for calculatin distance with RealSense camera:

    https://github.com/intel/intel-iot-refkit/blob/master/meta-refkit-extra/doc/computervision.rst

    I had to edit it to make it work with PyRealSense 2.0:

    #!/usr/bin/python3
    
    import sys
    
    import numpy as np
    import cv2
    import pyrealsense as pyrs
    
    with pyrs.Service() as serv:
        serv.start()
        with serv.Device() as cam:
            cat_cascade = cv2.CascadeClassifier("/usr/share/opencv/haarcascades/haarcascade_frontalcatface.xml")
    
            for x in range(30):
                # stabilize exposure
                cam.wait_for_frames()
    
            while True:
            # get image from web cam
                cam.wait_for_frames()
                img = cam.color
    
                cats = cat_cascade.detectMultiScale(img)
                for (x,y,w,h) in cats:
                    # find center
                    cx = int(round(x+(w/2)))
                    cy = int(round(y+(h/2)))
    
                    depth = cam.depth[cy][cx]
    
                    print("Cat found, distance " + str(depth/10.0) + " cm")
    

    It calculates distance when it shows a cat face. I have started to learn Tensorflow and my knowledge about OpenCV is poor. Can you explain me, what is the easiest way to port this code to TensorFlow or CAFFE.