forked from JoeyThompson10/spectrumAnalyzerProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmask_var_picker.py
More file actions
79 lines (60 loc) · 2.8 KB
/
mask_var_picker.py
File metadata and controls
79 lines (60 loc) · 2.8 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
import cv2
import numpy as np
import env_vars
import os
#does nothing but is required by trackbar
def required_func(x):
pass
# Start with a sample image or video frame
try:
# Specify the folder containing the videos
video_folder_path = env_vars.Env_Vars.VIDEO_FOLDER
# Check if the folder exists
if not os.path.exists(video_folder_path):
raise Exception(f"The specified folder does not exist: {video_folder_path}")
# List all files in the video folder
video_files = [f for f in os.listdir(video_folder_path) if f.endswith('.mp4')]
# Check if there are any mp4 files in the folder
if not video_files:
raise Exception(f"No videos found in {video_folder_path}.")
except Exception as e:
print(f"Error: {e}")
video_path = os.path.join(video_folder_path, video_files[0])
video_capture = cv2.VideoCapture(video_path)
print(video_capture.isOpened())
ret, image = video_capture.read()
# Convert to HSV
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Create trackbars for lower and upper mask limits
cv2.namedWindow('Set Upper and Lower Bounds', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO | cv2.WINDOW_GUI_EXPANDED)
cv2.createTrackbar('Lower H', 'Set Upper and Lower Bounds', 0, 180, required_func)
cv2.createTrackbar('Lower S', 'Set Upper and Lower Bounds', 0, 255, required_func)
cv2.createTrackbar('Lower V', 'Set Upper and Lower Bounds', 0, 255, required_func)
cv2.createTrackbar('Upper H', 'Set Upper and Lower Bounds', 180, 180, required_func)
cv2.createTrackbar('Upper S', 'Set Upper and Lower Bounds', 255, 255, required_func)
cv2.createTrackbar('Upper V', 'Set Upper and Lower Bounds', 255, 255, required_func)
cv2.setWindowProperty('Set Upper and Lower Bounds', cv2.WND_PROP_TOPMOST, 1)
while True:
# Get positions of the trackbars
l_h = cv2.getTrackbarPos('Lower H', 'Set Upper and Lower Bounds')
l_s = cv2.getTrackbarPos('Lower S', 'Set Upper and Lower Bounds')
l_v = cv2.getTrackbarPos('Lower V', 'Set Upper and Lower Bounds')
u_h = cv2.getTrackbarPos('Upper H', 'Set Upper and Lower Bounds')
u_s = cv2.getTrackbarPos('Upper S', 'Set Upper and Lower Bounds')
u_v = cv2.getTrackbarPos('Upper V', 'Set Upper and Lower Bounds')
# Set the lower and upper from trackbars
lower_HSV_bound = np.array([l_h, l_s, l_v])
upper_HSV_bound = np.array([u_h, u_s, u_v])
# Update mask
mask = cv2.inRange(hsv, lower_HSV_bound, upper_HSV_bound)
cv2.namedWindow('Mask', cv2.WINDOW_KEEPRATIO)
cv2.imshow('Mask', mask)
cv2.resizeWindow('Mask', 1200, 720)
key = cv2.waitKey(1)
if key == 13 or key == 10: # Esc key to stop
#set lower_HSV_bound, upper_HSV_bound
break
# Print the final chosen HSV range
print(f"Chosen Lower HSV: [{l_h}, {l_s}, {l_v}]")
print(f"Chosen Upper HSV: [{u_h}, {u_s}, {u_v}]")
cv2.destroyAllWindows()