利用OpenCV+Tensorflow实现的手势识别
作者:醉翁之意不在酒~ 发布时间:2022-04-03 02:12:33
标签:opencv,tensorflow,手势识别
一、效果展示
此次只选录了以下五种手势,当然你可以自己选择增加手势。
二、项目实现原理
首先通过opencv的手部检测器检测出我们的手,然后录入自己想要检测的手部信息,使用Tensorflow训练得到预训练权重文件(此处已经训练完成,直接调用即可!),调用预训练权重文件对opencv检测的手部信息进行预测,实时返回到摄像头画面,到此整体项目已经实现,此外还可以添加语音模块如speech,对检测到的手势信息进行语音播报。
三、项目环境安装
首先python的版本此处选择为3.7.7(其余版本相差不大的都可)
然后,我们所需要下载的环境如下所示,你可以将其存为txt格式直接在终端输入(具体格式如下图):
pip install -r environment.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
absl-py==1.2.0
attrs==22.1.0
cvzone==1.5.6
cycler==0.11.0
fonttools==4.37.4
kiwisolver==1.4.4
matplotlib==3.5.3
mediapipe==0.8.9.1
numpy==1.21.6
opencv-contrib-python==4.6.0.66
opencv-python==4.6.0.66
opencv-python-headless==4.6.0.66
packaging==21.3
Pillow==9.2.0
protobuf==3.19.1
pyparsing==3.0.9
python-dateutil==2.8.2
six==1.16.0
speech==0.5.2
typing_extensions==4.4.0
保存格式如下:
四、代码实现
模型预训练权重如下
点击这里下载
import cv2
from cvzone.HandTrackingModule import HandDetector
from cvzone.ClassificationModule import Classifier
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import math
import time
# import speech
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
detector = HandDetector(maxHands=1)
classifile = Classifier("./model/keras_model.h5", "./model/labels.txt")
offset = 20
imgSize = 300
counter = 0
labels = ['666', '鄙视', 'Good', '比心', '击掌', '握拳']
# folder = r"F:\opencv_game\HandSignDetection\Data\Love"
while True:
success, img = cap.read()
img = cv2.flip(img, 1)
imgOutput = img.copy()
hands, img = detector.findHands(img)
if hands:
hand = hands[0]
x, y, w, h = hand['bbox']
imgWhite = np.ones((imgSize, imgSize, 3), np.uint8)*255
imgCrop = img[y - offset:y + h + offset, x - offset:x + w + offset]
imgCropShape = imgCrop.shape
aspectRatio = h/w
if aspectRatio > 1:
k = imgSize/h
wCal = math.ceil(k*w)
imgResize = cv2.resize(imgCrop, (wCal, imgSize))
imgResizeShape = imgResize.shape
wGap = math.ceil((imgSize - wCal)/2)
imgWhite[:, wGap:wCal+wGap] = imgResize
prediction, index = classifile.getPrediction(imgWhite)
print(prediction, index)
else:
k = imgSize / w
hCal = math.ceil(k * h)
imgResize = cv2.resize(imgCrop, (imgSize, hCal))
imgResizeShape = imgResize.shape
hGap = math.ceil((imgSize - hCal) / 2)
imgWhite[hGap:hCal + hGap,:] = imgResize
prediction, index = classifile.getPrediction(imgWhite)
# 解决cv2.putText绘制中文乱码
def cv2AddChineseText(img, text, position, textColor=(255, 255, 255), textSize=50):
if (isinstance(img, np.ndarray)): # 判断是否OpenCV图片类型
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# 创建一个可以在给定图像上绘图的对象
draw = ImageDraw.Draw(img)
# 字体的格式
fontStyle = ImageFont.truetype(
"simsun.ttc", textSize, encoding="utf-8")
# 绘制文本
draw.text(position, text, textColor, font=fontStyle)
# 转换回OpenCV格式
return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
cv2.rectangle(imgOutput, (x - offset, y - offset - 50),
(x-offset+130, y-offset), (255, 0, 255), cv2.FILLED)
# cv2.putText(imgOutput, labels[index], (x,y-24),
# cv2.FONT_HERSHEY_COMPLEX, 1.5, (255, 255, 255), 2)
# 中文
img = cv2AddChineseText(imgOutput, labels[index], (x - offset, y - offset - 50))
cv2.rectangle(img, (x-offset, y-offset),
(x+w+offset, y+h+offset), (255,0,255),4)
# speech.say(labels[index])
# cv2.imshow('ImageCrop', imgCrop)
# cv2.imshow('ImageWhite', imgWhite)
cv2.imshow('Image', img)
key = cv2.waitKey(1)
if key == ord('s'):
pass
elif key == 27:
break
五、总结
来源:https://blog.csdn.net/qq_58535145/article/details/127783349


猜你喜欢
- //设置已存在表中字段为auto_incrementALTER TABLE tablename change id id int(2) no
- 自去年以来,我们正在开发区块链(Blockchain)业务。最近使用过Ethereum并使用PHP,所以我想我们应该聊聊这个话题。这里有个前
- 之前看到过很多头条,说哪国某人坚持了多少年自学使用excel画画,效果十分惊艳。 对于他们的耐心我十分敬佩。 但是作为一个程序员,自然也得挑
- 本文实例讲述了Python面向对象之接口、抽象类与多态。分享给大家供大家参考,具体如下:接口类继承有两种用途:一:继承基类的方法,并且做出自
- MySQL 赋予用户权限命令的简单格式可概括为:grant 权限 on 数据库对象 to 用户 [identified by &
- javascript中要判断一个变量是否为array通常是比较困难的,因为var a = [];alert(t
- 本文实例讲述了python通过pil模块将raw图片转换成png图片的方法。分享给大家供大家参考。具体分析如下:python通过pil模块将
- 本文实例讲述了Python实现二叉树及遍历方法。分享给大家供大家参考,具体如下:介绍:树是数据结构中非常重要的一种,主要的用途是用来提高查找
- 今天 Python3.8 发布啦,它是 Python2 终结前最后一个大版本,我们一起看看这个版本都添加了那些新功能和特性。PEP 572:
- A.截取从字符串左边开始N个字符 Declare @S1 varchar(100) Select @S1='http://www.x
- 本文实例讲述了python登录pop3邮件服务器接收邮件的方法。分享给大家供大家参考。具体实现方法如下:import poplib, str
- python修改大数据文件时,如果全加载到内存中,可能会导致内存溢出。因此可借用如下方法,将分件分段读取修改。with open('
- 同时装配两个相同类型数据库1.配置文件:spring: profiles: active: devda
- 一般常用的有两个方法:1、使用DataFrame.index = [newName],DataFrame.columns = [newNam
- 我遇到的一个小需求,就是希望通过判断pandas dataframe中一列的值在两个条件范围(比如下面代码中所描述的逻辑,取小于u-3ε和大
- 本文实例讲述了Python创建xml的方法。分享给大家供大家参考。具体实现方法如下:from xml.dom.minidom import
- 今天学到了如何使用Python的smtplib库发送邮件,中间也是遇到了各种各样的错误和困难,还好都一一的解决了。下面来谈一谈我的这段经历。
- matplotlib在widgets模块提供Cursor类用于支持十字光标的生成。另外官方还提供了自定义十字光标的实例。widgets模块C
- 字符替换是一个非常重要的功能,javascript 中有一个 String.replace( ),但是此方法有
- Python的数据结构有列表、元组、集合、字典等,可以吧列表当成一个清单,是有序的,我们可以通过索引访问到列表中的元素,列表还可以进行修改、