Spaces:
Runtime error
Runtime error
Update services/detection_service.py
Browse files
services/detection_service.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import cv2
|
| 3 |
|
|
@@ -6,4 +7,31 @@ object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
|
|
| 6 |
def detect_objects(image_path):
|
| 7 |
image = cv2.imread(image_path)
|
| 8 |
results = object_detector(image)
|
| 9 |
-
return [r for r in results if r['score'] > 0.7]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''
|
| 2 |
from transformers import pipeline
|
| 3 |
import cv2
|
| 4 |
|
|
|
|
| 7 |
def detect_objects(image_path):
|
| 8 |
image = cv2.imread(image_path)
|
| 9 |
results = object_detector(image)
|
| 10 |
+
return [r for r in results if r['score'] > 0.7]
|
| 11 |
+
|
| 12 |
+
'''
|
| 13 |
+
|
| 14 |
+
# services/detection_service.py
|
| 15 |
+
|
| 16 |
+
from transformers import pipeline
|
| 17 |
+
from PIL import Image
|
| 18 |
+
|
| 19 |
+
# ✅ Load Hugging Face DETR pipeline properly
|
| 20 |
+
object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
|
| 21 |
+
|
| 22 |
+
def detect_objects(image_path):
|
| 23 |
+
"""
|
| 24 |
+
Detect objects using Hugging Face DETR pipeline.
|
| 25 |
+
- Accepts a file path to a local image.
|
| 26 |
+
- Converts image to PIL format.
|
| 27 |
+
- Feeds into Hugging Face detector.
|
| 28 |
+
- Returns list of high-confidence detections.
|
| 29 |
+
"""
|
| 30 |
+
# ✅ Correct way: Open image properly
|
| 31 |
+
image = Image.open(image_path).convert("RGB")
|
| 32 |
+
|
| 33 |
+
# ✅ Run inference
|
| 34 |
+
results = object_detector(image)
|
| 35 |
+
|
| 36 |
+
# ✅ Return only high-confidence detections
|
| 37 |
+
return [r for r in results if r['score'] > 0.7]
|