温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Face++ API实现手势识别系统

发布时间:2021-06-03 16:15:14 来源:亿速云 阅读:131 作者:Leah 栏目:开发技术

使用Face++ API实现手势识别系统?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

1. 查看官方的API。找到Gesture API,先看一下是怎么说的。

使用Face++ API实现手势识别系统

调用参数:

使用Face++ API实现手势识别系统

使用Face++ API实现手势识别系统

官方还给出了一些调用错误返回的参数的说明,有兴趣的可以去官网看一下。

还给出了一个使用命令行调用API的实例:

使用Face++ API实现手势识别系统

从实例上不难看出,向 https://api-cn.faceplusplus.com/humanbodypp/beta/gesture 发送请求,默认的参数有 api_key,api_secret,image_file。api_key和api_secret可以通过控制台进行生成。

使用Face++ API实现手势识别系统

接下来开始写代码的调用,Python版本的,其他版本的类似。

我们将API封装成一个类 Gesture:

使用Face++ API实现手势识别系统

将其中的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)

封装好了Gesture类后接下来就是调用:先将官方给出的手势的图片保存起来,为了方便只保留单手的手势,然后生成随机数读取手势图片,我们去模仿手势,后台显示是正确手势的概率以及具体的位置,如果图像中没有手势则概率为0,位置为(0,0,0,0)。

'''
# -*- coding:utf-8 -*-
@author: TulLing
'''
import sys
sys.path.append("../gesture/")
 
import os
import random
import cv2 as cv
import time
import LearnGesture
 
def gestureLearning():
 os.system("cls")
 print("进入学习手势模式!")
 print("我们有13个手势,来和我学吧!(每次结束后可以选择输入 Q\q 退出!)")
 while(True):
 pic_num = random.randint(0,12) # 生成显示的图片的编号(随机数: 0 - 13)
 print(pic_num)
 pic_path = '../gesture/pic/gesture' + str(pic_num) + ".jpg" # 生成图片路径
 
 pic = cv.imread(pic_path) # 加载图片
 pic = cv.resize(pic,(120,120))
 cv.imshow("PIC",pic) # 显示要学习的手势
 
 print("即将打开摄像头,你有5秒种的时间准备手势,5秒种保持手势!")
 write_path = "../gesture/pic/test.jpg"
 cap = cv.VideoCapture(1)
 while(True):
  _,frame = cap.read()
  cv.imshow("Frame",frame)
  key = cv.waitKey(10)
  if(key == ord('Q') or key == ord('q')):
  cv.imwrite(write_path,frame)
  cv.waitKey(200)
  cap.release()
  cv.destroyAllWindows()
  break
  
 # 此处应该有手势识别
 files = {"image_file":open(write_path,'rb')}
 gesture = LearnGesture.Gesture()
 
 # 获取到手势文本
 ge_text = gesture.get_text(pic_num)
 # 获取手势信息
 gesture_dict,gesture_rectangle_dict = gesture.get_info(files)
 # 获取手势的概率
 ge_pro = gesture.get_pro(gesture_dict,pic_num)
 # 获取到手势的坐标
 ge_rect = gesture.get_rectangle(gesture_rectangle_dict)
 print("您学习的手势是:",ge_text)
 print("相似度达到:",ge_pro)
 print("具体位置:",ge_rect)
 
 
 # print("一轮学习结束,是否继续学习?(Y/N)")
 # 退出程序,回到主菜单或者继续
 commend = input("一轮学习结束,是否继续学习?(Y/N):")
 print(commend)
 
 if( commend == 'N' or commend == "n"):
  break
gestureLearning()

使用Face++ API实现手势识别系统

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI