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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
tags:
|
| 3 |
+
- yolov8
|
| 4 |
+
- image-classification
|
| 5 |
+
- hand-detection
|
| 6 |
+
- computer-vision
|
| 7 |
+
library_name: ultralytics
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# Hand Detection Model (YOLOv8)
|
| 11 |
+
|
| 12 |
+
This model classifies images into three categories:
|
| 13 |
+
- **hand**: Close-up hand with fingers visible
|
| 14 |
+
- **arm**: Forearm or elbow area
|
| 15 |
+
- **not_hand**: Neither hand nor arm
|
| 16 |
+
|
| 17 |
+
## Usage
|
| 18 |
+
|
| 19 |
+
```python
|
| 20 |
+
from ultralytics import YOLO
|
| 21 |
+
|
| 22 |
+
# Load model directly from HuggingFace
|
| 23 |
+
model = YOLO('https://huggingface.co/EtanHey/hand-detection-3class/resolve/main/model.pt')
|
| 24 |
+
|
| 25 |
+
# Predict on an image
|
| 26 |
+
results = model.predict('image.jpg')
|
| 27 |
+
|
| 28 |
+
# Get predictions
|
| 29 |
+
if results and results[0].probs:
|
| 30 |
+
probs = results[0].probs
|
| 31 |
+
top_class = probs.top1 # 0=hand, 1=arm, 2=not_hand
|
| 32 |
+
confidence = probs.top1conf.item()
|
| 33 |
+
|
| 34 |
+
classes = ['hand', 'arm', 'not_hand']
|
| 35 |
+
print(f"Detected: {classes[top_class]} ({confidence:.1%})")
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
## Usage in Next.js/Node.js
|
| 39 |
+
|
| 40 |
+
### Option 1: Python API Backend
|
| 41 |
+
|
| 42 |
+
```javascript
|
| 43 |
+
// app/api/detect/route.js (Next.js 13+ App Router)
|
| 44 |
+
export async function POST(request) {
|
| 45 |
+
const formData = await request.formData();
|
| 46 |
+
const image = formData.get('image');
|
| 47 |
+
|
| 48 |
+
// Call Python backend
|
| 49 |
+
const response = await fetch('http://localhost:8000/predict', {
|
| 50 |
+
method: 'POST',
|
| 51 |
+
body: formData
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
+
const result = await response.json();
|
| 55 |
+
return Response.json(result);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
// Frontend component
|
| 59 |
+
async function detectHand(file) {
|
| 60 |
+
const formData = new FormData();
|
| 61 |
+
formData.append('image', file);
|
| 62 |
+
|
| 63 |
+
const response = await fetch('/api/detect', {
|
| 64 |
+
method: 'POST',
|
| 65 |
+
body: formData
|
| 66 |
+
});
|
| 67 |
+
|
| 68 |
+
const result = await response.json();
|
| 69 |
+
// result = { class: 'hand', confidence: 0.98 }
|
| 70 |
+
return result;
|
| 71 |
+
}
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
### Option 2: Python Microservice (FastAPI)
|
| 75 |
+
|
| 76 |
+
```python
|
| 77 |
+
# backend/api.py
|
| 78 |
+
from fastapi import FastAPI, File, UploadFile
|
| 79 |
+
from ultralytics import YOLO
|
| 80 |
+
import numpy as np
|
| 81 |
+
from PIL import Image
|
| 82 |
+
import io
|
| 83 |
+
|
| 84 |
+
app = FastAPI()
|
| 85 |
+
model = YOLO('https://huggingface.co/EtanHey/hand-detection-3class/resolve/main/model.pt')
|
| 86 |
+
|
| 87 |
+
@app.post("/predict")
|
| 88 |
+
async def predict(file: UploadFile = File(...)):
|
| 89 |
+
contents = await file.read()
|
| 90 |
+
image = Image.open(io.BytesIO(contents))
|
| 91 |
+
|
| 92 |
+
results = model.predict(image)
|
| 93 |
+
probs = results[0].probs
|
| 94 |
+
|
| 95 |
+
classes = ['hand', 'arm', 'not_hand']
|
| 96 |
+
return {
|
| 97 |
+
"class": classes[probs.top1],
|
| 98 |
+
"confidence": float(probs.top1conf),
|
| 99 |
+
"all_probs": {
|
| 100 |
+
"hand": float(probs.data[0]),
|
| 101 |
+
"arm": float(probs.data[1]),
|
| 102 |
+
"not_hand": float(probs.data[2])
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
### Option 3: Using ONNX.js (Browser-based)
|
| 108 |
+
|
| 109 |
+
```javascript
|
| 110 |
+
// First convert model to ONNX (run once)
|
| 111 |
+
// python3 -c "from ultralytics import YOLO; YOLO('model.pt').export(format='onnx')"
|
| 112 |
+
|
| 113 |
+
import * as ort from 'onnxruntime-web';
|
| 114 |
+
|
| 115 |
+
async function detectHandBrowser(imageElement) {
|
| 116 |
+
// Load ONNX model
|
| 117 |
+
const session = await ort.InferenceSession.create('/model.onnx');
|
| 118 |
+
|
| 119 |
+
// Preprocess image to 224x224
|
| 120 |
+
const tensor = preprocessImage(imageElement);
|
| 121 |
+
|
| 122 |
+
// Run inference
|
| 123 |
+
const results = await session.run({ input: tensor });
|
| 124 |
+
const probs = results.output.data;
|
| 125 |
+
|
| 126 |
+
// Get prediction
|
| 127 |
+
const classes = ['hand', 'arm', 'not_hand'];
|
| 128 |
+
const maxIdx = probs.indexOf(Math.max(...probs));
|
| 129 |
+
|
| 130 |
+
return {
|
| 131 |
+
class: classes[maxIdx],
|
| 132 |
+
confidence: probs[maxIdx],
|
| 133 |
+
all_probs: {
|
| 134 |
+
hand: probs[0],
|
| 135 |
+
arm: probs[1],
|
| 136 |
+
not_hand: probs[2]
|
| 137 |
+
}
|
| 138 |
+
};
|
| 139 |
+
}
|
| 140 |
+
```
|
| 141 |
+
|
| 142 |
+
## Usage in React Native
|
| 143 |
+
|
| 144 |
+
```javascript
|
| 145 |
+
import { launchImageLibrary } from 'react-native-image-picker';
|
| 146 |
+
|
| 147 |
+
const detectHand = async () => {
|
| 148 |
+
const result = await launchImageLibrary({ mediaType: 'photo' });
|
| 149 |
+
|
| 150 |
+
if (result.assets) {
|
| 151 |
+
const formData = new FormData();
|
| 152 |
+
formData.append('image', {
|
| 153 |
+
uri: result.assets[0].uri,
|
| 154 |
+
type: 'image/jpeg',
|
| 155 |
+
name: 'photo.jpg'
|
| 156 |
+
});
|
| 157 |
+
|
| 158 |
+
const response = await fetch('YOUR_API_URL/predict', {
|
| 159 |
+
method: 'POST',
|
| 160 |
+
body: formData
|
| 161 |
+
});
|
| 162 |
+
|
| 163 |
+
const detection = await response.json();
|
| 164 |
+
console.log('Detected:', detection.class, detection.confidence);
|
| 165 |
+
}
|
| 166 |
+
};
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
## Usage with cURL
|
| 170 |
+
|
| 171 |
+
```bash
|
| 172 |
+
# Test the model with cURL
|
| 173 |
+
curl -X POST -F "image=@test.jpg" http://your-api-url/predict
|
| 174 |
+
|
| 175 |
+
# Response: {"class": "hand", "confidence": 0.98}
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
## Usage in Swift (iOS)
|
| 179 |
+
|
| 180 |
+
```swift
|
| 181 |
+
import CoreML
|
| 182 |
+
import Vision
|
| 183 |
+
|
| 184 |
+
func detectHand(image: UIImage) {
|
| 185 |
+
// First convert YOLO to CoreML format
|
| 186 |
+
// Then use in iOS app:
|
| 187 |
+
|
| 188 |
+
guard let model = try? VNCoreMLModel(for: HandDetector().model) else { return }
|
| 189 |
+
|
| 190 |
+
let request = VNCoreMLRequest(model: model) { request, error in
|
| 191 |
+
guard let results = request.results as? [VNClassificationObservation] else { return }
|
| 192 |
+
|
| 193 |
+
if let topResult = results.first {
|
| 194 |
+
let className = topResult.identifier // "hand", "arm", or "not_hand"
|
| 195 |
+
let confidence = topResult.confidence
|
| 196 |
+
print("Detected: \(className) with \(confidence * 100)% confidence")
|
| 197 |
+
}
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
// Process image...
|
| 201 |
+
}
|
| 202 |
+
```
|
| 203 |
+
|
| 204 |
+
## Model Details
|
| 205 |
+
|
| 206 |
+
- **Architecture**: YOLOv8s-cls
|
| 207 |
+
- **Classes**: 3 (hand, arm, not_hand)
|
| 208 |
+
- **Input Size**: 224x224
|
| 209 |
+
- **Training Data**: 1740 images
|
| 210 |
+
- **Accuracy**: >96%
|
| 211 |
+
|
| 212 |
+
## Training Details
|
| 213 |
+
|
| 214 |
+
Trained on a custom dataset with:
|
| 215 |
+
- 704 hand images
|
| 216 |
+
- 320 arm images
|
| 217 |
+
- 462 not_hand images
|
| 218 |
+
|
| 219 |
+
Split 80/20 for training/validation.
|