-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
39 lines (28 loc) · 1009 Bytes
/
Copy pathmain.py
File metadata and controls
39 lines (28 loc) · 1009 Bytes
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
import cv2
import numpy as np
from tensorflow.keras.models import load_model
from cvzone.HandTrackingModule import HandDetector
model = load_model("Model/keras_model.h5", compile=False)
labels = open("Model/labels.txt").readlines()
cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands=1)
while True:
success, img = cap.read()
hands, img = detector.findHands(img)
if hands:
hand = hands[0]
x,y,w,h = hand['bbox']
crop = img[y-20:y+h+20, x-20:x+w+20]
crop = cv2.resize(crop,(224,224))
image = np.asarray(crop,dtype=np.float32)
image = (image / 127.5) - 1
image = np.expand_dims(image, axis=0)
prediction = model.predict(image, verbose=0)
index = np.argmax(prediction)
label = labels[index].strip()
cv2.putText(img,label,(x,y-20),
cv2.FONT_HERSHEY_SIMPLEX,
1,(0,255,0),2)
cv2.imshow("Hand Sign Detection", img)
if cv2.waitKey(1)==27:
break