基于百度AI在ROS上实现人体检测功能

关于百度AI网站上的操作和之前关于人脸识别部分的操作一样,注意要在人体识别部分再重新建立一个应用,并且获得

APP_ID = '××××××'
API_KEY = '×××××××××××'
SECRET_KEY = '××××××××××××××'

这三个关键值。

#! /usr/bin/python
# Copyright (c) 2015, Rethink Robotics, Inc.

# Using this CvBridge Tutorial for converting
# ROS images to OpenCV2 images
# http://wiki.ros.org/cv_bridge/Tutorials/ConvertingBetweenROSImagesAndOpenCVImagesPython

# Using this OpenCV2 tutorial for saving Images:
# http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html

# rospy for the subscriber
from PIL import Image
from std_msgs.msg import String
from aip import AipFace
import base64
import PIL.Image
import rospy
# ROS Image message
from sensor_msgs.msg import Image
# ROS Image message -> OpenCV2 image converter
from cv_bridge import CvBridge, CvBridgeError
# OpenCV2 for saving an image
import cv2
from aip import AipBodyAnalysis

APP_ID = '×××××××××××'
API_KEY = '×××××××××××××××××××××××'
SECRET_KEY = '×××××××××××××××××××××××××××'

client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)

# Instantiate CvBridge
bridge = CvBridge()

def image_callback(msg):
    try:
        listen()
        # Convert your ROS Image message to OpenCV2
        cv2_img = bridge.imgmsg_to_cv2(msg, "bgr8")
    except CvBridgeError, e:
        print(e)
    else:
        # Save your OpenCV2 image as a jpeg 
        cv2.imwrite('/home/wly/catkin_ws/src/face_men/camera_photo/photo.jpeg', cv2_img)
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()
def listen():
    image = get_file_content('/home/wly/catkin_ws/src/face_men/camera_photo/photo.jpeg')
    a = client.bodyAnalysis(image);
    print(a)
def main():
    rospy.init_node('image_listener')
    # Define your image topic
    image_topic = "/usb_cam/image_raw"
    # Set up your subscriber and define its callback
    rospy.Subscriber(image_topic, Image, image_callback)
    print("Received an image!")
    # Spin until ctrl + c
    rospy.spin()

if __name__ == '__main__':
    main()

a = client.bodyAnalysis(image);

此部分调用bodyAnalysis()函数,返回人体关键点的一些信息。

字段 是否必选 类型 说明
person_num uint32 人体数目
person_info object[] 人体姿态信息
+body_parts object 身体部位信息,包含21个关键点
++top_head object 头顶
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++left_eye object 左眼
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++right_eye object 右眼
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++nose object 鼻子
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++left_ear object 左耳
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++right_ear object 右耳
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++left_mouth_corner object 左嘴角
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++right_mouth_corner object 右嘴角
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++neck object 颈部
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++left_shoulder object 左肩
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++right_shoulder object 右肩
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++left_elbow object 左手肘
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++right_elbow object 右手肘
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++left_wrist object 左手腕
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++right_wrist object 右手腕
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++left_hip object 左髋部
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++right_hip object 右髋部
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++left_knee object 左膝
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++right_knee object 右膝
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++left_ankle object 左脚踝
+++x float x坐标
+++y float y坐标
+++score float 概率分数
++right_ankle object 右脚踝
+++x float x坐标
+++y float y坐标
+++score float 概率分数
+location object 人体坐标信息
++height float 人体区域的高度
++left float 人体区域离左边界的距离
++top float 人体区域离上边界的距离
++width float 人体区域的宽度
++score float 人体框的概率分数
log_id uint64 唯一的log id,用于问题定位

还有一些其他函数:bodyAttr()函数

对于输入的一张图片(可正常解码,且长宽比适宜),检测图像中的所有人体并返回每个人体的矩形框位置,识别人体的静态属性和行为,共支持20余种属性,包括:性别、年龄阶段、衣着(含类别/颜色)、是否戴帽子、是否戴眼镜、是否背包、是否使用手机、身体朝向等

序号 属性 接口字段 输出项说明
1 性别 gender 男性、女性、不确定
2 年龄阶段 age 幼儿、青少年、青年、中年、老年、不确定
3 上身服饰 upper_wear 长袖、短袖、不确定
4 下身服饰 lower_wear 长裤、短裤、长裙、短裙、不确定
5 上身服饰颜色 upper_color 红、橙、黄、绿、蓝、紫、粉、黑、白、灰、棕、不确定
6 下身服饰颜色 lower_color 红、橙、黄、绿、蓝、紫、粉、黑、白、灰、棕、不确定
7 上身服饰纹理 upper_wear_texture 纯色、图案、碎花、条纹或格子、不确定
8 背包 bag 无背包、单肩包、双肩包、不确定
9 上身服饰细分类 upper_wear_fg T恤、无袖、衬衫、西装、毛衣、夹克、羽绒服、风衣、外套、不确定
10 是否戴帽子 headwear 无帽、普通帽、安全帽、不确定
11 是否戴眼镜 glasses 戴眼镜、戴墨镜、无眼镜、不确定
12 是否撑伞 umbrella 撑伞、无撑伞、不确定
13 是否使用手机 cellphone 未使用手机、使用手机、不确定
14 身体朝向 orientation 正面、背面、左侧面、右侧面、不确定
15 是否吸烟 smoke 吸烟、未吸烟、不确定
16 是否有手提物 carrying_item 无手提物、有手提物、不确定
17 交通工具 vehicle 无交通工具、骑摩托车、骑自行车、骑三轮车、不确定
18 上方截断 upper_cut 无上方截断、有上方截断、不确定
19 下方截断 lower_cut 无下方截断、有下方截断、不确定
20 遮挡 occlusion 无遮挡、轻度遮挡、重度遮挡、不确定
21 是否是正常人体 is_human 非正常人体、正常人体、不确定;正常人体:身体露出大于二分之一的人体,一般以能看到腰部肚挤眼为标准;非正常人体:严重截断、严重遮挡的人体,一般是看不到肚挤眼的,比如只有个脑袋、一条腿

其他函数参见:https://ai.baidu.com/docs#/BodyAnalysis-Python-SDK/top

发布了90 篇原创文章 · 获赞 37 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_25368751/article/details/102476892