Browse Source

修复bug

main
leaf 2 years ago
parent
commit
90c7612ea2
  1. 8
      TM.py
  2. 12
      ai.py
  3. 4
      ai_two.py
  4. 47
      gr.py

8
TM.py

@ -141,11 +141,12 @@ class Main:
def gesture_recognition(self, img, detector):
self.detector = detector
lm_list, bbox = detector.find_position(img)
img = self.detector.find_hands(img)
lm_list, bbox = self.detector.find_position(img)
if lm_list:
x_1, y_1 = bbox["bbox"][0], bbox["bbox"][1]
x1, x2, x3, x4, x5 = detector.fingers_up()
x1, x2, x3, x4, x5 = self.detector.fingers_up()
if (np.linalg.norm(lm_list[4]-lm_list[8]) < 50) and (np.linalg.norm(lm_list[4]-lm_list[12]) < 50):
cv2.putText(img, "7_SEVEN", (x_1, y_1), cv2.FONT_HERSHEY_PLAIN, 3,
(0, 0, 255), 3)
@ -170,6 +171,9 @@ class Main:
elif (x1 == 1 and x5 == 1) and (x3 == 0 and x4 == 0 and x2 == 0):
cv2.putText(img, "6_SIX", (x_1, y_1), cv2.FONT_HERSHEY_PLAIN, 3,
(0, 0, 255), 3)
elif x1 == 0 and x5 == 0 and x3 == 0 and x4 == 0 and x2 == 0:
cv2.putText(img, "0_ZERO", (x_1, y_1), cv2.FONT_HERSHEY_PLAIN, 3,
(0, 0, 255), 3)
else:
return 1
return 0

12
ai.py

@ -371,13 +371,23 @@ class Main:
data = data.unsqueeze(0)
test_output = cnn(data)
test_np = test_output.detach().numpy()[0]
# normal_temp = normalize(test_np)
# temp = normal_temp[np.argpartition(normal_temp, -2)[-2:]]
temp = test_np[np.argpartition(test_np, -2)[-2:]]
print(temp[1]-temp[0])
if temp[1]-temp[0] < 5.5:
return 1
self.result.append(torch.max(test_output, 1)[1].data.cpu().numpy()[0])
if len(self.result) > 5:
if len(self.result) > 4:
self.disp = str(out_label[stats.mode(self.result)[0][0]])
self.result = []
cv2.putText(img, self.disp, (x_1, y_1), cv2.FONT_HERSHEY_PLAIN, 3,
(0, 0, 255), 3)
return 0
def gesture_recognition_video(self, filedir):
self.detector = HandDetector()

4
ai_two.py

@ -221,8 +221,8 @@ class HandDetector:
class AI:
def __init__(self, datasets_dir):
self.EPOCH = 20
self.BATCH_SIZE = 2
self.EPOCH = 100
self.BATCH_SIZE = 4
self.LR = 10e-5
self.DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.datasets_dir = datasets_dir

47
gr.py

@ -2,6 +2,7 @@ import TM
import ai
import ai_two
import cv2
import copy
import torch
import torch.nn as nn
@ -98,15 +99,19 @@ class Main:
if diy:
cnn = torch.load("CNN.pkl")
cnn_two = torch.load("CNN_two.pkl")
tm_img = self.tm_detector.find_hands(img)
while True:
not_match = 0
img_tm = copy.deepcopy(img)
is_one_hand = self.at_main.gesture_recognition(self.at_detector, img, cnn_two)
if is_one_hand:
not_match = self.ai_main.gesture_recognition_camera(self.ai_detector, img, cnn)
if not_match:
self.tm_main.gesture_recognition(tm_img, self.tm_detector)
self.tm_main.gesture_recognition(img_tm, self.tm_detector)
cv2.imshow("camera", img)
if not_match:
cv2.imshow("camera", img_tm)
else:
cv2.imshow("camera", img)
key = cv2.waitKey(1)
if cv2.getWindowProperty('camera', cv2.WND_PROP_VISIBLE) < 1:
break
@ -120,13 +125,18 @@ class Main:
cnn_two = torch.load("CNN_two.pkl")
while True:
ret, img = cap.read()
tm_status = self.tm_main.gesture_recognition(self.tm_detector.find_hands(img), self.tm_detector)
if tm_status and diy:
is_one_hand = self.at_main.gesture_recognition(self.at_detector, img, cnn_two)
if is_one_hand:
self.ai_main.gesture_recognition_camera(self.ai_detector, img, cnn)
not_match = 0
img_tm = copy.deepcopy(img)
is_one_hand = self.at_main.gesture_recognition(self.at_detector, img, cnn_two)
if is_one_hand:
not_match = self.ai_main.gesture_recognition_camera(self.ai_detector, img, cnn)
if not_match:
self.tm_main.gesture_recognition(img_tm, self.tm_detector)
cv2.imshow("camera", img)
if not_match:
cv2.imshow("camera", img_tm)
else:
cv2.imshow("camera", img)
key = cv2.waitKey(1)
if cv2.getWindowProperty('camera', cv2.WND_PROP_VISIBLE) < 1:
break
@ -140,13 +150,18 @@ class Main:
cnn_two = torch.load("CNN_two.pkl")
while True:
frame, img = self.camera.read()
tm_status = self.tm_main.gesture_recognition(self.tm_detector.find_hands(img), self.tm_detector)
if tm_status and diy:
is_one_hand = self.at_main.gesture_recognition(self.at_detector, img, cnn_two)
if is_one_hand:
self.ai_main.gesture_recognition_camera(self.ai_detector, img, cnn)
not_match = 0
img_tm = copy.deepcopy(img)
is_one_hand = self.at_main.gesture_recognition(self.at_detector, img, cnn_two)
if is_one_hand:
not_match = self.ai_main.gesture_recognition_camera(self.ai_detector, img, cnn)
if not_match:
self.tm_main.gesture_recognition(img_tm, self.tm_detector)
cv2.imshow("camera", img)
if not_match:
cv2.imshow("camera", img_tm)
else:
cv2.imshow("camera", img)
key = cv2.waitKey(1)
if cv2.getWindowProperty('camera', cv2.WND_PROP_VISIBLE) < 1:
break
@ -162,4 +177,4 @@ class Main:
if __name__ == '__main__':
main = Main()
main.gr_img("C:/Users/leafl/Pictures/图片1.png", 0)
main.gr_img("", 0)

Loading…
Cancel
Save