【ROS-4】视觉停障--ZED2

基础环境:参考【ROS-3】ROS实现图像目标检测_WXG1011的博客-CSDN博客_ros图像识别

基于ZED2相机,实现视觉停障,只是简单的Demo。

实现思路:实时返回检测框中心的深度值。

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import sys
sys.path.append('../../../devel/lib/python2.7/dist-packages/darknet_ros_msgs/')
from darknet_ros_msgs.msg import BoundingBoxes, ObjectCount
from sensor_msgs.msg import Image, CompressedImage, CameraInfo
import rospy
import cv2
import numpy as np
from cv_bridge import CvBridge


x = 0
y = 0
len_box = 0
objectcount = 0


def boxdata(data):
    global x, y, len_box
    box = data.bounding_boxes
    len_box = len(box)
    # print(len_box)
    for i in range(len_box):
        xmin = box[i].xmin
        xmax = box[i].xmax
        ymin = box[i].ymin
        ymax = box[i].ymax
        # center
        x = xmin + (xmax - xmin)/2
        y = ymin + (ymax - ymin)/2


def depthdata(data):
    bridge = CvBridge()
    # 深度图
    depth_image = bridge.imgmsg_to_cv2(data)
    depth_image = np.array(depth_image, dtype=np.float)
    depth_image = depth_image * 1000
    depth_image = np.round(depth_image).astype(np.uint16)
    # print(len_box)
    if objectcount == 0:
        distance = 0
        # print("------------------------")

    else:
        real_z = depth_image[y, x] * 0.001
        # real_x = (x - ppx)/fx * real_z
        # real_y = (y - ppy)/fy * real_z
        # rospy.loginfo("potion:x=%f,y=%f,z=%f", real_x, real_y, real_z)
        distance = round(real_z, 2)
        print(distance)
        # rospy.loginfo("distance:", distance)
    if 0.5 < distance < 2:
        print("detect obstacle! distance =", distance)
    # 显示
    cv2.imshow('depth', depth_image)
    cv2.waitKey(2)


def countdata(data):
    global objectcount
    objectcount = data.count


if __name__ == '__main__':
    global fx, fy, ppx, ppy
    # fx = 261.3017
    # fy = 261.3017
    # ppx = 321.8623
    # ppy = 180.1331
    rospy.init_node('listener')
    rospy.Subscriber('/darknet_ros/bounding_boxes', BoundingBoxes, boxdata, queue_size=1)
    rospy.Subscriber('/zed2/zed_node/depth/depth_registered', Image, depthdata, queue_size=1)
    rospy.Subscriber('/darknet_ros/found_object', ObjectCount, countdata, queue_size=1)
    rospy.spin()

 测试过程中发现darknet-ros包有个bug,当摄像头从有目标切换到无目标时,bounding_boxes发布的话题不准确,故这里又订阅found_object话题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值