generated from ks6088ts/template-python
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from ks6088ts-labs/feature/issue-88_add-video-…
…stream-demo add video processing sample
- Loading branch information
Showing
7 changed files
with
130 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import cv2 | ||
import streamlit as st | ||
from dotenv import load_dotenv | ||
from ultralytics import YOLO | ||
|
||
load_dotenv() | ||
|
||
|
||
class Processor: | ||
def __init__(self, model_name): | ||
self.model = YOLO(model_name) | ||
|
||
def process(self, frame): | ||
results = self.model( | ||
frame, | ||
conf=0.5, | ||
classes=[0], | ||
) | ||
output_img = results[0].plot( | ||
labels=True, | ||
conf=True, | ||
) | ||
return cv2.cvtColor( | ||
src=output_img, | ||
code=cv2.COLOR_BGR2RGB, | ||
) | ||
|
||
|
||
with st.sidebar: | ||
model_name = st.selectbox( | ||
label="Select a model", | ||
options=[ | ||
"yolov8n.pt", | ||
"yolov9c.pt", | ||
"yolov10n.pt", | ||
# https://docs.ultralytics.com/models/yolov8/#supported-tasks-and-modes | ||
], | ||
key="model_name", | ||
index=0, | ||
) | ||
device = st.text_input( | ||
label="input your video/camera device", | ||
value="0", | ||
) | ||
if device.isnumeric(): | ||
# e.g. "0" -> 0 | ||
device = int(device) | ||
|
||
st.title("Video processing") | ||
|
||
start_button = st.button("Start") | ||
stop = st.button("Stop") | ||
|
||
image_loc = st.empty() | ||
processor = Processor( | ||
model_name=model_name, | ||
) | ||
|
||
if start_button: | ||
capture = cv2.VideoCapture(device) | ||
|
||
while capture.isOpened: | ||
ret, frame = capture.read() | ||
|
||
if not ret: | ||
st.error("Failed to capture image") | ||
continue | ||
|
||
processed_frame = processor.process( | ||
frame=frame, | ||
) | ||
|
||
image_loc.image( | ||
image=processed_frame, | ||
use_column_width=True, | ||
) | ||
|
||
if stop: | ||
break | ||
|
||
capture.release() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters