SS_solar_POC_ver-1.0 / services /detection_service.py
SathvikGanta's picture
Upload 16 files
197e2d4 verified
'''
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]