-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageManage_main.py
406 lines (375 loc) · 15.4 KB
/
ImageManage_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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
from PyQt5 import QtWidgets, QtCore, QtGui
import sys
import os
import ImageManage_main_ui
import ImageManage_mtcnndlib
import cv2
class ImageManageMain(QtWidgets.QMainWindow, ImageManage_main_ui.Ui_MainWindow):
def __init__(self):
super(ImageManageMain, self).__init__()
self.setupUi(self)
self.facePath = '' # 图片文件夹的根目录
self.txtpath = '' # txt文件(坐标文件)保存的路径
self.facefeature = [] # 储存标记完成为图片路径和对应的特征点的字典
self.historytem = [[0, 0], [0, 0]] # 储存标记的历史记录字典
self.tagfoldernum = 0 # 当前所要处理的文件夹的ID
self.tagpicnum = 0 # 当前要处理的图片的ID
self.picpath = [] # 储存当前所要处理de文件夹的图片的集合
self.beingpicpath = '' # 储存当前处理的图片文件地址(绝对路径)
self.imgfixflag = 0 # 图片修改是否完成FLAG
self.pointnum = -1
self.puttype = 1
# self.ponitmax = 2
self.x = 0
self.y = 0
self.tface = ImageManage_mtcnndlib.MtcnnDlib()
self.labelshowpic(self.picshowLabel)
self.savebutton.setEnabled(False)
self.testbutton.setEnabled(False)
def loadOnclicked(self):
self.picpath = []
self.facePath = self.facepathEdit.text()
self.txtpath = self.txtoutpathEdit.text()
if self.facePath == '':
QtWidgets.QMessageBox.critical(self, '错误', '请选择人脸目录')
self.facepathEdit.setFocus()
return
if self.txtpath == '':
QtWidgets.QMessageBox.critical(self, '错误', '请选择txt保存目录')
self.txtoutpathEdit.setFocus()
return
self.rootpathlist = os.listdir(self.facePath) # 总文件夹中的文件夹集合
self.rootpathlistLen = len(self.rootpathlist) # 总文件夹中的文件夹个数
self.folderlenLabel.setText(str(self.rootpathlistLen)) # 文件夹个数
self.tagfoldernum = self.startnumEdit.text()
self.tagfolderLabel.setText(str(self.tagfoldernum))
c = os.path.join(self.facePath, self.rootpathlist[int(self.tagfoldernum)])
# if not os.path.isdir(c):
# self.nextdir()
# return
self.typefolderlsit = os.listdir(c)
# picfile = os.listdir(os.path.join(self.facePath, self.rootpathlist[int(self.tagfoldernum)]))
# print(picfile)
picfile = []
tempath = ''
for i in self.typefolderlsit:
tempath = os.path.join(self.facePath, self.rootpathlist[int(self.tagfoldernum)], i)
a = os.listdir(tempath)
for j in a:
self.picpath.append(os.path.join(tempath, j))
if self.picpath == []:
a = self.nextdir()
if a == 0:
return
self.loadOnclicked()
print('jump')
return
# for j in self.typefolderlsit:
# for i in picfile:
# self.picpath.append(os.path.join(self.facePath, self.rootpathlist[int(self.tagfoldernum)], j, i))
# print(self.picpath)
def startOnclicked(self):
if self.picpath == []:
return 0
self.tagpicnum = 0
# self.beingpicpath = self.picpath[self.tagpicnum]
# self.points = self.tface.detectFeature(self.beingpicpath)
# self.labelshowpic(self.picshowLabel, self.beingpicpath)
self.getpoint()
# print(self.points)
def nextdir(self):
if self.tagfoldernum != self.rootpathlistLen:
self.tagfoldernum = int(self.tagfoldernum) + 1
if self.tagfoldernum + 1 > self.rootpathlistLen:
print('over')
QtWidgets.QMessageBox.information(self, '提示', '标注完毕')
return 0
self.startnumEdit.setText(str(self.tagfoldernum))
self.picpath = []
def labelshowpic(self, swidget, img=None):
# print(picurl)
if img is None:
pic = cv2.imread('None.jpg')
else:
pic = img
if pic is None:
print('pic is None')
else:
shrink = cv2.resize(pic, (540, 640), interpolation=cv2.INTER_AREA)
shrink = cv2.cvtColor(shrink, cv2.COLOR_BGR2RGB)
QtImg = QtGui.QImage(shrink.data,
shrink.shape[1],
shrink.shape[0],
QtGui.QImage.Format_RGB888)
swidget.setPixmap(QtGui.QPixmap.fromImage(QtImg))
def testOnclicked(self):
print('test')
def saveOnclicked(self):
print('save')
def skipOnclicked(self):
self.tagpicnum += 1
self.historytem = [[0, 0], [0, 0]]
if int(self.tagpicnum) + 1 > len(self.picpath):
self.txtwrite()
a = self.nextdir()
if a == 0:
return
self.loadOnclicked()
self.startOnclicked()
else:
self.getpoint()
def OnMouseAction(self, event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDBLCLK:
self.lcbutton(x, y)
elif event == cv2.EVENT_LBUTTONDOWN:
self.lbutton(x, y)
elif event == cv2.EVENT_RBUTTONDOWN:
self.rbutton(x, y)
def lbutton(self, x, y):
if self.puttype == 2:
self.puttype = 1
self.historytem[self.tema] = [x, y]
self.drawcircle()
elif self.puttype == 1:
self.pointnum += 1
if self.pointnum == 2:
self.pointnum -= 1
return
self.historytem[self.pointnum] = [x, y]
self.drawcircle()
def rbutton(self, x, y):
if self.pointnum != -1:
self.historytem[self.pointnum] = [0, 0]
self.pointnum -= 1
self.drawcircle()
elif self.pointnum == -1:
return
def lcbutton(self, x, y):
if self.historytem.count([x, y]) == 1:
self.tema = self.historytem.index([x, y])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x, y - 1]) == 1:
self.tema = self.historytem.index([x, y - 1])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x, y + 1]) == 1:
self.tema = self.historytem.index([x, y + 1])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x - 1, y]) == 1:
self.tema = self.historytem.index([x - 1, y])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x - 1, y - 1]) == 1:
self.tema = self.historytem.index([x - 1, y - 1])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x - 1, y + 1]) == 1:
self.tema = self.historytem.index([x - 1, y + 1])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x + 1, y + 1]) == 1:
self.tema = self.historytem.index([x + 1, y + 1])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x + 1, y]) == 1:
self.tema = self.historytem.index([x + 1, y])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x + 1, y - 1]) == 1:
self.tema = self.historytem.index([x + 1, y - 1])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x - 2, y - 2]) == 1:
self.tema = self.historytem.index([x - 2, y - 2])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x - 2, y - 1]) == 1:
self.tema = self.historytem.index([x - 2, y - 1])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x - 2, y]) == 1:
self.tema = self.historytem.index([x - 2, y])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x - 2, y + 1]) == 1:
self.tema = self.historytem.index([x - 2, y + 1])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x - 2, y + 2]) == 1:
self.tema = self.historytem.index([x - 2, y + 2])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x - 1, y + 2]) == 1:
self.tema = self.historytem.index([x - 1, y + 2])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x - 1, y - 2]) == 1:
self.tema = self.historytem.index([x - 1, y + 2])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x, y - 2]) == 1:
self.tema = self.historytem.index([x, y - 2])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x, y + 2]) == 1:
self.tema = self.historytem.index([x, y + 2])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x + 1, y - 2]) == 1:
self.tema = self.historytem.index([x + 1, y - 2])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x + 1, y + 2]) == 1:
self.tema = self.historytem.index([x + 1, y + 2])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x + 2, y - 2]) == 1:
self.tema = self.historytem.index([x + 2, y - 2])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x + 2, y + 2]) == 1:
self.tema = self.historytem.index([x + 2, y + 2])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x + 2, y - 1]) == 1:
self.tema = self.historytem.index([x + 2, y - 1])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x + 2, y + 1]) == 1:
self.tema = self.historytem.index([x + 2, y + 1])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
elif self.historytem.count([x + 2, y]) == 1:
self.tema = self.historytem.index([x + 2, y])
self.historytem[self.tema] = [0, 0]
self.puttype = 2
# print('puttype ==2 ')
self.drawcircle()
def drawcircle(self):
i = -1
self.beingimgcopy = self.beingimg.copy()
for a in self.historytem:
i += 1
if a == []:
continue
cv2.circle(self.beingimgcopy, (a[0], a[1]), 2, (255, 0, 0), -1)
cv2.putText(self.beingimgcopy, "%d" % (i), (a[0], a[1]), cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (255, 0, 0))
def fixOnclicked(self):
a = 1
self.beingimgcopy = self.beingimg.copy()
closeflag = 1
cv2.namedWindow('OpenCV', 0)
cv2.setMouseCallback('OpenCV', self.OnMouseAction)
if self.pointnum == 1:
self.drawcircle()
while closeflag:
cv2.imshow('OpenCV', self.beingimgcopy)
keyboard = cv2.waitKey(1) & 0xFF
if keyboard == ord('q'):
closeflag = 0
cv2.destroyAllWindows()
self.pointnum = -1
# 获取图片和该图片的坐标信息
def getpoint(self):
self.beingpicpath = self.picpath[self.tagpicnum]
self.beingimg, self.points = self.tface.detectFeature(self.beingpicpath)
tempic = self.beingimg.copy()
# print(len(self.points))
# print(self.points)
if len(self.points) == 0:
self.labelshowpic(self.picshowLabel, tempic)
else:
self.historytem[0] = [int(round(self.points[0][0])), int(round(self.points[5][0]))]
self.historytem[1] = [int(round(self.points[1][0])), int(round(self.points[6][0]))]
i = -1
self.pointnum = 1
for a in self.historytem:
i += 1
if a == []:
continue
cv2.circle(tempic, (a[0], a[1]), 3, (255, 0, 0), -1)
cv2.putText(tempic, "%d" % (i), (a[0], a[1]), cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (255, 0, 0))
self.labelshowpic(self.picshowLabel, tempic)
def nextOnclicked(self):
self.tagpicnum += 1
if self.historytem != [[0, 0], [0, 0]]:
self.facefeature.append(
[self.beingpicpath, int(round(self.historytem[0][0])), int(round(self.historytem[0][1])),
int(round(self.historytem[1][0])),
int(round(self.historytem[1][1]))])
self.historytem = [[0, 0], [0, 0]]
if int(self.tagpicnum) + 1 > len(self.picpath):
self.txtwrite()
a = self.nextdir()
if a == 0:
return
self.loadOnclicked()
self.startOnclicked()
else:
self.getpoint()
def txtwrite(self):
print(self.facefeature)
txtname = str(self.rootpathlist[int(self.tagfoldernum)]) + '.txt'
txtpathname = os.path.join(self.txtpath, txtname)
with open(txtpathname, 'w') as f:
for a in self.facefeature:
context = str(a[0]) + ' ' + str(a[1]) + ' ' + str(a[2]) + ' ' + str(a[3]) + ' ' + str(a[4]) + '\r'
f.writelines(context)
self.facefeature = []
def __del__(self):
del self.tface
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
am = ImageManageMain()
am.show()
sys.exit(app.exec_())