/
mainGUI.py
281 lines (234 loc) · 9.87 KB
/
mainGUI.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
import numpy as np
from MainConsts import *
from CommonFunctions import get_image_size, MyDict, stderr
from ImageProperties import ImageObject, ImageProperties
from CameraProperties import CameraProperties
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.graphics import Line, Color, Rectangle
from kivy.core.window import Window, WindowBase
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget, Canvas
from os import getcwd, chdir
from os.path import exists
import warnings
ImageObjects = ImageProperties(G_image_path)
Project = G_start_project
chdir(Project)
class ButtonsWidget(GridLayout):
def __init__(self):
super(ButtonsWidget, self).__init__()
self.rows = 2
self.path_input = TextInput(text=G_start_project)
self.open_button = Button(text='Open')
self.load_data_button = Button(text='Load')
self.save_data_button = Button(text='Save')
self.clear_data_button = Button(text='Clear')
self.quit_button = Button(text='Quit')
self.ground_button = Button(text='Ground(0)')
self.walls_button = Button(text='Walls(0)')
self.sky_button = Button(text='Sky(0)')
self.motion_path_button = Button(text='Motion(0)')
self.calculate_scene_button = Button(text='Calculate Scene')
self.render_scene_button = Button(text='Render Scene')
self.widgets = MyDict(length=3)
self.buttons = (self.path_input, self.load_data_button, self.ground_button,
self.sky_button, self.motion_path_button, self.quit_button, self.open_button,
self.save_data_button, self.walls_button,
self.clear_data_button, self.calculate_scene_button, self.render_scene_button)
self.reactions = (self.on_load_button_pressed, self.on_ground_button_pressed, self.on_sky_button_pressed,
self.on_motion_button_pressed, exit, self.on_open_button_pressed, self.on_save_button_pressed,
self.on_walls_button_pressed, self.on_clear_button_pressed, self.on_calculate_button_pressed,
self.on_render_button_pressed)
self.on_buttons_count = MyDict.from_dictionary({
'Ground': 0,
'Walls': 0,
'Sky': 0,
'Motion': 0,
}, length=6)
for i, w in enumerate(self.buttons):
self.add_widget(w)
self.widgets['TextInput' if i == 0 else w.text] = w
if i > 0:
w.on_press = self.reactions[i - 1]
self.in_process_action_type = None
self.current_points = []
self.current_touches = []
self.prev_draw_point = None
self.colors = MyDict.from_dictionary({'Ground': (1, 0, 0),
'Walls': (1, 1, 0),
'Sky': (0, 0, 1),
'Motion': (0, 1, 0)})
print 'colors:', self.colors
self.cp = None
def add_point(self, point):
if self.in_process_action_type is None:
return
self.current_points.append(point)
def add_touch(self, touch):
if self.in_process_action_type is None:
return
self.current_touches.append(touch)
def add_object(self):
if self.in_process_action_type is not None and self.current_points and self.current_points.__len__() > 2:
ImageObjects.add(ImageObject(points=self.current_points,
kivy_touches=self.current_touches,
type=self.in_process_action_type))
print 'Object added: %s %s' % (self.in_process_action_type, self.current_points.__str__())
self.add_button_count(self.in_process_action_type)
self.upgrade_buttons_count()
self.in_process_action_type = None
self.current_points = []
self.current_touches = []
self.prev_draw_point = None
def draw_point(self, touch):
if not self.in_process_action_type:
self.prev_draw_point = None
return
if self.prev_draw_point:
image_widget.draw_line_from_touches(self.prev_draw_point, touch, self.colors[self.in_process_action_type])
self.prev_draw_point = touch
def upgrade_buttons_count(self):
for key in self.on_buttons_count:
self.widgets[key].text = '%s(%d)' % (key, self.on_buttons_count[key])
def add_button_count(self, key):
self.on_buttons_count[key] += 1
def on_open_button_pressed(self):
try:
chdir('../%s' % self.widgets['TextInput'].text)
global Project
Project = self.widgets['TextInput'].text
image_widget.source = '../%s/%s' % (Project, G_image_path)
image_widget.size = get_image_size(G_image_path)
ImageObjects.copy(ImageProperties(G_image_path))
print image_widget.size
self.on_clear_button_pressed()
except WindowsError:
pass
def on_load_button_pressed(self):
global ImageObjects
self.add_object()
ImageObjects = ImageProperties.load()
image_widget.draw_all_lines()
def on_save_button_pressed(self):
self.add_object()
ImageObjects.save()
def on_clear_button_pressed(self):
global ImageObjects, image_widget
self.in_process_action_type = None
for key in self.on_buttons_count:
self.on_buttons_count[key] = 0
self.widgets[key].text = '%s(0)' % key
image_widget.clear()
def on_motion_button_pressed(self):
if self.in_process_action_type:
self.add_object()
else:
self.in_process_action_type = 'Motion'
def on_ground_button_pressed(self):
if self.in_process_action_type:
self.add_object()
else:
self.in_process_action_type = 'Ground'
def on_walls_button_pressed(self):
if self.in_process_action_type:
self.add_object()
else:
self.in_process_action_type = 'Walls'
def on_sky_button_pressed(self):
if self.in_process_action_type:
self.add_object()
else:
self.in_process_action_type = 'Sky'
def on_calculate_button_pressed(self):
self.add_object()
self.cp = CameraProperties(project=Project, image_path=G_image_path, ip=ImageObjects)
self.cp.calculate()
def on_render_button_pressed(self):
self.add_object()
if self.cp is not None and self.cp.calculated:
self.cp.render()
buttons_widget = ButtonsWidget()
class ImageWidget(Image):
def __init__(self):
super(ImageWidget, self).__init__()
self.source = G_image_path
self.size = get_image_size(G_image_path)
self.widget_prop = 1, 1
def on_touch_down(self, touch):
if not self.collide_point(touch.x, touch.y):
return
print self.collide_point(touch.x, touch.y)
buttons_widget.add_touch(touch)
buttons_widget.draw_point(touch)
buttons_widget.add_point(self.get_image_point((touch.x, touch.y)))
def clear(self):
for obj in self.canvas.children:
if type(obj) == Line:
self.canvas.remove(obj)
def draw_line_from_touches(self, touch1, touch2, (r, g, b)):
print >> stderr, 'DRAW!'
with self.canvas:
Color(r, g, b)
Line(points=touch1.pos + touch2.pos, width=2)
def draw_lines_from_points(self, points, (r, g, b), width=2):
with self.canvas:
Color(r, g, b)
Line(points=points, width=width)
def draw_all_lines(self):
for key, itself in ImageObjects:
if itself is None or itself is []:
continue
lines = []
for i, touches in enumerate(ImageObjects.kivy_objects[key]):
buttons_widget.add_button_count(key)
all_coordinates = map(lambda touch: [touch.x, touch.y], touches)
lines.append([])
for coordinate in all_coordinates:
lines[i] += coordinate
for line in lines:
self.draw_lines_from_points(line, buttons_widget.colors[key], width=2)
buttons_widget.upgrade_buttons_count()
def get_image_point(self, (x, y)):
win_size = self.size
img_size = map(float, get_image_size(self.source))
ww, wh = win_size
iw, ih = img_size
win_prop, img_prop = ww / wh, iw / ih
if win_prop > 1:
if img_prop > 1:
if img_prop > win_prop:
t = float(win_size[0]) / img_size[0]
ans_x = x / t
ans_y = (y - (win_size[1] - img_size[1] * t) * .5) / t
else:
t = float(win_size[1]) / img_size[1]
ans_y = y / t
ans_x = (x - (win_size[0] - img_size[0] * t) * .5) / t
print 'GET_IMAGE_POINT DEBUG'
print win_prop, img_prop
print t
print '%s -> %s' % ((x, y).__str__(), (ans_x, ans_y).__str__())
return ans_x, ans_y
return 0, 0
image_widget = ImageWidget()
class MainWidget(BoxLayout):
def __init__(self):
super(MainWidget, self).__init__()
self.orientation = 'vertical'
self.add_widget(buttons_widget)
self.add_widget(Widget())
self.add_widget(image_widget)
self.children[2].size_hint = 1, .18
self.children[1].size_hint = 1, .03
image_widget.widget_prop = 1, 1 - .21
main_widget = MainWidget()
class MainGUI(App):
def build(self):
return main_widget
if __name__ == '__main__':
MainGUI().run()