0

我一直在尝试用 R200 相机的值计算物体的距离。我安装了 PyRealsense 和 librealsense(legacy)。PyRealsense 的示例可以正常工作。

为此,我创建了一个代码:

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) 

它返回一个矩阵,其值根据对象的位置而变化:

 [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]

这个矩阵的哪一列代表距离值或者我应该怎么做来计算距离。

4

1 回答 1

0

在 Google 上搜索时,我发现了一个使用 RealSense 相机计算距离的示例:

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

我必须对其进行编辑以使其与 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")

它在显示猫脸时计算距离。我已经开始学习 Tensorflow,但我对 OpenCV 的了解很差。你能解释一下,将此代码移植到 TensorFlow 或 CAFFE 的最简单方法是什么。

于 2018-06-23T19:31:22.740 回答