This script processes a video to detect and analyze the movement of a pantograph, pantobar, and cables using YOLOv8 for object detection. It tracks horizontal and vertical movement, detects sparks, and records data in a CSV file.
- Python 3.6 or later
- OpenCV (
opencv-python) - NumPy
ultralytics(for YOLOv8)
You can install the required packages using pip:
pip install opencv-python-headless numpy ultralytics-
YOLO Model: Ensure you have a trained YOLOv8 model for object detection. Update the
model_pathvariable with the path to your model's weights. -
Video Paths: Update
input_video_pathandoutput_video_pathwith the paths to your input and output video files, respectively. -
CSV Output: Set
csv_output_pathto the desired location for saving the CSV file that will contain movement and sparks data.
-
Object Detection: Uses YOLOv8 to detect pantograph, pantobar, and cables in each frame of the video.
-
Movement Tracking: Calculates the horizontal and vertical movement of the pantograph between frames.
-
Sparks Detection: Identifies sparks in the pantobar region by analyzing bright spots.
-
Contact Points: Computes distances between the pantobar and cables at contact points.
-
CSV Logging: Logs timestamped movement data and the number of sparks detected into a CSV file.
-
Video Output: Saves the processed video with visual annotations.
-
Update the
input_video_path,output_video_path, andcsv_output_pathvariables in the script with appropriate paths. -
Run the script:
python your_script_name.py
-
The script will process the input video, detect relevant objects, track movements, detect sparks, and save the results to the specified output video and CSV file.
import cv2
import numpy as np
import csv
from ultralytics import YOLO
# YOLO model path
model_path = '/path/to/your/yolo_model.pt'
model = YOLO(model_path)
# Video and CSV paths
input_video_path = '/path/to/input_video.mp4'
output_video_path = '/path/to/output_video.mp4'
csv_output_path = '/path/to/output_data.csv'
# Your script here- Ensure the YOLO model is correctly trained and configured for detecting the required objects.
- Adjust the detection parameters as needed for different video conditions.
- The output video and CSV file will be saved in the specified paths.
This project is licensed under the MIT License. See the LICENSE file for details.