From d0137634edc762ead3903c33c5ab4a655524b228 Mon Sep 17 00:00:00 2001 From: Tabs <54019007+Tabshhh@users.noreply.github.com> Date: Mon, 13 Jun 2022 15:48:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E6=89=8B=E5=8A=BF?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=EF=BC=8C=E4=BC=98=E5=8C=96=E6=8C=87=E5=85=B3?= =?UTF-8?q?=E8=8A=82=E8=AF=86=E5=88=AB=20=E6=B7=BB=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/demo.py b/demo.py index 526867d..a23b831 100644 --- a/demo.py +++ b/demo.py @@ -11,6 +11,8 @@ import mediapipe as mp import cv2 # import HandDetector import math +from datetime import datetime +import time # 旋转函数 @@ -200,28 +202,34 @@ class HandDetector: :return: 竖起手指的列表 """ knuckles = [] + distan = 10 if self.results.multi_hand_landmarks: my_hand_type = self.hand_type() # Thumb + xx = self.re_lmList[self.tipIds[0]][0] + yy = self.re_lmList[self.tipIds[0] - 1][0] if my_hand_type == "Right": - if self.lmList[self.tipIds[0]][0] > self.lmList[self.tipIds[0] - 1][0]: + if -distan < xx - yy < distan: + knuckles.append(2) + elif xx > yy: knuckles.append(1) else: knuckles.append(0) else: - if self.lmList[self.tipIds[0]][0] < self.lmList[self.tipIds[0] - 1][0]: + if -distan < xx - yy < distan: + knuckles.append(2) + elif xx < yy: knuckles.append(1) else: knuckles.append(0) # 12 knuckles - distan = 0.22 for i in range(1, 5): - for j in range(4): - xx = self.lmList[self.tipIds[i]-j][1] - yy = self.lmList[self.tipIds[i]-j - 1][1] + for j in range(3): + xx = self.re_lmList[self.tipIds[i]-j][1] + yy = self.re_lmList[self.tipIds[i]-j - 1][1] if -distan < xx - yy < distan: knuckles.append(2) - elif xx < yy: + elif xx > yy: knuckles.append(1) else: knuckles.append(0) @@ -248,6 +256,8 @@ class Main: def gesture_recognition(self): self.detector = HandDetector() + xl = [] # 特征值存储 + startTime = time.time() while True: frame, img = self.camera.read() img = self.detector.find_hands(img) @@ -282,6 +292,19 @@ class Main: elif x1 and (x2 == 0, x3 == 0, x4 == 0, x5 == 0): cv2.putText(img, "GOOD!", (x_1, y_1), cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 255), 3) + print(time.time() - startTime) + if (time.time() - startTime) < 2: # 手势存储时间 + xl.append([x1, x2, x3, x4, x5]) + cv2.putText(img, 'Please put the gesture to be stored in 1 second', (50, 50), + cv2.FONT_HERSHEY_PLAIN, 1.2, (255, 255, 255), 2) + else: # 开始手势存储 + # startTime = time.time() + cv2.putText(img, 'Gesture stored, recognition started', (50, 50), + cv2.FONT_HERSHEY_PLAIN, 1.2, (255, 255, 255), 2) + else: + startTime = time.time() # 当检测不到手势时,初始化手势存储 + cv2.putText(img, 'Please put the gesture to be stored in 1 second', (50, 50), cv2.FONT_HERSHEY_PLAIN, + 1.2, (255, 255, 255), 2) cv2.imshow("camera", img) key = cv2.waitKey(1)