Views
No views yet
1from ultralytics import YOLO
2
3# Load model directly from HuggingFace
4model = YOLO('https://huggingface.co/EtanHey/hand-detection-3class/resolve/main/model.pt')
5
6# Predict on an image
7results = model.predict('image.jpg')
8
9# Get the prediction
10probs = results[0].probs
11class_id = probs.top1 # 0=arm, 1=hand, 2=not_hand (alphabetical order!)
12confidence = probs.top1conf.item()
13
14# Interpret results
15if class_id == 1: # hand is index 1
16 print(f"✋ Hand detected: {confidence:.1%}")
17elif class_id == 0: # arm is index 0
18 print(f"💪 Arm detected: {confidence:.1%}")
19else: # not_hand is index 2
20 print(f"❌ No hand/arm detected: {confidence:.1%}")1import cv2
2from ultralytics import YOLO
3
4model = YOLO('https://huggingface.co/EtanHey/hand-detection-3class/resolve/main/model.pt')
5cap = cv2.VideoCapture(0)
6
7while True:
8 ret, frame = cap.read()
9 if not ret:
10 break
11
12 results = model(frame)
13 probs = results[0].probs
14
15 # YOLO uses alphabetical order!
16 classes = ['arm', 'hand', 'not_hand'] # 0=arm, 1=hand, 2=not_hand
17 label = f"{classes[probs.top1]}: {probs.top1conf:.1%}"
18
19 cv2.putText(frame, label, (10, 30),
20 cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
21 cv2.imshow('Hand Detection', frame)
22
23 if cv2.waitKey(1) & 0xFF == ord('q'):
24 break
25
26cap.release()
27cv2.destroyAllWindows()npm install ai openai1// app/components/hand-detector.tsx
2'use client';
3
4import { useChat } from 'ai/react';
5import { useState } from 'react';
6
7export function HandDetectorWithAI() {
8 const [detection, setDetection] = useState(null);
9 const { messages, input, handleSubmit } = useChat({
10 api: '/api/chat',
11 initialMessages: [{
12 role: 'system',
13 content: 'You help interpret hand gestures and signs.'
14 }]
15 });
16
17 const detectAndAnalyze = async (file) => {
18 // 1. Detect hand
19 const formData = new FormData();
20 formData.append('image', file);
21
22 const response = await fetch('/api/detect-hand', {
23 method: 'POST',
24 body: formData
25 });
26
27 const result = await response.json();
28 setDetection(result);
29
30 // 2. If hand detected, ask AI about gesture
31 if (result.class === 'hand') {
32 await handleSubmit({
33 preventDefault: () => {},
34 currentTarget: {
35 input: { value: `What gesture is this hand making? Confidence: ${result.confidence}%` }
36 }
37 });
38 }
39 };
40
41 return (
42 <div>
43 <input type="file" onChange={(e) => detectAndAnalyze(e.target.files[0])} />
44 {detection && <p>Detected: {detection.class} ({detection.confidence}%)</p>}
45 {messages.map(m => (
46 <div key={m.id}>{m.role}: {m.content}</div>
47 ))}
48 </div>
49 );
50}1// app/api/chat/route.ts
2import { OpenAIStream, StreamingTextResponse } from 'ai';
3
4export async function POST(req: Request) {
5 const { messages } = await req.json();
6
7 // Your OpenAI/AI provider logic here
8 const stream = OpenAIStream(response);
9 return new StreamingTextResponse(stream);
10}1from fastapi import FastAPI, File, UploadFile
2from ultralytics import YOLO
3from PIL import Image
4import io
5
6app = FastAPI()
7model = YOLO('https://huggingface.co/EtanHey/hand-detection-3class/resolve/main/model.pt')
8
9@app.post("/detect")
10async def detect(file: UploadFile = File(...)):
11 image = Image.open(io.BytesIO(await file.read()))
12 results = model.predict(image)
13 probs = results[0].probs
14
15 return {
16 "class": ['arm', 'hand', 'not_hand'][probs.top1], # alphabetical order
17 "confidence": float(probs.top1conf)
18 }1async function detectHand(imageFile) {
2 const formData = new FormData();
3 formData.append('file', imageFile);
4
5 const response = await fetch('http://localhost:8000/detect', {
6 method: 'POST',
7 body: formData
8 });
9
10 const result = await response.json();
11 console.log(`Detected: ${result.class} (${result.confidence * 100}%)`);
12}1# Convert to ONNX first
2from ultralytics import YOLO
3model = YOLO('model.pt')
4model.export(format='onnx')1import * as ort from 'onnxruntime-web';
2
3const session = await ort.InferenceSession.create('/model.onnx');
4// Process and run inference...1const detectHand = async (imageUri) => {
2 const formData = new FormData();
3 formData.append('image', {
4 uri: imageUri,
5 type: 'image/jpeg',
6 name: 'photo.jpg'
7 });
8
9 const response = await fetch('YOUR_API_URL/detect', {
10 method: 'POST',
11 body: formData
12 });
13
14 const result = await response.json();
15 Alert.alert(`Detected: ${result.class}`);
16};curl -X POST -F "file=@test.jpg" http://localhost:8000/detect| Metric | Value |
|---|---|
| Validation Accuracy | 96.3% |
| Inference Speed | 30+ FPS (Apple M1) |
| Model Size | 2.97 MB |
@software{hand_detection_yolo_2024,
author = {EtanHey},
title = {Hand Detection YOLOv8 Model},
year = {2024},
publisher = {HuggingFace},
url = {https://huggingface.co/EtanHey/hand-detection-3class}
}