1
+ from enum import Enum
2
+
1
3
import cv2
2
4
import streamlit as st
3
5
from dotenv import load_dotenv
6
8
load_dotenv ()
7
9
8
10
11
+ # define enum for processors
12
+ class ProcessorType (Enum ):
13
+ BLUR = "blur"
14
+ CANNY = "canny"
15
+ INVERT = "invert"
16
+ YOLOV8 = "yolov8"
17
+
18
+
9
19
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
11
84
self .model = YOLO (model_name )
85
+ self .confidence = confidence
12
86
13
- def process (self , frame ):
87
+ def process (
88
+ self ,
89
+ frame : cv2 .UMat ,
90
+ ) -> cv2 .UMat :
14
91
results = self .model (
15
92
frame ,
16
- conf = 0.5 ,
93
+ conf = self . confidence ,
17
94
classes = [0 ],
18
95
)
19
96
output_img = results [0 ].plot (
@@ -26,35 +103,47 @@ def process(self, frame):
26
103
)
27
104
28
105
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
+
29
119
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
41
121
device = st .text_input (
42
122
label = "input your video/camera device" ,
43
123
value = "0" ,
44
124
)
45
125
if device .isnumeric ():
46
126
# e.g. "0" -> 0
47
127
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
+ )
48
139
49
140
st .title ("Video processing" )
50
141
51
142
start_button = st .button ("Start" )
52
143
stop = st .button ("Stop" )
53
144
54
145
image_loc = st .empty ()
55
- processor = Processor (
56
- model_name = model_name ,
57
- )
146
+ processor = get_processor (processor_type )
58
147
59
148
if start_button :
60
149
capture = cv2 .VideoCapture (device )
0 commit comments