-
Notifications
You must be signed in to change notification settings - Fork 0
/
LabelData.py
394 lines (359 loc) · 10.8 KB
/
LabelData.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import Tkinter
import tkFileDialog
import numpy as np
from scipy.misc import imsave
import kivy
import os
from kivy.uix.textinput import TextInput
from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line, Rectangle
from kivy.core.window import Window
from shutil import rmtree
from tempfile import tempdir, mkdtemp
from os import remove, makedirs
from os.path import isdir, exists, isfile
import collections
import time
from moviepy.editor import VideoFileClip
pad = 200
speed = 25
class iButton(Button):
pass
class MyPaintWidget(Widget):
def __init__(self, **kwargs):
super(MyPaintWidget, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(self._keyboard_closed, self, 'text')
self._keyboard.bind(on_key_down=self._on_keyboard_down)
Window.size = (1366, 768)
self.size = Window.size
# if smoke true - user can draw rectangle
self.smoke = False
# if fire true - user can draw rectangle
self.fire = False
# list with points of fire rectangle
self.rectanglePoints = []
# number of current frame
self.numberOfFrame = 0
# if video open isVideoOpen = True
self.isVideoOpen = False
# video
self.video = None
# filename of result txt file
self.txtFilename = ""
# path to temp directory
self.dirPath = ""
# dictionary with points
self.dictionary = {}
self.Forest = ""
self.In = ""
self.Out = ""
self.start = 0
self.finish = 0
self.type = "None"
self.move = False
self.time = time.time()
def _keyboard_closed(self):
print('My keyboard have been closed!')
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
# Keycode is composed of an integer + a string
# If we hit escape, release the keyboard
if time.time() - self.time == 0:
print True
return True
else:
self.time = time.time()
if keycode[1] == 'p':
print 'p'
if keycode[1] == 'f':
print "t"
self.NextFrame()
if keycode[1] == 'a':
print "a"
self.DrawSmoke()
if keycode[1] == 'd':
self.DrawFire()
if keycode[1] == 'z':
self.PrevFrame()
# Return True to accept the key. Otherwise, it will be used by
# the system.
return True
# def __init__(self, **kwargs):
# super(MyPaintWidget1, self).__init__(**kwargs)
# self._keyboard = Window.request_keyboard(
# self._keyboard_closed, self, 'text')
# if self._keyboard.widget:
# # If it exists, this widget is a VKeyboard object which you can use
# # to change the keyboard layout.
# pass
# self._keyboard.bind(on_key_down=self._on_keyboard_down)
#
# def _keyboard_closed(self):
# print('My keyboard have been closed!')
# self._keyboard.unbind(on_key_down=self._on_keyboard_down)
# self._keyboard = None
#
# def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
# print('The key', keycode, 'have been pressed')
# print(' - text is %r' % text)
# print(' - modifiers are %r' % modifiers)
#
# # Keycode is composed of an integer + a string
# # If we hit escape, release the keyboard
# if keycode[1] == 'escape':
# keyboard.release()
#
# # Return True to accept the key. Otherwise, it will be used by
# # the system.
# return True
def PrevFrame(self):
self.numberOfFrame -= 2 * speed
self.NextFrame()
def NextFrame(self):
for line in self.canvas.children:
if (type(line) == Line):
self.canvas.children.remove(line)
for obj in self.canvas.children:
if type(obj) == Rectangle:
self.rect = obj
filename = self.GetFrameFilename()
if (filename == None):
self.rect.size = (0, 0)
else:
self.rect.source = filename
def openVideoFile(self):
# open file explorer
root = Tkinter.Tk()
root.withdraw()
# get video filename
filename = tkFileDialog.askopenfilename(parent=root, title='Open file to encrypt')
try:
self.video = VideoFileClip(filename)
print self.video.fps
except Exception:
print "Error in opening file - " + filename
return None
# create default txt filename
self.txtFilename = filename[:filename.rfind(".")] + "_label.txt"
# display txt filename
self.ids.PathSaveFile.text = self.txtFilename
# create temp directory for frames from video
# self.dirPath = mkdtemp()
self.dirPath = "asdf"
makedirs("asdf")
print self.video.duration
# set number of frame
self.numberOfFrame = 0
# change winow size
Window.size = (self.video.size[0], self.video.size[1] + pad)
self.size = (self.video.size[0], self.video.size[1] + pad)
self.firstFrame = True
# find rectangle
for obj in self.canvas.children:
if type(obj) == Rectangle:
self.rect = obj
# set rectangle size
self.rect.size = (self.size[0], self.size[1] - pad)
# set frame to rectangle
self.rect.source = self.GetFrameFilename()
# return filename of frame from video
def GetFrameFilename(self):
# check if video open
if (self.video == None):
print "Error! Video doesn't open!"
return None
if (self.firstFrame):
imsave(self.dirPath + "/temp0.png", self.video.get_frame(self.numberOfFrame / self.video.fps))
self.firstFrame = False
self.dictionary[self.numberOfFrame] = {}
self.dictionary[self.numberOfFrame]["smoke"] = []
self.dictionary[self.numberOfFrame]["fire"] = []
return self.dirPath + "/temp0.png"
else:
# increase number of frame
self.numberOfFrame = int(self.numberOfFrame) + speed
if (self.numberOfFrame >= self.video.duration * self.video.fps):
self.numberOfFrame = int(self.numberOfFrame) - speed
self.Video = None
return None
self.dictionary[self.numberOfFrame] = {}
self.dictionary[self.numberOfFrame]["smoke"] = []
self.dictionary[self.numberOfFrame]["fire"] = []
# save frame from video
imsave(self.dirPath + "/temp" + str(self.numberOfFrame) + ".png",
self.video.get_frame(int(self.numberOfFrame / self.video.fps)))
print self.numberOfFrame / (self.video.fps * self.video.duration) * 100
return self.dirPath + "/temp" + str(self.numberOfFrame) + ".png"
def DrawSmoke(self):
# check if video open
if (self.video == None):
print "Error! Video doesn't open!"
return None
self.smoke = True
self.fire = False
def DrawFire(self):
# check if video open
if (self.video == None):
print "Error! Video doesn't open!"
return None
self.fire = True
self.smoke = False
def CleanSmoke(self):
points = self.dictionary.get(self.numberOfFrame)
if (points == None):
return None
pointsList = points.get("smoke")
if (points == None):
return None
else:
for points in pointsList:
# print "asdf"
for line in self.canvas.children:
if type(line) == Line:
for i in range(0, 4):
if line.points.count(points[i]) != 2:
break
else:
self.canvas.children.remove(line)
self.dictionary.get(self.numberOfFrame)["smoke"] = []
def CleanFire(self):
points = self.dictionary.get(self.numberOfFrame)
if (points == None):
return None
pointsList = points.get("fire")
# print "fire"
# print points
if (points == None):
return None
else:
for points in pointsList:
for line in self.canvas.children:
if type(line) == Line:
for i in range(0, 4):
if line.points.count(points[i]) != 2:
break
else:
self.canvas.children.remove(line)
self.dictionary.get(self.numberOfFrame)["fire"] = []
def FinishFire(self):
# check if video open
if (self.video == None):
print "Error! Video doesn't open!"
return None
self.finish = self.numberOfFrame
def StartFire(self):
# check if video open
if (self.video == None):
print "Error! Video doesn't open!"
return None
self.start = self.numberOfFrame
def In23(self):
# check if video open
if (self.video == None):
print "Error! Video doesn't open!"
return None
print "asdf"
self.type = "In"
self.ids.LInfo.text = self.type
def Out23(self):
# check if video open
if (self.video == None):
print "Error! Video doesn't open!"
return None
self.type = "Out"
self.ids.LInfo.text = self.type
def Forest23(self):
# check if video open
if (self.video == None):
print "Error! Video doesn't open!"
return None
self.type = "Forest"
self.ids.LInfo.text = self.type
def Quit(self):
print self.dirPath
if (isdir(self.dirPath)):
rmtree(self.dirPath)
App.get_running_app().stop()
def on_touch_down(self, touch):
super(MyPaintWidget, self).on_touch_down(touch)
color = (random(), 1, 1)
if (0 <= touch.y < self.size[1] - pad and 0 <= touch.x < self.size[0] and (self.smoke or self.fire)):
self.move = True
self.rectanglePoints.append(touch.x)
self.rectanglePoints.append(touch.y)
x = touch.x
y = touch.y
with self.canvas:
if (self.smoke):
Color(1, 0, 0)
else:
Color(0, 0, 1)
touch.ud['line'] = Line(rectangle=(x, y, 0, 0))
def on_touch_move(self, touch):
if (0 <= touch.y < self.size[1] - pad and 0 <= touch.x < self.size[0] and (
self.smoke or self.fire) and self.move):
x = self.rectanglePoints[0]
y = self.rectanglePoints[1]
touch.ud['line'].rectangle = (x, y, touch.x - x, touch.y - y)
def on_touch_up(self, touch):
if (0 <= touch.y < self.size[1] - pad and 0 <= touch.x < self.size[0] and (self.smoke or self.fire)):
self.move = False
self.rectanglePoints.append(touch.x)
self.rectanglePoints.append(touch.y)
if (self.smoke):
# self.dictionary[self.numberOfFrame]["smoke"] = self.rectanglePoints
self.dictionary[self.numberOfFrame]["smoke"].append(self.rectanglePoints)
# print "smoke dict"
# print self.dictionary[self.numberOfFrame]["smoke"]
else:
self.dictionary[self.numberOfFrame]["fire"].append(self.rectanglePoints)
# print "fire dict"
# print self.dictionary[self.numberOfFrame]["fire"]
self.smoke = False
self.fire = False
self.rectanglePoints = []
def SaveResult(self):
filename = self.ids.PathSaveFile.text
if (not (".txt" in filename)):
print "Error in text filename"
if isfile(filename):
i = 0
while (1):
filename = filename.replace(".txt", str(i) + ".txt")
if isfile(filename):
i += 1
continue
else:
break
f = open(filename, 'w')
f.write("type " + self.type + "\n")
f.write("start %s\n" % self.start)
f.write("finish %s\n" % self.finish)
self.dictionary = collections.OrderedDict(sorted(self.dictionary.items()))
for k, v in self.dictionary.iteritems():
f.write(str(k))
f.write(" smoke ")
if v.get("smoke") != None:
for item in v["smoke"]:
f.write("%s " % item)
else:
f.write(" 0 0 0 0 ")
f.write(" fire ")
if v.get("fire") != None:
for item in v["fire"]:
f.write("%s " % item)
else:
f.write("0 0 0 0")
f.write("\n")
f.close()
class MyPaintApp(App):
def build(self):
return MyPaintWidget()
if __name__ == '__main__':
MyPaintApp().run()
# from kivy.base import runTouchApp
# runTouchApp(MyPaintWidget())