ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
49.65k stars 16.11k forks source link

Video inference with YOLOv5 model in python #13269

Open mayurkatre18 opened 3 weeks ago

mayurkatre18 commented 3 weeks ago

Search before asking

Question

I have trained yolov5 model for instance segmentation and saved model in .pt format. Now I have to make video inference from this .pt model. Please provide me solution for this. Thank you.

Additional

No response

glenn-jocher commented 3 weeks ago

@mayurkatre18 to perform video inference with your YOLOv5 model, you can use the detect.py script. Here's a basic example:

python detect.py --weights your_model.pt --source your_video.mp4

Replace your_model.pt with your model's path and your_video.mp4 with your video file. For more details, refer to the YOLOv5 documentation.

mayurkatre18 commented 3 weeks ago

@glenn-jocher Thank you for above solution. But I have to perform it without detect.py file is their any solution for it in python.

And Important is I have to intergrate YOLOv5 model i.e. best.pt in mobile application which supports flutter language.

glenn-jocher commented 3 weeks ago

@mayurkatre18 you can perform video inference directly in Python without using detect.py by leveraging the YOLOv5 model and OpenCV. Here's a minimal example:

import cv2
import torch

# Load model
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')

# Open video
cap = cv2.VideoCapture('your_video.mp4')

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break

    # Inference
    results = model(frame)

    # Display results
    results.show()

cap.release()
cv2.destroyAllWindows()

For integrating with a Flutter mobile application, you might consider exporting your model to a format supported by TensorFlow Lite. More details on model export can be found in the YOLOv5 documentation.