-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_processing.py
More file actions
195 lines (168 loc) · 7.08 KB
/
video_processing.py
File metadata and controls
195 lines (168 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from customtkinter import *
import cv2
import numpy as np
global interface
def on_close():
import main_controller
global interface
interface.destroy()
main_controller.root.deiconify()
def switch_to_other_option():
import main_controller
global interface
interface.destroy()
main_controller.next("normal")
def edge_detection(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200)
return cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
def grayscale_quantization(frame, levels=4):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
quantized = (gray // (256 // levels)) * (256 // levels)
return cv2.cvtColor(quantized, cv2.COLOR_GRAY2BGR)
def contrast_enhancement(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
equalized = cv2.equalizeHist(gray)
return cv2.cvtColor(equalized, cv2.COLOR_GRAY2BGR)
def soft_appearance(frame):
return cv2.bilateralFilter(frame, 9, 75, 75)
def cartoon_filter(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 7)
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 5)
color = cv2.bilateralFilter(frame, 9, 300, 300)
cartoon = cv2.bitwise_and(color, color, mask=edges)
return cartoon
def pencil_sketch(frame):
gray, sketch = cv2.pencilSketch(frame, sigma_s=60, sigma_r=0.07, shade_factor=0.05)
return sketch
def mirror_filter(frame):
return cv2.flip(frame, 1)
def night_vision(frame, angle=60):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
hsv[..., 0] = angle
hsv[..., 1] = 255
hsv[..., 2] = cv2.equalizeHist(hsv[..., 2])
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
def main():
set_appearance_mode("light")
global interface
interface = CTkToplevel()
interface.geometry("500x600+150+150")
interface.resizable(False, False)
title_font = CTkFont(family="Times New Roman", size=30, weight="bold", slant="italic")
option_font = CTkFont(family="Times New Roman", size=15, weight="bold")
switch_to_advance = CTkButton(
interface,
text="Advance",
font=option_font,
text_color="#000000",
border_width=0,
width=100,
height=30,
fg_color="transparent",
hover=False,
cursor="hand2",
command=switch_to_other_option
)
switch_to_advance.place(relx=0.93, rely=0.02, anchor="center")
title = CTkLabel(interface, text=" Choose processing filter: ", font=title_font, text_color="#0000FF")
title.pack(pady=20)
selected_option = StringVar(value="edge_detection");
options = [
"edge_detection",
"grayscale_quantization",
"contrast_enhancement",
"soft_appearance",
"cartoon_filter",
"pencil_sketch",
"mirror_filter",
"night_vision"
]
vars_funcs = [
(selected_option, edge_detection),
(selected_option, grayscale_quantization),
(selected_option, contrast_enhancement),
(selected_option, soft_appearance),
(selected_option, cartoon_filter),
(selected_option, pencil_sketch),
(selected_option, mirror_filter),
(selected_option, night_vision),
]
labels = [
"1 - Edge Detection",
"2 - Grayscale Quantization",
"3 - Contrast Enhancement",
"4 - Soft and Polished Appearance",
"5 - Cartoon Filter",
"6 - Pencil Sketch",
"7 - Mirror Filter",
"8 - Night Vision"
]
mode_labels = [
"Green",
"Red",
"Blue"
]
combo_box = CTkComboBox(interface, font=option_font, text_color="#000000", values=mode_labels, state="readonly")
for i, (var_func, label_text, option_num) in enumerate(zip(vars_funcs, labels, options)):
var, _ = var_func
radio_button = CTkRadioButton(interface, text=label_text, font=option_font, text_color="#000000", variable=var, value=option_num)
radio_button.place(relx=0.1, rely=0.15 + i * 0.08, anchor="w")
rely_levels = 0.8
levels_var = StringVar(value="4")
levels_label = CTkLabel(interface, text="Grayscale levels:", font=option_font, text_color="#000000")
levels_label.place(relx=0.1, rely=rely_levels, anchor="w")
levels_entry = CTkEntry(interface, textvariable=levels_var, width=50)
levels_entry.place(relx=0.5, rely=rely_levels, anchor="center")
levels_var_example = CTkLabel(interface, text="(e.g. 4, 8, 16, or 256)", font=option_font, text_color="#000000")
levels_var_example.place(relx=0.7, rely=rely_levels, anchor="center")
msg_text_field = CTkLabel(interface, text="", font=option_font, text_color="#FF0000")
def start_processing():
cap = cv2.VideoCapture(0)
if not cap.isOpened():
msg_text_field.configure(text="Could not open camera.", text_color="#FF0000")
return
try:
levels = int(levels_var.get())
except:
levels = 4
msg_text_field.configure(text="Invalid grayscale level! Using default: 4", text_color="#FF0000")
while True:
ret, frame = cap.read()
if not ret:
break
processed = frame.copy()
processed = cv2.resize(frame, None, fx=1.5, fy=1.5, interpolation=cv2.INTER_LINEAR)
global_option = ""
for (var, func), option in zip(vars_funcs, options):
if var.get() == option:
msg_text_field.configure(text=option + " is now running", text_color="#006400")
msg_text_field.update_idletasks()
if func == grayscale_quantization:
global_option = option
processed = func(processed, levels=levels)
elif func == night_vision:
global_option = option
color_mode = combo_box.get()
angle_map = {"Red": 0, "Green": 60, "Blue": 120}
angle = angle_map.get(color_mode, 60)
processed = func(processed, angle=angle)
else:
global_option = option
processed = func(processed)
cv2.imshow("Processed Video", processed)
if cv2.waitKey(1) & 0xFF == ord('q') or cv2.getWindowProperty("Processed Video", cv2.WND_PROP_VISIBLE) < 1:
msg_text_field.configure(text=global_option + " has finished running", text_color="#006400")
msg_text_field.update_idletasks()
break
cap.release()
cv2.destroyAllWindows()
combo_label = CTkLabel(interface, text="Night Vision Mode:", font=option_font, text_color="#000000")
combo_label.place(relx=0.1, rely=0.85, anchor="w")
combo_box.place(relx=0.45, rely=0.85, anchor="w")
msg_text_field.place(relx=0.1, rely=0.9, anchor="w")
start_button = CTkButton(interface, text="Start Camera", font=option_font, command=start_processing)
start_button.place(relx=0.85, rely=0.97, anchor="center")
interface.protocol("WM_DELETE_WINDOW", on_close)
interface.mainloop()