forked from ngbio/ASLDetection_Model_2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
69 lines (53 loc) · 2.09 KB
/
Copy pathtest.py
File metadata and controls
69 lines (53 loc) · 2.09 KB
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
import cv2
from cvzone.HandTrackingModule import HandDetector # Nhập Module theo dõi chấm tay
from cvzone.ClassificationModule import Classifier
import numpy as np
import math
import time
cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands=1) # máy dò
classifier = Classifier("Model_4/hand_gesture_cnn.h5","Model_4/labels2.txt")
dolech = 20
imgSize = 28 #224
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"
]
while True:
success, img = cap.read()
imgOutput = img.copy()
hands, img= detector.findHands(img) # máy dò chấm, tìm bàn tay trả về hình ảnh
if hands:
hand = hands[0]
x, y, w, h = hand['bbox']
imgWhite = np.ones((imgSize,imgSize, 3), np.uint8) * 255#tạo ma trận số 1
imgCrop = img[y-dolech:y + h+dolech, x-dolech:x + w+dolech] # chiều cao bắt đầu và chiều cao kết thúc(tương tự chìu rộng)
imgCropShape = imgCrop.shape
aspectRatio = h/w # tỷ lệ khung hình = cao/r
if aspectRatio > 1:
k = imgSize/h
wCal = math.ceil(k*w)
imgResize = cv2.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 = cv2.resize(imgCrop, (imgSize, hCal))
imgResizeShape = imgResize.shape
hGap = math.ceil((imgSize - hCal) / 2)
imgWhite[hGap:hCal + hGap,:] = imgResize
prediction, index = classifier.getPrediction(imgWhite)
cv2.putText(imgOutput, labels[index], (x, y - 20), cv2.FONT_HERSHEY_COMPLEX, 2, (255,0, 255), 2)
cv2.imshow('ImageCrop', imgCrop)
cv2.imshow('ImageWhite', imgWhite)
cv2.imshow('Image', imgOutput)
cv2.waitKey(1) # dộ trễ 1 milis