-
Notifications
You must be signed in to change notification settings - Fork 3
/
Test.py
85 lines (68 loc) · 2.73 KB
/
Test.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def wordtest():
import cv2
from cvzone.HandTrackingModule import HandDetector
from cvzone.ClassificationModule import Classifier
import numpy as np
import math
import sys
cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands = 1)
classifier = Classifier("Model/keras_model.h5","Model/labels.txt")
offset = 20
imgSize = 300
folder = "Data\C"
counter = 0
labels = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
str1 = ""
while True:
success, img = cap.read()
imgOutput = img.copy()
hands, img = detector.findHands(img)
if hands:
hand = hands[0]
x,y,w,h = hand['bbox']
imgWhite = np.ones((imgSize,imgSize,3),np.uint8)*255
imgCrop = img[y - offset:y+h+offset,x - offset:x+w+offset]
imgCropShape = imgCrop.shape
aspectRatio = h/w
if aspectRatio > 1:
k = imgSize/h
wCal = math.ceil(k*w)
if imgCrop.size > 0:
imgResize = cv2.resize(imgCrop, (wCal, imgSize))
else:
continue
imgResizeShape = imgResize.shape
wGap = math.ceil((imgSize - wCal)/2)
if (x > 0 + offset and y > 0 + offset and w > 0 + offset and h > 0 + offset):
imgWhite[0:imgResizeShape[0], wGap:wCal + wGap] = imgResize
prediction, index = classifier.getPrediction(imgWhite)
print(prediction,index)
text = cv2.putText(imgOutput, f"{labels[index]}", (100,100), cv2.FONT_HERSHEY_COMPLEX_SMALL, 3, (17, 17, 17), 2,
cv2.LINE_AA)
elif aspectRatio < 1:
k = imgSize/w
hCal = math.ceil(k*h)
if imgCrop.size > 0:
imgResize = cv2.resize(imgCrop,(imgSize,hCal))
else:
continue
imgResizeShape = imgResize.shape
hGap = math.ceil((imgSize - hCal)/2)
prediction, index = classifier.getPrediction(imgWhite)
print(prediction, index)
text = cv2.putText(imgOutput, f"{labels[index]}", (100, 100), cv2.FONT_HERSHEY_COMPLEX_SMALL, 3, (17, 17, 17), 2,
cv2.LINE_AA)
cv2.imshow("Image",imgOutput)
key = cv2.waitKey(1)
if key == 13:
str1+=labels[index]
if key == ord("p"):
print(str1)
if key == 32:
str1 += " "
if key == 8:
str1 = str[:-1]
if key == 27:
cv2.destroyAllWindows()
return str1