利用OpenCV+Tensorflow實(shí)現(xiàn)的手勢(shì)識(shí)別
一、效果展示
此次只選錄了以下五種手勢(shì),當(dāng)然你可以自己選擇增加手勢(shì)。




二、項(xiàng)目實(shí)現(xiàn)原理
首先通過opencv的手部檢測(cè)器檢測(cè)出我們的手,然后錄入自己想要檢測(cè)的手部信息,使用Tensorflow訓(xùn)練得到預(yù)訓(xùn)練權(quán)重文件(此處已經(jīng)訓(xùn)練完成,直接調(diào)用即可?。?,調(diào)用預(yù)訓(xùn)練權(quán)重文件對(duì)opencv檢測(cè)的手部信息進(jìn)行預(yù)測(cè),實(shí)時(shí)返回到攝像頭畫面,到此整體項(xiàng)目已經(jīng)實(shí)現(xiàn),此外還可以添加語音模塊如speech,對(duì)檢測(cè)到的手勢(shì)信息進(jìn)行語音播報(bào)。
三、項(xiàng)目環(huán)境安裝
首先python的版本此處選擇為3.7.7(其余版本相差不大的都可)
然后,我們所需要下載的環(huán)境如下所示,你可以將其存為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
保存格式如下:

四、代碼實(shí)現(xiàn)
模型預(yù)訓(xùn)練權(quán)重如下
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))
# 創(chuàng)建一個(gè)可以在給定圖像上繪圖的對(duì)象
draw = ImageDraw.Draw(img)
# 字體的格式
fontStyle = ImageFont.truetype(
"simsun.ttc", textSize, encoding="utf-8")
# 繪制文本
draw.text(position, text, textColor, font=fontStyle)
# 轉(zhuǎn)換回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五、總結(jié)
到此這篇關(guān)于利用OpenCV+Tensorflow實(shí)現(xiàn)手勢(shì)識(shí)別的文章就介紹到這了,更多相關(guān)OpenCV+Tensorflow手勢(shì)識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Python實(shí)現(xiàn)字典合并的四種方法
這篇文章主要為大家詳細(xì)介紹了Python的合并字典的四種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
python3 打印輸出字典中特定的某個(gè)key的方法示例
這篇文章主要介紹了python3 打印輸出字典中特定的某個(gè)key的方法,涉及Python字典的遍歷、判斷、輸出等相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
Python Pycharm虛擬下百度飛漿PaddleX安裝報(bào)錯(cuò)問題及處理方法(親測(cè)100%有效)
最近很多朋友給小編留言在安裝PaddleX的時(shí)候總是出現(xiàn)各種奇葩問題,不知道該怎么處理,今天小編通過本文給大家介紹下Python Pycharm虛擬下百度飛漿PaddleX安裝報(bào)錯(cuò)問題及處理方法,真的有效,遇到同樣問題的朋友快來參考下吧2021-05-05
python GUI庫圖形界面開發(fā)之PyQt5下拉列表框控件QComboBox詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5下拉列表框控件QComboBox詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-02-02
Python使用struct處理二進(jìn)制的實(shí)例詳解
這篇文章主要介紹了Python使用struct處理二進(jìn)制的實(shí)例詳解的相關(guān)資料,希望通過本文大家能掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09
Python編程求質(zhì)數(shù)實(shí)例代碼
這篇文章主要介紹了Python編程求質(zhì)數(shù)實(shí)例代碼,對(duì)求200,1000和100000以內(nèi)的質(zhì)數(shù)進(jìn)行了測(cè)試,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01

