-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrameSelector.py
194 lines (163 loc) · 7.37 KB
/
FrameSelector.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
import fnmatch
import os
import shutil
import _thread
import time
from tkinter import *
from tkinter import filedialog
from PIL import ImageTk, Image
from utils.ImageUtils import ImageUtils
class FrameSelector(object):
# Init the app GUI
def __init__(self):
self.index = 0
self.images = []
self.imagesFilename = []
self.saveLocation = None
self.coords = {}
self.coordShow = None
self.isSlideToRun = False
# Create a windows and remove the resize option
self.root = Tk()
self.root.wm_title("Frame Selector")
self.root.minsize(width=640, height=480)
self.root.resizable(width=False, height=False)
self.configureGUI()
def configureGUI(self):
self.l = Label(self.root, bg="black")
self.l.bind('<Button-1>', self.click)
self.l.pack(fill=BOTH, expand=1)
# Create the options buttons and add to GUI
f = Frame(self.root)
Button(f, text="Open folder", command=self.openFolderClick).pack(side=LEFT, padx=5, pady=5)
Button(f, text="Save location", command=self.saveClick).pack(side=LEFT, padx=5, pady=5)
Button(f, text="Prev", command=self.prevClick).pack(side=LEFT, padx=5, pady=5)
Button(f, text="Next", command=self.nextClick).pack(side=LEFT, padx=5, pady=5)
self.slideText = StringVar()
Button(f, textvariable=self.slideText, command=self.slide).pack(side=LEFT, padx=5, pady=5)
self.slideText.set("Slide start")
f.place(x=0, y=0)
# Create a label option
f2 = Frame(self.root)
Label(f2, text="Image label").pack(side=LEFT, padx=5, pady=5)
self.imageLabel = Entry(f2)
self.imageLabel.pack(side=LEFT, padx=5, pady=5)
Button(f2, text="Select", command=self.selectClick).pack(side=LEFT, padx=5, pady=5)
f2.place(x=0, y=35)
f3 = Frame(self.root)
self.log = StringVar()
Label(f3, textvariable=self.log).pack(side=LEFT, padx=5, pady=5)
self.jumpTo = Entry(f3)
self.jumpTo.pack(side=LEFT, padx=5, pady=5)
Button(f3, text="Jump to", command=self.jump).pack(side=LEFT, padx=5, pady=5)
f3.place(x=0, y=445, height=35)
f4 = Frame(self.root)
self.coordsLog = StringVar()
self.coordsLog.set("Coords: (640,480)")
Label(f4, textvariable=self.coordsLog).pack(side=LEFT, padx=5, pady=5)
f4.place(x=540, y=0, width=100)
# Start application
self.root.mainloop()
def jump(self):
if self.jumpTo.get() != "" and self.jumpTo.get().isnumeric():
self.index = int(self.jumpTo.get()) - 1
self.nextClick()
def slide(self):
self.isSlideToRun = not self.isSlideToRun
_thread.start_new_thread(self.slideRun, ())
if self.isSlideToRun == False:
self.slideText.set("Slide start")
else:
self.slideText.set("Slide stop")
def slideRun(self):
while self.isSlideToRun:
self.nextClick()
time.sleep(0.1)
def click(self, event):
self.coords["x"] = event.x
self.coords["y"] = event.y
self.coordsLog.set("Coords: ("+str(event.x)+","+str(event.y)+")")
if not self.coordShow:
self.coordShow = Label(self.root, text=" ", bg="red")
self.coordShow.place_configure(x=event.x-5, y=event.y-5, width=10, height=10)
else:
self.coordShow.place_forget()
self.coordShow.place_configure(x=event.x-5, y=event.y-5, width=10, height=10)
# Action to open button click
def openFolderClick(self):
# Select a folder
folderName = filedialog.askdirectory()
# Verify if a folder is selected
if folderName:
self.log.set("Search start")
self.images = []
self.imagesFilename = []
self.index = 0
_thread.start_new_thread(self.searchImages, (folderName,))
def searchImages(self, folderName):
# Verify in every subfolder id a .bin file exist inside of a Color folder
for root, dirnames, filenames in os.walk(folderName):
for filename in fnmatch.filter(filenames, '*.bin'):
if "Color" in root:
# Add the find image to a list
self.imagesFilename.append(filename)
self.images.append(os.path.join(root, filename))
# Show first image in GUI
self.nextClick()
self.log.set("Search ended")
# Action to save button click
def saveClick(self):
# Select a folder
folderName = filedialog.askdirectory()
# Verify if a folder is selected
if folderName:
self.saveLocation = folderName
self.log.set("Save location: " + folderName)
# Action to select button click
def selectClick(self):
if self.images and len(self.images) != 0 and self.saveLocation:
if self.imageLabel.get() != "":
if len(self.coords) == 2 and self.coords["x"] and self.coords["y"]:
if not os.path.exists(self.saveLocation+"\\"+self.imagesFilename[self.index]):
# Save the file name label and coords
with open(self.saveLocation+"\info.txt","a+") as f:
f.write(self.imagesFilename[self.index]+"@"+self.imageLabel.get()+"@"+str(self.coords["x"])+","+str(self.coords["y"])+"\n")
shutil.copy2(self.images[self.index], self.saveLocation)
self.log.set(self.imagesFilename[self.index] + " copied")
else:
self.log.set(self.imagesFilename[self.index] + " alread exist")
else:
self.log.set("You need to specify a coord")
else:
self.log.set("You need to specify a label")
else:
self.log.set("You need to open a folder and set a save location")
# Action to next button click
def nextClick(self):
# Verify if the list have image and is not the last image
if len(self.images) != 0 and len(self.images) > self.index:
#Create a PIL image from the bin
imageb = ImageUtils.readAsByte(self.images[self.index])
img = ImageUtils.decodeBytesToImage(imageb, 640, 480)
self.index = self.index + 1
# Add the PIL image to GUI
self.photo = ImageTk.PhotoImage(image=img)
self.l.configure(image=self.photo)
self.log.set("Image " + str(self.index) + "/" + str(len(self.images)))
else:
self.log.set("No more images")
self.isSlideToRun = False
def prevClick(self):
# Verify if the list have image and is not the last image
if len(self.images) != 0 and self.index > 0:
self.index = self.index - 1
#Create a PIL image from the bin
imageb = ImageUtils.readAsByte(self.images[self.index])
img = ImageUtils.decodeBytesToImage(imageb, 640, 480)
# Add the PIL image to GUI
self.photo = ImageTk.PhotoImage(image=img)
self.l.configure(image=self.photo)
self.log.set("Image " + str(self.index) + "/" + str(len(self.images)))
else:
self.log.set("No more images")
self.isSlideToRun = False