1. 终端中查看消息格式
➜  ~ rosmsg show vision_msgs/Detection2DArray
std_msgs/Header header 
  uint32 seq
  time stamp
  string frame_id
vision_msgs/Detection2D[] detections
  std_msgs/Header header
    uint32 seq
    time stamp
    string frame_id
  vision_msgs/ObjectHypothesisWithPose[] results
    int64 id
    float64 score
    geometry_msgs/PoseWithCovariance pose
      geometry_msgs/Pose pose
        geometry_msgs/Point position
          float64 x
          float64 y
          float64 z
        geometry_msgs/Quaternion orientation
          float64 x
          float64 y
          float64 z
          float64 w
      float64[36] covariance
  vision_msgs/BoundingBox2D bbox
    geometry_msgs/Pose2D center
      float64 x
      float64 y
      float64 theta
    float64 size_x
    float64 size_y
  sensor_msgs/Image source_img
    std_msgs/Header header
      uint32 seq
      time stamp
      string frame_id
    uint32 height
    uint32 width
    string encoding
    uint8 is_bigendian
    uint32 step
    uint8[] data

2. 变量含义解读:
Detection2DArray
# A list of 2D detections, for a multi-object 2D detector.

Header header

# A list of the detected proposals. A multi-proposal detector might generate
#   this list with many candidate detections generated from a single input.
Detection2D[] detections

Detection2D[] detections
#Defines a 2D detection result.
#
#This is similar to a 2D classification, but includes position information,
#allowing a classification result for a specific crop or image point to
#to be located in the larger image.

Header header

#Class probabilities
ObjectHypothesisWithPose[] results

#2D bounding box surrounding the object.
BoundingBox2D bbox

#The 2D data that generated these results (i.e. region proposal cropped out of
#the image). Not required for all use cases, so it may be empty.
sensor_msgs/Image source_img

参考文档:http://docs.ros.org/api/vision_msgs/html/msg/Detection2DArray.html/

3. 代码实战:(基于Python,实现一个简单的人脸检测):
3.1:订阅话题
def main():
    rospy.init_node('track_node')
    rospy.Subscriber("/objects",Detection2DArray, trcakcallback)
    rospy.spin()   

这里订阅了"/object话题",从tensorflow相关节点中,获得图片信息,作为ROS的消息进行传送PS:
2. 这里使用ROS信息传输,非常占用本地带宽.虽然没有上传到云端.
3. 这里没有使用基于MIT-Racecar的模拟环境,但是可以想象,一秒三四十兆的上传和下载速率.同时开启gazebo模拟激光雷达和相机节点,还有模拟小车模型及运动,以及目标检测相关节点,就已经让我的电脑变得十分卡慢了

3.2: 回调函数
#控制台输出速度和转向参数
def vels(speed,turn):
    return "currently:\tspeed[ %s ]\tturn[ %s ]" % (speed,turn)

def Add_vels(speed_add_once,turn_add_once):
    return "AddOnce:\tSpeed[ %s ]\tTurn[ %s ]" % (speed_add_once,turn_add_once)

def trcakcallback(_msg_): 

    rospy.loginfo("=========Msgs=========")   
    rospy.loginfo(_msg_.header.seq)    
    rospy.loginfo(_msg_.detections[-1].header.frame_id)    
    rospy.loginfo(_msg_.detections[-1].results[-1].id)  
    re_id = _msg_.detections[-1].results[-1].id;
    rospy.loginfo(_msg_.detections[-1].bbox)   
    rospy.loginfo("=========Control=========") 
    if(re_id == "1"):
        control_speed=(_msg_.detections[-1].bbox.size_x-550)/300
        control_turn=(_msg_.detections[-1].bbox.center.x-250)/90
    else:
        print("NO--------------------1")
        control_speed=0
        control_turn=0

    rospy.loginfo(control_speed)   
    rospy.loginfo(control_turn)   

    pub = rospy.Publisher('/cmd_vel', Twist, queue_size=5)

    # 创建并发布twist消息
    twist = Twist()
    twist.linear.x = control_speed; 
    twist.linear.y = 0; 
    twist.linear.z = 0
    twist.angular.x = 0; 
    twist.angular.y = 0; 
    twist.angular.z = control_turn
    pub.publish(twist)

注意: 这里获得的数据格式是"字符串"型的,而不是简单的整数型.
函数说明: 简单的获得目标检测结果,根据目标的位置以及框的大小控制小车运动

GitHub 加速计划 / vi / vision
15.85 K
6.89 K
下载
pytorch/vision: 一个基于 PyTorch 的计算机视觉库,提供了各种计算机视觉算法和工具,适合用于实现计算机视觉应用程序。
最近提交(Master分支:2 个月前 )
e9a32135 8 天前
945bdad7 16 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐