-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0_main.py
More file actions
161 lines (134 loc) · 6.5 KB
/
_0_main.py
File metadata and controls
161 lines (134 loc) · 6.5 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
import cv2
import pickle
import fitz
import threading
import pyautogui
import _1_funcs as funcs
import _2_classes as classes
import _3_gvars as gvars
#midi_name = 'WORLDE easy control 0'
midi_name = 'X-TOUCH COMPACT'
#gvars.controller = "WORLDE EASY CONTROL 0"
gvars.controller = "BEHRINGER XTOUCH COMPACT"
usingCap = True
gvars.midiValues = classes.MidiValuesStruct()
gvars.usingCooldown = False
mdToProject = classes.SpotlightPoint(0)
meToProject = classes.SpotlightPoint(1)
vlToProject = classes.SpotlightPoint(2)
gvars.l_spotlightPoints.append(mdToProject)
gvars.l_spotlightPoints.append(meToProject)
gvars.l_spotlightPoints.append(vlToProject)
inportMidi, outportMidi = funcs.findPorts(midi_name)
gvars.outportMidi = outportMidi
try:
midi_thread_input = threading.Thread(target=funcs.midi_input_thread, args=(inportMidi,))
midi_thread_input.start()
timingThread = threading.Thread(target=funcs.timingThread)
timingThread.start()
lerpingThread = threading.Thread(target=funcs.lerpPosChangeThread)
lerpingThread.start()
# change to appropriate camera index if needed, use funcs.listCameras() and funcs.searchCameras() to help find it
cap = cv2.VideoCapture(2)
if not cap.isOpened():
usingCap = False
raise Exception("Error: Camera not accessible.")
cv2.namedWindow('clahe')
cv2.setMouseCallback('clahe', funcs.addPathwayPoint)
cv2.createTrackbar('curPedal', 'clahe', 0, 20, funcs.updatePedalToJump)
screenWidth, screenHeight = pyautogui.size()
gvars.scoreWindowWidth = int(screenWidth / 2.25)
gvars.scoreWindowHeight = int(screenHeight - (screenHeight/20))
gvars.scoreDoc = fitz.open(gvars.scorePath)
gvars.scoreNumPages = gvars.scoreDoc.page_count
cv2.namedWindow('score_view', cv2.WINDOW_NORMAL)
cv2.resizeWindow('score_view', gvars.scoreWindowWidth, gvars.scoreWindowHeight)
while True:
success, virginFrame = cap.read()
flipped_frame = cv2.flip(virginFrame, 1)
if not success:
raise Exception("Error: Failed to read frame from camera.")
frame = flipped_frame.copy()
height, width = frame.shape[:2]
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
clahe_frame = clahe.apply(frame_gray)
funcs.generateAndDrawSpotlightPoints(clahe_frame)
cv2.putText(clahe_frame, str(gvars.selectedPathwayId), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (250, 250, 250), 2, cv2.LINE_AA)
cv2.putText(clahe_frame, "p: " + str(gvars.curPedal), (width - 110, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (250, 250, 250), 2, cv2.LINE_AA)
cv2.putText(clahe_frame, "jump to: " + str(gvars.pedalToJump), (width - 110, height - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (250, 250, 250), 1, cv2.LINE_AA)
cv2.putText(clahe_frame, "curPathways: " + str(' '.join(map(str, gvars.curSelectedPathwaysIds))), (20, height - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (250, 250, 250), 1, cv2.LINE_AA)
funcs.drawPathways(clahe_frame)
cv2.imshow('clahe', clahe_frame)
img = funcs.displayPage(gvars.scoreCurPage)
cv2.imshow('score_view', img)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
if key == ord('*'):
gvars.curPedal = gvars.curPedal + 1
funcs.switchCasePedal(gvars.curPedal)
if key == ord(' '):
funcs.jumpToPedal(gvars.pedalToJump)
if key == ord('w'):
gvars.l_spotlightPoints[0].spotlightSwitch(False)
gvars.l_spotlightPoints[1].spotlightSwitch(False)
gvars.l_spotlightPoints[2].spotlightSwitch(False)
if key == ord('1'):
gvars.l_spotlightPoints[0].spotlightSwitch(not gvars.l_spotlightPoints[0].isProjecting)
if key == ord('2'):
gvars.l_spotlightPoints[1].spotlightSwitch(not gvars.l_spotlightPoints[1].isProjecting)
if key == ord('3'):
gvars.l_spotlightPoints[2].spotlightSwitch(not gvars.l_spotlightPoints[2].isProjecting)
if key == ord('4'):
gvars.l_spotlightPoints[0].curPathway = gvars.l_pathways[gvars.selectedPathwayId]
gvars.l_spotlightPoints[0].setBorderValues()
if key == ord('5'):
gvars.l_spotlightPoints[1].curPathway = gvars.l_pathways[gvars.selectedPathwayId]
gvars.l_spotlightPoints[1].setBorderValues()
if key == ord('6'):
gvars.l_spotlightPoints[2].curPathway = gvars.l_pathways[gvars.selectedPathwayId]
gvars.l_spotlightPoints[2].setBorderValues()
elif key == ord('h'):
newPathway = None
if len(gvars.l_pathways) == 0:
newPathway = classes.Pathway(len(gvars.l_pathways), 0, 63, True)
else:
newPathway = classes.Pathway(len(gvars.l_pathways), 0, 63)
gvars.l_pathways.append(newPathway)
gvars.selectedPathwayId = len(gvars.l_pathways) - 1
elif (key == ord('s')) | (key == ord('+')): # change selected Pathway +
nextRoi = gvars.selectedPathwayId + 1
if(nextRoi == len(gvars.l_pathways)):
nextRoi = 0
gvars.selectedPathwayId = nextRoi
elif key == ord('-'): # change selected Roi -
nextRoi = gvars.selectedPathwayId - 1
if(nextRoi == -1):
nextRoi = len(gvars.l_pathways) - 1
gvars.selectedPathwayId = nextRoi
elif key == ord('='): # rerender spotlights
gvars.client.send_message("/rerender", 1)
elif key == ord('p'): # save pathways in pickle
with open('savedPathways.pkl', 'wb') as file:
pickle.dump(gvars.l_pathways, file)
print(gvars.l_pathways)
print("saved pathways file--------------------")
elif key == ord('l'): # load pathways from pickle
with open('savedPathways.pkl', 'rb') as file:
gvars.l_pathways = pickle.load(file)
print("loaded pathways file--------------------")
if len(gvars.l_pathways) > 0:
gvars.selectedPathwayId = 0
gvars.anchorPoint = gvars.l_pathways[0].l_points[0]
gvars.pickleLoaded = True
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Release the video capture object
if 'cap' in locals() and cap.isOpened():
cap.release()
# Close all OpenCV windows
cv2.destroyAllWindows()
midi_thread_input.join()
timingThread.join()