-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
354 lines (274 loc) · 13.6 KB
/
main.py
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# Import necessary libraries
import cv2
from tkinter import *
from tkinter import messagebox
from PIL import Image, ImageTk
import numpy as np
import pyperclip
# Author and version information
__author__ = "Teeraphat Kullanankanjana"
__version__ = "0.1.0"
# Define the main class for the HSV Range Finder application
class HSVRangeFinder:
def __init__(self):
# Initialize camera index and flip options
self.camIndex = 0
self.flip_horizontal = False
self.flip_vertical = False
# Create the main tkinter window
self.window = Tk()
self.window.geometry('910x600')
self.window.title('HSV Range Finder')
self.window.resizable(0, 0)
# Initialize video capture using OpenCV
self.cap = cv2.VideoCapture(self.camIndex)
# --- Camera Frames ---
# Create a frame for the main camera feed
self.mainCameraFrame = LabelFrame(self.window, text='Main Camera')
self.mainCameraFrame.place(x=0, y=0)
# Create a label to display the video feed
self.vidLabel1 = Label(self.mainCameraFrame)
self.vidLabel1.configure(width=300, height=400)
self.vidLabel1.pack()
# Create a frame for the result camera feed
self.contourCameraFrame = LabelFrame(self.window, text='Result Camera')
self.contourCameraFrame.place(x=305, y=0)
# Create a label to display the filtered video feed
self.vidLabel2 = Label(self.contourCameraFrame)
self.vidLabel2.configure(width=300, height=400)
self.vidLabel2.pack()
# Create a frame for the binary mask
self.outCameraFrame = LabelFrame(self.window, text='Binary Mask')
self.outCameraFrame.place(x=600, y=0)
# Create a label to display the binary mask
self.vidLabel3 = Label(self.outCameraFrame)
self.vidLabel3.configure(width=300, height=400)
self.vidLabel3.pack()
# --- Camera Control Frame ---
self.cameraControlFrame = LabelFrame(self.window, text='Camera Control')
self.cameraControlFrame.place(x=0, y=425)
# Label to display the current camera channel
self.camChLabel = Label(self.cameraControlFrame, text='CH:0', font=('', 25, 'bold'))
self.camChLabel.grid(row=0, column=0, columnspan=2)
# Buttons for camera control
self.nextCambtn = Button(self.cameraControlFrame, text='Prev Camera', command=self.prev_cam)
self.nextCambtn.grid(row=1, column=0)
self.prevCambtn = Button(self.cameraControlFrame, text='Next Camera', command=self.next_cam)
self.prevCambtn.grid(row=1, column=1)
self.flipHCambtn = Button(self.cameraControlFrame, text='Flip Horizontal', command=self.flip_horizontal)
self.flipHCambtn.grid(row=2, column=0)
self.flipVCambtn = Button(self.cameraControlFrame, text='Flip Vertical', command=self.flip_vertical)
self.flipVCambtn.grid(row=2, column=1)
# --- Slider Section ---
# Initialize slider variables for HSV range adjustment
self.l_h, self.l_s, self.l_v = DoubleVar(), DoubleVar(), DoubleVar()
self.u_h, self.u_s, self.u_v = DoubleVar(), DoubleVar(), DoubleVar()
# Set default values for upper bound sliders
self.u_h.set(179)
self.u_s.set(255)
self.u_v.set(255)
# Functions to get slider values and update labels
def get_lh():
return '{:.0f}'.format(self.l_h.get())
def lh_changed(event):
self.lhShow.configure(text=get_lh())
def get_ls():
return '{:.0f}'.format(self.l_s.get())
def ls_changed(event):
self.lsShow.configure(text=get_ls())
def get_lv():
return '{:.0f}'.format(self.l_v.get())
def lv_changed(event):
self.lvShow.configure(text=get_lv())
def get_uh():
return '{:.0f}'.format(self.u_h.get())
def uh_changed(event):
self.uhShow.configure(text=get_uh())
def get_us():
return '{:.0f}'.format(self.u_s.get())
def us_changed(event):
self.usShow.configure(text=get_us())
def get_uv():
return '{:.0f}'.format(self.u_v.get())
def uv_changed(event):
self.uvShow.configure(text=get_uv())
# Create a frame for HSV range adjustment sliders
self.sliderFrame = LabelFrame(self.window, text='HSV Range Adjustment')
self.sliderFrame.place(x=185, y=425)
# Labels and sliders for lower and upper HSV range values
self.lhLabel = Label(self.sliderFrame, text='Lower Hue:')
self.lhLabel.grid(row=0, column=0)
self.lhSlider = Scale(self.sliderFrame, orient='horizontal', from_=0, to=179, command=lh_changed, variable=self.l_h)
self.lhSlider.grid(row=0, column=1)
self.lsLabel = Label(self.sliderFrame, text='Lower Saturation:')
self.lsLabel.grid(row=0, column=3)
self.lsSlider = Scale(self.sliderFrame, orient='horizontal', from_=0, to=255, command=ls_changed, variable=self.l_s)
self.lsSlider.grid(row=0, column=4)
self.lvLabel = Label(self.sliderFrame, text='Lower Value:')
self.lvLabel.grid(row=0, column=5)
self.lvSlider = Scale(self.sliderFrame, orient='horizontal', from_=0, to=255, command=lv_changed, variable=self.l_v)
self.lvSlider.grid(row=0, column=6)
self.uhLabel = Label(self.sliderFrame, text='Upper Hue:')
self.uhLabel.grid(row=1, column=0)
self.uhSlider = Scale(self.sliderFrame, orient='horizontal', from_=0, to=179, command=uh_changed, variable=self.u_h)
self.uhSlider.grid(row=1, column=1)
self.usLabel = Label(self.sliderFrame, text='Upper Saturation:')
self.usLabel.grid(row=1, column=3)
self.usSlider = Scale(self.sliderFrame, orient='horizontal', from_=0, to=255, command=us_changed, variable=self.u_s)
self.usSlider.grid(row=1, column=4)
self.uvLabel = Label(self.sliderFrame, text='Upper Value:')
self.uvLabel.grid(row=1, column=5)
self.uvSlider = Scale(self.sliderFrame, orient='horizontal', from_=0, to=255, command=uv_changed, variable=self.u_v)
self.uvSlider.grid(row=1, column=6)
# Labels to display current slider values
self.resultFrame = LabelFrame(self.window, text='Get Result')
self.resultFrame.place(x=745, y=425)
self.lrLabel = Label(self.resultFrame, text='HSV Lower Range')
self.lrLabel.grid(row=0, column=0, columnspan=3)
self.lhShow = Label(self.resultFrame, text='0')
self.lhShow.grid(row=1, column=0)
self.lsShow = Label(self.resultFrame, text='0')
self.lsShow.grid(row=1, column=1)
self.lvShow = Label(self.resultFrame, text='0')
self.lvShow.grid(row=1, column=2)
self.urLabel = Label(self.resultFrame, text='HSV Upper Range')
self.urLabel.grid(row=2, column=0, columnspan=3)
self.uhShow = Label(self.resultFrame, text='0')
self.uhShow.grid(row=3, column=0)
self.usShow = Label(self.resultFrame, text='0')
self.usShow.grid(row=3, column=1)
self.uvShow = Label(self.resultFrame, text='0')
self.uvShow.grid(row=3, column=2)
# Buttons to copy the lower and upper HSV range values to clipboard
self.cpyupperBtn = Button(self.resultFrame, text='Copy', command=self.get_lowerRange)
self.cpyupperBtn.grid(row=0, column=3, rowspan=3)
self.cpylowwerBtn = Button(self.resultFrame, text='Copy', command=self.get_upperRange)
self.cpylowwerBtn.grid(row=3, column=3, rowspan=3)
# Method to copy the lower HSV range to clipboard
def get_lowerRange(self):
lowerRange = '{},{},{}'.format(self.get_lh(), self.get_ls(), self.get_lv())
pyperclip.copy(lowerRange)
# Method to copy the upper HSV range to clipboard
def get_upperRange(self):
upperRange = '{},{},{}'.format(self.get_uh(), self.get_us(), self.get_uv())
pyperclip.copy(upperRange)
# Method to flip the camera feed horizontally
def flip_horizontal(self):
self.flip_horizontal = not self.flip_horizontal
self.flip_vertical = False
# Method to flip the camera feed vertically
def flip_vertical(self):
self.flip_vertical = not self.flip_vertical
self.flip_horizontal = False
# Method to switch to the next camera channel
def next_cam(self):
self.camIndex += 1
self.cap.release()
self.cap = cv2.VideoCapture(self.camIndex)
self.camChLabel.config(text='CH:{}'.format(self.camIndex))
# Method to switch to the previous camera channel
def prev_cam(self):
self.camIndex -= 1
if self.camIndex < 0:
messagebox.showerror("Error! Camera Channel Limitation!", "No previous camera")
self.camIndex = 0
else:
self.cap.release()
self.cap = cv2.VideoCapture(self.camIndex)
self.camChLabel.config(text='CH:{}'.format(self.camIndex))
# Method to update the video frame and apply HSV range filtering
def update_frame(self):
# Read a frame from the video capture
ret, frame = self.cap.read()
if self.flip_horizontal:
frame = cv2.flip(frame, 1) # Horizontal mirror
elif self.flip_vertical:
frame = cv2.flip(frame, 0) # Vertical flip
# Get the lower and upper bound values from the sliders
lower_bound = np.array([self.l_h.get(), self.l_s.get(), self.l_v.get()])
upper_bound = np.array([self.u_h.get(), self.u_s.get(), self.u_v.get()])
# Convert the frame to the HSV color space
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Get the binary mask
mask = cv2.inRange(hsv, lower_bound, upper_bound)
# Filter the original frame using the mask
filtered_frame = cv2.bitwise_and(frame, frame, mask=mask)
# Convert the filtered frame to grayscale
gray = cv2.cvtColor(filtered_frame, cv2.COLOR_BGR2GRAY)
# Threshold the grayscale image to get a binary image
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)
# Convert the frame to a PhotoImage object
img1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img1 = Image.fromarray(img1)
img1 = ImageTk.PhotoImage(image=img1)
# Convert the filtered frame to a PhotoImage object
img2 = cv2.cvtColor(filtered_frame, cv2.COLOR_BGR2RGB)
img2 = Image.fromarray(img2)
img2 = ImageTk.PhotoImage(image=img2)
# Convert the binary image to a PhotoImage object
img3 = cv2.cvtColor(binary, cv2.COLOR_GRAY2RGB)
img3 = Image.fromarray(img3)
img3 = ImageTk.PhotoImage(image=img3)
# Update the label's image
self.vidLabel1.config(image=img1)
self.vidLabel1.image = img1
# Update the label's image with the filtered frame
self.vidLabel2.config(image=img2)
self.vidLabel2.image = img2
self.vidLabel3.config(image=img3)
self.vidLabel3.image = img3
# Schedule the next frame update
self.window.after(10, self.update_frame)
# Getter method to format lower hue value
def get_lh(self):
return '{:.0f}'.format(self.l_h.get())
# Event handler for lower hue slider change
def lh_changed(self, event):
self.lhShow.configure(text=self.get_lh())
# Getter method to format lower saturation value
def get_ls(self):
return '{:.0f}'.format(self.l_s.get())
# Event handler for lower saturation slider change
def ls_changed(self, event):
self.lsShow.configure(text=self.get_ls())
# Getter method to format lower value value
def get_lv(self):
return '{:.0f}'.format(self.l_v.get())
# Event handler for lower value slider change
def lv_changed(self, event):
self.lvShow.configure(text=self.get_lv())
# Getter method to format upper hue value
def get_uh(self):
return '{:.0f}'.format(self.u_h.get())
# Event handler for upper hue slider change
def uh_changed(self, event):
self.uhShow.configure(text=self.get_uh())
# Getter method to format upper saturation value
def get_us(self):
return '{:.0f}'.format(self.u_s.get())
# Event handler for upper saturation slider change
def us_changed(self, event):
self.usShow.configure(text=self.get_us())
# Getter method to format upper value value
def get_uv(self):
return '{:.0f}'.format(self.u_v.get())
# Event handler for upper value slider change
def uv_changed(self, event):
self.uvShow.configure(text=self.get_uv())
# Method to release resources and close the application
def cleanup(self):
self.cap.release()
self.window.destroy()
self.window.after_cancel(self.update_frame)
# Method to start the application
def run(self):
# Create an OpenCV video capture object
self.cap = cv2.VideoCapture(self.camIndex)
# Start updating the frame
self.update_frame()
# Bind the cleanup method to the window's close event
self.window.protocol("WM_DELETE_WINDOW", self.cleanup)
# Run the Tkinter event loop
self.window.mainloop()
self.window.after_cancel(self.update_frame)
self.cap.release()