当前位置:   article > 正文

【ROS-4】视觉停障--ZED2_/zed2i/zed_node/depth/depth_registered

/zed2i/zed_node/depth/depth_registered

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

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

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

  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. sys.path.append('../../../devel/lib/python2.7/dist-packages/darknet_ros_msgs/')
  5. from darknet_ros_msgs.msg import BoundingBoxes, ObjectCount
  6. from sensor_msgs.msg import Image, CompressedImage, CameraInfo
  7. import rospy
  8. import cv2
  9. import numpy as np
  10. from cv_bridge import CvBridge
  11. x = 0
  12. y = 0
  13. len_box = 0
  14. objectcount = 0
  15. def boxdata(data):
  16. global x, y, len_box
  17. box = data.bounding_boxes
  18. len_box = len(box)
  19. # print(len_box)
  20. for i in range(len_box):
  21. xmin = box[i].xmin
  22. xmax = box[i].xmax
  23. ymin = box[i].ymin
  24. ymax = box[i].ymax
  25. # center
  26. x = xmin + (xmax - xmin)/2
  27. y = ymin + (ymax - ymin)/2
  28. def depthdata(data):
  29. bridge = CvBridge()
  30. # 深度图
  31. depth_image = bridge.imgmsg_to_cv2(data)
  32. depth_image = np.array(depth_image, dtype=np.float)
  33. depth_image = depth_image * 1000
  34. depth_image = np.round(depth_image).astype(np.uint16)
  35. # print(len_box)
  36. if objectcount == 0:
  37. distance = 0
  38. # print("------------------------")
  39. else:
  40. real_z = depth_image[y, x] * 0.001
  41. # real_x = (x - ppx)/fx * real_z
  42. # real_y = (y - ppy)/fy * real_z
  43. # rospy.loginfo("potion:x=%f,y=%f,z=%f", real_x, real_y, real_z)
  44. distance = round(real_z, 2)
  45. print(distance)
  46. # rospy.loginfo("distance:", distance)
  47. if 0.5 < distance < 2:
  48. print("detect obstacle! distance =", distance)
  49. # 显示
  50. cv2.imshow('depth', depth_image)
  51. cv2.waitKey(2)
  52. def countdata(data):
  53. global objectcount
  54. objectcount = data.count
  55. if __name__ == '__main__':
  56. global fx, fy, ppx, ppy
  57. # fx = 261.3017
  58. # fy = 261.3017
  59. # ppx = 321.8623
  60. # ppy = 180.1331
  61. rospy.init_node('listener')
  62. rospy.Subscriber('/darknet_ros/bounding_boxes', BoundingBoxes, boxdata, queue_size=1)
  63. rospy.Subscriber('/zed2/zed_node/depth/depth_registered', Image, depthdata, queue_size=1)
  64. rospy.Subscriber('/darknet_ros/found_object', ObjectCount, countdata, queue_size=1)
  65. rospy.spin()

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/384736
推荐阅读
相关标签
  

闽ICP备14008679号