File size: 1,039 Bytes
197e2d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'''
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]