x1. 本次例程使用的手势识别是基于百度智能云平台的服务,该服务每天有50000 次免费使用的机会,仅供学习,勿用商业用途,如果需要长期使用请购买相关服务,本公司概不负责。
2. 申请 API 说明。官方 API 地址参考:https://cloud.baidu.com/doc/BODY/s/ajwvxyyn0。用浏览器打开官方API 地址后,在右上方登录百度账号,如果没有百度账号请先注册。
登录完成后,点击菜单栏上的‘产品’-‘人工智能’-‘人脸与人体识别’-‘人体分析’。
点击产品列表中的‘手势识别’。
5.点击立即使用
接下来会自动进入人体分析的后台管理,点击创建应用
填写相关信息后点击立即创建,创建完成后点击应用列表就可以看到刚刚创建的应用的信息,此时把 AppID、API Key 和 Secret Key 下的信息都复制保存起来。
注意:这里需要去免费领取识别额度,领取那里可以把所以功能都选上
手势识别支持的手势及示例图:
API 函数
xxxxxxxxxx
from aip import AipBodyAnalysis
""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'
client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content('example.jpg')
""" 调用手势识别 """
Res = client.gesture(image);
手势识别 返回数据参数详情:
手势识别 返回示例:
xxxxxxxxxx
{
"log_id": 4466502370458351471,
"result_num": 2,
"result": [{
"probability": 0.9844077229499817,
"top": 20,
"height": 156,
"classname": "Face",
"width": 116,
"left": 173
},
{
"probability": 0.4679304957389832,
"top": 157,
"height": 106,
"classname": "Heart_2",
"width": 177,
"left": 183
}
}
代码路径:/home/yahboom/Dofbot/6.AI_Visual/4.Pack up for identification.ipynb\
导入百度api,重要的是需要吧秘钥改成自己申请的秘钥。
xxxxxxxxxx
import cv2
import time
import demjson
import pygame
from aip import AipBodyAnalysis
from aip import AipSpeech
from PIL import Image, ImageDraw, ImageFont
import numpy
import ipywidgets.widgets as widgets
# 具体手势请看官方提供 https://ai.baidu.com/ai-doc/BODY/4k3cpywrv
#Please refer to the official information for specific gestures https://ai.baidu.com/ai-doc/BODY/4k3cpywrv
hand={'One':'数字1','Two':'数字2','Three':'数字3','Four':'数字4',
'Five':'数字5', 'Six':'数字6','Seven':'数字7',
'Eight':'数字8','Nine':'数字9','Fist':'拳头','Ok':'OK',
'Prayer':'祈祷','Congratulation':'作揖','Honour':'作别',
'Heart_single':'比心心','Thumb_up':'点赞','Thumb_down':'Diss',
'ILY':'我爱你','Palm_up':'掌心向上','Heart_1':'双手比心1',
'Heart_2':'双手比心2','Heart_3':'双手比心3','Rock':'Rock',
'Insult':'竖中指','Face':'脸'}
# 下面的key要换成自己的
#The key below needs to be replaced with one's own
""" 人体分析 APPID AK SK """
APP_ID = '31069241'
API_KEY = 'pxVueLwdAGX4dafYeLsLdZa1'
SECRET_KEY = 'VsDmfGRlWGqzGhcWowoCT5km4TG4Gylq'
client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
g_camera = cv2.VideoCapture(0)
g_camera.set(3, 640)
g_camera.set(4, 480)
g_camera.set(5, 30) #设置帧率
g_camera.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
g_camera.set(cv2.CAP_PROP_BRIGHTNESS, 40) #设置亮度 -64 - 64 0.0
g_camera.set(cv2.CAP_PROP_CONTRAST, 50) #设置对比度 -64 - 64 2.0
g_camera.set(cv2.CAP_PROP_EXPOSURE, 156) #设置曝光值 1.0 - 5000 156.0
ret, frame = g_camera.read()
摄像头显示组件
xxxxxxxxxx
image_widget = widgets.Image(format='jpeg', width=600, height=500) #设置摄像头显示组件
display(image_widget)
image_widget.value = bgr8_to_jpeg(frame)
主要显示和显示结果程序。
xxxxxxxxxx
try:
while True:
"""1.拍照 """
ret, frame = g_camera.read()
#image = get_file_content('./image.jpg')
""" 2.调用手势识别 """
raw = str(client.gesture(image_widget.value))
text = demjson.decode(raw)
try:
res = text['result'][0]['classname']
except:
# print('识别结果:什么也没识别到哦~' )
# img = cv2ImgAddText(frame, "未识别", 250, 30, (0, 0 , 255), 30)
img = frame
else:
# print('识别结果:' + hand[res])
# img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30)
if res == 'Prayer': # 1 祈祷
print('识别结果:' + hand[res])
img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30)
elif res == 'Thumb_up':# 2 点赞
print('识别结果:' + hand[res])
img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30)
elif res == 'Ok': # 3 OK
print('识别结果:' + hand[res])
img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30)
elif res == 'Heart_single': # 4 单手比心
print('识别结果:' + hand[res])
img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30)
elif res == 'Five': # 5 数字5
print('识别结果:' + hand[res])
img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30)
elif res == "Eight": # 数字8
print('识别结果:' + hand[res])
img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30)
elif res == "Rock": # rock
print('识别结果:' + hand[res])
img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30)
elif res == "Congratulation": # 作揖
print('识别结果:' + hand[res])
img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30)
elif res == "Seven": # 数字7
print('识别结果:' + hand[res])
img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30)
elif res == "Thumb_down": # 拇指向下
print('识别结果:' + hand[res])
img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30)
else:
img = frame
image_widget.value = bgr8_to_jpeg(img)
except KeyboardInterrupt:
print(" Program closed! ")
pass
代码块运行完之后能看到识别的画面。注意,要是不能识别得到百度申请自己的秘钥。