Skip to content

Commit 6ebb7bd

Browse files
committed
add some vision processors
1 parent f6dc01d commit 6ebb7bd

File tree

1 file changed

+106
-17
lines changed

1 file changed

+106
-17
lines changed

apps/99_streamlit_examples/pages/12_Video_processing.py

Lines changed: 106 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from enum import Enum
2+
13
import cv2
24
import streamlit as st
35
from dotenv import load_dotenv
@@ -6,14 +8,89 @@
68
load_dotenv()
79

810

11+
# define enum for processors
12+
class ProcessorType(Enum):
13+
BLUR = "blur"
14+
CANNY = "canny"
15+
INVERT = "invert"
16+
YOLOV8 = "yolov8"
17+
18+
919
class Processor:
10-
def __init__(self, model_name):
20+
def process(
21+
self,
22+
frame: cv2.UMat,
23+
) -> cv2.UMat:
24+
raise NotImplementedError
25+
26+
27+
class BlurProcessor(Processor):
28+
def process(
29+
self,
30+
frame: cv2.UMat,
31+
) -> cv2.UMat:
32+
output_img = cv2.GaussianBlur(
33+
src=frame,
34+
ksize=(21, 21),
35+
sigmaX=0,
36+
)
37+
return cv2.cvtColor(
38+
src=output_img,
39+
code=cv2.COLOR_BGR2RGB,
40+
)
41+
42+
43+
class CannyProcessor(Processor):
44+
def process(
45+
self,
46+
frame: cv2.UMat,
47+
) -> cv2.UMat:
48+
gray = cv2.cvtColor(
49+
src=frame,
50+
code=cv2.COLOR_BGR2GRAY,
51+
)
52+
output_img = cv2.Canny(
53+
image=gray,
54+
threshold1=100,
55+
threshold2=200,
56+
)
57+
return cv2.cvtColor(
58+
src=output_img,
59+
code=cv2.COLOR_GRAY2RGB,
60+
)
61+
62+
63+
class InvertProcessor(Processor):
64+
def process(
65+
self,
66+
frame: cv2.UMat,
67+
) -> cv2.UMat:
68+
output_img = cv2.bitwise_not(
69+
src=frame,
70+
)
71+
return cv2.cvtColor(
72+
src=output_img,
73+
code=cv2.COLOR_BGR2RGB,
74+
)
75+
76+
77+
class Yolov8Processor(Processor):
78+
def __init__(
79+
self,
80+
model_name: str = "yolov8n.pt",
81+
confidence: float = 0.5,
82+
):
83+
# model_name: https://docs.ultralytics.com/models/yolov8/#supported-tasks-and-modes
1184
self.model = YOLO(model_name)
85+
self.confidence = confidence
1286

13-
def process(self, frame):
87+
def process(
88+
self,
89+
frame: cv2.UMat,
90+
) -> cv2.UMat:
1491
results = self.model(
1592
frame,
16-
conf=0.5,
93+
conf=self.confidence,
1794
classes=[0],
1895
)
1996
output_img = results[0].plot(
@@ -26,35 +103,47 @@ def process(self, frame):
26103
)
27104

28105

106+
def get_processor(processor_type: ProcessorType) -> Processor:
107+
if processor_type == ProcessorType.BLUR:
108+
return BlurProcessor()
109+
elif processor_type == ProcessorType.CANNY:
110+
return CannyProcessor()
111+
elif processor_type == ProcessorType.INVERT:
112+
return InvertProcessor()
113+
elif processor_type == ProcessorType.YOLOV8:
114+
return Yolov8Processor()
115+
else:
116+
raise ValueError(f"Unknown processor type: {processor_type}")
117+
118+
29119
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-
)
120+
# device: https://docs.opencv.org/4.10.0/d8/dfe/classcv_1_1VideoCapture.html#a5d5f5dacb77bbebdcbfb341e3d4355c1
41121
device = st.text_input(
42122
label="input your video/camera device",
43123
value="0",
44124
)
45125
if device.isnumeric():
46126
# e.g. "0" -> 0
47127
device = int(device)
128+
processor_type = st.radio(
129+
label="processor type",
130+
options=[
131+
ProcessorType.BLUR,
132+
ProcessorType.CANNY,
133+
ProcessorType.INVERT,
134+
ProcessorType.YOLOV8,
135+
],
136+
index=0,
137+
format_func=lambda x: x.value,
138+
)
48139

49140
st.title("Video processing")
50141

51142
start_button = st.button("Start")
52143
stop = st.button("Stop")
53144

54145
image_loc = st.empty()
55-
processor = Processor(
56-
model_name=model_name,
57-
)
146+
processor = get_processor(processor_type)
58147

59148
if start_button:
60149
capture = cv2.VideoCapture(device)

0 commit comments

Comments
 (0)