Update model card with cleaner examples
Browse files
README.md
CHANGED
|
@@ -10,11 +10,11 @@ library_name: ultralytics
|
|
| 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 |
-
##
|
| 18 |
|
| 19 |
```python
|
| 20 |
from ultralytics import YOLO
|
|
@@ -25,195 +25,175 @@ model = YOLO('https://huggingface.co/EtanHey/hand-detection-3class/resolve/main/
|
|
| 25 |
# Predict on an image
|
| 26 |
results = model.predict('image.jpg')
|
| 27 |
|
| 28 |
-
# Get
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
```
|
| 37 |
|
| 38 |
-
##
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
export async function POST(request) {
|
| 45 |
-
const formData = await request.formData();
|
| 46 |
-
const image = formData.get('image');
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
});
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
}
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
const formData = new FormData();
|
| 61 |
-
formData.append('image', file);
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
});
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
```
|
| 73 |
|
| 74 |
-
##
|
|
|
|
|
|
|
| 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("/
|
| 88 |
-
async def
|
| 89 |
-
|
| 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":
|
| 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 |
-
|
| 108 |
-
|
| 109 |
```javascript
|
| 110 |
-
|
| 111 |
-
|
|
|
|
| 112 |
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
| 118 |
|
| 119 |
-
|
| 120 |
-
const tensor = preprocessImage(imageElement);
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 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 |
-
##
|
| 143 |
|
| 144 |
```javascript
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
body: formData
|
| 161 |
-
});
|
| 162 |
-
|
| 163 |
-
const detection = await response.json();
|
| 164 |
-
console.log('Detected:', detection.class, detection.confidence);
|
| 165 |
-
}
|
| 166 |
};
|
| 167 |
```
|
| 168 |
|
| 169 |
-
##
|
| 170 |
|
| 171 |
```bash
|
| 172 |
-
|
| 173 |
-
curl -X POST -F "image=@test.jpg" http://your-api-url/predict
|
| 174 |
-
|
| 175 |
-
# Response: {"class": "hand", "confidence": 0.98}
|
| 176 |
```
|
| 177 |
|
| 178 |
-
##
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
-
|
| 191 |
-
guard let results = request.results as? [VNClassificationObservation] else { return }
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
|
| 200 |
-
|
| 201 |
-
}
|
| 202 |
-
```
|
| 203 |
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
-
|
| 207 |
-
- **Classes**: 3 (hand, arm, not_hand)
|
| 208 |
-
- **Input Size**: 224x224
|
| 209 |
-
- **Training Data**: 1740 images
|
| 210 |
-
- **Accuracy**: >96%
|
| 211 |
|
| 212 |
-
|
| 213 |
|
| 214 |
-
|
| 215 |
-
- 704 hand images
|
| 216 |
-
- 320 arm images
|
| 217 |
-
- 462 not_hand images
|
| 218 |
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
## Quick Start
|
| 18 |
|
| 19 |
```python
|
| 20 |
from ultralytics import YOLO
|
|
|
|
| 25 |
# Predict on an image
|
| 26 |
results = model.predict('image.jpg')
|
| 27 |
|
| 28 |
+
# Get the prediction
|
| 29 |
+
probs = results[0].probs
|
| 30 |
+
class_id = probs.top1 # 0=hand, 1=arm, 2=not_hand
|
| 31 |
+
confidence = probs.top1conf.item()
|
| 32 |
+
|
| 33 |
+
# Interpret results
|
| 34 |
+
if class_id == 0:
|
| 35 |
+
print(f"✋ Hand detected: {confidence:.1%}")
|
| 36 |
+
elif class_id == 1:
|
| 37 |
+
print(f"💪 Arm detected: {confidence:.1%}")
|
| 38 |
+
else:
|
| 39 |
+
print(f"❌ No hand/arm detected: {confidence:.1%}")
|
| 40 |
```
|
| 41 |
|
| 42 |
+
## Live Demo (Webcam)
|
| 43 |
|
| 44 |
+
```python
|
| 45 |
+
import cv2
|
| 46 |
+
from ultralytics import YOLO
|
| 47 |
|
| 48 |
+
model = YOLO('https://huggingface.co/EtanHey/hand-detection-3class/resolve/main/model.pt')
|
| 49 |
+
cap = cv2.VideoCapture(0)
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
while True:
|
| 52 |
+
ret, frame = cap.read()
|
| 53 |
+
if not ret:
|
| 54 |
+
break
|
|
|
|
| 55 |
|
| 56 |
+
results = model(frame)
|
| 57 |
+
probs = results[0].probs
|
|
|
|
| 58 |
|
| 59 |
+
classes = ['hand', 'arm', 'not_hand']
|
| 60 |
+
label = f"{classes[probs.top1]}: {probs.top1conf:.1%}"
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
cv2.putText(frame, label, (10, 30),
|
| 63 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
| 64 |
+
cv2.imshow('Hand Detection', frame)
|
|
|
|
| 65 |
|
| 66 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 67 |
+
break
|
| 68 |
+
|
| 69 |
+
cap.release()
|
| 70 |
+
cv2.destroyAllWindows()
|
| 71 |
```
|
| 72 |
|
| 73 |
+
## Use in Next.js/Node.js
|
| 74 |
+
|
| 75 |
+
### Option 1: FastAPI Backend + Next.js
|
| 76 |
|
| 77 |
+
**Backend (Python):**
|
| 78 |
```python
|
|
|
|
| 79 |
from fastapi import FastAPI, File, UploadFile
|
| 80 |
from ultralytics import YOLO
|
|
|
|
| 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("/detect")
|
| 88 |
+
async def detect(file: UploadFile = File(...)):
|
| 89 |
+
image = Image.open(io.BytesIO(await file.read()))
|
|
|
|
|
|
|
| 90 |
results = model.predict(image)
|
| 91 |
probs = results[0].probs
|
| 92 |
|
|
|
|
| 93 |
return {
|
| 94 |
+
"class": ['hand', 'arm', 'not_hand'][probs.top1],
|
| 95 |
+
"confidence": float(probs.top1conf)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
}
|
| 97 |
```
|
| 98 |
|
| 99 |
+
**Frontend (Next.js):**
|
|
|
|
| 100 |
```javascript
|
| 101 |
+
async function detectHand(imageFile) {
|
| 102 |
+
const formData = new FormData();
|
| 103 |
+
formData.append('file', imageFile);
|
| 104 |
|
| 105 |
+
const response = await fetch('http://localhost:8000/detect', {
|
| 106 |
+
method: 'POST',
|
| 107 |
+
body: formData
|
| 108 |
+
});
|
| 109 |
|
| 110 |
+
const result = await response.json();
|
| 111 |
+
console.log(`Detected: ${result.class} (${result.confidence * 100}%)`);
|
| 112 |
+
}
|
| 113 |
+
```
|
| 114 |
|
| 115 |
+
### Option 2: ONNX for Browser
|
|
|
|
| 116 |
|
| 117 |
+
```bash
|
| 118 |
+
# Convert to ONNX first
|
| 119 |
+
from ultralytics import YOLO
|
| 120 |
+
model = YOLO('model.pt')
|
| 121 |
+
model.export(format='onnx')
|
| 122 |
+
```
|
| 123 |
|
| 124 |
+
Then use with ONNX Runtime Web:
|
| 125 |
+
```javascript
|
| 126 |
+
import * as ort from 'onnxruntime-web';
|
| 127 |
|
| 128 |
+
const session = await ort.InferenceSession.create('/model.onnx');
|
| 129 |
+
// Process and run inference...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
```
|
| 131 |
|
| 132 |
+
## React Native
|
| 133 |
|
| 134 |
```javascript
|
| 135 |
+
const detectHand = async (imageUri) => {
|
| 136 |
+
const formData = new FormData();
|
| 137 |
+
formData.append('image', {
|
| 138 |
+
uri: imageUri,
|
| 139 |
+
type: 'image/jpeg',
|
| 140 |
+
name: 'photo.jpg'
|
| 141 |
+
});
|
| 142 |
+
|
| 143 |
+
const response = await fetch('YOUR_API_URL/detect', {
|
| 144 |
+
method: 'POST',
|
| 145 |
+
body: formData
|
| 146 |
+
});
|
| 147 |
+
|
| 148 |
+
const result = await response.json();
|
| 149 |
+
Alert.alert(`Detected: ${result.class}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
};
|
| 151 |
```
|
| 152 |
|
| 153 |
+
## cURL Test
|
| 154 |
|
| 155 |
```bash
|
| 156 |
+
curl -X POST -F "file=@test.jpg" http://localhost:8000/detect
|
|
|
|
|
|
|
|
|
|
| 157 |
```
|
| 158 |
|
| 159 |
+
## Model Details
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
+
- **Architecture**: YOLOv8s-cls (5M parameters)
|
| 162 |
+
- **Classes**: 3 (hand, arm, not_hand)
|
| 163 |
+
- **Input Size**: 224x224
|
| 164 |
+
- **Accuracy**: >96% on validation set
|
| 165 |
+
- **Size**: ~3MB
|
| 166 |
|
| 167 |
+
## Training Data
|
|
|
|
| 168 |
|
| 169 |
+
- **Total Images**: 1,740
|
| 170 |
+
- **Distribution**:
|
| 171 |
+
- Hand: 704 images (40%)
|
| 172 |
+
- Arm: 320 images (18%)
|
| 173 |
+
- Not Hand: 462 images (27%)
|
| 174 |
+
- Val: 254 images (15%)
|
| 175 |
|
| 176 |
+
## Performance
|
|
|
|
|
|
|
| 177 |
|
| 178 |
+
| Metric | Value |
|
| 179 |
+
|--------|-------|
|
| 180 |
+
| Validation Accuracy | 96.3% |
|
| 181 |
+
| Inference Speed | 30+ FPS (Apple M1) |
|
| 182 |
+
| Model Size | 2.97 MB |
|
| 183 |
|
| 184 |
+
## License
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
+
MIT - Free for commercial use
|
| 187 |
|
| 188 |
+
## Citation
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
+
If you use this model, please cite:
|
| 191 |
+
```
|
| 192 |
+
@software{hand_detection_yolo_2024,
|
| 193 |
+
author = {EtanHey},
|
| 194 |
+
title = {Hand Detection YOLOv8 Model},
|
| 195 |
+
year = {2024},
|
| 196 |
+
publisher = {HuggingFace},
|
| 197 |
+
url = {https://huggingface.co/EtanHey/hand-detection-3class}
|
| 198 |
+
}
|
| 199 |
+
```
|