toinsson / pyrealsense

Cross-platform ctypes/Cython wrapper to the librealsense library (v1.x)
http://pyrealsense.readthedocs.io
Apache License 2.0
121 stars 46 forks source link

Cant measure distances with PyRealSense #80

Closed mustafaxfe closed 6 years ago

mustafaxfe commented 6 years ago

Hi, I have found some codes and edited which is about calculating measures with realsense r200 cameras. Yesterday I could read the values with my realsense camera. But, today I get just: Cat found, distance 0.0 cm

| Camera Model | R200 | | Firmware Version | 1.0.72.10 | | Operating System & Version | ubilinux 4 (Based on Debian | | librealsense version | v1.12.1 | | pyrealsense version | 2.2 |

My code is:

#!/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")

And its result is: Cat found, distance 65.8 cm Cat found, distance 0.0 cm Cat found, distance 0.0 cm Cat found, distance 0.0 cm Cat found, distance 0.0 cm Cat found, distance 0.0 cm Cat found, distance 134.0 cm Cat found, distance 0.0 cm Cat found, distance 0.0 cm Cat found, distance 0.0 cm Cat found, distance 0.0 cm

Its distance was updating, but now just gives 0. What should I do to fix this issue.

toinsson commented 6 years ago

Does not seem pyrealsense related. Did you consider the pixel you are looking at being invalid? This happens when the camera cannot decide which is the correct distance at that point - more likely if the distance in question is big (~1m?).