Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
1020 lines (906 loc) · 43.5 KB
/
Copy pathmain.py
File metadata and controls
1020 lines (906 loc) · 43.5 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
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# GCPDS - Universidad Nacional de Colombia
# Proyecto caracterización termográfica de extremidades inferiores durante aplicación de anestesia epidural
# Mayo de 2021
# Disponible en https//:github.com/blotero/FEET-GUI
import os
import re
from pathlib import Path
import json
import sys
import matplotlib.pyplot as plt
import numpy as np
import cv2
import time
import pytesseract
from PySide2.QtWidgets import QApplication, QMainWindow, QFileDialog
from PySide2.QtCore import QFile, QObject, SIGNAL, QDir, QTimer
from PySide2.QtUiTools import QUiLoader
from segment import ImageToSegment, SessionToSegment
from manualseg import manualSeg
from temperatures import mean_temperature, dermatomes_temperatures
from scipy.interpolate import make_interp_spline
import cv2
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *
from datetime import datetime
import tflite_runtime.interpreter as tflite
from postprocessing import PostProcessing
from report import plot_report
import threading
class RemotePullException(Exception):
def __init__(self, repoURL):
self.message = "Error pulling new changes into local DB from origin: " + str(repoURL)
super.__init__(self.message)
class RemoteOriginUnauthorizedException(Exception):
"""
Exception raised when there is no authorization for actions on remote image repository
"""
def __init__(self, URL):
self.URL = URL
self.message = f'Error in authorization with remote image repository {URL}.'
super.__init__(self.message)
class NotImplementedError(Exception):
"""
Error raised from methods that have not been implemented
"""
def __init__(self):
self.message = "This feature has not been implemented"
super.__init__(self.message)
class Window:
def __init__(self):
super(Window, self).__init__()
self.load_UI()
self.imgs = []
self.subj = []
self.make_connect()
self.init_logs()
self.inputExists = False
self.defaultDirectoryExists = False
self.isSegmented = False
self.files = None
self.temperaturesWereAcquired = False
self.scaleModeAuto = True
self.modelsPathExists = True #As soon as the model is present in the expected path
self.model = 'default_model.tflite'
self.fullScreen = True
#Loading segmentation models
self.s2s = SessionToSegment()
self.i2s = ImageToSegment()
self.s2s.setModel(self.model)
self.i2s.setModel(self.model)
self.s2s.loadModel()
self.i2s.loadModel()
self.ui.loadedModelLabel.setText(self.model)
self.camera_index = 0
self.setup_camera()
self.sessionIsCreated = False
self.driveURL = None
self.rcloneIsConfigured = False
self.repoUrl = 'https://github.com/blotero/FEET-GUI.git'
self.digits_model = tflite.Interpreter(model_path = './digits_recognition.tflite')
self.digits_model.allocate_tensors()
self.set_default_input_cmap()
self.file_system_model = QFileSystemModel()
self.file_system_model.setRootPath(QDir.currentPath())
self.ui.treeView.setModel(self.file_system_model)
plt.style.use('bmh')
self.session_info = {}
self.ui.progressBar.setVisible(False)
self.timer_cron = QTimer()
self.timer_cron.timeout.connect(self.tick)
def tick(self):
if self.current_secs < 10:
self.ui.lcdNumber.display(f'{self.current_mins}:0{self.current_secs}')
else:
self.ui.lcdNumber.display(f'{self.current_mins}:{self.current_secs}')
self.current_secs += 1
if self.current_secs%60 == 0:
self.current_mins += 1
self.current_secs = 0
def load_UI(self):
"""
Load xml file with visual objects for the interface
"""
loader = QUiLoader()
path = os.fspath(Path(__file__).resolve().parent / "form.ui")
ui_file = QFile(path)
ui_file.open(QFile.ReadOnly)
self.ui = loader.load(ui_file)
self.ui.showFullScreen()
ui_file.close()
def predict_number(self,image):
"""
Predicts digit value from a certain region image
"""
image_2 = cv2.resize(image, (28, 28), interpolation = cv2.INTER_NEAREST)
image_2 = cv2.cvtColor(np.uint8(image_2), cv2.COLOR_BGR2GRAY)
image_2 = np.expand_dims(image_2, -1)
image_2 = np.expand_dims(image_2, 0)
input_details = self.digits_model.get_input_details()
output_details = self.digits_model.get_output_details()
input_shape = input_details[0]['shape']
input_data = np.float32(image_2)
self.digits_model.set_tensor(input_details[0]['index'], input_data)
self.digits_model.invoke() # predict
output_data = self.digits_model.get_tensor(output_details[0]['index'])
return np.argmax(output_data)
def predict_number_with_pytesseract(self, img):
"""
Obtain number from section of an image
"""
uint8img = img.astype("uint8")
#print(np.unique(uint8img))
#print(uint8img.shape)
thresh = cv2.threshold(uint8img , 100, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1]
#plt.figure()
# plt.imshow(thresh)
text = pytesseract.image_to_string(thresh, config = '--psm 7')
#Text cleaning and replacement...
clean_text = text.replace('\n','').replace('-]', '4').replace(']', '1').replace(' ', '').replace(',', '.').replace('%', '7').replace('€','9').replace('[','').replace('&', '5').replace('-','3')
# plt.title(clean_text)
# plt.show()
try:
num = float(clean_text)
if num>=100:
num/=10
except:
print(f"Could not convert string {clean_text} into number")
self.message_print(f"No se ha podido detectar escalas automáticamente de: Texto base: {text}, Texto limpio: {clean_text}. Dejando rango por defecto: [25, 45]")
return -100
return num
def extract_scales_with_pytesseract(self,x):
"""
Extracts float lower and upper scales from a thermal image with pytesseract
"""
lower_seg = x[445: 467, 575: 625,0]
upper_seg = x[14: 34, 576: 624,0]
lower_prediction = self.predict_number_with_pytesseract(lower_seg)
upper_prediction = self.predict_number_with_pytesseract(upper_seg)
if lower_prediction == -100:
lower_prediction = 25
if upper_prediction == -100:
upper_prediction = 45
return lower_prediction, upper_prediction
def extract_scales_2(self,x):
"""
Exctract scales with easyocr (deprecated)
"""
x = np.uint8(x[:,560:,:])
#print(x)
result = self.reader.readtext(x,detail=0)
#print(result)
try:
result = [float(number) for number in result]
except:
self.message_print("No se ha podido detectar escalas automáticamente. Dejando rango por defecto: [25, 45]")
return 25, 45
lower = min(result)
upper = max(result)
self.message_print(f"Escala leida: {lower, upper}. Por favor verifique que sea la correcta y corríjala en caso de que no lo sea.")
return lower, upper
def extract_scales(self, x):
"""
Extracts float lower and upper scales from a thermal image
"""
lower_digit_1 = self.predict_number(x[445: 467, 575: 591])
lower_digit_2 = self.predict_number(x[445: 467, 589: 605])
lower_digit_3 = self.predict_number(x[445: 467, 609: 625])
upper_digit_1 = self.predict_number(x[14: 34, 576: 590])
upper_digit_2 = self.predict_number(x[14: 34, 590: 604])
upper_digit_3 = self.predict_number(x[14: 34, 610: 624])
lower_bound = lower_digit_1*10 + lower_digit_2 + lower_digit_3*0.1
upper_bound = upper_digit_1*10 + upper_digit_2 + upper_digit_3*0.1
return lower_bound, upper_bound
def extract_multiple_scales(self, X):
"""
Extracts scales from a whole imported session
"""
scales = []
for i in range(X.shape[0]):
scales.append(self.extract_scales_with_pytesseract(X[i]))
return scales
def populate_session_info(self):
"""
Fill dictionary attribute with the parms given by the info tab
"""
#Initial information obtained during session creation
self.session_info['Nombre'] = self.ui.nameField.text()
self.session_info['Edad'] = self.ui.ageField.text()
self.session_info['Tipo_de_documento'] = self.ui.weightField.value() #Spinbox
self.session_info['Nro_de_documento'] = self.ui.weightField.text()
self.session_info['Semanas_de_gestacion'] = self.ui.weeksField.value() #Spinbox
self.session_info['Peso'] = self.ui.weightField.value() #Spinbox
self.session_info['Estatura'] = self.ui.heightField.value() #Spinbox
if self.session_info['Estatura'] !=0 and self.session_info['Peso'] !=0:
self.session_info['IMC'] = self.session_info['Peso'] / ( ( self.session_info['Estatura'] / 100 ) ** 2 ) #IMC=PESO/ESTATURA^2
self.session_info['ASA'] = self.ui.ASAField.currentText() #Combobox
self.session_info['Membranas'] = self.ui.membField.currentText() #Combobox
self.session_info['Dilatación'] = self.ui.dilatationField.value() #Spinbox
self.session_info['Paridad'] = self.ui.parityField.currentText() #Combobox
#Calculated additional information
self.session_info['Temperaturas_medias'] = self.meanTemperatures
self.session_info['Escalas_de_temperatura'] = self.scale_range
self.session_info['Temperaturas_de_dermatomas'] = self.dermatomes_temps.tolist()
def setup_camera(self):
"""
Initialize camera.
"""
self.capture = cv2.VideoCapture(self.camera_index)
self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
self.timer = QTimer()
self.timer.timeout.connect(self.display_frame)
self.timer.start(30)
def display_frame(self):
"""
Refresh frame from camera
"""
try:
self.ret, self.frame = self.capture.read()
self.frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
# image = qimage2ndarray.array2qimage(self.frame)
self.image = QImage(self.frame, self.frame.shape[1], self.frame.shape[0],
self.frame.strides[0], QImage.Format_RGB888)
self.ui.inputImg.setPixmap(QPixmap.fromImage(self.image))
except:
time.sleep(1)
self.message_print(f'No se detectó cámara {self.camera_index}. Reintentando...')
print(f'Camera was not detected on index {self.camera_index}')
if self.camera_index < 5:
self.camera_index += 1
print(f'Retrying with index {self.camera_index}...')
else:
self.message_print("Error detectando cámara. Por favor revisar conexión.")
self.timer.stop()
pass
def capture_image(self):
"""
Captures a new image. Creates a new session with current timestamp if a session had
not been created previously
"""
self.current_secs = 1
self.current_mins = 0
self.timer_cron.start(1000)
if (not self.sessionIsCreated):
self.message_print("No se ha creado una sesion. Creando nueva...")
time.sleep(1)
self.create_session()
if len(os.listdir(self.session_dir)) <= 1:
image_number = len(os.listdir(self.session_dir))
else:
image_number = 5*len(os.listdir(self.session_dir)) - 5
self.save_name = f't{image_number}.jpg'
plt.imsave(os.path.join(self.session_dir, self.save_name), self.frame)
self.ui.outputImg.setPixmap(QPixmap.fromImage(self.image))
self.ui.imgName.setText(self.save_name[:-4])
this_image = f"{self.defaultDirectory}/t{image_number}.jpg"
self.ui.inputImgImport.setPixmap(this_image)
self.find_images()
if self.ui.autoScaleCheckBox.isChecked():
# Read and set the temperature range:
temp_scale = self.extract_scales_with_pytesseract(self.frame)
self.ui.minSpinBox.setValue(temp_scale[0])
self.ui.maxSpinBox.setValue(temp_scale[1])
def wipe_outputs(self, hard=False):
self.message_print("Limpiando sesión...")
self.imgs = []
self.subj = []
self.inputExists = False
self.defaultDirectoryExists = False
self.isSegmented = False
self.files = None
self.temperaturesWereAcquired = False
self.s2s = SessionToSegment()
self.i2s = ImageToSegment()
self.s2s.setModel(self.model)
self.i2s.setModel(self.model)
self.s2s.loadModel()
self.i2s.loadModel()
self.sessionIsCreated = False
self.ui.outputImgImport.setPixmap("")
self.ui.inputImgImport.setPixmap("")
self.ui.outputImg.setPixmap("")
self.ui.temperatureLabelImport.setText("")
if hard:
self.ui.nameField.setText("")
self.ui.ageField.setText("")
self.ui.weightField.setValue(0)
self.ui.weightField.setValue(0)
self.ui.weeksField.setValue(0)
self.ui.weightField.setValue(0)
self.ui.heightField.setValue(0)
self.ui.ASAField.setCurrentIndex(0)
self.ui.membField.setCurrentIndex(0)
self.ui.dilatationField.setValue(0)
self.ui.parityField.setCurrentIndex(0)
def create_session(self):
"""
Creates a new session, including a directory in ./outputs/<session_dir> with given input parameters
from GUI
The session is named as the current timestamp if current session_dir is null
"""
self.name = self.ui.nameField.text()
formatted_today = datetime.today().strftime("%Y-%m-%d_%H:%M")
self.dir_name = f"{self.name.replace(' ','_')}{formatted_today}"
try:
self.wipe_outputs()
self.session_dir = os.path.join('outputs',self.dir_name)
os.mkdir(self.session_dir)
self.sessionIsCreated = True
self.message_print("Sesión " + self.session_dir + " creada exitosamente." )
self.defaultDirectoryExists = True
self.defaultDirectory = os.path.abspath(self.session_dir)
self.inputExists = True
self.sessionIsSegmented = False
self.input_type = 2 #Video input capture
except Exception as ex:
self.message_print("Fallo al crear la sesión. Lea el manual de ayuda para encontrar solución, o reporte bugs al " + self.bugsURL)
print(ex)
def sync_local_info_to_drive(self):
"""
Syncs info from the output directory to the configured sync path
"""
self.message_print("Sincronizando información al repositorio remoto...")
try:
status = os.system("rclone copy outputs drive:")
self.message_print("Sincronizando información al repositorio remoto...")
if self.rcloneIsConfigured:
if status == 0:
self.message_print("Se ha sincronizado exitosamente la información")
return
raise Exception("Error sincronizando imagenes al repositorio remoto")
raise RemoteOriginUnauthorizedException(self.driveURL)
except RemoteOriginUnauthorizedException as ue:
self.message_print("Error de autorización durante la sincronización. Dirígase a Ayuda > Acerca de para más información.")
print(ue)
except Exception as e:
self.message_print("Error al sincronizar la información al repositorio. Verifique que ha seguido los pasos de instalación y configuración de rclone. Para más información, dirígase a Ayuda > Acerca de.")
print(e)
def repo_config_dialog(self):
"""
Shows a dialog window for first time configuring the remote repository sync for the current device
"""
raise NotImplementedError()
def make_connect(self):
"""
Makes all connections between singleton methods and objects in UI xml
"""
QObject.connect(self.ui.actionCargar_imagen, SIGNAL ('triggered()'), self.open_image)
QObject.connect(self.ui.actionCargar_carpeta , SIGNAL ('triggered()'), self.open_folder)
QObject.connect(self.ui.actionCargar_modelos , SIGNAL ('triggered()'), self.get_models_path)
QObject.connect(self.ui.actionPantalla_completa , SIGNAL ('triggered()'), self.toggle_fullscreen)
QObject.connect(self.ui.actionSalir , SIGNAL ('triggered()'), self.exit_)
QObject.connect(self.ui.actionC_mo_usar , SIGNAL ('triggered()'), self.display_how_to_use)
QObject.connect(self.ui.actionUpdate , SIGNAL ('triggered()'), self.update_software)
QObject.connect(self.ui.actionRepoSync , SIGNAL ('triggered()'), self.sync_local_info_to_drive)
QObject.connect(self.ui.actionRepoConfig , SIGNAL ('triggered()'), self.repo_config_dialog)
QObject.connect(self.ui.segButtonImport, SIGNAL ('clicked()'), self.segment)
QObject.connect(self.ui.tempButtonImport, SIGNAL ('clicked()'), self.temp_extract)
QObject.connect(self.ui.captureButton, SIGNAL ('clicked()'), self.capture_image)
QObject.connect(self.ui.nextImageButton , SIGNAL ('clicked()'), self.next_image)
QObject.connect(self.ui.previousImageButton , SIGNAL ('clicked()'), self.previous_image)
#QObject.connect(self.ui.reportButton , SIGNAL ('clicked()'), self.export_report)
QObject.connect(self.ui.reportButton , SIGNAL ('clicked()'), self.generate_full_session_plot)
QObject.connect(self.ui.loadModelButton , SIGNAL ('clicked()'), self.toggle_model)
QObject.connect(self.ui.createSession, SIGNAL ('clicked()'), self.create_session)
QObject.connect(self.ui.segButton, SIGNAL ('clicked()'), self.segment_capture)
#Comboboxes:
self.ui.inputColormapComboBox.currentIndexChanged['QString'].connect(self.toggle_input_colormap)
def toggle_input_colormap(self):
self.input_cmap = self.accepted_cmaps[self.ui.inputColormapComboBox.currentIndex()]
self.message_print(f"Se ha cambiado exitosamente el colormap de entrada a {self.input_cmap}")
def set_default_input_cmap(self):
self.accepted_cmaps = ['Gris', 'Hierro', 'Arcoiris', 'Lava']
self.input_cmap = self.accepted_cmaps[0]
self.ui.inputColormapComboBox.addItems(self.accepted_cmaps)
def segment_capture(self):
"""
Segment newly acquired capture with current loaded segmentation model
"""
self.message_print("Segmentando imagen...")
self.i2s.setModel(self.model)
self.i2s.setPath(os.path.join(self.session_dir,self.save_name))
self.i2s.loadModel()
self.i2s.extract(cmap = self.input_cmap)
threshold = 0.5
img = plt.imread(os.path.join(self.session_dir, self.save_name))/255
Y = self.i2s.Y_pred
Y = Y / Y.max()
Y = np.where( Y >= threshold , 1 , 0)
post_processing = PostProcessing(self.ui.morphoSpinBox.value())
u = post_processing.execute(Y[0])
self.Y = u[0] #Eventually required by temp_extract
Y = np.copy(u)
Y = cv2.resize(Y[0], (img.shape[1],img.shape[0]), interpolation = cv2.INTER_NEAREST) # Resize the prediction to have the same dimensions as the input
if self.ui.rainbowCheckBoxImport.isChecked():
cmap = 'rainbow'
else:
cmap = 'gray'
# plt.figure()
# plt.plot(Y*img[:,:,0])
# plt.savefig("outputs/output.jpg")
plt.imsave("outputs/output.jpg" , Y*img[:,:,0] , cmap=cmap)
self.ui.outputImg.setPixmap("outputs/output.jpg")
self.isSegmented = True
self.message_print("Imagen segmentada exitosamente")
def set_default_config_settings(self, model_dir, session_dir):
"""
Sets default config settings
"""
self.config = {'models_directory': model_dir,
'session_directory': session_dir }
def update_user_configuration(self):
"""
Updates basic configuration
"""
self.modelsPath = self.config['models_directory']
self.defaultDirectory = self.config['session_directory']
def init_logs(self):
log_path = "outputs/logs.html"
open(log_path, 'w').close()
out_file = open(log_path , "a")
final_msg = f'<meta charset="UTF-8">\n'
out_file.write(final_msg)
out_file.close()
def message_print(self, message):
"""
Prints on interface console
"""
log_path = "outputs/logs.html"
out_file = open(log_path , "a")
final_msg = f"\n <br> >>> </br> {message}\n"
out_file.write(final_msg)
out_file.close()
self.ui.textBrowser.setSource(log_path)
self.ui.textBrowser.reload()
self.ui.textBrowser.moveCursor(QTextCursor.End)
def find_images(self):
"""
Finds image from the path established in self.defaultDirectory obtained
from the method self.open_folder
"""
self.fileList = [] #Absolute paths
self.files = [] #Relative paths
self.outfiles=[] #Relative path to output files
for root, dirs, files in os.walk(self.defaultDirectory):
for file in files:
if (file.endswith(".jpg")):
self.fileList.append(os.path.join(root,file))
self.files.append(file)
self.outfiles.append("outputs/" + file) #Creating future output file names
self.imageQuantity = len(self.fileList)
self.imageIndex = 0
self.sort_files()
self.ui.inputLabel.setText(self.files[self.imageIndex])
def sort_files(self):
"""
Sort file list to an alphanumeric reasonable sense
"""
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
self.fileList = sorted(self.fileList, key = alphanum_key)
self.files = sorted(self.files, key = alphanum_key)
def get_times(self):
"""
Converts standarized names of file list into a list of
integers with time capture in minutes from the acquired self.fileList
from self.find_images
"""
if (type(self.fileList)==str):
self.timeList = int(self.fileList).rsplit(".")[0][1:]
elif type(self.fileList)==list:
out_list = []
for i in range(len(self.fileList)):
out_list.append(int(self.files[i].rsplit(".")[0][1:]))
self.timeList = out_list
else:
return None
def next_image(self):
"""
Displays next image from self.fileList
"""
if self.imageIndex < len(self.fileList)-1:
self.imageIndex += 1
self.ui.inputImgImport.setPixmap(self.fileList[self.imageIndex])
self.opdir = self.fileList[self.imageIndex]
self.ui.inputLabel.setText(self.files[self.imageIndex])
if self.sessionIsSegmented:
#Sentences to display next output image if session was already
#segmented
self.show_output_image_from_session()
if self.temperaturesWereAcquired:
self.message_print(f"La temperatura media de pies es: {np.round(self.meanTemperatures[self.imageIndex], 2)} para el tiempo:{self.files[self.imageIndex].replace('.jpg','')}")
rounded_temp = np.round(self.meanTemperatures[self.imageIndex], 2)
self.ui.temperatureLabelImport.setText(f'{rounded_temp} °C')
self.ui.minSpinBoxImport.setValue(self.scale_range[self.imageIndex][0])
self.ui.maxSpinBoxImport.setValue(self.scale_range[self.imageIndex][1])
def previous_image(self):
"""
Displays previous image from self.fileList
"""
if self.imageIndex >= 1:
self.imageIndex -= 1
self.ui.inputImgImport.setPixmap(self.fileList[self.imageIndex])
self.opdir = self.fileList[self.imageIndex]
self.ui.inputLabel.setText(self.files[self.imageIndex])
if self.sessionIsSegmented:
#Sentences to display next output image if session was already
#segmented
self.show_output_image_from_session()
if self.temperaturesWereAcquired:
self.message_print(f"La temperatura media de pies es: {np.round(self.meanTemperatures[self.imageIndex], 2)} para el tiempo:{self.files[self.imageIndex].replace('.jpg','')}")
rounded_temp = np.round(self.meanTemperatures[self.imageIndex], 2)
self.ui.temperatureLabelImport.setText(f'{rounded_temp} °C')
self.ui.minSpinBoxImport.setValue(self.scale_range[self.imageIndex][0])
self.ui.maxSpinBoxImport.setValue(self.scale_range[self.imageIndex][1])
def save_image(self):
"""
Saves segmented image
"""
raise NotImplementedError()
def get_models_path(self):
"""
Display a file manager dialog for selecting model list root directory
"""
self.modelDialog=QFileDialog(self)
self.modelDialog.setDirectory(QDir.currentPath())
self.modelDialog.setFileMode(QFileDialog.FileMode.Directory)
self.modelsPath = self.modelDialog.getExistingDirectory()
if self.modelsPath:
self.modelsPathExists = True
self.modelList = []
for root, dirs, files in os.walk(self.modelsPath):
for file in files:
self.modelList.append(os.path.join(root,file))
self.modelQuantity = len(self.modelList)
self.modelIndex = 0
self.models = files
self.ui.modelComboBox.addItems(self.models)
def feet_segment(self):
"""
Segments a single feet image
"""
self.message_print("Segmentando imagen...")
self.i2s.setModel(self.model)
self.i2s.setPath(self.opdir)
self.i2s.extract()
self.show_segmented_image()
self.isSegmented = True
self.message_print("Imagen segmentada exitosamente")
def session_segment(self):
"""
Segments a whole feet session
"""
self.ui.progressBar.setVisible(True)
self.ui.progressBar.setFormat("Segmentando..%p%")
self.ui.progressBar.setValue(0)
time.sleep(0.5)
self.sessionIsSegmented = False
self.s2s.setModel(self.model)
self.s2s.setPath(self.defaultDirectory)
self.s2s.whole_extract(self.fileList, cmap = self.input_cmap, progressBar = self.ui.progressBar)
self.produce_segmented_session_output()
self.show_output_image_from_session()
self.message_print("Se ha segmentado exitosamente la sesion con "+ self.i2s.model)
self.sessionIsSegmented = True
self.ui.progressBar.setValue(100)
# time.sleep(0.5)
self.ui.progressBar.setVisible(False)
self.ui.progressBar.setFormat("%p%")
def show_segmented_image(self):
"""
Shows segmented image
"""
#Applies segmented zone to input image, showing only feet
threshold = 0.5
img = plt.imread(self.opdir)/255
Y = self.i2s.Y_pred
Y = Y / Y.max()
Y = np.where( Y >= threshold , 1 , 0)
self.Y =posprocessing( Y[0])[0] #Eventually required by temp_extract
Y = posprocessing(Y[0])
Y = cv2.resize(Y[0], (img.shape[1],img.shape[0]), interpolation = cv2.INTER_NEAREST) # Resize the prediction to have the same dimensions as the input
if self.ui.rainbowCheckBoxImport.isChecked():
cmap = 'rainbow'
else:
cmap = 'gray'
plt.figure()
plt.plot(Y*img[:,:,0])
plt.savefig("outputs/output.jpg")
plt.imsave("outputs/output.jpg" , Y*img[:,:,0] , cmap=cmap)
self.ui.outputImgImport.setPixmap("outputs/output.jpg")
def produce_segmented_session_output(self):
"""
Produce output images from a whole session and """
#Recursively applies show_segmented_image to whole session
self.Y=[]
post_processing = PostProcessing(self.ui.morphoSpinBox.value())
for i in range(len(self.outfiles)):
threshold = 0.5
img = plt.imread(self.fileList[i])/255
Y = self.s2s.Y_pred[i]
Y = Y / Y.max()
Y = np.where( Y >= threshold , 1 , 0)
Y = post_processing.execute(Y[0])
self.Y.append(Y) #Eventually required by temp_extract
#print(f"Dimensiones de la salida: {Y.shape}")
Y = cv2.resize(Y, (img.shape[1],img.shape[0]), interpolation = cv2.INTER_NEAREST) # Resize the prediction to have the same dimensions as the input
if self.ui.rainbowCheckBox.isChecked():
cmap = 'rainbow'
else:
cmap = 'gray'
# plt.figure()
# plt.imshow(Y*img[:,:,0])
# plt.axis('off')
# plt.savefig(self.outfiles[i])
plt.imsave(self.outfiles[i], Y*img[:,:,0] , cmap=cmap)
def show_output_image_from_session(self):
"""
Display segmented image from current one selected from the index
established by self.previous_image or self.next_image methods
"""
self.ui.outputImgImport.setPixmap(self.outfiles[self.imageIndex])
def segment(self):
"""
Makes segmentation action depending on the current state (single image or whole session)
"""
if self.input_type >= 1:
#Session
if self.defaultDirectoryExists and self.i2s.model!=None and self.s2s.model!=None:
self.message_print("Segmentando toda la sesión...")
self.session_segment()
else:
self.message_print("Error. Por favor verifique que se ha cargado el modelo y la sesión de entrada.")
elif self.input_type == 0:
#Single image
if self.inputExists and self.modelsPathExists and self.model!=None:
self.feet_segment()
else:
self.message_print("No se ha seleccionado imagen de entrada")
def manual_segment(self):
"""
Display manual segmentation utility
"""
#print("Se abrirá diálogo de extracción manual")
#self.manual=manualSeg()
#self.manual.show()
raise NotImplementedError()
def temp_extract(self):
"""
Extract temperatures from a segmented image or a whole session
"""
self.ui.progressBar.setVisible(True)
self.ui.progressBar.setValue(0)
self.message_print("Obteniendo temperaturas...")
if (self.inputExists and (self.isSegmented or self.sessionIsSegmented)):
self.message_print("Obteniendo temperaturas de la sesión...")
self.ui.progressBar.setFormat("Extrayendo temperaturas... %p%")
if self.ui.autoScaleCheckBoxImport.isChecked and self.input_type>=1:
#Get automatic scales
self.scale_range = self.extract_multiple_scales(self.s2s.img_array)
elif not self.ui.autoScaleCheckBoxImport.isChecked():
self.scale_range = [self.ui.minSpinBoxImport.value() , self.ui.maxSpinBoxImport.value()]
if self.input_type>=1: #If segmentation was for full session
self.meanTemperatures = [] #Whole feet mean temperature for all images in session
segmented_temps = []
original_temps = []
dermatomes_temps = []
dermatomes_masks = []
if self.ui.autoScaleCheckBoxImport.isChecked():
for i in range(len(self.outfiles)):
mean_out, temp, original_temp = mean_temperature(self.s2s.Xarray[i,:,:,0] , self.Y[i][:,:,0] , self.scale_range[i], plot = False)
derm_temps, derm_mask = dermatomes_temperatures(original_temp, self.Y[i])
self.meanTemperatures.append(mean_out)
segmented_temps.append(temp)
original_temps.append(original_temp)
dermatomes_temps.append(derm_temps)
dermatomes_masks.append(derm_mask)
self.ui.progressBar.setValue((100*i+1)/len(self.outfiles))
self.dermatomes_temps = np.array(dermatomes_temps)
self.dermatomes_masks = np.array(dermatomes_masks)
self.segmented_temps = np.array(segmented_temps)
self.original_temps = np.array(original_temps)
else:
for i in range(len(self.outfiles)):
mean_out, temp, original_temp = mean_temperature(self.s2s.Xarray[i,:,:,0] , self.Y[i][:,:,0] , self.scale_range, plot = False)
derm_temps, derm_mask = dermatomes_temperatures(original_temp, self.Y[i])
self.meanTemperatures.append(mean_out)
segmented_temps.append(temp)
original_temps.append(original_temp)
dermatomes_temps.append(derm_temps)
dermatomes_masks.append(derm_mask)
self.ui.progressBar.setValue((100*i+1)/len(self.outfiles))
self.dermatomes_temps = np.array(dermatomes_temps)
self.dermatomes_masks = np.array(dermatomes_masks)
self.segmented_temps = np.array(segmented_temps)
self.original_temps = np.array(original_temps)
self.message_print("La temperatura media es: " + str(self.meanTemperatures[self.imageIndex]))
self.message_print(f"La escala leida es: {self.scale_range[self.imageIndex]}")
rounded_temp = np.round(self.meanTemperatures[self.imageIndex], 3)
self.ui.temperatureLabelImport.setText(f'{rounded_temp} °C')
self.ui.minSpinBoxImport.setValue(self.scale_range[self.imageIndex][0])
self.ui.maxSpinBoxImport.setValue(self.scale_range[self.imageIndex][1])
self.temperaturesWereAcquired = True
else: #If segmentation was for single image
if self.ui.autoScaleCheckBoxImport.isChecked():
self.scale_range = self.extract_scales_with_pytesseract(self.i2s.img)
else:
self.scale_range = [self.ui.minSpinBoxImport.value() , self.ui.maxSpinBoxImport.value()]
time.sleep(1.5)
mean, _ = mean_temperature(self.i2s.Xarray[:,:,0] , self.Y[:,:,0] , self.scale_range, plot = False)
self.message_print("La temperatura media es: " + str(mean))
rounded_temp = np.round(mean, 3)
self.ui.temperatureLabelImport.setText(f'{rounded_temp} °C')
if (self.ui.plotCheckBoxImport.isChecked() and self.input_type>=1): #If user asked for plot
#self.message_print("Se generara plot de temperatura...")
self.get_times()
# self.temp_plot()
elif self.inputExists:
#If input exists but session has not been segmented
self.message_print("No se ha segmentado previamente la sesión. Segmentando... ")
time.sleep(1)
self.session_segment()
self.temp_extract()
elif self.ui.tabWidget.currentIndex() == 0:
#Live video tab
self.message_print("Obteniendo temperaturas para la última captura...")
time.sleep(1)
if not self.sessionIsCreated:
self.message_print("No se ha creado una sesión de entrada. Presione capturar para crear una sesión por defecto o cree una con los parámetros deseados")
return
if len(os.listdir(self.session_dir)) < 1:
self.message_print("No se ha hecho ninguna captura.")
return
#HERE COMES TO LOGIC FOR OBTAINING FULL PLOT FOR LIVE VIDEO
else:
self.message_print("No se han seleccionado imagenes de entrada")
self.ui.progressBar.setVisible(False)
self.ui.progressBar.setFormat("%p%")
def toggle_model(self):
"""
Change model loaded if user changes the model modelComboBox
"""
self.modelIndex = self.ui.modelComboBox.currentIndex()
self.message_print("Cargando modelo: " + self.models[self.modelIndex]
+" Esto puede tomar unos momentos...")
try:
self.model = self.modelList[self.modelIndex]
self.s2s.setModel(self.model)
self.i2s.setModel(self.model)
self.s2s.loadModel()
self.i2s.loadModel()
self.ui.loadedModelLabel.setText(self.model)
self.message_print("Modelo " + self.models[self.modelIndex] + " cargado exitosamente")
except:
self.message_print("Error al cargar el modelo "+ self.models[self.modelIndex])
def temp_plot(self):
"""
Plots from acquired temperature samples from a session
"""
plt.figure()
x = np.linspace(min(self.timeList), max(self.timeList), 200)
spl = make_interp_spline(self.timeList, self.meanTemperatures, k=3)
y = spl(x)
plt.plot(x , y, '-.', color='salmon')
plt.plot(self.timeList , self.meanTemperatures , '-o', color='slategrey')
plt.title("Temperatura media de pies")
plt.xlabel("Tiempo (min)")
plt.ylabel("Temperatura (°C)")
plt.grid()
plt.show()
self.message_print("Plot de temperatura generado exitosamente")
#Produce plot
def generate_full_session_plot(self):
if not self.temperaturesWereAcquired :
self.message_print("No se han extraido las temperaturas, extrayendo...")
self.temp_extract()
self.generate_full_session_plot()
else:
exit_value = plot_report(img_temps = self.original_temps, segmented_temps = self.segmented_temps, mean_temps = self.meanTemperatures, times = self.timeList,
path = os.path.join(self.defaultDirectory,'report'), dermatomes_temps = self.dermatomes_temps, dermatomes_masks = self.dermatomes_masks)
if exit_value == 0:
#Generación de información extra para la sesión
self.message_print("Se ha generado exitosamente el plot completo de sesión")
else:
self.message_print("Advertencia, se ha encontrado un valor no válido (nan) en los dígitos de escala de temperatura. Verifique que la imagen es del formato y referencia de cámara correctos")
self.populate_session_info()
self.export_report()
def open_image(self):
"""
Displays a dialog for loading a single image
"""
self.fileDialog=QFileDialog(self)
if self.defaultDirectoryExists:
self.fileDialog.setDirectory(self.defaultDirectory)
else:
self.fileDialog.setDirectory(QDir.currentPath())
filters = ["*.png", "*.xpm", "*.jpg"]
self.fileDialog.setNameFilters("Images (*.png *.jpg)")
self.fileDialog.selectNameFilter("Images (*.png *.jpg)")
#self.fileDialog.setFilter(self.fileDialog.selectedNameFilter())
self.opdir = self.fileDialog.getOpenFileName()[0]
self.imagesDir = os.path.dirname(self.opdir)
if self.opdir:
self.wipe_outputs(hard=True)
self.input_type = 0
self.inputExists = True
self.ui.inputImgImport.setPixmap(self.opdir)
self.message_print(f"Se ha importado exitosamente la imagen {self.opdir} ")
self.ui.tabWidget.setProperty('currentIndex', 1)
def open_folder(self):
"""
Displays a dialog for loading a whole session
"""
self.folderDialog=QFileDialog()
self.folderDialog.setDirectory(QDir.currentPath())
self.folderDialog.setFileMode(QFileDialog.FileMode.Directory)
self.defaultDirectory = self.folderDialog.getExistingDirectory()
self.imagesDir = self.defaultDirectory
if self.defaultDirectory:
self.wipe_outputs(hard=True)
self.input_type = 1
self.defaultDirectoryExists = True
first_image = str(self.defaultDirectory + "/t0.jpg")
self.ui.inputImgImport.setPixmap(first_image)
self.opdir = first_image
self.inputExists = True
self.find_images()
self.sessionIsSegmented = False
self.ui.tabWidget.setProperty('currentIndex', 1)
self.message_print(f"Se ha importado exitosamente la sesión {self.defaultDirectory} ")
#self.file_system_model.setRootPath(QDir(self.defaultDirectory))
#self.ui.treeView.setModel(self.file_system_model)
def toggle_fullscreen(self):
"""
Toggles fullscreen state
"""
if self.fullScreen:
self.ui.showNormal()
self.fullScreen = False
else:
self.ui.showFullScreen()
self.fullScreen = True
def exit_(self):
sys.exit(app.exec_())
def display_how_to_use(self):
"""
Displays user manual on system's default pdf viewer
"""
os.system("xdg-open README.html")
def export_report(self):
"""
Generates a json document with session information
"""
with open(f"{self.defaultDirectory}/report.json", "w") as outfile:
json.dump(self.session_info, outfile)
def animate(self):
"""
Produces gif animation based on mean temperatures for whole session
Initially, all feet has same color, for section segmentation has been not implemented yet
"""
self.message_print("Iniciando animacion...")
self.message_print("Desarrollo no implementado, disponible en futuras versiones.")
raise NotImplementedError()
def update_software(self):
"""
Updates software from remote origin repository
"""