【百度大脑新品体验】手势识别
作者:才能我浪费99
1.功能描述:
识别图片中的手部位置和手势类型,可识别24种常见手势,包括拳头、OK、比心、作揖、作别、祈祷、我爱你、点赞、Diss、Rock、竖中指、数字等
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/43eb40cf
说明的比较清晰,这里就不重复了。
大家需要注意的是:
API访问URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/gesture
图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M。图片的base64编码是不包含图片头的,如(data:image/jpg;base64,),支持图片格式:jpg、bmp、png,最短边至少50px,最长边最大4096px
Python3调用代码如下:
#画出手势识别结果
def draw_gestures(originfilename,gestures,resultfilename):
from PIL import Image, ImageDraw
image_origin = Image.open(originfilename)
draw =ImageDraw.Draw(image_origin)
for gesture in gestures:
draw.rectangle((gesture['left'],gesture['top'],gesture['left']+gesture['width'],gesture['top']+gesture['height']),outline = "red")
draw.text((gesture['left'],gesture['top']), gesture['classname'],"blue")
image_origin.save(resultfilename, "JPEG")
#手势识别
#filename:原图片名(本地存储包括路径)
def gesture(filename,resultfilename):
request_url = “https://aip.baidubce.com/rest/2.0/image-classify/v1/gesture”
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)
result=data['result']
print(result)
draw_gestures(filename,result,resultfilename)
gesture(’…/img/gesture4.jpg’,’…/img/gesture4_result.jpg’)
4.功能评测:
选用不同的数据对效果进行测试,具体效果如下(以下例子均来自网上):
处理时长:0.54秒
[{‘probability’: 0.5360070466995239, ‘top’: 97, ‘height’: 206, ‘classname’: ‘Two’, ‘width’: 170, ‘left’: 7}, {‘probability’: 0.4020222723484039, ‘top’: 104, ‘height’: 137, ‘classname’: ‘Face’, ‘width’: 139, ‘left’: 138}]
处理时长:0.58秒
[{‘probability’: 0.5303723812103271, ‘top’: 28, ‘height’: 267, ‘classname’: ‘Nine’, ‘width’: 334, ‘left’: 28}]
处理时长:0.81秒
[{‘probability’: 0.9833292961120605, ‘top’: 2, ‘height’: 179, ‘classname’: ‘Face’, ‘width’: 161, ‘left’: 168}, {‘probability’: 0.9321802258491516, ‘top’: 232, ‘height’: 132, ‘classname’: ‘Six’, ‘width’: 202, ‘left’: 215}]
处理时长:0.60秒
[{‘probability’: 0.9572300314903259, ‘top’: 148, ‘height’: 150, ‘classname’: ‘Five’, ‘width’: 149, ‘left’: 345}, {‘probability’: 0.9406964778900146, ‘top’: 112, ‘height’: 151, ‘classname’: ‘Five’, ‘width’: 155, ‘left’: 96}, {‘probability’: 0.8912005424499512, ‘top’: 184, ‘height’: 135, ‘classname’: ‘Five’, ‘width’: 111, ‘left’: 13}]
5.测试结论和建议
测试下来,整体识别效果不错。对于手势有较强的识别能力,效果很好,速度也很快。可以广泛的应用于:
智能家居:智能家电、家用机器人、可穿戴、儿童教具等硬件设备,通过用户的手势控制对应的功能,人机交互方式更加智能化、自然化
视频直播:视频直播或者拍照过程中,结合用户的手势(如点赞、比心),实时增加相应的贴纸或特效,丰富交互体验
智能驾驶:将手势识别应用到驾驶辅助系统中,使用手势来控制车内的各种功能、参数,一定程度上解放双眼,将更多的注意力放在道路上,提升驾车安全性
等领域,不过对于多个手势的图片,识别率还有提高的空间,希望后续进一步提高。
同时建议增加打响指的手势识别,这样我们才能做出像钢铁侠那样打个响指拯救世界的效果。
来源:CSDN
作者:百度大脑AI开放平台
链接:https://blog.csdn.net/weixin_45449540/article/details/103560319