Skip to content

Commit 84a7292

Browse files
committed
add video processing sample
1 parent 28f0509 commit 84a7292

File tree

7 files changed

+130
-26
lines changed

7 files changed

+130
-26
lines changed

apps/99_streamlit_examples/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ Access to http://localhost:8501 and select the sample you want to run from the s
7777

7878
![Pose estimation](../../docs/images/99_streamlit_examples.poseestimation.png)
7979

80+
#### 12. Video processing
81+
82+
![Video processing](../../docs/images/99_streamlit_examples.videoprocessing.png)
83+
8084
## References
8185

8286
- [🎈 Streamlit + LLM Examples App](https://github.com/streamlit/llm-examples)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import cv2
2+
import streamlit as st
3+
from dotenv import load_dotenv
4+
from ultralytics import YOLO
5+
6+
load_dotenv()
7+
8+
9+
class Processor:
10+
def __init__(self, model_name):
11+
self.model = YOLO(model_name)
12+
13+
def process(self, frame):
14+
results = self.model(
15+
frame,
16+
conf=0.5,
17+
classes=[0],
18+
)
19+
output_img = results[0].plot(
20+
labels=True,
21+
conf=True,
22+
)
23+
return cv2.cvtColor(
24+
src=output_img,
25+
code=cv2.COLOR_BGR2RGB,
26+
)
27+
28+
29+
with st.sidebar:
30+
model_name = st.selectbox(
31+
label="Select a model",
32+
options=[
33+
"yolov8n.pt",
34+
"yolov9c.pt",
35+
"yolov10n.pt",
36+
# https://docs.ultralytics.com/models/yolov8/#supported-tasks-and-modes
37+
],
38+
key="model_name",
39+
index=0,
40+
)
41+
device = st.text_input(
42+
label="input your video/camera device",
43+
value="0",
44+
)
45+
if device.isnumeric():
46+
# e.g. "0" -> 0
47+
device = int(device)
48+
49+
st.title("Video processing")
50+
51+
start_button = st.button("Start")
52+
stop = st.button("Stop")
53+
54+
image_loc = st.empty()
55+
processor = Processor(
56+
model_name=model_name,
57+
)
58+
59+
if start_button:
60+
capture = cv2.VideoCapture(device)
61+
62+
while capture.isOpened:
63+
ret, frame = capture.read()
64+
65+
if not ret:
66+
st.error("Failed to capture image")
67+
continue
68+
69+
processed_frame = processor.process(
70+
frame=frame,
71+
)
72+
73+
image_loc.image(
74+
image=processed_frame,
75+
use_column_width=True,
76+
)
77+
78+
if stop:
79+
break
80+
81+
capture.release()
2.2 MB
Loading

poetry.lock

Lines changed: 38 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mediapipe = "^0.10.14"
2525
azure-ai-documentintelligence = "^1.0.0b3"
2626
azure-storage-blob = "^12.22.0"
2727
requests = "^2.32.3"
28+
opencv-python-headless = "^4.10.0.84"
2829

2930
[tool.poetry.group.dev.dependencies]
3031
pre-commit = "^3.8.0"

requirements.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ langchain-openai==0.1.20
99
langchain-community==0.2.11
1010
azure-search-documents==11.5.1
1111
azure-identity==1.17.1
12+
azure-ai-documentintelligence==1.0.0b3
13+
azure-storage-blob==12.22.0
14+
requests==2.32.3
1215

1316
# To run 99_streamlit_examples/pages/10_Object_Detection.py
1417
# ultralytics==8.2.77
1518

1619
# To run 99_streamlit_examples/pages/11_Pose_Estimation.py
1720
# mediapipe==0.10.14
1821

19-
azure-ai-documentintelligence==1.0.0b3
22+
# To run 99_streamlit_examples/pages/12_Video_processing.py
23+
# opencv-python-headless==4.10.0.84

tests/test_smoke.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def test_smoke():
2323
# fixme: disabled due to flaky test
2424
# "apps/99_streamlit_examples/pages/10_Object_detection.py",
2525
# "apps/99_streamlit_examples/pages/11_Pose_estimation.py",
26+
"apps/99_streamlit_examples/pages/12_Video_processing.py",
2627
]
2728
for path in paths:
2829
at = AppTest(

0 commit comments

Comments
 (0)