一、效果展示
手势识别+特效展示!(此处只录入四种,简单运行!!!)
二、 项目介绍
本次项目是在上一篇博文上的改进版本,在此次设计中录入了四种手势增加了训练图片和训练轮数,使得准确率提升,并且改进了互动性,当我们作出对应的手势时在我们手部上方会显示不同的特效,上一篇博文位置如下:
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
保存格式如下:
四、代码实现
-
import cv2
-
import cvzone
-
from cvzone.HandTrackingModule
import HandDetector
-
import numpy
as np
-
-
cap = cv2.VideoCapture(
0)
-
cap.
set(
3,
1280)
-
cap.
set(
4,
720)
-
-
# Importing all images
-
imgBackground = cv2.imread(
"./Resources/Background.png")
-
imgGameOver = cv2.imread(
"./Resources/gameOver.png")
-
imgBall = cv2.imread(
"./Resources/Ball.png", cv2.IMREAD_UNCHANGED)
-
imgBat1 = cv2.imread(
"./Resources/bat1.png", cv2.IMREAD_UNCHANGED)
-
imgBat2 = cv2.imread(
"./Resources/bat2.png", cv2.IMREAD_UNCHANGED)
-
-
# Hand Detector
-
detector = HandDetector(detectionCon=
0.8, maxHands=
2)
-
-
# Variables
-
ballPos = [
100,
100]
-
speedX =
10
-
speedY =
10
-
gameOver =
False
-
score = [
0,
0]
-
-
while
True:
-
_, img = cap.read()
-
img = cv2.flip(img,
1)
-
imgRaw = img.copy()
-
-
# Find the hand and its landmarks
-
hands, img = detector.findHands(img, flipType=
False)
# with draw
-
-
# Overlaying the background image
-
img = cv2.addWeighted(img,
0.2, imgBackground,
0.8,
0)
-
-
# Check for hands
-
if hands:
-
for hand
in hands:
-
x, y, w, h = hand[
'bbox']
-
h1, w1, _ = imgBat1.shape
-
y1 = y - h1 //
2
-
y1 = np.clip(y1,
20,
415)
-
-
if hand[
'type'] ==
"Left":
-
img = cvzone.overlayPNG(img, imgBat1, (
59, y1))
-
if
59 < ballPos[
0] <
59 + w1
and y1 < ballPos[
1] < y1 + h1:
-
speedX = -speedX
-
ballPos[
0] +=
30
-
score[
0] +=
1
-
-
if hand[
'type'] ==
"Right":
-
img = cvzone.overlayPNG(img, imgBat2, (
1195, y1))
-
if
1195 -
50 < ballPos[
0] <
1195
and y1 < ballPos[
1] < y1 + h1:
-
speedX = -speedX
-
ballPos[
0] -=
30
-
score[
1] +=
1
-
-
# Game Over
-
if ballPos[
0] <
40
or ballPos[
0] >
1200:
-
gameOver =
True
-
-
if gameOver:
-
img = imgGameOver
-
cv2.putText(img,
str(score[
1] + score[
0]).zfill(
2), (
585,
360), cv2.FONT_HERSHEY_COMPLEX,
-
2.5, (
200,
0,
200),
5)
-
#who win?
-
if ballPos[
0] >
1200:
-
cv2.putText(img,
str(
' Left Win'), (
430,
530), cv2.FONT_HERSHEY_COMPLEX,
-
2, (
0,
0,
255),
3)
-
-
elif ballPos[
0] <
40:
-
cv2.putText(img,
str(
'Right Win'), (
430,
530), cv2.FONT_HERSHEY_COMPLEX,
-
2, (
0,
0,
255),
3)
-
-
-
-
# If game not over move the ball
-
else:
-
-
# Move the Ball
-
if ballPos[
1] >=
500
or ballPos[
1] <=
10:
-
speedY = -speedY
-
-
ballPos[
0] += speedX
-
ballPos[
1] += speedY
-
-
# Draw the ball
-
img = cvzone.overlayPNG(img, imgBall, ballPos)
-
-
cv2.putText(img,
str(score[
0]), (
300,
650), cv2.FONT_HERSHEY_COMPLEX,
3, (
255,
255,
255),
5)
-
cv2.putText(img,
str(score[
1]), (
900,
650), cv2.FONT_HERSHEY_COMPLEX,
3, (
255,
255,
255),
5)
-
-
img[
580:
700,
20:
233] = cv2.resize(imgRaw, (
213,
120))
-
-
cv2.imshow(
"Image", img)
-
key = cv2.waitKey(
1)
-
if key ==
27:
-
break
-
if key ==
ord(
'r'):
-
ballPos = [
100,
100]
-
speedX =
15
-
speedY =
15
-
gameOver =
False
-
score = [
0,
0]
-
imgGameOver = cv2.imread(
"Resources/gameOver.png")
五、总结
由于上篇博文关注人数不多(摆烂!),所以模型训练权重文件和图片文件不再统一发放,但如果需要请评论留言我会免费私信发你。
转载:https://blog.csdn.net/qq_58535145/article/details/127904161