Run YOLOv5 on Your Webcam

How to perform inference locally with your custom YOLOv5 model weights.

Written by Mohamed Traore

Last published at: May 5th, 2022

Local Inference with YOLOv5 on a Webcam

Clone the Ultralytics YOLOv5 Github repository to your computer and run after saving the model weights from your custom YOLOv5 model training.

  • Make a note of the directory (folder) in which you clone the repository. Save the file path, if you can, for easy access later.
  • GitHub commands cheatsheet

Include the model weights in the "yolov5" folder that was cloned to your file system (computer) from GitHub.

Running the Webcam Model

Once the custom weights have been added to the yolov5 directory on your local computer, then you can run the model in your webcam after changing to the yolov5 directory in your terminal.

Basic Command: Run YOLOv5 on a Webcam

As the "yolov5" directory in the example image resides in the "mtraor3" directory, and we are using the yolov5s model weights saved to the yolov5 folder ( /Users/mtraor3/yolov5 ), the sample code we enter in our terminal to run locally on the webcam would look like this:

python ./Users/mtraor3/yolov5/ --weights ./Users/mtraor3/yolov5/ --save_text --save_conf --conf-thres 0.40 --source 0

Saving predictions and confidence scores to a .txt-file with inference performed at a confidence threshold of 40%

python path/to/ --weights /path/to/ --save_text --save_conf --conf-thres 0.40 --source 0

If the model weights file were titled "" then the code becomes:

python ./Users/mtraor3/yolov5/ --weights ./Users/mtraor3/yolov5/ --save_text --save_conf --conf-thres 0.40 --source 0

Optional Arguments

There are more optional arguments for doing things like changing the confidence threshold, saving cropped prediction boxes, etc.

More Examples:

Saving the predictions and confidence scores to a .txt-file, with inference at a confidence threshold of 40%.