File size: 2,001 Bytes
4db3489
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import cv2
import numpy as np
from Classifier_ASL import Classifier
from Hand_Tracking_ASL import HandDetector

cap=cv2.VideoCapture(0)
detector=HandDetector(maxHands=1)
classifier=Classifier("best_model.h5")
offset=20


def main():
    while True:
        success, img=cap.read()
        hands, img=detector.findHands(img)

        # initialization for variables
        imgCrop=None
        pred_label=""
        pred_prob=0.0

        if hands:
            hand=hands[0]
            x, y, w, h=hand['bbox']

            imgHeight, imgWidth=img.shape[:2]
            y1=max(0, y-offset)
            y2=min(imgHeight, y+h+offset)
            x1=max(0, x-offset)
            x2=min(imgWidth, x+w+offset)

            imgCrop=img[y1:y2, x1:x2]

            if imgCrop.size!=0:  # Check if the crop is not empty
                pred_label, pred_prob=classifier.get_prediction(imgCrop, hand)

                cv2.imshow("ImageCrop", imgCrop)

                large_img=np.zeros((600, 600, 3), dtype=np.uint8)

                center_y=large_img.shape[0]//2
                center_x=large_img.shape[1]//2

                top_y=center_y-imgCrop.shape[0]//2
                top_x=center_x-imgCrop.shape[1]//2

                large_img[top_y:top_y+imgCrop.shape[0], top_x:top_x+imgCrop.shape[1]]=imgCrop

                cv2.putText(large_img, f"Label: {pred_label}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
                cv2.putText(large_img, f"Prob: {pred_prob:.2%}", (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
                cv2.imshow("Larger Image", large_img)
                print(f'{pred_label}: {pred_prob:.2%}')

        cv2.putText(img, f"{pred_label}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        cv2.putText(img, f"{pred_prob:.2%}", (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

        if cv2.waitKey(1)&0xFF==ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()


if __name__=='__main__':
    main()