-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightStage.py
213 lines (181 loc) · 7.61 KB
/
LightStage.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
import cv2
import tkinter as tk
import tkinter.colorchooser
from tkinter.filedialog import askopenfile
from executeConfig import parse_config
import sequencePopup
import gradientColor
from createGradient import create_gradient
cam = cv2.VideoCapture(0)
class LightStage:
def __init__(self):
self.window = tk.Tk()
# Set window to fullscreen
self.window.attributes('-fullscreen', True)
self.fullScreenState = False
self.window.bind("<F11>", self.toggleFullScreen)
self.window.bind("<Escape>", self.quitFullScreen)
# Initialize Canvas
self.x = self.y = 0
self.window.configure(bg='black')
self.canvas = tk.Canvas(self.window, bg='black', highlightthickness=0)
self.canvas.bind("<ButtonPress-1>", self.on_mouse_click)
self.canvas.bind("<B1-Motion>", self.on_mouse_drag)
self.canvas.bind("<ButtonRelease-1>", self.on_mouse_release)
# Variables to keep track of
self.shape = "R"
self.window.bind("r", self.draw_rect)
self.window.bind("c", self.draw_circle)
self.rect = None
self.start_x = None
self.start_y = None
self.curr_item = None
# Right click menu
self.m = tk.Menu(self.window, tearoff=0)
self.m.add_command(label="Color", command=self.change_color)
self.m.add_command(label="Delete", command=self.delete_item)
self.canvas.bind("<ButtonPress-2>", self.popup)
# General right click menu
self.m2 = tk.Menu(self.window, tearoff=0)
self.m2.add_command(label="Color All", command=self.color_all)
self.m2.add_command(label="Delete All", command=self.delete_all)
self.m2.add_command(label="Import Config", command=lambda: self.open_file())
self.m2.add_command(label="Sequence", command=self.sequence)
self.m2.add_command(label="Gradient", command=self.gradient)
self.m2.add_command(label="Snapshot", command=self.snapshot)
# Sequence options
self.seq_time = 50
self.seq_rows = 4
self.seq_columns = 4
self.img_count = 0
# Gradient
self.m3 = tk.Menu(self.window, tearoff=0)
self.m3.add_command(label="Delete Gradient", command=self.delete_gradient)
self.m3.add_command(label="Snapshot", command=self.snapshot)
self.color1 = "black"
self.color2 = "black"
self.horizontal = True
# Pack
self.canvas.pack(fill=tk.BOTH, expand=True)
self.window.mainloop()
def toggleFullScreen(self, event):
self.fullScreenState = not self.fullScreenState
self.window.attributes("-fullscreen", self.fullScreenState)
def quitFullScreen(self, event):
self.fullScreenState = False
self.window.attributes("-fullscreen", self.fullScreenState)
# Toggle draw rectangle
def draw_rect(self, event):
self.shape = "R"
# Toggle draw circle
def draw_circle(self, event):
self.shape = "C"
# Start dragging rectangle
def on_mouse_click(self, event):
self.start_x = event.x
self.start_y = event.y
if self.shape == "R": self.rect = self.canvas.create_rectangle(self.x, self.y, 1, 1, fill="white", tags="R", outline="")
else: self.rect = self.canvas.create_oval(self.x, self.y, 1, 1, fill="white", tags="C", outline="")
# Draw rectangle on mouse drag
def on_mouse_drag(self, event):
curX, curY = (event.x, event.y)
self.canvas.coords(self.rect, self.start_x, self.start_y, curX, curY)
def on_mouse_release(self, event):
pass
# Popup menus
def popup(self, event):
overlap = self.canvas.find_overlapping(event.x, event.y, event.x, event.y)
if self.canvas.find_withtag("gradient"):
try:
self.m3.tk_popup(event.x_root, event.y_root)
finally:
self.m3.grab_release()
elif len(overlap) > 0:
self.curr_item = overlap[-1]
try:
self.m.tk_popup(event.x_root, event.y_root)
finally:
self.m.grab_release()
else:
try:
self.m2.tk_popup(event.x_root, event.y_root)
finally:
self.m2.grab_release()
# Delete current item
def delete_item(self):
self.canvas.delete(self.curr_item)
print("Deleted item: ", self.curr_item)
# Delete specific item
def delete(self, item):
self.canvas.delete(item)
# Change color of current item
def change_color(self):
color = tk.colorchooser.askcolor()
self.canvas.itemconfig(self.curr_item, fill=color[-1])
print("Changed color of item: ", self.curr_item)
def delete_all(self):
self.canvas.delete("all")
print("Deleted all")
def color_all(self):
color = tk.colorchooser.askcolor()
self.canvas.itemconfig("all", fill=color[-1])
print("Changed color for all")
# Open and read config file
def open_file(self):
file = askopenfile(mode='r', filetypes=[('Text Files', '*.txt')])
if file is not None:
config = file.read()
print(config)
parse_config(config, self.canvas, self.window)
# Create rectangles for sequence
def create_rect(self, x, y, w, h, color):
self.canvas.delete("all")
rect = self.canvas.create_rectangle(x, y, w, h, fill=color, tags="R", outline="")
self.canvas.after(self.seq_time//2, self.snapshot)
self.canvas.after(self.seq_time, self.delete, rect)
# Take snapshot
def snapshot(self):
# Replace with your directory
img_name = "/Users/halde/Documents/test_snapshots/{:04d}.png".format(self.img_count)
ret, frame = cam.read()
cv2.imwrite(img_name, frame)
# Replace with your directory
config = open("/Users/halde/Documents/test_snapshots/{:04d}.txt".format(self.img_count), "w")
if self.canvas.find_withtag("gradient"):
config.write("G " + self.color1 + " " + self.color2 + " ")
if self.horizontal: config.write("H\n")
else: config.write("V\n")
else:
items = self.canvas.find_all()
for item in items:
config.write(self.canvas.itemcget(item, "tags") + " " + str(self.canvas.bbox(item)[0]) + " "
+ str(self.canvas.bbox(item)[1]) + " " + str(self.canvas.bbox(item)[2]) + " " +
str(self.canvas.bbox(item)[3]) + " " + self.canvas.itemcget(item, "fill") + "\n")
config.close()
self.img_count += 1
# Run sequence
def sequence(self):
seq = sequencePopup.MyDialog(self.window)
if not seq.cont: return
self.seq_rows, self.seq_cols, self.seq_time, color = int(seq.rows), int(seq.cols), int(seq.time), seq.color
width = seq.w
height = seq.h
time = 0
y = 0
for i in range(self.seq_rows):
x = 0
for j in range(self.seq_cols):
self.canvas.after(time, self.create_rect, x, y, x + width, y + height, color)
time += self.seq_time
x += width
y += height
# Draw gradient
def gradient(self):
gradient = gradientColor.GradientDialogue(self.window)
self.color1, self.color2, self.horizontal = gradient.selection1, gradient.selection2, gradient.horizontal
if self.color1 is None or self.color2 is None: return
create_gradient(self.window, self.canvas, self.horizontal, self.color1, self.color2)
def delete_gradient(self):
self.canvas.delete("gradient")
if __name__ == '__main__':
app = LightStage()