This repository contains code for annotating videos using the YOLOv10 object detection model. The application processes each frame of a video, detects objects, annotates them, and saves both the annotated video and a JSON file with the detection results.
- Object Detection: Uses YOLOv10 to detect objects in each frame of a video.
- Video Processing: Reads a video file, annotates each frame with bounding boxes and labels, and writes the results to a new video file.
- Results Export: Saves detection results in a JSON file, including object classes, confidence scores, and bounding boxes.
- Python 3.7 or later
cv2
(OpenCV) - for video processing and annotationultralytics
- for YOLO object detectiontorch
- for PyTorch backend
You can install the required Python packages using pip:
pip install opencv-python ultralytics torch
-
Prepare the Model:
- Download the YOLOv10 model weights (
best.pt
) and place it in the project directory.
- Download the YOLOv10 model weights (
-
Prepare the Video:
- Place your video file in the
test_objects
directory or update thevideo_path
variable in the code to point to your video file.
- Place your video file in the
-
Run the Script:
Save the provided code in a file named
videoResNativa.py
and run:python videoResNativa.py
-
Outputs:
- The annotated video will be saved as
annotated_output.mp4
. - Detection results for each frame will be saved in
predictions.json
.
- The annotated video will be saved as
-
Device Setup:
- Checks if CUDA is available and sets the device to GPU or CPU accordingly.
-
Model Loading:
- Loads the YOLOv10 model (
best.pt
) and moves it to the selected device.
- Loads the YOLOv10 model (
-
Video Processing:
- Opens the video file, retrieves video properties, and sets up a
VideoWriter
to save the annotated video. - Processes each frame, performs object detection, annotates the frame, and saves it to the output video.
- Opens the video file, retrieves video properties, and sets up a
-
Results Handling:
- Collects detection results for each frame and saves them in a JSON file.
-
Resource Management:
- Releases video capture and writer resources, and closes any OpenCV windows.
- Model Path: Update the
YOLO
constructor argument with the path to your model weights if different frombest.pt
. - Video Path: Update the
video_path
variable with the path to your input video file. - Output Paths: Modify
output_path
and JSON file paths as needed.
- Ensure that the video file path is correct and the file is accessible.
- Make sure all required packages are installed and compatible with your Python version.
- Check the console output for any errors or warnings during execution.