Upload 3 files
Browse files- app.py +63 -0
- keras_model.h5 +3 -0
- labels.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
from cvzone.HandTrackingModule import HandDetector
|
| 3 |
+
from cvzone.ClassificationModule import Classifier
|
| 4 |
+
import numpy as np
|
| 5 |
+
import math
|
| 6 |
+
|
| 7 |
+
cap = cv2.VideoCapture(0)
|
| 8 |
+
detector = HandDetector(maxHands=1)
|
| 9 |
+
classifier = Classifier("keras_model.h5", "labels.txt")
|
| 10 |
+
offset = 20
|
| 11 |
+
imgSize = 300
|
| 12 |
+
counter = 0
|
| 13 |
+
|
| 14 |
+
labels = ["iam", "ok", "going", "no", "yes" , "hi",]
|
| 15 |
+
|
| 16 |
+
while True:
|
| 17 |
+
success, img = cap.read()
|
| 18 |
+
imgOutput = img.copy()
|
| 19 |
+
hands, img = detector.findHands(img)
|
| 20 |
+
if hands:
|
| 21 |
+
hand = hands[0]
|
| 22 |
+
x, y, w, h = hand['bbox']
|
| 23 |
+
|
| 24 |
+
imgWhite = np.ones((imgSize, imgSize, 3), np.uint8) * 255
|
| 25 |
+
|
| 26 |
+
imgCrop = img[y - offset:y + h + offset, x - offset:x + w + offset]
|
| 27 |
+
|
| 28 |
+
# Add a check to ensure imgCrop is not empty
|
| 29 |
+
if imgCrop.size == 0:
|
| 30 |
+
continue
|
| 31 |
+
|
| 32 |
+
imgCropShape = imgCrop.shape
|
| 33 |
+
aspectRatio = h / w
|
| 34 |
+
|
| 35 |
+
if aspectRatio > 1:
|
| 36 |
+
k = imgSize / h
|
| 37 |
+
wCal = math.ceil(k * w)
|
| 38 |
+
imgResize = cv2.resize(imgCrop, (wCal, imgSize))
|
| 39 |
+
imgResizeShape = imgResize.shape
|
| 40 |
+
wGap = math.ceil((imgSize - wCal) / 2)
|
| 41 |
+
imgWhite[:, wGap: wCal + wGap] = imgResize
|
| 42 |
+
prediction, index = classifier.getPrediction(imgWhite, draw=False)
|
| 43 |
+
print(prediction, index)
|
| 44 |
+
|
| 45 |
+
else:
|
| 46 |
+
k = imgSize / w
|
| 47 |
+
hCal = math.ceil(k * h)
|
| 48 |
+
imgResize = cv2.resize(imgCrop, (imgSize, hCal))
|
| 49 |
+
imgResizeShape = imgResize.shape
|
| 50 |
+
hGap = math.ceil((imgSize - hCal) / 2)
|
| 51 |
+
imgWhite[hGap: hCal + hGap, :] = imgResize
|
| 52 |
+
prediction, index = classifier.getPrediction(imgWhite, draw=False)
|
| 53 |
+
|
| 54 |
+
cv2.rectangle(imgOutput, (x - offset, y - offset - 70), (x - offset + 400, y - offset + 60 - 50), (0, 255, 0),
|
| 55 |
+
cv2.FILLED)
|
| 56 |
+
cv2.putText(imgOutput, labels[index], (x, y - 30), cv2.FONT_HERSHEY_COMPLEX, 2, (0, 0, 0), 2)
|
| 57 |
+
cv2.rectangle(imgOutput, (x - offset, y - offset), (x + w + offset, y + h + offset), (0, 255, 0), 4)
|
| 58 |
+
|
| 59 |
+
cv2.imshow('ImageCrop', imgCrop)
|
| 60 |
+
cv2.imshow('ImageWhite', imgWhite)
|
| 61 |
+
|
| 62 |
+
cv2.imshow('Image', imgOutput)
|
| 63 |
+
cv2.waitKey(1)
|
keras_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b639c6b7cfb968077c7539ac921341b5dd3c8aabd8cbddd8357f356a30f43d46
|
| 3 |
+
size 2457008
|
labels.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 iam
|
| 2 |
+
1 ok
|
| 3 |
+
2 going
|
| 4 |
+
3 no
|
| 5 |
+
4 yes
|
| 6 |
+
5 hi
|