-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
382 lines (305 loc) · 14.4 KB
/
main.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
# pyinstaller --icon=icon.ico --onefile --noconsole main.py
from PIL import Image
import os, sys
from PyQt5.QtWidgets import QApplication, QAction, QFileDialog, QLabel, QPushButton, QMainWindow, QMessageBox, QComboBox, QProgressBar, QVBoxLayout, QPlainTextEdit
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
from PyQt5.QtCore import QThread
from PyQt5 import QtWidgets, QtGui
from ftplib import FTP
from datetime import datetime
from collections import Counter
from tabulate import tabulate
import numpy as np
header = ["Korean", "English"]
k_name = ['기장', '녹두', '들깨', '땅콩', '수수', '옥수수', '조', '참깨', '콩', '팥']
e_name = ['proso_millet', 'green_gram', 'perilla', 'peanut', 'great_millet', 'corn', 'foxtail_millet', 'sesame', 'bean', 'red_bean']
result = np.concatenate((np.asarray(k_name).reshape(-1,1), np.asarray(e_name).reshape(-1,1)), 1)
if not os.path.exists('./log'):
os.mkdir('./log/')
class FileSearchThread(QThread):
def __init__(self, settings, species_list):
super().__init__()
self.settings = settings
self.species_list = species_list
def run(self):
# find status.
self.user_species_counter = Counter()
# last date.
self.last_date = '0000-00-00'
try:
# set ftp.
ftp = FTP()
ftp.connect(self.settings['ip'], 21)
ftp.login(self.settings['user'], self.settings['pass'])
ftp.encoding = 'cp949'
# search file names.
for sp in self.species_list:
ftp.cwd(self.settings['target_path'] + sp + '/images')
try:
files = ftp.nlst()
for f in files:
if self.settings['name'] in f:
taken_date = f.split('_')[0]
if taken_date > self.last_date:
self.last_date = taken_date
target_degree = f.split('_')[3]
self.user_species_counter[f'{sp} - {target_degree}'] += 1
except:
print('no file found.')
except Exception as e:
print(e)
return
if ftp:
ftp.close()
self.quit()
class SearchingStatusDialog(QtWidgets.QDialog):
def __init__(self, settings, species_list):
super().__init__()
self.settings = settings
self.setWindowTitle('Your status.')
self.setWindowIcon(QIcon('res/info.png'))
self.resize(300, 50)
self.layout = QVBoxLayout()
self.message = QPlainTextEdit("Wait a moment...")
self.layout.addWidget(self.message)
self.setLayout(self.layout)
self.thread = FileSearchThread(self.settings, species_list)
self.thread.finished.connect(self.show_status)
self.thread.start()
def show_status(self):
print('finish')
message_temp = '=' * 44 + '\n'
message_temp += f'{self.settings["name"].upper()} current status.\n'
message_temp += '=' * 44 + '\n'
message_temp += f'Last uploaded image taken date : {self.thread.last_date}\n'
message_temp += '-' * 44 + '\n'
total_val = 0
for key, value in sorted(self.thread.user_species_counter.items(), reverse=False):
message_temp += f"{key} : {value} shots\n"
total_val += value
message_temp += '-' * 44 + '\n'
message_temp += f'Total : {total_val} shots\n'
message_temp += '=' * 44
self.message.setPlainText(message_temp)
self.resize(350, 600)
self.update()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.image_extension = ['png', 'jpg']
self.species_list = ['proso_millet', 'green_gram', 'perilla', 'peanut',
'great_millet', 'corn', 'foxtail_millet', 'sesame', 'bean', 'red_bean']
self.degree_list = ['0', '45', '90']
self.setting_options = ['ip', 'user', 'path', 'name']
self.selected_species = self.species_list[0]
self.selected_degree = self.degree_list[0]
self.settings = dict()
self.image_path = None
self.initUI()
def initUI(self):
self.setWindowTitle('Plants Image Uploader')
self.setWindowIcon(QIcon('res/leaf.png'))
openFile = QAction(QIcon('res/folder.png'), 'select folder', self)
# openFile.setShortcut('Ctrl+O')
openFile.triggered.connect(self.show_dialog)
openInfo = QAction(QIcon('res/info.png'), 'Info', self)
openInfo.triggered.connect(self.show_info)
openclassdetail = QAction(QIcon('res/info.png'), 'classes_detail', self)
openclassdetail.triggered.connect(self.show_class_detail)
openStatus = QAction(QIcon('res/info.png'), 'Status', self)
openStatus.triggered.connect(self.show_status)
menubar = self.menuBar()
menubar.setStyleSheet("background-color: rgb(230,230,230)")
menubar.setNativeMenuBar(False)
filemenu = menubar.addMenu('&File')
filemenu.addAction(openFile)
infomenu = menubar.addMenu('&Info')
infomenu.addAction(openInfo)
infomenu.addAction(openStatus)
infomenu.addAction(openclassdetail)
self.label1 = QLabel('Selected path :', self)
self.label1.move(10, 50)
self.label2 = QLabel('', self)
self.label2.move(100, 50)
self.label2.resize(self.frameGeometry().width() - self.label1.frameGeometry().width(), self.label1.frameGeometry().height())
self.species = QComboBox(self)
self.species.move(10, 80)
self.species.resize(100, 23)
[self.species.addItem(sp) for sp in self.species_list]
self.species.activated[str].connect(self.on_activated_species)
self.degrees = QComboBox(self)
self.degrees.move(self.species.frameGeometry().width() + 15, 80)
self.degrees.resize(100, 23)
[self.degrees.addItem(de) for de in self.degree_list]
self.degrees.activated[str].connect(self.on_activated_degree)
self.btn = QPushButton('upload', self)
self.btn.resize(50, 23)
self.btn.move(self.species.frameGeometry().width() + self.degrees.frameGeometry().width() + 25, 80)
self.btn.clicked.connect(self.upload_clicked)
self.statusBar().setStyleSheet("border-width: 1px;border-top-style : solid;border-color : lightgray;margin : 2px")
self.statusBar().showMessage('Ready')
self.progressbar = QProgressBar()
self.progressbar.setMinimum(0)
self.progressbar.setMaximum(100)
self.progressbar.setAlignment(Qt.AlignCenter)
self.statusBar().addPermanentWidget(self.progressbar)
# load setting.
if self.load_setting():
# setup uploader name.
self.label0 = QLabel(f'{self.settings["name"].upper()}', self)
self.label0.move(10, 20)
font0 = self.label0.font()
font0.setBold(True)
font0.setPointSize(12)
self.label0.setFont(font0)
self.label0.resize(self.frameGeometry().width(), self.label0.frameGeometry().height())
# setup ui.
self.move(300, 300)
self.resize(600, 134)
self.setFixedSize(600, 134)
self.show()
mes = '[IMPORTANT]\n"setup.bin" 파일을 텍스트 에디터로 열어서 알맞게 수정해주세요.\n\n1. 특정 각도, 한 종류의 식물 사진을 특정 디렉터리로 옮깁니다.\n2. "File - select folder"를 클릭해서 사진이 있는 디렉터리를 선택합니다.\n3. 하단의 식물 종류와 각도를 선택하고 upload를 누릅니다.'
mes_en = '[IMPORTANT]\nPlease open the "setup.bin" file as a text editor and modify it accordingly.\n\n1. Move a picture of a particular angle, one type of plant, to a particular directory.\n2. Click "File - select folder" to select the directory where the pictures are located.\n3. Select the plant type and angle at the bottom and press upload.'
self.show_message('Instruction', mes + '\n\n' + mes_en, False)
else:
self.show_message('Warning', 'There is a problem with the setup.bin file.\nCheck this file and run again.', True)
def on_activated_species(self, item):
self.selected_species = item
print(item)
def on_activated_degree(self, item):
self.selected_degree = item
print(item)
def load_setting(self):
setting_path = './setup.bin'
if os.path.exists(setting_path):
f = open(setting_path, 'r')
settings = f.readlines()
for item in settings:
if not item.startswith('//') and '=' in item:
setup = list(map(str.strip, item.replace('\n', '').split('=')))
self.settings[setup[0]] = setup[1]
f.close()
return True
return False
def upload_clicked(self):
if self.image_path:
print('uploading...')
# return False or filtered file names.
filtered = self.is_files_exists()
if filtered:
print('start uploading..')
reply = QMessageBox.question(self,
'Check',
f'Please Check information.\n\n1.Name: {self.settings["name"]}\n2.Species: {self.selected_species}\n3.Degree: {self.selected_degree}',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
print('processing....')
# upload pictures.
self.upload_images(filtered)
else:
print('canceled.')
else:
self.show_message('Path Alert', 'Please select the folder first.', False)
def show_status(self):
msg = SearchingStatusDialog(self.settings, self.species_list)
msg.show()
msg.exec_()
def show_info(self):
msgbox = QMessageBox()
msgbox.setWindowIcon(QIcon('res/info.png'))
msgbox.setText('Plants data uploader (1.0.3)\n\nIf it\'s useful, put a tip on Jong Hyeok\'s desk... \n(I just love doing lots of homework on Saturdays~****)')
msgbox.show()
msgbox.exec_()
###########
def show_class_detail(self):
msgbox = QMessageBox()
msgbox.setWindowIcon(QIcon('res/info.png'))
msgbox.setText(tabulate(np.ndarray.tolist(result), headers = header, tablefmt="youtrack"))
msgbox.show()
msgbox.exec_()
########
def show_dialog(self):
get_path = QFileDialog.getExistingDirectory(self, 'select target folder', './')
if get_path:
self.image_path = get_path
self.statusBar().showMessage(f'Path loaded', 3000)
self.label2.setText(f'{self.image_path}')
self.update()
def is_files_exists(self):
item_list = os.listdir(self.image_path)
if len(item_list) > 0:
filtered = [i for i in item_list if len(i.split('.')) == 2 and i.split('.')[1].lower() in self.image_extension]
if len(filtered) > 0:
return filtered
else:
# miss match extension recognized.
self.show_message('Message', 'This program only support (*png, *jpg) extensions.\nImage file not found.', False)
elif len(item_list) == 0:
# no file.
self.show_message('Message', 'Image files not exist in this path.', False)
return False
def show_message(self, title, text, exit_sys):
msg_box = QMessageBox()
msg_box.setWindowTitle(title)
msg_box.setWindowIcon(QIcon('res/info.png'))
msg_box.setText(text)
msg_box.show()
msg_box.exec_()
if exit_sys:
sys.exit()
def upload_images(self, filtered_list):
self.f = open(f"./log/log_{str(int(datetime.now().timestamp() * 1000))}.txt", 'w')
try:
try:
# set ftp.
ftp = FTP()
ftp.connect(self.settings['ip'], 21)
ftp.login(self.settings['user'], self.settings['pass'])
ftp.encoding = 'cp949'
ftp.cwd(self.settings['target_path'] + self.selected_species + '/images')
except Exception as e:
self.statusBar().showMessage('Upload Error : Connection Failed', 5000)
self.show_message('Warning', 'Server Connection Failed', False)
self.f.write('Server Connection Failed\n')
self.f.write(str(e))
self.f.close()
return
for idx, i_name in enumerate(filtered_list):
m_path = self.image_path + '/' + i_name
img = Image.open(m_path)
try:
meta_date = img._getexif()[36867].split()[0].replace(':', '-')
except:
meta_date = '0000-00-00'
time_mark = str(int(datetime.now().timestamp() * 1000))
final_file_name = meta_date + '_' + \
self.settings['name'] + '_' + \
self.selected_species + '_' + \
self.selected_degree + '_' + time_mark + '.' + i_name.split('.')[1]
self.statusBar().showMessage(f'Uploading: {i_name}')
self.progressbar.setValue(idx * 100 // (len(filtered_list) - 1))
print(final_file_name)
# upload images.
try:
m_image = open(m_path, 'rb')
ftp.storbinary('STOR ' + final_file_name, m_image)
self.f.write(f'[Success] {m_path},{final_file_name}\n')
except:
self.statusBar().showMessage('Upload Error : target_name')
self.f.write(f'[Fail] {m_path},{final_file_name}\n')
# upload end.
if ftp:
ftp.close()
except Exception as eall:
self.f.write('[Upload Failed]\n')
self.f.write(str(eall))
self.f.close()
self.f.close()
self.statusBar().showMessage('Finish', 5000)
self.progressbar.reset()
self.show_message('Finish', 'Upload finished!', False)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())