-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
399 lines (302 loc) · 11.2 KB
/
run.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
395
396
397
398
399
# @author: CypherpunkSamurai
# @license: MIT
# A program to assist in tagging of images
import sys
import logging
import shelve # for settings
from PyQt5 import QtWidgets, uic
# Required
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtWidgets import QMessageBox
# Image
from PyQt5.QtWidgets import QGraphicsScene
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt, QRectF
# Lists
from src import utils
import os
from PyQt5.QtWidgets import QListWidgetItem
from PyQt5.QtGui import QColor
# Short Cuts
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QShortcut
# theme
from PyQt5.QtWidgets import QStyle
from PyQt5.QtWidgets import qApp
# Use MainWindow UI
from src.ui.MainWindow import Ui_MainWindow
from src.ui.Theme import ThemeChooseDlg, apply_theme
__version__ = 0.2
__author__ = "Cypherpunk Samurai"
__author_email__ = "cypherpunksamurai@protonmail.com"
logging.basicConfig(
level=logging.INFO
)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
"""
Main Window Class
"""
current_folder = None
current_image = None
# image handling
current_image_pixmap = None
current_scene = None
# colors
colors = {
"green": QColor("#8dd4b2"),
"red": QColor("#f09ead"),
"yellow": QColor("#f2bc5d"),
}
# cache
cached_caption = {}
def __init__(self, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
self.bind_connect()
def open_folder(self):
"""
Open a project folder
"""
if self.current_folder:
self.close_folder()
# Open Folder
current_folder = QFileDialog.getExistingDirectory(self, "Select Directory")
# If not selected
logging.info(f"opening folder: {current_folder}")
if current_folder == None or current_folder == "":
logging.info("No folder was selected...")
return
else:
self.current_folder = str(current_folder)
# List Files
for file in utils.list_images(self.current_folder):
item = QListWidgetItem(file)
# check if caption file is present
filename = os.path.join(self.current_folder, file)
caption_path = utils.change_file_ext(filename, "txt")
# if caption file present set green
if os.path.exists(caption_path) and not utils.is_empty(caption_path):
item.setBackground(self.colors["green"])
elif utils.is_empty(caption_path):
item.setBackground(self.colors["red"])
# add item
self.listFile.addItem(item)
# enable widget
self.txtCaption.setEnabled(True)
self.listFile.setEnabled(True)
def close_folder(self):
"""
Handles File Menu "Close"
"""
# Close the folder
self.current_folder = None
self.listFile.clear()
self.img.setScene(QGraphicsScene())
self.txtCaption.clear()
# btn
self.btnSaveCaption.setEnabled(False)
self.txtCaption.setEnabled(False)
self.listFile.setEnabled(False)
# Status
self.setStatusTip("Ready")
def message(self, text):
"""
Message
"""
QMessageBox.information(self, "Info", text)
def list_item_select(self, current_item, old_item):
"""
Runs when the list item is clicked
"""
# list was cleared
if current_item is None:
return
# get file
text = current_item.text()
file_path = os.path.join(self.current_folder, text)
# path
if not os.path.exists(file_path):
logging.info("file not found")
return
# save current
if not self.txtCaption.document().isEmpty() and self.txtCaption.document().toPlainText() and self.txtCaption.document().isModified():
# cache
self.cached_caption[old_item.text()] = self.txtCaption.document().toPlainText()
self.txtCaption.clear()
logging.info("caption was cached...")
# old item color
old_item.setBackground(self.colors["yellow"])
self.current_image = file_path
# load the image
self.load_image()
# render the new image
self.render_scene()
# btn
self.btnSaveCaption.setEnabled(True)
# load caption if exists
self.load_caption()
def load_image(self, url=None):
"""
loads a image to a self.img_scene pixmap for rendering and renders
"""
if url == None and not self.current_image == None: url=self.current_image
else: return
logging.info("currently selected: " + url)
self.img.setAlignment(Qt.AlignCenter)
# set
self.current_image_pixmap = QPixmap(url)
self.img_scene = QGraphicsScene()
self.img_scene.addPixmap(self.current_image_pixmap)
def render_scene(self):
"""
Render self.img_scene without reloading the image
"""
# Scene
self.img.setScene(self.img_scene)
# Scale
self.img.fitInView(QRectF(0, 0, self.current_image_pixmap.width(), self.current_image_pixmap.height()), Qt.KeepAspectRatio)
def resizeEvent(self, event):
"""
Overrides the resize event
"""
# render image everytime window resizes
if self.current_image: self.render_scene()
# resize
QtWidgets.QMainWindow.resizeEvent(self, event)
def load_caption(self, filename=None):
"""
Load caption from cache or txt if exits else return empty
"""
# clear
self.txtCaption.clear()
image_filename = self.listFile.currentItem().text()
# filename
if filename == None:
filename = os.path.join(self.current_folder, image_filename)
# caption file
caption_path = utils.change_file_ext(filename, "txt")
# caption
caption = None
# log("%i cached caption" % len(self.cached_caption))
if os.path.exists(caption_path):
caption = self.read_caption(caption_path)
elif image_filename in self.cached_caption:
caption = self.cached_caption.get(image_filename)
self.txtCaption.document().setPlainText(caption)
# simulate loading file
# change detection false positive avert
self.txtCaption.document().setModified(False)
def btn_save_caption_clicked(self):
"""
Handles SaveCaption button click
"""
# cap
caption = self.txtCaption.document().toPlainText()
# save caption in cache
self.cached_caption[self.listFile.currentItem().text()] = caption
# filename
filename = os.path.join(self.current_folder, self.listFile.currentItem().text())
# caption file
caption_path = utils.change_file_ext(filename, "txt")
self.save_caption(caption_path, caption)
# set the current item to green
if not len(caption) == 0: self.listFile.currentItem().setBackground(self.colors["green"])
else: self.listFile.currentItem().setBackground(self.colors["red"])
# simulate file save
self.txtCaption.document().setModified(False)
def save_caption(self, filename, caption):
"""
Save the caption in a txt file
"""
with open(filename, "w+") as file:
file.write(caption)
logging.info(f"[+] {filename}")
logging.info("Saved caption...")
def read_caption(self, filename):
"""
Read the caption from a txt file
"""
caption = None
with open(filename, "r", encoding="utf-8", errors="ignore") as file:
caption = file.read()
return caption
def choose_theme_dialog(self):
"""
Show a choose theme dialog
"""
dlg = ThemeChooseDlg()
if dlg.exec_():
# get returned value
values = dlg.getResult()
# apply theme
apply_theme(values)
# property
QtWidgets.QApplication.instance().setProperty("current_style", values)
# save current settings
try:
with shelve.open("config") as settings:
settings["current_style"] = values
except Exception as e:
logging.info("cannot write current_style to shelve. ", e)
return
else:
logging.info("no theme chosen")
return
def about(self):
self.message(f"Captioner {__version__}\n\nA Captioning tool created for image captioning. \n\n💻Authour: {__author__}\n📫Email: {__author_email__}")
def caption_shortcut_1(self):
"""
set's caption to list view
"""
logging.info("set focus to list view")
self.listFile.setFocus()
self.listFile.setCurrentRow(self.listFile.currentRow())
@pyqtSlot()
def caption_shortcut_2(self):
"""
set's caption to caption text box
"""
self.txtCaption.setFocus()
def bind_connect(self):
"""
Connection Binder
"""
# Exit
self.mnuExit.triggered.connect(sys.exit)
# Folder Select
self.mnuOpenFolder.triggered.connect(self.open_folder)
# Folder Close
self.mnuCloseFolder.triggered.connect(self.close_folder)
# Theme
self.mnuTheme.triggered.connect(self.choose_theme_dialog)
# About
self.mnuAbout.triggered.connect(self.about)
# ListWidget
self.listFile.currentItemChanged.connect(self.list_item_select)
# Caption Button
self.btnSaveCaption.clicked.connect(self.btn_save_caption_clicked)
# Short Cuts
self.cs1 = QShortcut(QKeySequence("Ctrl+Alt+Left"), self)
self.cs1.activated.connect(self.caption_shortcut_1)
self.cs2 = QShortcut(QKeySequence("Ctrl+Alt+Right"), self)
self.cs2.activated.connect(self.caption_shortcut_2)
if __name__ == "__main__":
# Run
app = QtWidgets.QApplication(sys.argv)
app.setProperty("current_style", None)
# read settings
if os.path.exists("config.dat"):
try:
with shelve.open("config") as settings:
if "current_style" in settings: app.setProperty("current_style", settings["current_style"])
except Exception as e:
logging.exception(e)
finally: pass
# current style
if app.property("current_style"):
apply_theme(app.property("current_style"))
# Windows
window = MainWindow()
window.show()
app.exec()