11from ...state import AllState ,VehicleState ,ObjectPose ,ObjectFrameEnum ,AgentState ,AgentEnum ,AgentActivityEnum
22from ..interface .gem import GEMInterface
33from ..component import Component
4- # from ultralytics import YOLO
5- # import cv2
4+ from ultralytics import YOLO
5+ import cv2
66from typing import Dict
77
88def 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 = {}
0 commit comments