Spaces:
Runtime error
Runtime error
import cv2 | |
import gradio as gr | |
from cvzone.HandTrackingModule import HandDetector | |
from cvzone.ClassificationModule import Classifier | |
import numpy as np | |
import math | |
# Load HandDetector and Classifier | |
detector = HandDetector(maxHands=1) | |
classifier = Classifier("keras_model.h5", "labels.txt") | |
offset = 20 | |
imgSize = 300 | |
# Try different camera indices until a valid one is found | |
camera_index = 0 | |
cap = None | |
while cap is None or not cap.isOpened(): | |
cap = cv2.VideoCapture(camera_index) | |
if cap is None or not cap.isOpened(): | |
camera_index += 1 | |
print(f"Error: Could not open camera with index {camera_index - 1}. Trying index {camera_index}.") | |
if cap.isOpened(): | |
print(f"Camera opened successfully with index {camera_index}.") | |
else: | |
print("Error: No valid camera index found.") | |
exit() | |
def classify_hand(img): | |
imgOutput = img.copy() | |
# Detect hands | |
hands, _ = 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] | |
if imgCrop.size != 0: | |
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 | |
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 | |
# Get hand gesture prediction | |
prediction, index = classifier.getPrediction(imgWhite, draw=False) | |
# Draw bounding box and label | |
cv2.rectangle(imgOutput, (x - offset, y - offset - 70), (x - offset + 400, y - offset + 60 - 50), (0, 255, 0), | |
cv2.FILLED) | |
cv2.putText(imgOutput, labels[index], (x, y - 30), cv2.FONT_HERSHEY_COMPLEX, 2, (0, 0, 0), 2) | |
cv2.rectangle(imgOutput, (x - offset, y - offset), (x + w + offset, y + h + offset), (0, 255, 0), 4) | |
return imgOutput | |
iface = gr.Interface(fn=classify_hand, inputs='webcam', outputs='image', live=True) | |
iface.launch() | |