飞道的博客

opencv+tensorflow手势识别+图片特效

322人阅读  评论(0)

一、效果展示

手势识别+特效展示!(此处只录入四种,简单运行!!!)

二、 项目介绍

        本次项目是在上一篇博文上的改进版本,在此次设计中录入了四种手势增加了训练图片和训练轮数,使得准确率提升,并且改进了互动性,当我们作出对应的手势时在我们手部上方会显示不同的特效,上一篇博文位置如下:

OpenCV+Tensorflow的手势识别_醉翁之意不在酒~的博客-CSDN博客

三、项目环境安装

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

  保存格式如下:

 

四、代码实现


  
  1. import cv2
  2. import cvzone
  3. from cvzone.HandTrackingModule import HandDetector
  4. import numpy as np
  5. cap = cv2.VideoCapture( 0)
  6. cap. set( 3, 1280)
  7. cap. set( 4, 720)
  8. # Importing all images
  9. imgBackground = cv2.imread( "./Resources/Background.png")
  10. imgGameOver = cv2.imread( "./Resources/gameOver.png")
  11. imgBall = cv2.imread( "./Resources/Ball.png", cv2.IMREAD_UNCHANGED)
  12. imgBat1 = cv2.imread( "./Resources/bat1.png", cv2.IMREAD_UNCHANGED)
  13. imgBat2 = cv2.imread( "./Resources/bat2.png", cv2.IMREAD_UNCHANGED)
  14. # Hand Detector
  15. detector = HandDetector(detectionCon= 0.8, maxHands= 2)
  16. # Variables
  17. ballPos = [ 100, 100]
  18. speedX = 10
  19. speedY = 10
  20. gameOver = False
  21. score = [ 0, 0]
  22. while True:
  23. _, img = cap.read()
  24. img = cv2.flip(img, 1)
  25. imgRaw = img.copy()
  26. # Find the hand and its landmarks
  27. hands, img = detector.findHands(img, flipType= False) # with draw
  28. # Overlaying the background image
  29. img = cv2.addWeighted(img, 0.2, imgBackground, 0.8, 0)
  30. # Check for hands
  31. if hands:
  32. for hand in hands:
  33. x, y, w, h = hand[ 'bbox']
  34. h1, w1, _ = imgBat1.shape
  35. y1 = y - h1 // 2
  36. y1 = np.clip(y1, 20, 415)
  37. if hand[ 'type'] == "Left":
  38. img = cvzone.overlayPNG(img, imgBat1, ( 59, y1))
  39. if 59 < ballPos[ 0] < 59 + w1 and y1 < ballPos[ 1] < y1 + h1:
  40. speedX = -speedX
  41. ballPos[ 0] += 30
  42. score[ 0] += 1
  43. if hand[ 'type'] == "Right":
  44. img = cvzone.overlayPNG(img, imgBat2, ( 1195, y1))
  45. if 1195 - 50 < ballPos[ 0] < 1195 and y1 < ballPos[ 1] < y1 + h1:
  46. speedX = -speedX
  47. ballPos[ 0] -= 30
  48. score[ 1] += 1
  49. # Game Over
  50. if ballPos[ 0] < 40 or ballPos[ 0] > 1200:
  51. gameOver = True
  52. if gameOver:
  53. img = imgGameOver
  54. cv2.putText(img, str(score[ 1] + score[ 0]).zfill( 2), ( 585, 360), cv2.FONT_HERSHEY_COMPLEX,
  55. 2.5, ( 200, 0, 200), 5)
  56. #who win?
  57. if ballPos[ 0] > 1200:
  58. cv2.putText(img, str( ' Left Win'), ( 430, 530), cv2.FONT_HERSHEY_COMPLEX,
  59. 2, ( 0, 0, 255), 3)
  60. elif ballPos[ 0] < 40:
  61. cv2.putText(img, str( 'Right Win'), ( 430, 530), cv2.FONT_HERSHEY_COMPLEX,
  62. 2, ( 0, 0, 255), 3)
  63. # If game not over move the ball
  64. else:
  65. # Move the Ball
  66. if ballPos[ 1] >= 500 or ballPos[ 1] <= 10:
  67. speedY = -speedY
  68. ballPos[ 0] += speedX
  69. ballPos[ 1] += speedY
  70. # Draw the ball
  71. img = cvzone.overlayPNG(img, imgBall, ballPos)
  72. cv2.putText(img, str(score[ 0]), ( 300, 650), cv2.FONT_HERSHEY_COMPLEX, 3, ( 255, 255, 255), 5)
  73. cv2.putText(img, str(score[ 1]), ( 900, 650), cv2.FONT_HERSHEY_COMPLEX, 3, ( 255, 255, 255), 5)
  74. img[ 580: 700, 20: 233] = cv2.resize(imgRaw, ( 213, 120))
  75. cv2.imshow( "Image", img)
  76. key = cv2.waitKey( 1)
  77. if key == 27:
  78. break
  79. if key == ord( 'r'):
  80. ballPos = [ 100, 100]
  81. speedX = 15
  82. speedY = 15
  83. gameOver = False
  84. score = [ 0, 0]
  85. imgGameOver = cv2.imread( "Resources/gameOver.png")

五、总结

        由于上篇博文关注人数不多(摆烂!),所以模型训练权重文件和图片文件不再统一发放,但如果需要请评论留言我会免费私信发你。


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