-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
226 lines (214 loc) · 9.19 KB
/
main.py
File metadata and controls
226 lines (214 loc) · 9.19 KB
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
import sys, cv2, shutil, time, os
import numpy as np
from skimage.metrics import structural_similarity as compare_ssim
from PyQt5.QtWidgets import *
from PyQt5 import uic,QtCore
form_class = uic.loadUiType("./src/UI.ui")[0]
class MCS(QtCore.QThread,QtCore.QObject):
result = QtCore.pyqtSignal(str)
label = QtCore.pyqtSignal(str)
pbar_value = QtCore.pyqtSignal(float)
go_stop = QtCore.pyqtSignal(bool)
def __init__(self,p1,p2,p3,st,c1,c2,parent=None):
super(MCS,self).__init__(parent)
self.p1 = p1
self.p2 = p2
self.p3 = p3
self.st = st
self.c1 = c1
self.c2 = c2
self.thread_type = ''
self.working = True
self.go_stop.emit(True)
def monster_search (self,sPath):
source = cv2.imread(sPath,cv2.IMREAD_COLOR) # 검사 대상
if source.shape[1]==1366:
source = source[268:345,369:959] # 1366 * 768 에서 출력되는 위치 자르기
elif source.shape[1]==800:
source = source[100:177,86:676]
elif source.shape[1]==1024:
source = source[268:345,198:788]
elif source.shape[1]==1280:
source = source[220:297,326:916]
elif source.shape[1]==1920:
source = source[424:501,646:1236]
else:
print("지원되지 않는 형식!")
return 0
target = np.load('src//target.npy') # 탐색 목표 이미지
mask = target[:,:,-1] #타겟의 알파 마스크 출력
tmp,mask_r=cv2.threshold(mask,254,255,cv2.THRESH_BINARY) # tmp는 사용하지 않음, 유사 부울린 마스크(mask_r) 생성
target = cv2.cvtColor(target,cv2.COLOR_RGBA2RGB)
w,h=source.shape[:2]
tgt = np.zeros((w,h,3),np.uint8)
cv2.copyTo(target,mask_r,tgt)
src = np.zeros((w,h,3),np.uint8)
cv2.copyTo(source,mask_r,src)
score , tmp = compare_ssim(src, tgt, full=True,multichannel=True)
# full=True: 이미지 전체에 대해서 구조비교를 수행한다.
return score * 100
def file_list(self,root_dir,listbool):
img_list = []
img_name = ['.jpg', '.jpeg', '.JPG', '.PNG', '.png']
if listbool == 2:
for (root, dirs, files) in os.walk(root_dir):
if len(files) > 0:
for file_name in files:
if file_name.startswith('Maple_A') and os.path.splitext(file_name)[1] in img_name:
img_path = root + "\\" + file_name
img_list.append(img_path)
else :
for files in os.listdir(root_dir):
if files.startswith('Maple_A') and '.'+files.split('.')[-1] in img_name:
img_path = root_dir + "\\" + files
img_list.append(img_path)
return(img_list)
def run(self):
timestr = time.strftime("%y-%m-%d_%H-%M-%S ")
if self.thread_type =='class' and self.working:
not_mon_log = "\n\n몬컬이 아닌 캡쳐 목록:"
mon_log = "\n\n몬컬 캡처 목록:"
cap_list = self.file_list(self.p1,self.c1)
all_n = len(cap_list)
i = 0
for source in cap_list:
filename = source.split('\\')[-1]
score= self.monster_search(source)
if score < self.st :
shutil.move(source,self.p2+'\\'+filename)
not_mon_log = not_mon_log+'\n'+filename+':'+f"{score:.1f}"+'%'
else :
shutil.move(source,self.p3+'\\'+filename)
mon_log = mon_log+'\n'+filename+':'+f"{score:.1f}"+'%'
i=i+1
self.label.emit(f'{i}'+'/'+f'{all_n}')
self.result.emit(filename+':'+f"{score:.1f}"+'%')
self.pbar_value.emit((i+1)/all_n)
if self.c2 :
f = open(f"{os.getcwd()}\\log-{timestr}.txt",'w',encoding='UTF-8')
f.write("시작시간: "+timestr+not_mon_log+mon_log)
f.close()
self.go_stop.emit(False)
elif self.thread_type =='log' and self.working:
not_mon_log = "\n\n몬컬이 아닌 캡쳐 목록:"
mon_log = "\n\n몬컬 캡처 목록:"
cap_list = self.file_list(self.p1,self.c1)
all_n = len(cap_list)
i = 0
for source in cap_list:
filename = source.split('\\')[-1]
score= self.monster_search(source)
if score < self.st :
not_mon_log = not_mon_log+'\n'+filename+':'+f"{score:.1f}"+'%'
else :
mon_log = mon_log+'\n'+filename+':'+f"{score:.1f}"+'%'
i=i+1
self.label.emit(f'{i}'+'/'+f'{all_n}')
self.result.emit(filename+':'+f"{score:.1f}"+'%')
self.pbar_value.emit((i+1)/all_n)
if self.c2 :
f = open(f"{os.getcwd()}\\log-{timestr}.txt",'w',encoding='UTF-8')
f.write("시작시간: "+timestr+not_mon_log+mon_log)
f.close()
self.go_stop.emit(False)
else:
self.terminate()
self.quit()
class MyWindow(QMainWindow, form_class):
path_signal = QtCore.pyqtSignal(str)
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.getFilepath1)
self.pushButton_2.clicked.connect(self.getFilepath2)
self.pushButton_3.clicked.connect(self.getFilepath3)
self.pushButton_4.clicked.connect(self.classifying)
self.pushButton_5.clicked.connect(self.logonly)
def getFilepath1(self):
path_name = QFileDialog.getExistingDirectory(self)
self.lineEdit.setText(path_name)
if path_name :
self.pushButton_5.setEnabled(True)
if self.lineEdit_3.text() and self.lineEdit_2.text() and self.lineEdit.text():
self.pushButton_4.setEnabled(True)
def getFilepath2(self):
path_name = QFileDialog.getExistingDirectory(self)
self.lineEdit_2.setText(path_name)
if self.lineEdit_3.text() and self.lineEdit_2.text() and self.lineEdit.text():
self.pushButton_4.setEnabled(True)
def getFilepath3(self):
path_name = QFileDialog.getExistingDirectory(self)
self.lineEdit_3.setText(path_name)
if self.lineEdit_3.text() and self.lineEdit_2.text() and self.lineEdit.text():
self.pushButton_4.setEnabled(True)
def classifying(self):
self.progressBar.setEnabled(True)
self.textEdit.setText("")
check1 = self.checkBox.checkState()
check2 = self.checkBox_2.checkState()
p1 = self.lineEdit.text()
p2 = self.lineEdit_2.text()
p3 = self.lineEdit_3.text()
st = self.spinBox.value()
self.switch_widgets(True)
self.th = MCS(p1,p2,p3,st,check1,check2,parent=self)
self.th.result.connect(self.setResultBox)
self.th.pbar_value.connect(self.pBarUpdate)
self.th.label.connect(self.label5update)
self.th.go_stop.connect(self.thread_stop)
self.th.start()
self.th.thread_type = 'class'
def logonly(self):
self.progressBar.setEnabled(True)
self.textEdit.setText("")
self.switch_widgets(True)
check1 = self.checkBox.checkState()
check2 = self.checkBox_2.checkState()
p1 = self.lineEdit.text()
st = self.spinBox.value()
self.th = MCS(p1,'','',st,check1,check2,parent=self)
self.th.result.connect(self.setResultBox)
self.th.pbar_value.connect(self.pBarUpdate)
self.th.label.connect(self.label5update)
self.th.go_stop.connect(self.thread_stop)
self.th.start()
self.th.thread_type = 'log'
def setResultBox(self, result):
self.textEdit.append(result)
def switch_widgets(self,onoff):
if onoff:
self.pushButton.setDisabled(True)
self.pushButton_2.setDisabled(True)
self.pushButton_3.setDisabled(True)
self.pushButton_4.setDisabled(True)
self.pushButton_5.setDisabled(True)
self.checkBox.setDisabled(True)
self.checkBox_2.setDisabled(True)
self.spinBox.setDisabled(True)
else:
self.pushButton.setEnabled(True)
self.pushButton_2.setEnabled(True)
self.pushButton_3.setEnabled(True)
if self.lineEdit_3.text() and self.lineEdit_2.text() and self.lineEdit.text():
self.pushButton_4.setEnabled(True)
self.pushButton_5.setEnabled(True)
self.checkBox.setEnabled(True)
self.checkBox_2.setEnabled(True)
self.spinBox.setEnabled(True)
@QtCore.pyqtSlot(float)
def pBarUpdate(self,val):
self.progressBar.setValue(val*100)
@QtCore.pyqtSlot(str)
def label5update(self,val):
self.label_5.setText(val)
@QtCore.pyqtSlot(bool)
def thread_stop(self,val):
if val == False:
self.th.terminate()
self.th.working = False
self.switch_widgets(False)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()