DataWizard9742 commited on
Commit
d884a50
·
1 Parent(s): b910b48

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import time
3
+ import cv2
4
+ import numpy as np
5
+ from cvzone.HandTrackingModule import HandDetector
6
+ from cvzone.ClassificationModule import Classifier
7
+
8
+ classifier = Classifier("model/keras_model.h5","model/labels.txt")
9
+
10
+ labels = ["HELLO","HOW ARE YOU","WHERE ARE YOU"]
11
+
12
+ cap = cv2.VideoCapture(0)
13
+ detector = HandDetector(maxHands=2,)
14
+ offset = 20
15
+ imgSize = 300
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
+ imgWhite = np.ones((imgSize,imgSize,3),np.uint8)*255
24
+ imgCrop = img[y-offset:y+h+offset,x-offset:x+w+offset]
25
+ imgCropshape = imgCrop.shape
26
+ ascpectRatio = h/w
27
+ if ascpectRatio>1:
28
+ k = imgSize/h
29
+ wcal = math.ceil(k*w)
30
+ imgResize = cv2.resize(imgCrop,(wcal,imgSize))
31
+ imageResizeshape = imgResize.shape
32
+ wgap = math.ceil((imgSize-wcal)/2)
33
+ imgWhite[:,wgap:wcal+wgap] = imgResize
34
+ prediction , index = classifier.getPrediction(imgWhite,draw=False)
35
+ print(prediction,index)
36
+ else:
37
+ k = imgSize/w
38
+ hcal = math.ceil(k*h)
39
+ imgResize = cv2.resize(imgCrop,(imgSize,hcal))
40
+ imageResizeshape = imgResize.shape
41
+ hgap = math.ceil((imgSize-hcal)/2)
42
+ imgWhite[hgap:hcal+hgap,:] = imgResize
43
+ prediction,index = classifier.getPrediction(imgWhite,draw=False)
44
+ print(prediction,index)
45
+
46
+ cv2.putText(imgoutput, labels[index], (x, y - 26), cv2.FONT_HERSHEY_COMPLEX, 1.7, (98, 200, 55))
47
+
48
+ #cv2.imshow("ImageCrop",imgCrop)
49
+ #cv2.imshow("ImageWhite",imgWhite)
50
+ cv2.imshow("image",imgoutput)
51
+ cv2.waitKey(1)
52
+