-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestSentence.py
77 lines (61 loc) · 2.47 KB
/
TestSentence.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
import cv2 as cv
import numpy as np
import math
from cvzone.ClassificationModule import Classifier
from cvzone.HandTrackingModule import HandDetector
cap = cv.VideoCapture(0)
detector = HandDetector(maxHands=1)
classifier = Classifier("Model/keras_model.h5", "Model/labels.txt")
offset = 20
imgSize = 300
counter = 0
labels = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X"]
sequence = []
sentence = []
while True:
success, img = cap.read()
imgOut = 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)
imgResize = cv.resize(imgCrop, (wCal, imgSize))
imgResizeShape = imgResize.shape
wGap = math.ceil((imgSize - wCal)/2)
imgWhite[:, wGap:wCal+wGap] = imgResize
prediction, index = classifier.getPrediction(imgWhite)
# print(prediction, index)
else:
k = imgSize/w
hCal = math.ceil(k*h)
imgResize = cv.resize(imgCrop, (imgSize, hCal))
imgResizeShape = imgResize.shape
hGap = math.ceil((imgSize - hCal)/2)
imgWhite[hGap:hCal+hGap, :] = imgResize
prediction, index = classifier.getPrediction(imgWhite)
sequence.insert(0, prediction)
sequence = sequence[-30:]
if len(sequence) == 30:
print("Printing the Letter", labels[index])
# Visualize my Sentence
if len(sentence) > 0:
if labels[index] != sentence[-1]:
sentence.append(labels[index])
else:
sentence.append(labels[index])
if len(sentence) > 5:
sentence = sentence[-5:]
cv.rectangle(imgOut, (0, 0), (640, 40), (245, 117, 16), -1)
cv.putText(imgOut, ' '.join(sentence), (3, 30), cv.FONT_HERSHEY_COMPLEX, 1, (255, 225, 255), 2, cv.LINE_AA)
cv.putText(imgOut, labels[index], (x, y-20), cv.FONT_HERSHEY_COMPLEX, 2, (255, 0, 255), 2)
# imgCrop_flip = cv.flip(imgWhite, 1) # code to flip image
cv.imshow("Image Cropped", imgWhite)
cv.imshow("Image", imgOut)
cv.waitKey(1)