小言_互联网的博客

OpenCV+Tensorflow的手势识别

376人阅读  评论(0)

一、效果展示

此次只选录了以下4种手势,当然你可以自己选择增加手势。

                                                     

二、项目实现原理

        首先通过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

 保存格式如下:

四、代码实现

模型预训练权重如下

链接:https://pan.baidu.com/s/1pAJvE0zvhdw8cpwQ4Gmz1Q?pwd=good 
提取码:good


  
  1. import cv2
  2. from cvzone.HandTrackingModule import HandDetector
  3. from cvzone.ClassificationModule import Classifier
  4. from PIL import Image, ImageDraw, ImageFont
  5. import numpy as np
  6. import math
  7. import time
  8. # import speech
  9. cap = cv2.VideoCapture( 0)
  10. cap. set( 3, 1280)
  11. cap. set( 4, 720)
  12. detector = HandDetector(maxHands= 1)
  13. classifile = Classifier( "./model/keras_model.h5", "./model/labels.txt")
  14. offset = 20
  15. imgSize = 300
  16. counter = 0
  17. labels = [ '666', '鄙视', 'Good', '比心', '击掌', '握拳']
  18. # folder = r"F:\opencv_game\HandSignDetection\Data\Love"
  19. while True:
  20. success, img = cap.read()
  21. img = cv2.flip(img, 1)
  22. imgOutput = img.copy()
  23. hands, img = detector.findHands(img)
  24. if hands:
  25. hand = hands[ 0]
  26. x, y, w, h = hand[ 'bbox']
  27. imgWhite = np.ones((imgSize, imgSize, 3), np.uint8)* 255
  28. imgCrop = img[y - offset:y + h + offset, x - offset:x + w + offset]
  29. imgCropShape = imgCrop.shape
  30. aspectRatio = h/w
  31. if aspectRatio > 1:
  32. k = imgSize/h
  33. wCal = math.ceil(k*w)
  34. imgResize = cv2.resize(imgCrop, (wCal, imgSize))
  35. imgResizeShape = imgResize.shape
  36. wGap = math.ceil((imgSize - wCal)/ 2)
  37. imgWhite[:, wGap:wCal+wGap] = imgResize
  38. prediction, index = classifile.getPrediction(imgWhite)
  39. print(prediction, index)
  40. else:
  41. k = imgSize / w
  42. hCal = math.ceil(k * h)
  43. imgResize = cv2.resize(imgCrop, (imgSize, hCal))
  44. imgResizeShape = imgResize.shape
  45. hGap = math.ceil((imgSize - hCal) / 2)
  46. imgWhite[hGap:hCal + hGap,:] = imgResize
  47. prediction, index = classifile.getPrediction(imgWhite)
  48. # 解决cv2.putText绘制中文乱码
  49. def cv2AddChineseText( img, text, position, textColor=(255, 255, 255), textSize=50):
  50. if ( isinstance(img, np.ndarray)): # 判断是否OpenCV图片类型
  51. img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
  52. # 创建一个可以在给定图像上绘图的对象
  53. draw = ImageDraw.Draw(img)
  54. # 字体的格式
  55. fontStyle = ImageFont.truetype(
  56. "simsun.ttc", textSize, encoding= "utf-8")
  57. # 绘制文本
  58. draw.text(position, text, textColor, font=fontStyle)
  59. # 转换回OpenCV格式
  60. return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
  61. cv2.rectangle(imgOutput, (x - offset, y - offset - 50),
  62. (x-offset+ 130, y-offset), ( 255, 0, 255), cv2.FILLED)
  63. # cv2.putText(imgOutput, labels[index], (x,y-24),
  64. # cv2.FONT_HERSHEY_COMPLEX, 1.5, (255, 255, 255), 2)
  65. # 中文
  66. img = cv2AddChineseText(imgOutput, labels[index], (x - offset, y - offset - 50))
  67. cv2.rectangle(img, (x-offset, y-offset),
  68. (x+w+offset, y+h+offset), ( 255, 0, 255), 4)
  69. # speech.say(labels[index])
  70. # cv2.imshow('ImageCrop', imgCrop)
  71. # cv2.imshow('ImageWhite', imgWhite)
  72. cv2.imshow( 'Image', img)
  73. key = cv2.waitKey( 1)
  74. if key == ord( 's'):
  75. pass
  76. elif key == 27:
  77. break

四、总结

        如有帮助,点赞收藏,感谢!!


转载:https://blog.csdn.net/qq_58535145/article/details/127783349
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场