通過普通攝像頭拍攝出的照片來進行識別是存在很大的困難的,但是有困難才能找到更好的方法去解決。在百度上大致找了一下手語識別的案例,很少。API只是看到了Face++發布的手勢識別,在我寫文章的時候又看到了百度發布的手勢識別API,之后會嘗試去進行使用。
這次使用的是Face++的API,Face++的API是在之前發現的,功能上的話還是比較強大的,但是沒有離線版本,需要將數據進行上傳,然后對JSON進行解析得到結果。
這是官網給出的一個Demo,識別率挺不錯的,最后給出的是一個在20種手勢上的分布概率,接下來我們自己調用一下API分析自己的手勢。
1. 查看官方的API。找到Gesture API,先看一下是怎么說的。
調用參數:
官方還給出了一些調用錯誤返回的參數的說明,有興趣的可以去官網看一下。
還給出了一個使用命令行調用API的實例:
從實例上不難看出,向 https://api-cn.faceplusplus.com/humanbodypp/beta/gesture 發送請求,默認的參數有 api_key,api_secret,image_file。api_key和api_secret可以通過控制臺進行生成。
接下來開始寫代碼的調用,Python版本的,其他版本的類似。
我們將API封裝成一個類 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 = ["我最帥", "拳頭,停下", "我發誓", "數字5", "比心", "數字1", "好的呢,OK", "打電話", "手心向上", "愛你,520", "差評,不好的", "好評,Good,很棒", "勝利,開心"]# 將字典排序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} # 獲取手勢信息 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'] # 獲取到手勢的字典 gesture_dict = hands_dict[0]['gesture'] return gesture_dict,gesture_rectangle_dict else: return [],[]; # 獲取到手勢文本信息 def get_text(self,index): return gesture_chinese[index] # 獲取到手勢對應的概率 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]] # 獲取到手勢的位置 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)
新聞熱點
疑難解答