Add Streamlit Inference Python `model` arg (#14563)

Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
pull/14583/head
rulosant 8 months ago committed by GitHub
parent 291883a23f
commit f4af1bccc6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 20
      docs/en/guides/streamlit-live-inference.md
  2. 7
      ultralytics/solutions/streamlit_inference.py

@ -31,7 +31,7 @@ Streamlit makes it simple to build and deploy interactive web applications. Comb
=== "Python"
```Python
```python
from ultralytics import solutions
solutions.inference()
@ -47,6 +47,21 @@ Streamlit makes it simple to build and deploy interactive web applications. Comb
This will launch the Streamlit application in your default web browser. You will see the main title, subtitle, and the sidebar with configuration options. Select your desired YOLOv8 model, set the confidence and NMS thresholds, and click the "Start" button to begin the real-time object detection.
You can optionally supply a specific model in Python:
!!! Example "Streamlit Application with a custom model"
=== "Python"
```python
from ultralytics import solutions
# Pass a model as an argument
solutions.inference(model="path/to/model.pt")
### Make sure to run the file using command `streamlit run <file-name.py>`
```
## Conclusion
By following this guide, you have successfully created a real-time object detection application using Streamlit and Ultralytics YOLOv8. This application allows you to experience the power of YOLOv8 in detecting objects through your webcam, with a user-friendly interface and the ability to stop the video stream at any time.
@ -82,8 +97,9 @@ Then, you can create a basic Streamlit application to run live inference:
=== "Python"
```Python
```python
from ultralytics import solutions
solutions.inference()
### Make sure to run the file using command `streamlit run <file-name.py>`

@ -10,7 +10,7 @@ from ultralytics.utils.checks import check_requirements
from ultralytics.utils.downloads import GITHUB_ASSETS_STEMS
def inference():
def inference(model=None):
"""Runs real-time object detection on video input using Ultralytics YOLOv8 in a Streamlit application."""
check_requirements("streamlit>=1.29.0") # scope imports for faster ultralytics package load speeds
import streamlit as st
@ -67,7 +67,10 @@ def inference():
vid_file_name = 0
# Add dropdown menu for model selection
available_models = (x.replace("yolo", "YOLO") for x in GITHUB_ASSETS_STEMS if x.startswith("yolov8"))
available_models = [x.replace("yolo", "YOLO") for x in GITHUB_ASSETS_STEMS if x.startswith("yolov8")]
if model:
available_models.insert(0, model)
selected_model = st.sidebar.selectbox("Model", available_models)
with st.spinner("Model is downloading..."):
model = YOLO(f"{selected_model.lower()}.pt") # Load the YOLO model

Loading…
Cancel
Save