【百度大脑新品体验】人体关键点识别

【百度大脑新品体验】人体关键点识别
作者:才能我浪费99

1.功能描述:

检测图像中的人体并返回人体矩形框位置,精准定位21个核心关键点,包含头顶、五官、颈部、四肢主要关节部位,支持多人检测、大动作等复杂场景

2.平台接入

具体接入方式比较简单,可以参考我的另一个帖子,这里就不重复了:
http://ai.baidu.com/forum/topic/show/943327

3.调用攻略(Python3)及评测

3.1首先认证授权:

在开始调用任何API之前需要先进行认证授权,具体的说明请参考:

http://ai.baidu.com/docs#/Auth/top

具体Python3代码如下:

-- coding: utf-8 --

#!/usr/bin/env python

import urllib
import base64
import json
#client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id =【百度云应用的AK】
client_secret =【百度云应用的SK】

#获取token
def get_token():
host = ‘https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=’ + client_id + ‘&client_secret=’ + client_secret
request = urllib.request.Request(host)
request.add_header(‘Content-Type’, ‘application/json; charset=UTF-8’)
response = urllib.request.urlopen(request)
token_content = response.read()
if token_content:
token_info = json.loads(token_content)
token_key = token_info[‘access_token’]
return token_key

3.2人体关键点识别分析接口调用:

详细说明请参考: https://ai.baidu.com/docs#/Body-API/b717c300

说明的比较清晰,这里就不重复了。

大家需要注意的是:
API访问URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/body_analysis
图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M。图片的base64编码是不包含图片头的,如(data:image/jpg;base64,),支持图片格式:jpg、bmp、png,最短边至少50px,最长边最大4096px

Python3调用代码如下:

#画出人体识别结果
def draw_bodys(originfilename,bodys,resultfilename,pointsize):
from PIL import Image, ImageDraw

image_origin = Image.open(originfilename)
draw =ImageDraw.Draw(image_origin)

for body in bodys:
    
    for body_part in body['body_parts'].values():
        #print(body_part)
        draw.ellipse((body_part['x']-pointsize,body_part['y']-pointsize,body_part['x']+pointsize,body_part['y']+pointsize),fill = "blue")
    gesture = body['location'] 
    draw.rectangle((gesture['left'],gesture['top'],gesture['left']+gesture['width'],gesture['top']+gesture['height']),outline = "red")

image_origin.save(resultfilename, "JPEG")

#人体识别
#filename:原图片名(本地存储包括路径)
def body_analysis(filename,resultfilename,pointsize):
request_url = “https://aip.baidubce.com/rest/2.0/image-classify/v1/body_analysis”
print(filename)
# 二进制方式打开图片文件
f = open(filename, ‘rb’)
img = base64.b64encode(f.read())

params = dict()
params['image'] = img
params = urllib.parse.urlencode(params).encode("utf-8")
#params = json.dumps(params).encode('utf-8')

access_token = get_token()
begin = time.perf_counter()
request_url = request_url + "?access_token=" + access_token
request = urllib.request.Request(url=request_url, data=params)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
response = urllib.request.urlopen(request)
content = response.read()
end = time.perf_counter()

print('处理时长:'+'%.2f'%(end-begin)+'秒')
if content:
    #print(content)
    content=content.decode('utf-8')
    print(content)
    data = json.loads(content)
    #print(data)
    #print(data)
    result=data['person_info']
    
    draw_bodys(filename,result,resultfilename,pointsize)

4.功能评测:
选用不同的数据对效果进行测试,具体效果如下(以下例子均来自网上):

单人不同姿态:

在这里插入图片描述

处理时长:0.49秒
person_num: 1

在这里插入图片描述

处理时长:0.46秒
person_num: 1
在这里插入图片描述

处理时长:0.39秒
person_num: 1

在这里插入图片描述

处理时长:0.44秒
person_num: 1

黑白照片:
在这里插入图片描述

处理时长:0.68秒
person_num: 1

多人照片:
在这里插入图片描述

多人复杂场景:
在这里插入图片描述

处理时长:0.81秒
person_num: 3

将海报内容误识别为人。不过海报的内容好像也是一张人脸的一部分。

最后再来一张多人各种动作都有的:
在这里插入图片描述

处理时长:0.59秒
person_num: 7

可以看到效果非常好,非常准确。

5.测试结论和建议

测试下来,整体识别效果不错。对于人体关键点有较强的识别能力,效果很好,速度也很快。可以广泛的应用于体育健身,娱乐互动,安防监控等场景。

不过对于倒立及多人复杂场景人体关键点的图片,识别率还有提高的空间,希望后续进一步提高。

希望能够增加返回的内容选项,包括身体倾斜度,手臂、腿部与身体的夹角等。方便在实际应用中使用。

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

猜你喜欢

转载自blog.csdn.net/weixin_45449540/article/details/103560364