Spaces:
Runtime error
Runtime error
File size: 5,104 Bytes
fc62531 6308c7b fc62531 2dd4b1a fc62531 6308c7b fc62531 6308c7b fc62531 2dd4b1a fc62531 2dd4b1a fc62531 6308c7b fc62531 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 |
import cv2
from cvzone.HandTrackingModule import HandDetector
from cvzone.ClassificationModule import Classifier
import numpy as np
import math
import gradio as gr
detector = HandDetector(mode=True,maxHands=1)
classifier = Classifier("Model/keras_model.h5", "Model/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])
with gr.Blocks() 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))
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) |