File size: 1,771 Bytes
d884a50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import math
import time
import cv2
import numpy as np
from cvzone.HandTrackingModule import HandDetector
from cvzone.ClassificationModule import Classifier

classifier = Classifier("model/keras_model.h5","model/labels.txt")

labels = ["HELLO","HOW ARE YOU","WHERE ARE YOU"]

cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands=2,)
offset = 20
imgSize = 300
while True:
    success,img = cap.read()
    imgoutput = 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
        ascpectRatio = h/w
        if ascpectRatio>1:
            k = imgSize/h
            wcal = math.ceil(k*w)
            imgResize = cv2.resize(imgCrop,(wcal,imgSize))
            imageResizeshape = imgResize.shape
            wgap = math.ceil((imgSize-wcal)/2)
            imgWhite[:,wgap:wcal+wgap] = imgResize
            prediction , index = classifier.getPrediction(imgWhite,draw=False)
            print(prediction,index)
        else:
            k = imgSize/w
            hcal = math.ceil(k*h)
            imgResize = cv2.resize(imgCrop,(imgSize,hcal))
            imageResizeshape = imgResize.shape
            hgap = math.ceil((imgSize-hcal)/2)
            imgWhite[hgap:hcal+hgap,:] = imgResize
            prediction,index = classifier.getPrediction(imgWhite,draw=False)
            print(prediction,index)

        cv2.putText(imgoutput, labels[index], (x, y - 26), cv2.FONT_HERSHEY_COMPLEX, 1.7, (98, 200, 55))

        #cv2.imshow("ImageCrop",imgCrop)
        #cv2.imshow("ImageWhite",imgWhite)
    cv2.imshow("image",imgoutput)
    cv2.waitKey(1)