通過(guò)普通攝像頭拍攝出的照片來(lái)進(jìn)行識(shí)別是存在很大的困難的,但是有困難才能找到更好的方法去解決。在百度上大致找了一下手語(yǔ)識(shí)別的案例,很少。API只是看到了Face++發(fā)布的手勢(shì)識(shí)別,在我寫文章的時(shí)候又看到了百度發(fā)布的手勢(shì)識(shí)別API,之后會(huì)嘗試去進(jìn)行使用。
這次使用的是Face++的API,F(xiàn)ace++的API是在之前發(fā)現(xiàn)的,功能上的話還是比較強(qiáng)大的,但是沒(méi)有離線版本,需要將數(shù)據(jù)進(jìn)行上傳,然后對(duì)JSON進(jìn)行解析得到結(jié)果。

這是官網(wǎng)給出的一個(gè)Demo,識(shí)別率挺不錯(cuò)的,最后給出的是一個(gè)在20種手勢(shì)上的分布概率,接下來(lái)我們自己調(diào)用一下API分析自己的手勢(shì)。
1. 查看官方的API。找到Gesture API,先看一下是怎么說(shuō)的。

調(diào)用參數(shù):


官方還給出了一些調(diào)用錯(cuò)誤返回的參數(shù)的說(shuō)明,有興趣的可以去官網(wǎng)看一下。
還給出了一個(gè)使用命令行調(diào)用API的實(shí)例:

從實(shí)例上不難看出,向 https://api-cn.faceplusplus.com/humanbodypp/beta/gesture 發(fā)送請(qǐng)求,默認(rèn)的參數(shù)有 api_key,api_secret,image_file。api_key和api_secret可以通過(guò)控制臺(tái)進(jìn)行生成。

接下來(lái)開(kāi)始寫代碼的調(diào)用,Python版本的,其他版本的類似。
我們將API封裝成一個(gè)類 Gesture:

將其中的key和secret替換成自己的就可以使用:
'''# -*- coding:utf-8 -*-@author: TulLing'''import requests from json import JSONDecoder gesture_englist = ['big_v','fist','double_finger_up','hand_open','heart_d','index_finger_up','ok','phonecall','palm_up','rock','thumb_down','thumb_up','victory']gesture_chinese = ["我最帥", "拳頭,停下", "我發(fā)誓", "數(shù)字5", "比心", "數(shù)字1", "好的呢,OK", "打電話", "手心向上", "愛(ài)你,520", "差評(píng),不好的", "好評(píng),Good,很棒", "勝利,開(kāi)心"]# 將字典排序def sort_dict(adict): return sorted(adict.items(),key= lambda item:item[1]) class Gesture(object): def __init__(self): self.http_url = 'https://api-cn.faceplusplus.com/humanbodypp/beta/gesture' self.key = '*****' self.secret = '******' self.data = {"api_key":self.key,"api_secret":self.secret} # 獲取手勢(shì)信息 def get_info(self,files): response = requests.post(self.http_url,data=self.data,files=files) req_con = response.content.decode('utf-8') req_dict = JSONDecoder().decode(req_con) #print(req_dict) if('error_message' not in req_dict.keys()) and (len(req_dict['hands'])): # 獲取 hands_dict = req_dict['hands'] #print(type(hands_dict)) # 獲取到手的矩形的字典 gesture_rectangle_dict = hands_dict[0]['hand_rectangle'] # 獲取到手勢(shì)的字典 gesture_dict = hands_dict[0]['gesture'] return gesture_dict,gesture_rectangle_dict else: return [],[]; # 獲取到手勢(shì)文本信息 def get_text(self,index): return gesture_chinese[index] # 獲取到手勢(shì)對(duì)應(yīng)的概率 def get_pro(self,gesture_dict,index): # print(gesture_dict) if(gesture_dict is None or gesture_dict == []): return 0 return gesture_dict[gesture_englist[index]] # 獲取到手勢(shì)的位置 def get_rectangle(self,gesture_rectangle_dict): if(gesture_rectangle_dict is None or gesture_rectangle_dict == []): return (0,0,0,0) x = gesture_rectangle_dict['top'] y = gesture_rectangle_dict['left'] width = gesture_rectangle_dict['width'] height = gesture_rectangle_dict['height'] return (x,y,width,height)
新聞熱點(diǎn)
疑難解答
圖片精選