In this truncated session, we will focus solely on running YOLOv5 for object detection on a custom video and visualizing the results.
To run the code, you'll need:
- PyTorch
- OpenCV (
cv2
) - YOLOv5 (from GitHub)
You can clone the YOLOv5 repository from GitHub:
git clone https://github.com/ultralytics/yolov5.git
cd yolov5
pip install -U -r requirements.txt
Assume you have a video file named my_video.mp4
. Place it in a directory of your choosing.
Run the following command to perform object detection:
python detect.py --source /path/to/my_video.mp4 --weights yolov5s.pt --conf 0.4
Replace /path/to/my_video.mp4
with the path to your video.
This command will generate an output video with bounding boxes around detected objects.
You can use any video player to view the output video generated by YOLOv5, or you can use OpenCV to visualize it programmatically.
Here's a Python code snippet that uses OpenCV to play the output video:
import cv2
# Initialize OpenCV video capture
video = cv2.VideoCapture('runs/detect/exp/your_output_video.mp4') # Change the path to your output video
while video.isOpened():
ret, frame = video.read()
if not ret:
print("Failed to grab frame or video has ended. Exiting.")
break
# Display the frame
cv2.imshow('YOLOv5 Object Detection', frame)
# Exit if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video and destroy OpenCV windows
video.release()
cv2.destroyAllWindows()
In this code, OpenCV will read the output video generated by YOLOv5 (your_output_video.mp4
) and play it in a window. Press 'q' to close the window and stop the video.
In this simplified session, you learned how to run YOLOv5 object detection on a custom video and visualize the results. You can further explore the various configurations and options YOLOv5 offers to fine-tune the detection to your specific needs.