1+ """
2+ Demo script to run YOLO object detection on a Gazebo simulation.
3+
4+ This code subscribes to the front camera feed from the GEM e2/e4 model and applies YOLO-based object detection to the incoming images.
5+
6+ Visualization:
7+ - Use RViz or rqt to monitor the topics.
8+
9+ ROS Topics:
10+ - Raw camera feed: /gem/debug
11+ - YOLO detection output: /gem/image_detection
12+ """
13+
114
215# Python
316import os
2639
2740
2841class ObjectDetection (Component ):
29- # TODO: Pull params into a JSON/yaml
30- # TODO: Convert some lists into np.arrays, vectorize calculations
31- # TODO: Implement logging instead of print, cleanup comments
32- # TODO: Cleanup funcs + split into separate classes
33- # TODO: Decide if we want to name dets "peds" or "objs"/"agents"
34- # Maybe peds for now and Agents in agent_detection.py?
35- def __init__ (self , vehicle_interface : GEMInterface ) -> None :
3642
43+ def __init__ (self , vehicle_interface : GEMInterface ) -> None :
44+ """
45+ Initializes essential functions required to run the YOLO model.
46+ """
3747
3848 # vehicle interface
3949 self .vehicle_interface = vehicle_interface
40-
41-
4250 # cv2 to ros converter
4351 self .bridge = CvBridge ()
4452
45-
4653 # yolo model
4754 self .detector = YOLO (os .getcwd ()+ '/GEMstack/knowledge/detection/yolov8n.pt' )
4855 self .confidence = 0.1
4956 self .classes_to_detect = 0
5057
5158
5259 def initialize (self ):
60+ """
61+ Defines callback functions for subscribing to the front camera image stream and sets up publishers for debugging purposes.
62+ """
63+
5364 super ().initialize ()
5465 self .vehicle_interface .subscribe_sensor ('front_camera' ,self .front_camera_callback , type = cv2 .Mat )
55- # self.pub_pedestrians_pc2 = rospy.Publisher("/point_cloud/pedestrians", PointCloud2, queue_size=10)
56- # self.pub_obj_centers_pc2 = rospy.Publisher("/point_cloud/obj_centers", PointCloud2, queue_size=10)
57- # self.pub_bboxes_markers = rospy.Publisher("/markers/bboxes", MarkerArray, queue_size=10)
5866 self .pub_image = rospy .Publisher ("/gem/debug" , Image , queue_size = 1 )
5967 self .pub_detimage = rospy .Publisher ("/gem/image_detection" , Image , queue_size = 1 )
6068
6169
6270 def update (self , vehicle : VehicleState ) -> Dict [str ,AgentState ]:
6371
72+ """
73+ Displays vehicle statistics in the GLOBAL reference frame.
74+
75+ This function also allows switching between coordinate frames using the `VehicleState -> pose` method.
76+
77+ Returning an `AgentState` will automatically log detected objects.
78+ """
79+
80+
6481 print (f"VEHICLE State at time: { vehicle .pose .t } " )
6582
6683 print (f"x: { vehicle .pose .x } " )
@@ -79,6 +96,10 @@ def update(self, vehicle : VehicleState) -> Dict[str,AgentState]:
7996
8097 def front_camera_callback (self , image : cv2 .Mat ):
8198
99+ """
100+ A simple callback function that processes incoming images, runs them through a YOLO model, and visualizes the detections.
101+ """
102+
82103
83104 ros_img = self .bridge .cv2_to_imgmsg (image , 'bgr8' )
84105 self .pub_image .publish (ros_img )
@@ -132,10 +153,6 @@ def front_camera_callback(self, image: cv2.Mat):
132153
133154 ros_img = self .bridge .cv2_to_imgmsg (image , 'bgr8' )
134155 self .pub_detimage .publish (ros_img )
135-
136-
137-
138-
139156
140157
141158 def rate (self ):
0 commit comments