File size: 6,464 Bytes
fc62531
 
 
 
 
 
 
e8e5b05
82eb3c0
fc62531
 
 
 
 
 
 
2dd4b1a
fc62531
 
 
 
 
 
 
 
 
6308c7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69099d3
6308c7b
 
 
 
 
69099d3
fc62531
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6308c7b
fc62531
 
 
 
 
 
2dd4b1a
fc62531
2dd4b1a
fc62531
 
 
 
 
 
 
 
6308c7b
 
 
6fcd558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06b30e3
6fcd558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f91b0b1
fc62531
6308c7b
 
 
 
 
 
 
2480254
6308c7b
 
 
fc62531
 
 
 
 
 
 
 
 
 
 
6308c7b
 
 
 
 
 
fc62531
2dd4b1a
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import cv2
from cvzone.HandTrackingModule import HandDetector
from cvzone.ClassificationModule import Classifier
import numpy as np
import math
import gradio as gr

detector = HandDetector(maxHands=1)
classifier = Classifier("ModelFull/keras_model.h5", "ModelFull/labels.txt")

offset = 20
imgSize = 300

folder = "Data/C"
counter = 0

labels = ["A", "B","C","D","E","F","G","H","I","J","K","L","M","N", "O","P","Q","R","S","T","U","V","W","X","Y","Z"]

def sign(img):
    #img = cv2.imread("sign.jpg")
    imgOutput = cv2.flip(img.copy(),1)
    hands, img = detector.findHands(cv2.flip(img[:,:,::-1],1))
    if hands:
        print('hand detected')
        hand = hands[0]
        x, y, w, h = hand['bbox']
        imlist = hand['lmList']
        print(imlist)
        if ((imlist[10][0] < imlist[4][0] < imlist[6][0]) or (imlist[6][0] < imlist[4][0] < imlist[10][0])):
          if ((imlist[4][1] < imlist[8][1]) and (imlist[4][1] < imlist[12][1]) ):
            print('In T')
            cv2.rectangle(imgOutput, (x-offset, y-offset),(x + w+offset, y + h+offset), (255, 0, 255), 4)
            imgOutput = cv2.flip(imgOutput,1)
            cv2.rectangle(imgOutput, (0,30),(80,80), (255, 0, 255), cv2.FILLED)
            cv2.putText(imgOutput, 'T', (20, 75), cv2.FONT_HERSHEY_COMPLEX, 1.7, (255, 255, 255), 2)
            return imgOutput
          else:
            print('In K')
            cv2.rectangle(imgOutput, (x-offset, y-offset),(x + w+offset, y + h+offset), (255, 0, 255), 4)
            imgOutput = cv2.flip(imgOutput,1)
            cv2.rectangle(imgOutput, (0,30),(80,80), (255, 0, 255), cv2.FILLED)
            cv2.putText(imgOutput, 'K', (20, 75), cv2.FONT_HERSHEY_COMPLEX, 1.7, (255, 255, 255), 2)
            return imgOutput            
        '''if imlist[4][0]>imlist[8][0] and imlist[4][0]>imlist[12][0] and imlist[4][0]>imlist[16][0] and imlist[4][0]>imlist[20][0]:
          print('In M')
          cv2.rectangle(imgOutput, (x-offset, y-offset),(x + w+offset, y + h+offset), (255, 0, 255), 4)
          imgOutput = cv2.flip(imgOutput,1)
          cv2.rectangle(imgOutput, (0,30),(80,80), (255, 0, 255), cv2.FILLED)
          cv2.putText(imgOutput, 'M', (20, 75), cv2.FONT_HERSHEY_COMPLEX, 1.7, (255, 255, 255), 2)
          return imgOutput'''   

        imgWhite = np.ones((imgSize, imgSize, 3), np.uint8) * 255
        imgCrop = img[y - offset:y + h + offset, x - offset:x + w + offset]

        imgCropShape = imgCrop.shape

        aspectRatio = h / w

        if aspectRatio > 1:
            k = imgSize / h
            wCal = math.ceil(k * w)
            imgResize = cv2.resize(imgCrop, (wCal, imgSize))
            imgResizeShape = 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))
            imgResizeShape = imgResize.shape
            hGap = math.ceil((imgSize - hCal) / 2)
            imgWhite[hGap:hCal + hGap, :] = imgResize
            prediction, index = classifier.getPrediction(imgWhite, draw=False)

        cv2.imwrite("check.jpg",imgWhite)
        cv2.rectangle(imgOutput, (x-offset, y-offset),
                      (x + w+offset, y + h+offset), (255, 0, 255), 4)
        imgOutput = cv2.flip(imgOutput,1)
        #cv2.rectangle(imgOutput, (x - offset, y - offset-50),
        #              (x - offset+90, y - offset-50+50), (255, 0, 255), cv2.FILLED)
        #cv2.putText(imgOutput, labels[index], (x, y -26), cv2.FONT_HERSHEY_COMPLEX, 1.7, (255, 255, 255), 2)
        cv2.rectangle(imgOutput, (0,30),
                      (80,80), (255, 0, 255), cv2.FILLED)
        cv2.putText(imgOutput, labels[index], (20, 75), cv2.FONT_HERSHEY_COMPLEX, 1.7, (255, 255, 255), 2)


        #cv2.imshow("ImageCrop", imgCrop)
        #cv2.imshow("ImageWhite", imgWhite)

    #cv2.imshow("Image", imgOutput)
    return imgOutput

def set_example_image(example: list) -> dict:
    return gr.inputs.Image.update(value=example[0])


css = """
.gr-button-lg {
    z-index: 14;
    width: 113px;
    height: 30px;
    left: 0px;
    top: 0px;
    padding: 0px;
    cursor: pointer !important; 
    background: none rgb(17, 20, 45) !important;
    border: none !important;
    text-align: center !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    color: rgb(255, 255, 255) !important;
    line-height: 1 !important;
    border-radius: 6px !important;
    transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
    box-shadow: none !important;
}
.gr-button-lg:hover{
    z-index: 14;
    width: 113px;
    height: 30px;
    left: 0px;
    top: 0px;
    padding: 0px;
    cursor: pointer !important; 
    background: none rgb(66, 133, 244) !important;
    border: none !important;
    text-align: center !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    color: rgb(255, 255, 255) !important;
    line-height: 1 !important;
    border-radius: 6px !important;
    transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
    box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
}

footer {display:none !important} 
.output-markdown{display:none !important} 
#out_image {height: 22rem !important;}
"""

with gr.Blocks(title="American Sign Language Detection | Data Science Dojo", css=css) as demo:

  with gr.Tabs():
    with gr.TabItem('Upload'):
      with gr.Row():
        with gr.Column():
          img_input = gr.Image(shape=(640,480))
          image_button = gr.Button("Submit")

        with gr.Column():
          output = gr.Image(shape=(640,480), elem_id="out_image")
      with gr.Row():
        example_images = gr.Dataset(components=[img_input],samples=[["ex2.jpg"]])
      
    with gr.TabItem('Webcam'):
      with gr.Row():
        with gr.Column():
          img_input2 = gr.Webcam()
          image_button2 = gr.Button("Submit")

        with gr.Column():
          output2 = gr.outputs.Image()

    image_button2.click(fn=sign,
        inputs = img_input2,
        outputs = output2)
    image_button.click(fn=sign,
        inputs = img_input,
        outputs = output)
    example_images.click(fn=set_example_image,inputs=[example_images],outputs=[img_input])
 

demo.launch(debug=True)