Spaces:
Sleeping
Sleeping
Download src/hf_space.py from Ephab/Hand_Digit_Recognition: direct link, hf CLI and curl.
- Browser
- Download file 1.62 kB
-
https://huggingface.co/spaces/Ephab/Hand_Digit_Recognition/resolve/main/src/hf_space.py
- Command line
-
hf download hf://spaces/Ephab/Hand_Digit_Recognition/src/hf_space.py
-
curl -L -o hf_space.py https://huggingface.co/spaces/Ephab/Hand_Digit_Recognition/resolve/main/src/hf_space.py
1.62 kB
| from pathlib import Path | |
| import cv2 | |
| import gradio as gr | |
| import mediapipe as mp | |
| import torch | |
| from . import test | |
| from .hand_tracker import clean_coordinates_for_csv, clean_data | |
| hand_detector = mp.solutions.hands.Hands( | |
| static_image_mode=True, | |
| max_num_hands=1, | |
| min_detection_confidence=0.5, | |
| min_tracking_confidence=0.5 | |
| ) | |
| device = "cpu" | |
| checkpoint_path = Path(__file__).resolve().parent / "checkpoints" / "hand_gesture_model.pth" | |
| model = test.load_model(str(checkpoint_path)).to(device) | |
| def predict_num(image): | |
| if image is None: | |
| return None, {"No image provided": 1.0} | |
| processed_hand = hand_detector.process(image) | |
| if processed_hand.multi_hand_landmarks: | |
| for hand_landmarks in processed_hand.multi_hand_landmarks: | |
| landmarks = clean_data(1, hand_landmarks.landmark) | |
| coordinates = clean_coordinates_for_csv(landmarks) | |
| x = torch.tensor(coordinates, dtype=torch.float32).reshape(1, -1).to(device) | |
| logits = model(x) | |
| probs = torch.softmax(logits, dim=1) | |
| mp.solutions.drawing_utils.draw_landmarks( | |
| image, | |
| hand_landmarks, | |
| mp.solutions.hands.HAND_CONNECTIONS, | |
| ) | |
| prob_dict = {str(i): float(probs.reshape(-1)[i - 1].item()) for i in range(1, 6)} | |
| return image, prob_dict | |
| return image, {"No hand detected": 1.0} | |
| demo = gr.Interface( | |
| fn=predict_num, | |
| inputs=gr.Image(sources=["webcam"], type="numpy"), | |
| outputs=[gr.Image(label="Detected Hand"), gr.Label(num_top_classes=5, label="Predicted Digit")], | |
| live=True, | |
| ) | |