Spaces:
Runtime error
Runtime error
| 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) |