forked from dguari1/Auto-eFace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini_Emotrics_old.py
719 lines (564 loc) · 39.4 KB
/
mini_Emotrics_old.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 12 18:41:24 2017
@author: Diego L.Guarin -- diego_guarin at meei.harvard.edu
"""
import os
import sys
import cv2
import numpy as np
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import QtCore
from results_window import ShowResults
from results_window import CustomTabResult
from ImageViewerandProcess import ImageViewer
from measurements import get_measurements_from_data
from utilities import estimate_lines
from utilities import get_info_from_txt
#from utilities import get_landmarks
#from utilities import get_pupil_from_image
from utilities import mark_picture
from utilities import save_snaptshot_to_file
from utilities import save_txt_file
from utilities import save_xls_file
from utilities import save_xls_file_patient
from ProcessLandmarks import GetLandmarks
"""
This is the main window of the program, it contains a ToolBar and a GraphicsView objects.
The toolbar includes actions for:
- Load Image: Loads an image and localizes the landmarks and iris in the image.
If the image is not a single face then it skips the landmark and iris
localization. If the landmark information is available in a txt file then
the program uses this to place the landmarks and skips the automatic landmark
localization.
- Create Patient: Opens up a new window where the user can load two images
that will be compared, the landmark and iris localization is perform during
the image loading so that if the image is not a single face the user will be
informed and won't be allowed to continue. The only ways to close the new
window are by loading to valid images (single faces) or cancelling. In this
window is possible to assign an ID to the patient (by default is the current
date) and to each photo.
Future improvements might include facial recognition to verify that both
photos are from the same patient
- Change photo: allows to move between the patient photos. It is un-active
if there is no patient. Once the patient is created this action becomes
active
- Fit image to window: Fits the image to the current size of the window.
Useful after zoom-in the picture to go back to full-view in one click
- Match iris diameter: Makes sure that both iris have the same diameter by
enlarging the smaller circle fitted to the iris. Is usefull when one eye is
closed and is difficul to properly find the iris size.
- Find face center: It fits a line connecting the center of both iris and
a new, perperdicular line in the middle. It is usefull to divide the
face vertically.
- Toggle landmarks: It toggles on or off the landmarks from the face, is
usefull if you want to see the face without anything added to it.
- Facial metrics: It opens up a new window displaying a set of important
facial metrics. In there is possible to see a description and a graphical
explanation of each metric. It has two different modes: If a single image
is being processed, the new window will present a table containing the
metrics for both sides of the face, the absolute difference and a percent
difference based on the non-paralyzed side measurements. If a patient is
being analyzed (two photos) the new window contains three tabs, one for each
image and a third one computing the variation in the metrics between both
images. This is useful to compare pre and post-operative cases.
- Save results: Produces two files in the folder where the image is located.
One text (.txt) file containing the landmarks information, and an excel (.xls)
file containing the facial metrics. If the a patient is being processed (two
images) then the excel file contains information about each image and the
difference. The excel file will inherit the name of the image file or the
patient ID depending on the mode.
- Save current view: Saves the current view as png or jpg file.
- Settings (not implemented yet): Allows for further customization of the
software. It facilitates the selection of scale (currently we assume that
the iris diamater is 11.77mm), and the diameter and color of the landmaks
- Exit: Exits the program
"""
class Emotrics(QtWidgets.QDialog):
def __init__(self, FileName):
super(Emotrics, self).__init__()
#self.setGeometry(5,60,700,500)
self.setWindowTitle('Emotrics')
scriptDir = os.path.dirname(os.path.realpath(sys.argv[0]))
self.setWindowIcon(QtGui.QIcon(scriptDir + os.path.sep + 'include' +os.path.sep +'icon_color'+ os.path.sep + 'meei_3WR_icon.ico'))
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowFlags(self.windowFlags() |
QtCore.Qt.WindowSystemMenuHint |
QtCore.Qt.WindowMinMaxButtonsHint)
self._new_window = None
self._file_name = FileName
self._Patient = None
self._tab1_results = None
self._tab2_results = None
self._tab3_results = None
self._toggle_landmaks = True
self._toggle_lines = True
self._Scale = 1 #this variable carries the scale of the image if it
#needs to be resized, if Scale = 1 then the origina
#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!
#initialize the User Interface
self.initUI()
def initUI(self):
#local directory
scriptDir = os.getcwd()#os.path.dirname(os.path.realpath(sys.argv[0]))
#image
#read the image from file
#img_Qt = QtGui.QImage(scriptDir + os.path.sep + 'include' +os.path.sep +'icon_color'+ os.path.sep + 'Facial-Nerve-Center.jpg')
#img_show = QtGui.QPixmap.fromImage(img_Qt)
#the image will be displayed in the custom ImageViewer
self.displayImage = ImageViewer()
#self.displayImage.setPhoto(img_show)
#toolbar
fitAction = QtWidgets.QAction('Fit image to window', self)
fitAction.setIcon(QtGui.QIcon(scriptDir + os.path.sep + 'include' +os.path.sep +'icon_color'+ os.path.sep + 'fit_to_size_icon.png'))
fitAction.triggered.connect(self.displayImage.show_entire_image)
eyeAction = QtWidgets.QAction('Match iris diameter', self)
eyeAction.setIcon(QtGui.QIcon(scriptDir + os.path.sep + 'include' +os.path.sep +'icon_color'+ os.path.sep + 'eye_icon.png'))
eyeAction.triggered.connect(self.match_iris)
eyeLoad = QtWidgets.QAction('Import iris position and diameter', self)
eyeLoad.setIcon(QtGui.QIcon(scriptDir + os.path.sep + 'include' +os.path.sep +'icon_color'+ os.path.sep + 'eye_icon_import.png'))
eyeLoad.triggered.connect(self.load_iris)
centerAction = QtWidgets.QAction('Find face center', self)
centerAction.setIcon(QtGui.QIcon(scriptDir + os.path.sep + 'include' +os.path.sep +'icon_color'+ os.path.sep + 'center_icon.png'))
centerAction.triggered.connect(self.face_center)
toggleAction = QtWidgets.QAction('Toggle landmarks', self)
toggleAction.setIcon(QtGui.QIcon(scriptDir + os.path.sep + 'include' +os.path.sep +'icon_color'+ os.path.sep + 'toggle-icon.png'))
toggleAction.triggered.connect(self.toggle_landmarks)
measuresAction = QtWidgets.QAction('Facial metrics', self)
measuresAction.setIcon(QtGui.QIcon(scriptDir + os.path.sep + 'include' +os.path.sep +'icon_color'+ os.path.sep + 'ruler_icon.png'))
measuresAction.triggered.connect(self.create_new_window)
saveAction = QtWidgets.QAction('Save results', self)
saveAction.setIcon(QtGui.QIcon(scriptDir + os.path.sep + 'include' +os.path.sep +'icon_color'+ os.path.sep + 'save_icon.png'))
saveAction.triggered.connect(self.save_results)
snapshotAction = QtWidgets.QAction('Save current view', self)
snapshotAction.setIcon(QtGui.QIcon(scriptDir + os.path.sep + 'include' +os.path.sep +'icon_color'+ os.path.sep + 'snapshot_icon.png'))
snapshotAction.triggered.connect(self.save_snapshot)
exitAction = QtWidgets.QAction('Exit', self)
exitAction.setIcon(QtGui.QIcon(scriptDir + os.path.sep + 'include' +os.path.sep +'icon_color'+ os.path.sep + 'exit_icon.png'))
exitAction.triggered.connect(self.close_app)
#create the toolbar and add the actions
self.toolBar = QtWidgets.QToolBar(self)
self.toolBar.addActions((fitAction, eyeAction, eyeLoad,centerAction, toggleAction,
measuresAction, snapshotAction, saveAction, exitAction))
#set the size of each icon to 50x50
self.toolBar.setIconSize(QtCore.QSize(50,50))
for action in self.toolBar.actions():
widget = self.toolBar.widgetForAction(action)
widget.setFixedSize(50, 50)
self.toolBar.setMinimumSize(self.toolBar.sizeHint())
self.toolBar.setStyleSheet('QToolBar{spacing:5px;}')
#the main window consist of the toolbar and the ImageViewer
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.toolBar)
layout.addWidget(self.displayImage)
self.setLayout(layout)
self.load_file(self._file_name)
self.displayImage.update_view()
self.show()
def create_new_window(self):
#this creates a new window to display all the facial metrics, there
#are two modes, one if there is no Patient (self._Patient = None)
#and another if there is a patient (two photos)
if self._Patient is None:
if self.displayImage._shape is not None:
#if the measurements window is already open then close it
if self._new_window is not None:
self._new_window.close()
self._new_window = None
#compute the facial metrics using the landmarks
MeasurementsLeft, MeasurementsRight, MeasurementsDeviation, MeasurementsPercentual = get_measurements_from_data(self.displayImage._shape, self.displayImage._lefteye, self.displayImage._righteye)
#send all the information the the appropiate places in the window
self._tab1_results = CustomTabResult()
#filling t_new_window_tab1_results he info for the right
self._tab1_results._CE_right.setText('{0:.2f}'.format(MeasurementsRight.CommissureExcursion))
self._tab1_results._SA_right.setText('{0:.2f}'.format(MeasurementsRight.SmileAngle))
self._tab1_results._DS_right.setText('{0:.2f}'.format(MeasurementsRight.DentalShow))
self._tab1_results._MRD1_right.setText('{0:.2f}'.format(MeasurementsRight.MarginalReflexDistance1))
self._tab1_results._MRD2_right.setText('{0:.2f}'.format(MeasurementsRight.MarginalReflexDistance2))
self._tab1_results._BH_right.setText('{0:.2f}'.format(MeasurementsRight.BrowHeight))
#filling the info for the left
self._tab1_results._CE_left.setText('{0:.2f}'.format(MeasurementsLeft.CommissureExcursion))
self._tab1_results._SA_left.setText('{0:.2f}'.format(MeasurementsLeft.SmileAngle))
self._tab1_results._DS_left.setText('{0:.2f}'.format(MeasurementsLeft.DentalShow))
self._tab1_results._MRD1_left.setText('{0:.2f}'.format(MeasurementsLeft.MarginalReflexDistance1))
self._tab1_results._MRD2_left.setText('{0:.2f}'.format(MeasurementsLeft.MarginalReflexDistance2))
self._tab1_results._BH_left.setText('{0:.2f}'.format(MeasurementsLeft.BrowHeight))
#deviation
self._tab1_results._CE_dev.setText('{0:.2f}'.format(MeasurementsDeviation.CommissureExcursion))
self._tab1_results._SA_dev.setText('{0:.2f}'.format(MeasurementsDeviation.SmileAngle))
self._tab1_results._MRD1_dev.setText('{0:.2f}'.format(MeasurementsDeviation.MarginalReflexDistance1))
self._tab1_results._MRD2_dev.setText('{0:.2f}'.format(MeasurementsDeviation.MarginalReflexDistance2))
self._tab1_results._BH_dev.setText('{0:.2f}'.format(MeasurementsDeviation.BrowHeight))
self._tab1_results._DS_dev.setText('{0:.2f}'.format(MeasurementsDeviation.DentalShow))
self._tab1_results._CH_dev.setText('{0:.2f}'.format(MeasurementsDeviation.CommisureHeightDeviation))
self._tab1_results._UVH_dev.setText('{0:.2f}'.format(MeasurementsDeviation.UpperLipHeightDeviation))
self._tab1_results._LVH_dev.setText('{0:.2f}'.format(MeasurementsDeviation.LowerLipHeightDeviation))
self._tab1_results._CE_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.CommissureExcursion))
self._tab1_results._SA_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.SmileAngle))
self._tab1_results._MRD1_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.MarginalReflexDistance1))
self._tab1_results._MRD2_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.MarginalReflexDistance2))
self._tab1_results._BH_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.BrowHeight))
self._tab1_results._DS_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.DentalShow))
delimiter = os.path.sep
temp=self._file_name.split(delimiter)
photo_name=temp[-1]
photo_name=photo_name[0:-4]
self._tab1_results._tab_name=photo_name
#say to the window that presents the results that there is only 1 tab
self._new_window = ShowResults(self._tab1_results)
#show the window with the results
self._new_window.show()
self.displayImage.update_view()
else:
#here there is a patient and so the result window will have three tabs
if (self._Patient.FirstPhoto._shape is not None) and (self._Patient.SecondPhoto._shape is not None):
#if the measurements window is already open then close it
if self._new_window is not None:
self._new_window.close()
self._new_window = None
#compute the facial metrics for the first photo and fill the information
MeasurementsLeftFirst, MeasurementsRightFirst, MeasurementsDeviation, MeasurementsPercentual = get_measurements_from_data(self._Patient.FirstPhoto._shape, self._Patient.FirstPhoto._lefteye, self._Patient.FirstPhoto._righteye)
self._tab1_results = CustomTabResult()
#filling t_new_window_tab1_results he info for the right
self._tab1_results._CE_right.setText('{0:.2f}'.format(MeasurementsRightFirst.CommissureExcursion))
self._tab1_results._SA_right.setText('{0:.2f}'.format(MeasurementsRightFirst.SmileAngle))
self._tab1_results._DS_right.setText('{0:.2f}'.format(MeasurementsRightFirst.DentalShow))
self._tab1_results._MRD1_right.setText('{0:.2f}'.format(MeasurementsRightFirst.MarginalReflexDistance1))
self._tab1_results._MRD2_right.setText('{0:.2f}'.format(MeasurementsRightFirst.MarginalReflexDistance2))
self._tab1_results._BH_right.setText('{0:.2f}'.format(MeasurementsRightFirst.BrowHeight))
#filling the info for the left
self._tab1_results._CE_left.setText('{0:.2f}'.format(MeasurementsLeftFirst.CommissureExcursion))
self._tab1_results._SA_left.setText('{0:.2f}'.format(MeasurementsLeftFirst.SmileAngle))
self._tab1_results._DS_left.setText('{0:.2f}'.format(MeasurementsLeftFirst.DentalShow))
self._tab1_results._MRD1_left.setText('{0:.2f}'.format(MeasurementsLeftFirst.MarginalReflexDistance1))
self._tab1_results._MRD2_left.setText('{0:.2f}'.format(MeasurementsLeftFirst.MarginalReflexDistance2))
self._tab1_results._BH_left.setText('{0:.2f}'.format(MeasurementsLeftFirst.BrowHeight))
#deviation
self._tab1_results._CE_dev.setText('{0:.2f}'.format(MeasurementsDeviation.CommissureExcursion))
self._tab1_results._SA_dev.setText('{0:.2f}'.format(MeasurementsDeviation.SmileAngle))
self._tab1_results._MRD1_dev.setText('{0:.2f}'.format(MeasurementsDeviation.MarginalReflexDistance1))
self._tab1_results._MRD2_dev.setText('{0:.2f}'.format(MeasurementsDeviation.MarginalReflexDistance2))
self._tab1_results._BH_dev.setText('{0:.2f}'.format(MeasurementsDeviation.BrowHeight))
self._tab1_results._DS_dev.setText('{0:.2f}'.format(MeasurementsDeviation.DentalShow))
self._tab1_results._CH_dev.setText('{0:.2f}'.format(MeasurementsDeviation.CommisureHeightDeviation))
self._tab1_results._UVH_dev.setText('{0:.2f}'.format(MeasurementsDeviation.UpperLipHeightDeviation))
self._tab1_results._LVH_dev.setText('{0:.2f}'.format(MeasurementsDeviation.LowerLipHeightDeviation))
self._tab1_results._CE_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.CommissureExcursion))
self._tab1_results._SA_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.SmileAngle))
self._tab1_results._MRD1_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.MarginalReflexDistance1))
self._tab1_results._MRD2_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.MarginalReflexDistance2))
self._tab1_results._BH_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.BrowHeight))
self._tab1_results._DS_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.DentalShow))
self._tab1_results._tab_name=self._Patient.FirstPhoto._ID
#compute the facial metrics for the second photo and fill the information
MeasurementsLeftSecond, MeasurementsRightSecond, MeasurementsDeviation, MeasurementsPercentual = get_measurements_from_data(self._Patient.SecondPhoto._shape, self._Patient.SecondPhoto._lefteye, self._Patient.SecondPhoto._righteye)
self._tab2_results = CustomTabResult()
#filling t_new_window_tab1_results he info for the right
self._tab2_results._CE_right.setText('{0:.2f}'.format(MeasurementsRightSecond.CommissureExcursion))
self._tab2_results._SA_right.setText('{0:.2f}'.format(MeasurementsRightSecond.SmileAngle))
self._tab2_results._DS_right.setText('{0:.2f}'.format(MeasurementsRightSecond.DentalShow))
self._tab2_results._MRD1_right.setText('{0:.2f}'.format(MeasurementsRightSecond.MarginalReflexDistance1))
self._tab2_results._MRD2_right.setText('{0:.2f}'.format(MeasurementsRightSecond.MarginalReflexDistance2))
self._tab2_results._BH_right.setText('{0:.2f}'.format(MeasurementsRightSecond.BrowHeight))
#filling the info for the left
self._tab2_results._CE_left.setText('{0:.2f}'.format(MeasurementsLeftSecond.CommissureExcursion))
self._tab2_results._SA_left.setText('{0:.2f}'.format(MeasurementsLeftSecond.SmileAngle))
self._tab2_results._DS_left.setText('{0:.2f}'.format(MeasurementsLeftSecond.DentalShow))
self._tab2_results._MRD1_left.setText('{0:.2f}'.format(MeasurementsLeftSecond.MarginalReflexDistance1))
self._tab2_results._MRD2_left.setText('{0:.2f}'.format(MeasurementsLeftSecond.MarginalReflexDistance2))
self._tab2_results._BH_left.setText('{0:.2f}'.format(MeasurementsLeftSecond.BrowHeight))
#deviation
self._tab2_results._CE_dev.setText('{0:.2f}'.format(MeasurementsDeviation.CommissureExcursion))
self._tab2_results._SA_dev.setText('{0:.2f}'.format(MeasurementsDeviation.SmileAngle))
self._tab2_results._MRD1_dev.setText('{0:.2f}'.format(MeasurementsDeviation.MarginalReflexDistance1))
self._tab2_results._MRD2_dev.setText('{0:.2f}'.format(MeasurementsDeviation.MarginalReflexDistance2))
self._tab2_results._BH_dev.setText('{0:.2f}'.format(MeasurementsDeviation.BrowHeight))
self._tab2_results._DS_dev.setText('{0:.2f}'.format(MeasurementsDeviation.DentalShow))
self._tab2_results._CH_dev.setText('{0:.2f}'.format(MeasurementsDeviation.CommisureHeightDeviation))
self._tab2_results._UVH_dev.setText('{0:.2f}'.format(MeasurementsDeviation.UpperLipHeightDeviation))
self._tab2_results._LVH_dev.setText('{0:.2f}'.format(MeasurementsDeviation.LowerLipHeightDeviation))
self._tab2_results._CE_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.CommissureExcursion))
self._tab2_results._SA_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.SmileAngle))
self._tab2_results._MRD1_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.MarginalReflexDistance1))
self._tab2_results._MRD2_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.MarginalReflexDistance2))
self._tab2_results._BH_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.BrowHeight))
self._tab2_results._DS_dev_p.setText('{0:.2f}'.format(MeasurementsPercentual.DentalShow))
self._tab2_results._tab_name=self._Patient.SecondPhoto._ID
#compute the the different between both photos and fill the information
self._tab3_results = CustomTabResult()
#filling tab3_results with the difference between the two photos
self._tab3_results._CE_right.setText('{0:.2f}'.format(-MeasurementsRightFirst.CommissureExcursion+MeasurementsRightSecond.CommissureExcursion))
self._tab3_results._SA_right.setText('{0:.2f}'.format(-MeasurementsRightFirst.SmileAngle+MeasurementsRightSecond.SmileAngle))
self._tab3_results._DS_right.setText('{0:.2f}'.format(-MeasurementsRightFirst.DentalShow+MeasurementsRightSecond.DentalShow))
self._tab3_results._MRD1_right.setText('{0:.2f}'.format(-MeasurementsRightFirst.MarginalReflexDistance1+MeasurementsRightSecond.MarginalReflexDistance1))
self._tab3_results._MRD2_right.setText('{0:.2f}'.format(-MeasurementsRightFirst.MarginalReflexDistance2+MeasurementsRightSecond.MarginalReflexDistance2))
self._tab3_results._BH_right.setText('{0:.2f}'.format(-MeasurementsRightFirst.BrowHeight+MeasurementsRightSecond.BrowHeight))
#filling the info for the left
self._tab3_results._CE_left.setText('{0:.2f}'.format(-MeasurementsLeftFirst.CommissureExcursion+MeasurementsLeftSecond.CommissureExcursion))
self._tab3_results._SA_left.setText('{0:.2f}'.format(-MeasurementsLeftFirst.SmileAngle+MeasurementsLeftSecond.SmileAngle))
self._tab3_results._DS_left.setText('{0:.2f}'.format(-MeasurementsLeftFirst.DentalShow+MeasurementsLeftSecond.DentalShow))
self._tab3_results._MRD1_left.setText('{0:.2f}'.format(-MeasurementsLeftFirst.MarginalReflexDistance1+MeasurementsLeftSecond.MarginalReflexDistance1))
self._tab3_results._MRD2_left.setText('{0:.2f}'.format(-MeasurementsLeftFirst.MarginalReflexDistance2+MeasurementsLeftSecond.MarginalReflexDistance2))
self._tab3_results._BH_left.setText('{0:.2f}'.format(-MeasurementsLeftFirst.BrowHeight+MeasurementsLeftSecond.BrowHeight))
#say to the window that presents the results that there are 3 tabs
self._new_window = ShowResults(self._tab1_results, self._tab2_results, self._tab3_results)
#show the window with the results
self._new_window.show()
def match_iris(self):
#make both iris have the same diameter as the bigger one
if self.displayImage._lefteye is not None :
if self.displayImage._lefteye[2] < self.displayImage._righteye[2]:
self.displayImage._lefteye[2] = self.displayImage._righteye[2]
elif self.displayImage._lefteye[2] > self.displayImage._righteye[2]:
self.displayImage._righteye[2] = self.displayImage._lefteye[2]
elif self.displayImage._lefteye[2] == self.displayImage._righteye[2]:
pass
self._toggle_lines = True
self.displayImage._points = None
self.displayImage.set_update_photo()
def face_center(self):
#find a line connecting the center of both iris and then fit a perperdicular
#line in the middle
if self.displayImage._shape is not None:
if self._toggle_lines == True:
self._toggle_lines = False
points = estimate_lines(self.displayImage._opencvimage,
self.displayImage._lefteye,
self.displayImage._righteye)
self.displayImage._points = points
self.displayImage.set_update_photo()
else:
self.displayImage._points = None
self.displayImage.set_update_photo()
self._toggle_lines = True
def load_file(self, name):
# #load a file using the widget
# name,_ = QtWidgets.QFileDialog.getOpenFileName(
# self,'Load Image',
# '',"Image files (*.png *.jpg *.jpeg *.tif *.tiff *.PNG *.JPG *.JPEG *.TIF *.TIFF)")
#
# if not name:
# pass
# else:
#the user will load an single image so get rid of Patient and the
# changephotoAction in the toolbar
self._Patient = None
#if windows then transform / to \ (python stuffs)
name = os.path.normpath(name)
self._file_name = name
#if the measurements window is open then close it
if self._new_window is not None:
self._new_window.close()
#load image
self.displayImage._opencvimage = cv2.imread(name)
#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
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.displayImage._lefteye = lefteye
self.displayImage._righteye = righteye
self.displayImage._shape = shape
self.displayImage._boundingbox = boundingbox
self.displayImage._points = None
self.displayImage.update_view()
else:
#if the image is too large then it needs to be resized....
h,w,d = self.displayImage._opencvimage.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))
#self.displayImage._opencvimage=cv2.resize(self.displayImage._opencvimage, (w_n, h_n), interpolation=cv2.INTER_AREA)
temp_image = cv2.resize(self.displayImage._opencvimage, (w_n, h_n), interpolation=cv2.INTER_AREA)
#self._image = image
else :
w_n = 1500
self._Scale = w/w_n
h_n = int(np.round(h/self._Scale,0))
#self.displayImage._opencvimage=cv2.resize(self.displayImage._opencvimage, (w_n, h_n), interpolation=cv2.INTER_AREA)
temp_image = cv2.resize(self.displayImage._opencvimage, (w_n, h_n), interpolation=cv2.INTER_AREA)
#self._image = image
# #now that the image has been reduced, ask the user if the image
# #should be saved for continue the processing, otherwise the
# #processing cannot continue with the large image
#
# #get the image name (separete it from the path)
# delimiter = os.path.sep
# split_name=name.split(delimiter)
#
# #the variable 'name' contains the file name and the path, we now
# #get the file name and assign it to the photo object
# file_name = split_name[-1]
# new_file_name = file_name[:-4]+'_small.png'
#
# choice = QtWidgets.QMessageBox.information(self, 'Large Image',
# 'The image is too large to process.\n\nPressing OK will create a new file\n%s\nin the current folder. This file will be used for processing.\nOtherwise, click Close to finalize the App.'%new_file_name,
# QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Close, QtWidgets.QMessageBox.Ok)
#
# if choice == QtWidgets.QMessageBox.Close :
# self.close()
# app.exec_()
# else:
# #create a new, smaller image and use that for processing
# name = name[:-4]+'_small.png'
# self._file_name = name
# cv2.imwrite(name,self.displayImage._opencvimage)
else:
#the image is of appropiate dimensions so no need for modification
temp_image = self.displayImage._opencvimage.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)
#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.displayImage._shape = shape
self.displayImage._lefteye = lefteye
self.displayImage._righteye = righteye
self.displayImage._boundingbox = boundingbox
#
self.displayImage._points = None
elif numFaces == 0:
#no face in image then shape is None
self.displayImage._shape = None
#inform the user
QtWidgets.QMessageBox.warning(self,"Warning",
"No face in the image.\nIf the image does contain a face plase modify the brightness and try again.",
QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.NoButton)
elif numFaces > 1:
#multiple faces in image then shape is None
self.displayImage._shape = 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)
self.displayImage.update_view()
def load_iris(self):
#load a file using the widget
name,_ = QtWidgets.QFileDialog.getOpenFileName(
self,'Load Iris Position and Diameter',
'',"Image files (*.png *.jpg *.jpeg *.tif *.tiff *.PNG *.JPG *.JPEG *.TIF *.TIFF)")
if not name:
pass
else:
#if windows then transform / to \ (python stuffs)
name = os.path.normpath(name)
#if the measurements window is open then close it, the measures will be updated with the new eyes position
if self._new_window is not None:
self._new_window.close()
#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
file_txt=name[:-4]
file_txt = (file_txt + '.txt')
if os.path.isfile(file_txt):
shape,lefteye,righteye,_ = get_info_from_txt(file_txt)
dx_left = lefteye[0]-shape[27,0]
dy_left = shape[27,1]-lefteye[1]
dx_right = shape[27,0]-righteye[0]
dy_right = shape[27,1]-righteye[1]
self.displayImage._lefteye = [self.displayImage._shape[27,0]+dx_left, self.displayImage._shape[27,1]-dy_left,lefteye[2]]
self.displayImage._righteye = [self.displayImage._shape[27,0]-dx_right, self.displayImage._shape[27,1]-dy_right,lefteye[2]]
self.displayImage.set_update_photo()
else:
QtWidgets.QMessageBox.warning(self,"Warning",
"Iris information for this photograph is not avaliable",
QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.NoButton)
# self.displayImage._lefteye = lefteye
# self.displayImage._righteye = righteye
# self.displayImage.set_update_photo()
def toggle_landmarks(self):
#Hide - show the landmarks
if self._toggle_landmaks is True:
self._toggle_landmaks = False
self.displayImage.set_update_photo(self._toggle_landmaks)
elif self._toggle_landmaks is False:
self._toggle_landmaks = True
self.displayImage.set_update_photo(self._toggle_landmaks)
def save_snapshot(self):
#save the current view
if self.displayImage._opencvimage is not None:
proposed_name = self._file_name[:-4]+'-landmarks'
name,_ = QtWidgets.QFileDialog.getSaveFileName(self, 'Save File',proposed_name, 'png (*.png);;jpg (*.jpg);; jpeg (*.jpeg)')
if not name:
pass
else:
#if shape then add shape to image
temp_image = self.displayImage._opencvimage.copy()
#draw 68 landmark points
if self.displayImage._shape is not None:
temp_image = mark_picture(temp_image, self.displayImage._shape, self.displayImage._lefteye, self.displayImage._righteye, self.displayImage._points)
save_snaptshot_to_file(temp_image,name)
def save_results(self):
#save the results in a txt and xls files. There are two modes, one if
#there is no patient and another is the is a patient (two photos)
if self._Patient is None: #this implies that there is a single photo
if self._file_name is not None:
if self.displayImage._shape is not None:
save_txt_file(self._file_name, self.displayImage._shape, self.displayImage._lefteye, self.displayImage._righteye, self.displayImage._boundingbox)
MeasurementsLeft, MeasurementsRight, MeasurementsDeviation, MeasurementsPercentual = get_measurements_from_data(self.displayImage._shape, self.displayImage._lefteye, self.displayImage._righteye)
save_xls_file(self._file_name, MeasurementsLeft, MeasurementsRight, MeasurementsDeviation, MeasurementsPercentual)
else:#this implies that the user created a patient and wants to analize two photos
save_txt_file(self._Patient.FirstPhoto._file_name, self._Patient.FirstPhoto._shape, self._Patient.FirstPhoto._lefteye, self._Patient.FirstPhoto._righteye, self._Patient.FirstPhoto._boundingbox)
save_txt_file(self._Patient.SecondPhoto._file_name, self._Patient.SecondPhoto._shape, self._Patient.SecondPhoto._lefteye, self._Patient.SecondPhoto._righteye, self._Patient.SecondPhoto._boundingbox)
save_xls_file_patient(self._file_name,self._Patient)
def close_app(self):
#ask is the user really wants to close the app
choice = QtWidgets.QMessageBox.question(self, 'Message',
'Do you want to exit?', QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)
if choice == QtWidgets.QMessageBox.Yes :
self.close()
app.exec_()
else:
pass
def closeEvent(self, event):
#we need to close all the windows before closing the program
if self._new_window is not None:
self._new_window.close()
event.accept()
if __name__ == '__main__':
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
app.setStyle(QtWidgets.QStyleFactory.create('Cleanlooks'))
GUI = Emotrics()
GUI.show()
app.exec_()