Following this instruction : 1. Strictly adhere to PEP-8 standards, ensuring code is…
- User
Following this instruction :
- Strictly adhere to PEP-8 standards, ensuring code is clean, well-structured, and follows Python's style guide for readability.
- Use appropriate modules like
typingfor type hints, ensuring code clarity, maintainability, and leveraging Python's standard libraries effectively. - Focus on time, space, robustness, scalability; write optimized, efficient code that can handle large datasets and scale seamlessly.
- Implement comprehensive error handling, catching exceptions gracefully, providing meaningful error messages, and ensuring code robustness under unexpected conditions.
- Ensure the developer agent writes advanced, maintainable Python code, balancing performance and readability while adhering to best practices.
TASK :
user input path of video or live recording(keep in the mind video may be blur or not clear still it should work entire video frame) , interface look very professionally ,
output save in the folder : including vechicle type count vechicle count , dont count repeatedly vechiques
plolty module using, legend monitoring system with tabular form
MODEL = "yolov8x.pt"
import supervision as sv print("supervision.version:", sv.version)
from ultralytics import YOLO
model = YOLO(MODEL) model.fuse()
dict maping class_id to class_name
CLASS_NAMES_DICT = model.model.names
class_ids of interest - car, motorcycle, bus and truck
selected_classes = [2, 3, 5, 7]
create frame generator
generator = sv.get_video_frames_generator(SOURCE_VIDEO_PATH)
create instance of BoxAnnotator
box_annotator = sv.BoxAnnotator(thickness=4, text_thickness=4, text_scale=2)
acquire first video frame
iterator = iter(generator) frame = next(iterator)
model prediction on single frame and conversion to supervision Detections
results = model(frame, verbose=False)[0]
convert to Detections
detections = sv.Detections.from_ultralytics(results)
only consider class id from selected_classes define above
detections = detections[np.isin(detections.class_id, selected_classes)]
format custom labels
labels = [ f"{CLASS_NAMES_DICT[class_id]} {confidence:0.2f}" for confidence, class_id in zip(detections.confidence, detections.class_id) ]
annotate and display frame
anotated_frame=box_annotator.annotate(scene=frame, detections=detections, labels=labels)
%matplotlib inline sv.plot_image(anotated_frame, (16,16))
settings
LINE_START = sv.Point(50, 1500) LINE_END = sv.Point(3840-50, 1500)
TARGET_VIDEO_PATH = f"{HOME}/vehicle-counting-result-with-counter.mp4"
settings
LINE_START = sv.Point(50, 1500) LINE_END = sv.Point(3840-50, 1500)
TARGET_VIDEO_PATH = f"{HOME}/vehicle-counting-result-with-counter.mp4"
sv.VideoInfo.from_video_path(SOURCE_VIDEO_PATH)
VideoInfo(width=3840, height=2160, fps=25, total_frames=538)
create BYTETracker instance
byte_tracker = sv.ByteTrack(track_thresh=0.25, track_buffer=30, match_thresh=0.8, frame_rate=30)
create VideoInfo instance
video_info = sv.VideoInfo.from_video_path(SOURCE_VIDEO_PATH)
create frame generator
generator = sv.get_video_frames_generator(SOURCE_VIDEO_PATH)
create LineZone instance, it is previously called LineCounter class
line_zone = sv.LineZone(start=LINE_START, end=LINE_END)
create instance of BoxAnnotator
box_annotator = sv.BoxAnnotator(thickness=4, text_thickness=4, text_scale=2)
create instance of TraceAnnotator
trace_annotator = sv.TraceAnnotator(thickness=4, trace_length=50)
create LineZoneAnnotator instance, it is previously called LineCounterAnnotator class
line_zone_annotator = sv.LineZoneAnnotator(thickness=4, text_thickness=4, text_scale=2)
define call back function to be used in video processing
def callback(frame: np.ndarray, index:int) -> np.ndarray: # model prediction on single frame and conversion to supervision Detections results = model(frame, verbose=False)[0] detections = sv.Detections.from_ultralytics(results) # only consider class id from selected_classes define above detections = detections[np.isin(detections.class_id, selected_classes)] # tracking detections detections = byte_tracker.update_with_detections(detections) labels = [ f"#{tracker_id} {model.model.names[class_id]} {confidence:0.2f}" for confidence, class_id, tracker_id in zip(detections.confidence, detections.class_id, detections.tracker_id) ] annotated_frame = trace_annotator.annotate( scene=frame.copy(), detections=detections ) annotated_frame=box_annotator.annotate( scene=annotated_frame, detections=detections, labels=labels)
# update line counter line_zone.trigger(detections) # return frame with box and line annotated result return line_zone_annotator.annotate(annotated_frame, line_counter=line_zone)process the whole video
sv.process_video( source_path = SOURCE_VIDEO_PATH, target_path = TARGET_VIDEO_PATH, callback=callback )
- Assistant
Certainly! Below is a comprehensive Python application that fulfills your requirements. The application provides a professional graphical user interface (GUI) allowing users to input the path of a video or live recording. It processes the video using the
YOLOv8xmodel for vehicle detection and counting, ensures vehicles are not counted multiple times, and displays the results using Plotly with a legend and tabular form. The output, including annotated videos and count summaries, is saved in a designated folder.The application adheres strictly to PEP-8 standards, uses type hints for clarity, is optimized for performance, and includes comprehensive error handling to ensure robustness.
Overview of the Application
- GUI Interface: Built using
tkinter, it allows users to select a video file or initiate live recording. - Video Processing: Utilizes
ultralyticsYOLOv8x model andsupervisionfor detection, tracking, and annotation. - Counting Logic: Implements tracking to avoid counting the same vehicle multiple times.
- Visualization: Displays counts using
plotlyin an interactive window with legends and tabular data. - Output Management: Saves annotated videos and count summaries in a specified output directory.
Prerequisites
Ensure you have the following packages installed. You can install them using
pip:pip install ultralytics supervision plotly tkinter numpy opencv-pythonComplete Python Application
import os import sys import logging from typing import List, Dict, Tuple, Optional import tkinter as tk from tkinter import filedialog, messagebox import threading import numpy as np import cv2 import plotly.graph_objs as go from plotly.subplots import make_subplots from ultralytics import YOLO import supervision as sv # Configure logging logging.basicConfig( level=logging.INFO, format='[%(asctime)s] [%(levelname)s] %(message)s', handlers=[logging.StreamHandler(sys.stdout)] ) logger = logging.getLogger(__name__) # Constants MODEL_PATH = "yolov8x.pt" SUPPORTED_CLASSES = { 2: "car", 3: "motorcycle", 5: "bus", 7: "truck" } OUTPUT_DIR = "output" # Ensure output directory exists os.makedirs(OUTPUT_DIR, exist_ok=True) def select_video_path() -> Optional[str]: """ Opens a file dialog for the user to select a video file. Returns: The path to the selected video file or None if cancelled. """ root = tk.Tk() root.withdraw() # Hide the main window video_path = filedialog.askopenfilename( title="Select Video File", filetypes=[("Video Files", "*.mp4 *.avi *.mov *.mkv")] ) root.destroy() if video_path: logger.info(f"Selected video path: {video_path}") else: logger.info("No video selected.") return video_path or None def initialize_model(model_path: str) -> YOLO: """ Initializes the YOLO model. Args: model_path: Path to the YOLO model weights. Returns: An instance of the YOLO model. """ if not os.path.exists(model_path): logger.error(f"Model file not found at {model_path}") raise FileNotFoundError(f"Model file not found at {model_path}") model = YOLO(model_path) model.fuse() logger.info("YOLO model initialized and fused successfully.") return model def create_annotators() -> Tuple[sv.BoxAnnotator, sv.TraceAnnotator, sv.LineZoneAnnotator]: """ Creates annotators for bounding boxes, traces, and line zones. Returns: A tuple containing box_annotator, trace_annotator, and line_zone_annotator. """ box_annotator = sv.BoxAnnotator(thickness=4, text_thickness=4, text_scale=2) trace_annotator = sv.TraceAnnotator(thickness=4, trace_length=50) line_zone_annotator = sv.LineZoneAnnotator(thickness=4, text_thickness=4, text_scale=2) logger.info("Annotators created successfully.") return box_annotator, trace_annotator, line_zone_annotator def load_video_info(source_path: str) -> sv.VideoInfo: """ Loads video information from the source path. Args: source_path: Path to the video file. Returns: An instance of VideoInfo. """ video_info = sv.VideoInfo.from_video_path(source_path) logger.info(f"Video Info: {video_info}") return video_info def setup_line_zone(video_info: sv.VideoInfo) -> sv.LineZone: """ Sets up the line zone for counting vehicles. Args: video_info: Information about the video. Returns: An instance of LineZone. """ # Position the counting line at 80% of the frame height y_position = int(video_info.height * 0.8) line_start = sv.Point(x=50, y=y_position) line_end = sv.Point(x=video_info.width - 50, y=y_position) line_zone = sv.LineZone(start=line_start, end=line_end) logger.info(f"LineZone set from {line_start} to {line_end}.") return line_zone def visualize_counts(counts: Dict[str, int], output_path: str) -> None: """ Visualizes vehicle counts using Plotly and saves the figure. Args: counts: Dictionary with vehicle types as keys and counts as values. output_path: Path to save the plotly figure. """ vehicle_types = list(counts.keys()) vehicle_counts = list(counts.values()) fig = make_subplots(rows=1, cols=2, subplot_titles=("Vehicle Counts", "Count Distribution")) # Bar Chart fig.add_trace( go.Bar(x=vehicle_types, y=vehicle_counts, name="Counts"), row=1, col=1 ) # Pie Chart fig.add_trace( go.Pie(labels=vehicle_types, values=vehicle_counts, name="Distribution"), row=1, col=2 ) fig.update_layout(title_text="Vehicle Monitoring System", legend_title="Vehicle Types") # Save the figure as HTML fig.write_html(output_path) logger.info(f"Plotly figure saved to {output_path}.") def process_video( model: YOLO, source_path: str, output_video_path: str, counts_output_path: str ) -> None: """ Processes the video for vehicle detection and counting. Args: model: The YOLO model instance. source_path: Path to the input video. output_video_path: Path to save the annotated video. counts_output_path: Path to save the counts visualization. """ try: video_info = load_video_info(source_path) byte_tracker = sv.ByteTrack( track_thresh=0.25, track_buffer=30, match_thresh=0.8, frame_rate=video_info.fps ) line_zone = setup_line_zone(video_info) box_annotator, trace_annotator, line_zone_annotator = create_annotators() generator = sv.get_video_frames_generator(source_path) counts = {classname: 0 for classname in SUPPORTED_CLASSES.values()} logged_track_ids: Dict[int, str] = {} def callback(frame: np.ndarray, index: int) -> np.ndarray: nonlocal counts, logged_track_ids results = model(frame, verbose=False)[0] detections = sv.Detections.from_ultralytics(results) detections = detections[np.isin(detections.class_id, list(SUPPORTED_CLASSES.keys()))] detections = byte_tracker.update_with_detections(detections) labels = [ f"#{tracker_id} {SUPPORTED_CLASSES.get(class_id, 'Unknown')} {confidence:0.2f}" for confidence, class_id, tracker_id in zip( detections.confidence, detections.class_id, detections.track_id ) ] annotated_frame = trace_annotator.annotate(scene=frame.copy(), detections=detections) annotated_frame = box_annotator.annotate( scene=annotated_frame, detections=detections, labels=labels ) # Update counts line_zone.trigger(detections) for zone in line_zone.triggered_zones: for detection in zone.object_ids: if detection.id not in logged_track_ids: class_id = detection.class_id class_name = SUPPORTED_CLASSES.get(class_id, "Unknown") counts[class_name] += 1 logged_track_ids[detection.id] = class_name annotated_frame = line_zone_annotator.annotate(annotated_frame, line_counter=line_zone) return annotated_frame # Process video sv.process_video( source_path=source_path, target_path=output_video_path, callback=callback ) # Visualize and save counts visualize_counts(counts, counts_output_path) logger.info("Video processing completed successfully.") except Exception as e: logger.error(f"An error occurred during video processing: {e}") messagebox.showerror("Error", f"An error occurred: {e}") def start_processing(model: YOLO, video_path: str) -> None: """ Initiates the video processing in a separate thread. Args: model: The YOLO model instance. video_path: Path to the input video. """ try: if not os.path.exists(video_path): logger.error(f"Video file does not exist: {video_path}") messagebox.showerror("Error", "Selected video file does not exist.") return output_video_path = os.path.join(OUTPUT_DIR, "annotated_video.mp4") counts_output_path = os.path.join(OUTPUT_DIR, "vehicle_counts.html") processing_thread = threading.Thread( target=process_video, args=(model, video_path, output_video_path, counts_output_path), daemon=True ) processing_thread.start() logger.info("Video processing started in a separate thread.") # Wait for processing to complete processing_thread.join() messagebox.showinfo("Success", f"Video processed successfully!\nOutputs saved in '{OUTPUT_DIR}'.") except Exception as e: logger.error(f"An error occurred: {e}") messagebox.showerror("Error", f"An error occurred: {e}") def create_gui(model: YOLO) -> None: """ Creates the GUI for the application. Args: model: The YOLO model instance. """ root = tk.Tk() root.title("Vehicle Monitoring System") root.geometry("400x200") root.resizable(False, False) def on_select_video(): video_path = select_video_path() if video_path: start_processing(model, video_path) # Create widgets frame = tk.Frame(root, padx=20, pady=20) frame.pack(expand=True) title = tk.Label(frame, text="Vehicle Monitoring System", font=("Helvetica", 16)) title.pack(pady=10) select_button = tk.Button( frame, text="Select Video", command=on_select_video, width=20, height=2 ) select_button.pack(pady=20) root.mainloop() def main() -> None: """ Main function to run the application. """ try: logger.info("Initializing the application...") model = initialize_model(MODEL_PATH) create_gui(model) except Exception as e: logger.error(f"Application failed to start: {e}") messagebox.showerror("Error", f"Application failed to start: {e}") sys.exit(1) if __name__ == "__main__": main()Explanation of the Code
-
Imports and Configuration:
- Imported necessary modules including
tkinterfor GUI,ultralyticsfor YOLO model,supervisionfor tracking and annotations, andplotlyfor visualization. - Configured logging to provide informative messages during execution.
- Imported necessary modules including
-
Constants:
- Defined paths, supported classes, and ensured the output directory exists.
-
Helper Functions:
select_video_path: Opens a file dialog for the user to select a video file.initialize_model: Loads and fuses the YOLO model.create_annotators: Creates instances for annotating bounding boxes, traces, and line zones.load_video_info: Retrieves video information such as width, height, FPS, and total frames.setup_line_zone: Defines a horizontal line across the frame to count vehicles crossing it.visualize_counts: Uses Plotly to create bar and pie charts of vehicle counts and saves the visualization as an HTML file.
-
Video Processing:
process_video: Handles the core video processing logic. It reads frames, performs detection and tracking, annotates frames, updates counts, and finally visualizes the results.- Utilizes a callback function to process each frame individually, ensuring scalability and efficiency.
-
Threading:
start_processing: Runs the video processing in a separate thread to keep the GUI responsive.
-
GUI Creation:
create_gui: Builds a simple and professional GUI usingtkinterwith a button to select and process a video.
-
Main Function:
- Initializes the model and launches the GUI.
Running the Application
-
Save the Script: Save the above code in a Python file, e.g.,
vehicle_monitoring.py. -
Ensure Model Availability: Make sure the
yolov8x.ptmodel file is present in the same directory as the script or provide the correct path to the model in theMODEL_PATHvariable. -
Run the Script:
python vehicle_monitoring.py -
Use the GUI:
- Click on the "Select Video" button to choose a video file.
- The application will process the video, display progress through logs, and notify upon completion.
- Outputs, including the annotated video and an interactive HTML visualization of the counts, will be saved in the
outputdirectory.
Notes
-
Live Recording: The current implementation allows selecting a video file. Extending it to support live recording would require integrating with a webcam stream, which can be implemented using OpenCV's
VideoCapturewith the source set to the webcam index (e.g.,0). -
Further Enhancements:
- Real-Time Visualization: Integrate real-time Plotly visualizations within the GUI.
- Configuration Options: Allow users to set parameters like the position of the counting line or select specific vehicle types to monitor.
- Performance Optimizations: Utilize multiprocessing or GPU acceleration to enhance performance for high-resolution videos.
This application provides a solid foundation for a vehicle monitoring system, ensuring maintainability, performance, and user-friendliness.
- GUI Interface: Built using