Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import cv2
import numpy as np
from cvzone.HandTrackingModule import HandDetector
from cvzone.ClassificationModule import Classifier
from Func.CloseProgram import Close
from Func.Helpers import *
offset = 20
size = 300
capture = cv2.VideoCapture(0)
detector = HandDetector(maxHands=1)
classifier = Classifier("Model/keras_model.h5", "Model/labels.txt")
letters = GetLabels("Model/labels.txt")
if not capture.isOpened():
print("Failed to open video capture.")
exit()
while True:
success, img = capture.read()
imgOutput = img.copy()
if not success:
print('can\'t read image')
break
hands, img = detector.findHands(img)
if hands:
hand = hands[0]
x, y, w, h = hand['bbox']
imgFixed = np.ones((size, size, 3), np.uint8)
imgCropped = img[y - offset:y + h + offset, x - offset:x + w + offset]
aspectRatio = h / w
if aspectRatio > 1:
CalculateWidth(size, h, w, imgCropped, imgFixed)
prediction, index = GetPrediction(classifier, imgFixed)
else:
CalculateHeight(size, h, w, imgCropped, imgFixed)
prediction, index = GetPrediction(classifier, imgFixed)
print(prediction)
cv2.putText(imgOutput, letters[index], (x, y), cv2.FONT_HERSHEY_TRIPLEX, 2, (255, 0, 255), 2)
cv2.imshow("img", imgOutput)
key = cv2.waitKey(1)
if key == ord('p'):
Close(capture)
break