This system enables real-time monitoring of vehicular traffic using UAV footage. It employs advanced computer vision techniques for:
config.py
) containing model paths and video parameters.CustomTrackZone
ClassThis class extends the TrackZone
solution from Ultralytics to include:
Speed and Direction Calculation:
Data Logging:
Trajectory Visualization:
parse_arguments()
FunctionParses command-line arguments for specifying:
CustomTrackZone
.Run the script with the following arguments:
python uav_traffic_monitoring.py \ --source_video_path path/to/source/video \ --target_video_path path/to/output/video \ --model_path path/to/model
python uav_traffic_monitoring.py \ --source_video_path ./input.mp4 \ --target_video_path ./output.mp4 \ --model_path ./models/yolov8.pt
The generated CSV file includes the following fields:
Here is a brief explanation of some key components:
def calculate_speed_and_direction(self, track_id): # Ensure sufficient history exists for calculation if track_id not in self.track_history or len(self.track_history[track_id]) < 2: return 0, "Unknown", 0 history = self.track_history[track_id] start_pos, end_pos = history[0], history[-1] dx, dy = end_pos[0] - start_pos[0], end_pos[1] - start_pos[1] angle = math.degrees(math.atan2(dy, dx)) % 360 # Determine movement direction direction = "Unknown" for start_angle, end_angle, dir_name in DIRECTION_THRESHOLDS: if start_angle <= angle < end_angle: direction = dir_name break # Compute speed in km/h real_dx = dx / UAV_X_SCALE real_dy = dy / UAV_Y_SCALE distance = math.sqrt(real_dx**2 + real_dy**2) time = len(history) / self.fps speed = (distance / time) * 3.6 # Convert m/s to km/h return speed, direction, angle
def trackzone(self, im0, frame_idx): self.annotator = Annotator(im0, line_width=self.line_width) masked_frame = cv2.bitwise_and(im0, im0, mask=cv2.fillPoly(np.zeros_like(im0[:, :, 0]), [self.region], 255)) self.extract_tracks(masked_frame) current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") with open(self.csv_path, 'a', newline='') as f: writer = csv.writer(f) for box, track_id, cls in zip(self.boxes, self.track_ids, self.clss): x1, y1, x2, y2 = map(int, box) center_x, center_y = (x1 + x2) // 2, (y1 + y2) // 2 # Update and visualize trajectory if track_id not in self.track_history: self.track_history[track_id] = [] self.track_history[track_id].append((center_x, center_y)) points = np.array(self.track_history[track_id], dtype=np.int32) if len(points) >= 2: cv2.polylines(im0, [points], isClosed=False, color=colors(track_id, True), thickness=2) # Log data and annotate frame speed, direction, _ = self.calculate_speed_and_direction(track_id) writer.writerow([current_time, frame_idx, track_id, self.names[cls], round(speed, 2), direction, center_x, center_y]) label = f"{self.names[cls]}:{track_id} {int(speed)}km/h {direction}" if speed > 2 else f"{self.names[cls]}:{track_id} (Stop)" self.annotator.box_label(box, label=label, color=colors(track_id, True)) return im0
This UAV-based traffic monitoring system effectively demonstrates how modern computer vision techniques can be applied to traffic management. By integrating advanced object detection and tracking algorithms, the system achieves accurate vehicle detection, classification, and tracking. The inclusion of trajectory plotting and speed estimation enhances its utility for analyzing traffic patterns and behavior. Furthermore, exporting data to CSV files allows for deeper insights through post-processing and analytics.
The system's modular design makes it extensible for further improvements, such as incorporating multi-camera setups or leveraging additional sensor data. This project serves as a foundation for developing scalable traffic monitoring solutions using UAVs.
There are no models linked
There are no models linked