forked from dguari1/Auto-eFace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThumbNailViewer.py
471 lines (359 loc) · 20.4 KB
/
ThumbNailViewer.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 12 09:31:28 2018
@author: guarind
"""
import cv2
import os
import sys
import numpy as np
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import QtCore
from utilities import get_info_from_txt
from ProcessLandmarks import GetLandmarks
class ThumbNailViewer(QtWidgets.QGraphicsView):
dropped = QtCore.pyqtSignal(object)
def __init__(self, ModelName, InfoPhotograph):
#usual parameters to make sure the image can be zoom-in and out and is
#possible to move around the zoomed-in view
super(ThumbNailViewer, self).__init__()
self._scene = QtWidgets.QGraphicsScene(self)
self._photo = QtWidgets.QGraphicsPixmapItem()
self._scene.addItem(self._photo)
self.setScene(self._scene)
self.setTransformationAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(255,204,204)))
self.setFrameShape(QtWidgets.QFrame.NoFrame)
#self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
self.setAcceptDrops(True)
self._hasImage = False
self._ImageAddress = None
self._validextensions = ['.png', '.jpg', '.jpeg', '.tif', '.tiff', '.PNG', '.JPG', '.JPEG', '.TIF', '.TIFF']
self.setBackground()
#QtWidgets.QGraphicsView.RubberBandDrag
self.WidgetName = None
self.InfoPhotograph = InfoPhotograph #this variable contains all
#the information from the photo, including the
#file, name, location, ID, and the results that
#are produced by dlib
self._Scale = 1 #this variable carries the scale of the image if it
#needs to be resized, if Scale = 1 then the original
#image was used for processing. If Scale > 1 then
#the original image was too large and a resized image
#was used for processing
# create Thread to take care of the landmarks and iris estimation
self.thread_landmarks = QtCore.QThread() # no parent!
self._ModelName = ModelName
def setBackground(self):
scriptDir = os.path.dirname(os.path.realpath(sys.argv[0]))
pixmap = QtGui.QPixmap(scriptDir + os.path.sep + 'include' + os.path.sep + 'drophere.jpg')
self.setPhoto(pixmap)
self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(255,204,204)))
def setPhoto(self, pixmap = None):
#this function puts an image in the scece (if pixmap is not None), it
#sets the zoom to zero
if pixmap and not pixmap.isNull():
#self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
self._photo.setPixmap(pixmap)
self.fitInView()
else:
self.setDragMode(QtWidgets.QGraphicsView.NoDrag)
self._photo.setPixmap(QtGui.QPixmap())
def fitInView(self):
#this function takes care of accomodating the view so that it can fit
#in the scene
rect = QtCore.QRectF(self._photo.pixmap().rect())
self.setSceneRect(rect)
if not rect.isNull():
unity = self.transform().mapRect(QtCore.QRectF(0, 0, 1, 1))
self.scale(1 / unity.width(), 1 / unity.height())
viewrect = self.viewport().rect()
scenerect = self.transform().mapRect(rect)
factor = min(viewrect.width() / scenerect.width(),
viewrect.height() / scenerect.height())
self.scale(factor, factor)
self.centerOn(rect.center())
def resizeEvent(self, event):
#this function assure that when the main window is resized the image
#is also resized preserving the h/w ratio
self.fitInView()
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls():
for url in event.mimeData().urls():
local_address = str(url.toLocalFile())
file_name,extension = os.path.splitext(local_address)
if extension in self._validextensions:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
else:
event.ignore()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls():
for url in event.mimeData().urls():
local_address = str(url.toLocalFile())
file_name,extension = os.path.splitext(local_address)
if extension in self._validextensions:
event.setDropAction(QtCore.Qt.CopyAction)
#conditiones meet, accept event and set background
event.accept()
self.setBackground()
pixmap = QtGui.QPixmap(local_address)
self.setPhoto(pixmap)
self._hasImage = True #indicate that there is an image
self._ImageAddress = os.path.normpath(local_address) #store the image address in a class variable
#verify is a new patient was added comparing the address of the existing images with the one that was dropeed
if self.InfoPhotograph._file_name == self._ImageAddress:
#this is not a new patient, the user is just adding the same image ...
self.InfoPhotograph._NewPatient = False
else:
#this is a new patient, we have to inform about this to the main window
self.InfoPhotograph._NewPatient = True
#put some info in the appropiate place
self.InfoPhotograph._file_name = self._ImageAddress
self.InfoPhotograph._photo = cv2.imread(self._ImageAddress)
#split the file name from its extension
file_name,extension = os.path.splitext(self.InfoPhotograph._file_name)
delimiter = os.path.sep
name=file_name.split(delimiter)
self.InfoPhotograph._name = name[-1] #keep only the last portion (the rest is the physical address of the file)
self.InfoPhotograph._extension = extension[1:]
self.InfoPhotograph._ID = self.WidgetName
if self.WidgetName == "Rest":
self.InfoPhotograph._Tag = "Rest"
elif self.WidgetName == "SmallSmile":
self.InfoPhotograph._Tag = "Best Smile"
elif self.WidgetName == "LargeSmile":
self.InfoPhotograph._Tag = "Biggest Smile"
elif self.WidgetName == "EyeBrow":
self.InfoPhotograph._Tag = "Brow Elevation"
elif self.WidgetName == "EyeClosureGently":
self.InfoPhotograph._Tag = "Gentle Eye Closure"
elif self.WidgetName == "EyeClosureTight":
self.InfoPhotograph._Tag = "Tight Eye Closure"
elif self.WidgetName == "PuckeringLips":
self.InfoPhotograph._Tag = "Pucker Lips"
elif self.WidgetName == "DentalShow":
self.InfoPhotograph._Tag = "Show Teeth"
self.InfoPhotograph._lefteye = None
self.InfoPhotograph._righteye = None
self.InfoPhotograph._shape = None
self.InfoPhotograph._boundingbox = None
self.InfoPhotograph._points = None
self.InfoPhotograph._OpenEmotrics = False
#now verify if there is a txt file already avaliable
if os.path.isfile(file_name+'.txt'):
#if the txt file already exists then the information
#is extracted and placed in memory
shape,lefteye,righteye,boundingbox = get_info_from_txt(file_name+'.txt')
self.InfoPhotograph._lefteye = lefteye
self.InfoPhotograph._righteye = righteye
self.InfoPhotograph._shape = shape
self.InfoPhotograph._boundingbox = boundingbox
self.InfoPhotograph._points = None
#set background green to inform that shape information is already avaliable
self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(204,255,204)))
#we now have all the info, emit the information to the main widget
self.dropped.emit(self.InfoPhotograph)
else:
event.ignore()
else:
event.ignore()
def picture_loaded(self, local_address, WidgetName):
self.WidgetName = WidgetName
pixmap = QtGui.QPixmap(local_address)
self.setPhoto(pixmap)
self._hasImage = True #indicate that there is an image
self._ImageAddress = os.path.normpath(local_address) #store the image address in a class variable
file_name,extension = os.path.splitext(local_address)
#put some info in the appropiate place
self.InfoPhotograph._file_name = self._ImageAddress
self.InfoPhotograph._photo = cv2.imread(self._ImageAddress)
#split the file name from its extension
file_name,extension = os.path.splitext(self.InfoPhotograph._file_name)
delimiter = os.path.sep
name=file_name.split(delimiter)
self.InfoPhotograph._name = name[-1] #keep only the last portion (the rest is the physical address of the file)
self.InfoPhotograph._extension = extension[1:]
self.InfoPhotograph._ID = self.WidgetName
if self.WidgetName == "Rest":
self.InfoPhotograph._Tag = "Rest"
elif self.WidgetName == "SmallSmile":
self.InfoPhotograph._Tag = "Best Smile"
elif self.WidgetName == "LargeSmile":
self.InfoPhotograph._Tag = "Biggest Smile"
elif self.WidgetName == "EyeBrow":
self.InfoPhotograph._Tag = "Brow Elevation"
elif self.WidgetName == "EyeClosureGently":
self.InfoPhotograph._Tag = "Gentle Eye Closure"
elif self.WidgetName == "EyeClosureTight":
self.InfoPhotograph._Tag = "Tight Eye Closure"
elif self.WidgetName == "PuckeringLips":
self.InfoPhotograph._Tag = "Pucker Lips"
elif self.WidgetName == "DentalShow":
self.InfoPhotograph._Tag = "Show Teeth"
self.InfoPhotograph._lefteye = None
self.InfoPhotograph._righteye = None
self.InfoPhotograph._shape = None
self.InfoPhotograph._boundingbox = None
self.InfoPhotograph._points = None
self.InfoPhotograph._OpenEmotrics = False
#now verify if there is a txt file already avaliable
if os.path.isfile(file_name+'.txt'):
#if the txt file already exists then the information
#is extracted and placed in memory
shape,lefteye,righteye,boundingbox = get_info_from_txt(file_name+'.txt')
self.InfoPhotograph._lefteye = lefteye
self.InfoPhotograph._righteye = righteye
self.InfoPhotograph._shape = shape
self.InfoPhotograph._boundingbox = boundingbox
self.InfoPhotograph._points = None
#set background green to inform that shape information is already avaliable
self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(204,255,204)))
#we now have all the info, emit the information to the main widget
self.dropped.emit(self.InfoPhotograph)
def mouseDoubleClickEvent(self, event):
if self._hasImage:
event.accept()
#the user wants to open Emotrics
#if the landmark information is already avaliable simply open Emotrics
if self.InfoPhotograph._shape is not None:
self.InfoPhotograph._OpenEmotrics = True
self.dropped.emit(self.InfoPhotograph)
else:
#if not, then first estimate it and then open Emotrics
self.InfoPhotograph._OpenEmotrics = True
self.Process_File()
else:
event.ignore()
QtWidgets.QGraphicsView.mouseDoubleClickEvent(self, event)
def Process_File(self):
#put some info in the appropiate place
self.InfoPhotograph._file_name = self._ImageAddress
self.InfoPhotograph._photo = cv2.imread(self._ImageAddress)
#split the file name from its extension
file_name,extension = os.path.splitext(self.InfoPhotograph._file_name)
delimiter = os.path.sep
name=file_name.split(delimiter)
self.InfoPhotograph._name = name[-1] #keep only the last portion (the rest is the physical address of the file)
self.InfoPhotograph._extension = extension[1:]
self.InfoPhotograph._ID = self.WidgetName
if self.WidgetName == "Rest":
self.InfoPhotograph._Tag = "Rest"
elif self.WidgetName == "SmallSmile":
self.InfoPhotograph._Tag = "Best Smile"
elif self.WidgetName == "LargeSmile":
self.InfoPhotograph._Tag = "Biggest Smile"
elif self.WidgetName == "EyeBrow":
self.InfoPhotograph._Tag = "Brow Elevation"
elif self.WidgetName == "EyeClosureGently":
self.InfoPhotograph._Tag = "Gentle Eye Closure"
elif self.WidgetName == "EyeClosureTight":
self.InfoPhotograph._Tag = "Tight Eye Closure"
elif self.WidgetName == "PuckeringLips":
self.InfoPhotograph._Tag = "Pucker Lips"
elif self.WidgetName == "DentalShow":
self.InfoPhotograph._Tag = "Show Teeth"
#if the photo was already processed then get the information for the
#txt file, otherwise process the photo using the landmark ans pupil
#localization algorithms
name = os.path.normpath(self._ImageAddress)
file_txt=name[:-4]
file_txt = (file_txt + '.txt')
if os.path.isfile(file_txt):
shape,lefteye,righteye,boundingbox = get_info_from_txt(file_txt)
self.InfoPhotograph._lefteye = lefteye
self.InfoPhotograph._righteye = righteye
self.InfoPhotograph._shape = shape
self.InfoPhotograph._boundingbox = boundingbox
self.InfoPhotograph._points = None
#we now have all the info, emit the information to the main widget
self.dropped.emit(self.InfoPhotograph)
else:
#if the image is too large then it needs to be resized....
h,w,d = self.InfoPhotograph._photo.shape
#if the image is too big then we need to resize it so that the landmark
#localization process can be performed in a reasonable time
self._Scale = 1 #start from a clear initial scale
if h > 1500 or w > 1500 :
if h >= w :
h_n = 1500
self._Scale = h/h_n
w_n = int(np.round(w/self._Scale,0))
temp_image = cv2.resize(self.InfoPhotograph._photo, (w_n, h_n), interpolation=cv2.INTER_AREA)
else :
w_n = 1500
self._Scale = w/w_n
h_n = int(np.round(h/self._Scale,0))
temp_image = cv2.resize(self.InfoPhotograph._photo, (w_n, h_n), interpolation=cv2.INTER_AREA)
else:
#the image is of appropiate dimensions so no need for modification
temp_image = self.InfoPhotograph._photo.copy()
#pass
#get the landmarks using dlib, and the and the iris
#using Dougman's algorithm
#This is done in a separate thread to prevent the gui from
#freezing and crashing
#create worker, pass the image to the worker
#self.landmarks = GetLandmarks(self.displayImage._opencvimage)
self.landmarks = GetLandmarks(temp_image, self._ModelName)
#move worker to new thread
self.landmarks.moveToThread(self.thread_landmarks)
#start the new thread where the landmark processing will be performed
self.thread_landmarks.start()
#Connect Thread started signal to Worker operational slot method
self.thread_landmarks.started.connect(self.landmarks.getlandmarks)
#connect signal emmited by landmarks to a function
self.landmarks.landmarks.connect(self.ProcessShape)
#define the end of the thread
self.landmarks.finished.connect(self.thread_landmarks.quit)
def ProcessShape(self, shape, numFaces, lefteye, righteye, boundingbox):
if numFaces == 1 :
if self._Scale is not 1: #in case that a smaller image was used for
#processing, then update the landmark
#position with the scale factor
for k in range(0,68):
shape[k] = [int(np.round(shape[k,0]*self._Scale,0)) ,
int(np.round(shape[k,1]*self._Scale,0))]
for k in range(0,3):
lefteye[k] = int(np.round(lefteye[k]*self._Scale,0))
righteye[k] = int(np.round(righteye[k]*self._Scale,0))
for k in range(0,4):
boundingbox[k] = int(np.round(boundingbox[k]*self._Scale,0))
self.InfoPhotograph._shape = shape
self.InfoPhotograph._lefteye = lefteye
self.InfoPhotograph._righteye = righteye
self.InfoPhotograph._boundingbox = boundingbox
self.InfoPhotograph._points = None
#we now have all the info, emit the information to the main widget
self.dropped.emit(self.InfoPhotograph)
elif numFaces == 0:
#no face in image then shape is None
self.InfoPhotograph._shape = None
self.InfoPhotograph._lefteye = None
self.InfoPhotograph._righteye = None
self.InfoPhotograph._boundingbox = None
#inform the user
QtWidgets.QMessageBox.warning(self,"Warning",
"No face in the image.\nIf the image does contain a face please modify the brightness and try again.",
QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.NoButton)
elif numFaces > 1:
#multiple faces in image then shape is None
self.InfoPhotograph._shape = None
self.InfoPhotograph._lefteye = None
self.InfoPhotograph._righteye = None
self.InfoPhotograph._boundingbox = None
#inform the user
QtWidgets.QMessageBox.warning(self,"Warning",
"Multiple faces in the image.\nPlease load an image with a single face.",
QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.NoButton)