surveillance / services /detection_service.py
SuriRaja's picture
Update services/detection_service.py
b5a8947
raw
history blame
1.04 kB
'''
from transformers import pipeline
import cv2
object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
def detect_objects(image_path):
image = cv2.imread(image_path)
results = object_detector(image)
return [r for r in results if r['score'] > 0.7]
'''
# services/detection_service.py
from transformers import pipeline
from PIL import Image
# βœ… Load Hugging Face DETR pipeline properly
object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
def detect_objects(image_path):
"""
Detect objects using Hugging Face DETR pipeline.
- Accepts a file path to a local image.
- Converts image to PIL format.
- Feeds into Hugging Face detector.
- Returns list of high-confidence detections.
"""
# βœ… Correct way: Open image properly
image = Image.open(image_path).convert("RGB")
# βœ… Run inference
results = object_detector(image)
# βœ… Return only high-confidence detections
return [r for r in results if r['score'] > 0.7]