-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetector.py
More file actions
60 lines (48 loc) · 1.83 KB
/
detector.py
File metadata and controls
60 lines (48 loc) · 1.83 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from ultralytics import YOLO
import cv2
import numpy as np
from PIL import Image
class ObjectDetector:
def __init__(self, model_path='yolov8m.pt'):
"""
Initialize the YOLOv8 model.
Uses the nano model by default for speed.
"""
self.model = YOLO(model_path)
def process_image(self, image_bytes, confidence_threshold=0.25):
"""
Process an image and return detection results.
Args:
image_bytes: Image data in bytes
confidence_threshold: Minimum confidence score to include a detection
Returns:
dict: Detection results containing boxes, labels, and original image size
"""
# Convert bytes to numpy array
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if img is None:
raise ValueError("Could not decode image")
# Run inference
results = self.model(img, conf=confidence_threshold)
detections = []
result = results[0] # We only process one image at a time
# Get class names dictionary
names = result.names
for box in result.boxes:
# Get box coordinates (xyxy format)
x1, y1, x2, y2 = box.xyxy[0].tolist()
# Get confidence and class id
conf = float(box.conf[0])
cls_id = int(box.cls[0])
label = names[cls_id]
detections.append({
'bbox': [x1, y1, x2, y2],
'confidence': conf,
'label': label,
'class_id': cls_id
})
return {
'detections': detections,
'image_size': {'width': img.shape[1], 'height': img.shape[0]}
}