Skip to content

Commit 992dd9e

Browse files
committed
Initial Commit for hw1
1 parent 6f4aa44 commit 992dd9e

2 files changed

Lines changed: 51 additions & 17 deletions

File tree

homework/pedestrian_detection.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from ...state import AllState,VehicleState,ObjectPose,ObjectFrameEnum,AgentState,AgentEnum,AgentActivityEnum
22
from ..interface.gem import GEMInterface
33
from ..component import Component
4-
#from ultralytics import YOLO
5-
#import cv2
4+
from ultralytics import YOLO
5+
import cv2
66
from typing import Dict
77

88
def box_to_fake_agent(box):
@@ -20,7 +20,7 @@ class PedestrianDetector2D(Component):
2020
"""Detects pedestrians."""
2121
def __init__(self,vehicle_interface : GEMInterface):
2222
self.vehicle_interface = vehicle_interface
23-
#self.detector = YOLO('../../knowledge/detection/yolov8n.pt')
23+
# self.detector = YOLO('../../knowledge/detection/yolov11n.pt')
2424
self.last_person_boxes = []
2525

2626
def rate(self):
@@ -34,17 +34,22 @@ def state_outputs(self):
3434

3535
def initialize(self):
3636
#tell the vehicle to use image_callback whenever 'front_camera' gets a reading, and it expects images of type cv2.Mat
37-
#self.vehicle_interface.subscribe_sensor('front_camera',self.image_callback,cv2.Mat)
38-
pass
39-
40-
#def image_callback(self, image : cv2.Mat):
41-
# detection_result = self.detector(image)
42-
# self.last_person_boxes = []
43-
# #uncomment if you want to debug the detector...
44-
# #for bb in self.last_person_boxes:
45-
# # x,y,w,h = bb
46-
# # cv2.rectangle(image, (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), (255, 0, 255), 3)
47-
# #cv2.imwrite("pedestrian_detections.png",image)
37+
self.detector = YOLO('../../knowledge/detection/yolov11n.pt')
38+
self.vehicle_interface.subscribe_sensor('front_camera', self.image_callback, cv2.Mat)
39+
40+
def image_callback(self, image: cv2.Mat):
41+
"""Image processing callback with original detection logic"""
42+
results = self.detector(image, conf=0.5)
43+
boxes = results[0].boxes
44+
45+
if len(boxes) == 0:
46+
self.last_person_boxes = []
47+
return
48+
cls_ids = boxes.cls.cpu()
49+
xywh = boxes.xywh.cpu()
50+
person_mask = (cls_ids == 0)
51+
52+
self.last_person_boxes = [tuple(map(float, box)) for box in xywh[person_mask]]
4853

4954
def update(self, vehicle : VehicleState) -> Dict[str,AgentState]:
5055
res = {}

homework/person_detector.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
11
#from ultralytics import YOLO
22
import cv2
33
import sys
4+
from ultralytics import YOLO
5+
import numpy as np
46

5-
def person_detector(img : cv2.Mat):
6-
#TODO: implement me to produce a list of (x,y,w,h) bounding boxes of people in the image
7-
return []
7+
8+
def person_detector(img: cv2.Mat) -> list[tuple[float, float, float, float]]:
9+
"""Detect persons in an image and return their bounding boxes in xywh format.
10+
11+
Args:
12+
img (cv2.Mat): Input image in OpenCV BGR format
13+
14+
Returns:
15+
List of bounding box tuples (x_center, y_center, width, height)
16+
"""
17+
# Initialize YOLOv11 model
18+
model = YOLO("yolo11n.pt")
19+
20+
# Perform inference with confidence threshold
21+
results = model(img, conf=0.5)
22+
23+
# Extract detection boxes from results
24+
boxes = results[0].boxes
25+
if len(boxes) == 0:
26+
return []
27+
28+
# Convert tensor data to CPU (assuming CUDA acceleration)
29+
cls_ids = boxes.cls.cpu() # Class IDs tensor
30+
xywh = boxes.xywh.cpu() # Box coordinates in xywh format
31+
32+
# Create boolean mask for person class (ID 0)
33+
person_mask = (cls_ids == 0)
34+
35+
# Convert qualified boxes to Python native types
36+
return [tuple(map(float, box)) for box in xywh[person_mask]]
837

938
def main(fn):
1039
image = cv2.imread(fn)

0 commit comments

Comments
 (0)