This is a approach for real-time instance segmentation using YOLOv5 and ONNX Runtime. The project uses YOLOv5 to detect objects in the input video stream and then performs instance segmentation to create a binary mask for each detected object. The resulting masks are then overlaid on the original video frames to highlight the detected objects.
To run this project, you need the following libraries installed:
- PyTorch
- OpenCV
- NumPy
- ONNX Runtime
The following command:
pip install -r requirements.txt
-
Clone this repository
-
Download the YOLOv5 segmentation model
This project uses a custom dataset trained on YOLOv5 from Ultralytics. You can download the YOLOv5 model checkpoints from the official repository.
- Update the
model
variable in therealtime_segmentation.py
file with the path to your downloaded YOLOv5 model.
model = "path/to/yolov5.pt"
- Run the
realtime_segmentation.py
file.
python realtime_segmentation.py
- The program will open the default camera on your computer and start detecting and segmenting objects in real-time. You can press the 'q' key to quit the program.
This project also includes an implementation using FastAPI to stream the segmented frames to a web browser using both HTTP and WebSockets.
- Run the FastAPI app for HTTP streaming using the following command:
uvicorn app:app --host 0.0.0.0 --port 8000
-
Open your web browser and visit http://0.0.0.0:8000/ to view the processed video stream using HTTP.
-
To use WebSockets for streaming, run the script
websockets_streaming.py
. -
Run the FastAPI app for WebSocket streaming using the same command as in step 1:
uvicorn app:app --host 0.0.0.0 --port 8000
- Open your web browser and visit http://0.0.0.0:8000/ to view the processed video stream using WebSockets.
This project is inspired by the Ultralytics YOLOv5 repository. The project uses their implementation of YOLOv5 for object detection, instance segmentation and their color mapping function for coloring the object masks. The project also uses the ONNX Runtime library for inference.