-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
808 lines (654 loc) · 29.1 KB
/
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
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
import cv2
import numpy as np
import os
from PIL import Image
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from PySide6.QtCore import Signal, QObject, Slot, Qt
import resources as res
import segment_engine as seg
import widgets as wid
import re
# parameters
# path (change if necessary)
OUT_BINARY_MASK = 'combined_binary_mask.png'
OUT_COLOR_MASK = 'combined_color_mask.png'
OUT_BINARY_SKELETON = 'skeleton_image.png'
OUT_COLOR_SKELETON = 'skeleton_color.png'
"""
class CustomOutputStream(QObject):
outputSignal = Signal(str)
def write(self, text):
if "step" in text:
self.outputSignal.emit('read')
else:
self.outputSignal.emit(text)
def flush(self):
pass
sys.stdout = CustomOutputStream()
"""
class Helper(QObject):
updateSignal = Signal(str)
def emit_update(self, value):
self.updateSignal.emit(value)
class CustomDoubleValidator(QDoubleValidator):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def validate(self, string, pos):
# Replace comma with period for validation
string = string.replace(',', '.')
return super().validate(string, pos)
def fixup(self, string):
# Replace comma with period in the final string
return string.replace(',', '.')
class ExclusiveButtonGroup:
def __init__(self):
self.buttons = []
def addButton(self, button):
self.buttons.append(button)
# Correctly capture the button reference in the lambda
button.clicked.connect(lambda checked=False, b=button: self.uncheck_others(b))
def uncheck_others(self, clicked_button):
for button in self.buttons:
if button != clicked_button:
button.setChecked(False)
class ConfigDialog(QDialog):
def __init__(self):
super().__init__()
# Set dialog title
self.setWindowTitle("Export Options")
# Layout
layout = QVBoxLayout(self)
# Label and File path
layout.addWidget(QLabel("Choose output folder"))
self.file_path_edit = QLineEdit(self)
browse_button = QPushButton("Browse", self)
browse_button.clicked.connect(self.browse_folder)
file_path_layout = QHBoxLayout()
file_path_layout.addWidget(self.file_path_edit)
file_path_layout.addWidget(browse_button)
layout.addLayout(file_path_layout)
self.folder = ''
# prefix selection
self.prefix_edit = QLineEdit(self)
layout.addWidget(QLabel("Image prefix"))
layout.addWidget(self.prefix_edit)
# Split image checkbox
self.split_image_checkbox = QCheckBox("Split the image", self)
self.split_image_checkbox.stateChanged.connect(self.on_split_image_checked)
layout.addWidget(self.split_image_checkbox)
# Overlap factor
self.overlap_factor_edit = QLineEdit(self)
self.overlap_factor_edit.setValidator(QDoubleValidator(0, 1, 2, self))
self.overlap_factor_edit.setEnabled(False)
layout.addWidget(QLabel("Overlap factor"))
layout.addWidget(self.overlap_factor_edit)
# save mask checkbox
self.save_mask_check = QCheckBox("Save masks", self)
layout.addWidget(self.save_mask_check)
# Chosen width and height
self.width_edit = QLineEdit(self)
self.height_edit = QLineEdit(self)
self.width_edit.setValidator(QIntValidator(0, 2000, self))
self.height_edit.setValidator(QIntValidator(0, 2000, self))
layout.addWidget(QLabel("Chosen width"))
layout.addWidget(self.width_edit)
layout.addWidget(QLabel("Chosen height"))
layout.addWidget(self.height_edit)
# Annotation type combobox
self.annotation_combobox = QComboBox(self)
self.annotation_combobox.addItems(["Object detection annotation", "Instance segmentation annotation"])
layout.addWidget(self.annotation_combobox)
# OK and Cancel buttons
ok_button = QPushButton("OK", self)
cancel_button = QPushButton("Cancel", self)
ok_button.clicked.connect(self.accept)
cancel_button.clicked.connect(self.reject)
button_layout = QHBoxLayout()
button_layout.addWidget(ok_button)
button_layout.addWidget(cancel_button)
layout.addLayout(button_layout)
@Slot()
def browse_folder(self):
self.folder = QFileDialog.getExistingDirectory(self, "Select Folder")
if self.folder:
self.file_path_edit.setText(self.folder)
def on_split_image_checked(self):
if self.split_image_checkbox.isChecked():
self.overlap_factor_edit.setEnabled(True)
else:
self.overlap_factor_edit.setEnabled(False)
class ScaleDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Compute Scale")
# Create widgets
self.known_length_edit = QLineEdit()
self.known_distance_edit = QLineEdit()
self.result_label = QLabel("Resolution will be shown here")
self.ok_button = QPushButton("OK")
# Set input constraints (only numbers)
float_validator = CustomDoubleValidator()
float_validator.setNotation(QDoubleValidator.StandardNotation)
float_validator.setBottom(0) # Minimum value (e.g., 0 for no negative numbers)
float_validator.setDecimals(2)
self.known_length_edit.setValidator(float_validator)
self.known_distance_edit.setValidator(float_validator)
# Layout
layout = QVBoxLayout()
layout.addWidget(QLabel("Known length (in pixels):"))
layout.addWidget(self.known_length_edit)
layout.addWidget(QLabel("Known distance (in mm):"))
layout.addWidget(self.known_distance_edit)
layout.addWidget(self.ok_button)
layout.addWidget(self.result_label)
self.setLayout(layout)
# Initially disable the OK button
self.ok_button.setEnabled(False)
# Connect signals
self.known_length_edit.textChanged.connect(self.check_inputs)
self.known_distance_edit.textChanged.connect(self.check_inputs)
self.ok_button.clicked.connect(self.accept)
def check_inputs(self):
# Check if both fields are filled
if self.known_length_edit.text() and self.known_distance_edit.text():
try:
# Attempt to parse the inputs
known_length = float(self.known_length_edit.text().replace(',', '.'))
known_distance = float(self.known_distance_edit.text().replace(',', '.'))
# Check that neither known length nor known distance is zero
if known_length != 0 and known_distance != 0:
self.resolution = known_distance / known_length
self.result_label.setText(f"Resolution: {self.resolution} mm/pixel")
self.ok_button.setEnabled(True)
else:
self.result_label.setText("Known length and distance cannot be zero")
self.ok_button.setEnabled(False)
except ValueError:
self.result_label.setText("Invalid input")
self.ok_button.setEnabled(False)
else:
# Disable OK button if either field is empty
self.ok_button.setEnabled(False)
self.result_label.setText("Resolution will be shown here")
class CrackApp(QMainWindow):
def __init__(self, is_dark_theme):
super().__init__()
basepath = os.path.dirname(__file__)
basename = 'interface'
uifile = os.path.join(basepath, 'ui/%s.ui' % basename)
wid.loadUi(uifile, self)
self.setWindowTitle("What the crack!")
# add actions to action group
ag = QActionGroup(self)
ag.setExclusive(True)
ag.addAction(self.actionMeasure)
ag.addAction(self.actionMeasure_path)
ag.addAction(self.actionHand_selector)
ag.addAction(self.actionPaint_mask)
ag.addAction(self.actionEraser_mask)
# mutually exclusive pushbuttons
self.exclusiveGroup = ExclusiveButtonGroup()
self.exclusiveGroup.addButton(self.pushButton_show_skel)
self.exclusiveGroup.addButton(self.pushButton_show_mask)
self.viewer = wid.PhotoViewer(self)
self.verticalLayout.addWidget(self.viewer)
# initial parameters
self.list_image = ['Original image']
# self.comboBox.addItems(self.list_image)
self.active_img = self.list_image[0]
self.image_loaded = False
self.has_mask = False
self.resolution = 0
self.image_w = 0
self.image_h = 0
self.crack_length = 0
self.output_binary_mask = OUT_BINARY_MASK
self.output_color_mask = OUT_COLOR_MASK
self.output_skeleton = OUT_BINARY_SKELETON
self.output_color_skeleton = OUT_COLOR_SKELETON
# initialize status
self.update_progress(nb=100, text="Load an image first!")
# list of line measurements
self.line_meas_list = []
self.path_meas_list = []
# connections
self.actionLoad_image.triggered.connect(self.get_image)
self.actionSegment.triggered.connect(self.go_segment)
self.actionSet_scale.triggered.connect(self.set_scale)
self.actionMeasure.triggered.connect(self.line_meas)
self.actionMeasure_path.triggered.connect(self.path_meas)
self.actionHand_selector.triggered.connect(self.hand_pan)
self.actionPaint_mask.triggered.connect(self.paint_mask)
self.actionEraser_mask.triggered.connect(self.erase_mask)
# export actions
self.actionExport_dxf.triggered.connect(self.export_dxf)
self.actionExport_as_annotation.triggered.connect(self.export_as_annotation)
# pushbuttons
self.pushButton_show_image.clicked.connect(self.update_view)
self.pushButton_show_skel.clicked.connect(self.update_view)
self.pushButton_show_mask.clicked.connect(self.update_view)
self.pushButton_show_linemeas.clicked.connect(self.toggle_all_meas)
self.pushButton_export.clicked.connect(self.export_view)
self.pushButton_export_view.clicked.connect(self.export_current_view)
# drawing ends
self.viewer.endDrawing_line_meas.connect(self.get_line_meas)
self.viewer.endPainting.connect(self.update_image_mask)
self.viewer.photoClicked.connect(self.get_path)
self.viewer.pathAdded.connect(self.get_path_measurements)
# Custom output stream
# sys.stdout.outputSignal.connect(self.onOutput)
self.helper = Helper()
self.helper.updateSignal.connect(self.update_yolo_steps)
if is_dark_theme:
suf = '_white_tint'
suf2 = '_white'
else:
suf = ''
suf2 = ''
self.add_icon(res.find(f'img/camera{suf}.png'), self.actionLoad_image)
self.add_icon(res.find(f'img/crack{suf}.png'), self.actionSegment)
self.add_icon(res.find(f'img/hand{suf}.png'), self.actionHand_selector)
self.add_icon(res.find(f'img/ruler{suf}.png'), self.actionMeasure)
self.add_icon(res.find(f'img/magic{suf}.png'), self.actionMeasure_path)
self.add_icon(res.find(f'img/size{suf}.png'), self.actionSet_scale)
self.add_icon(res.find(f'img/brush{suf}.png'), self.actionPaint_mask)
self.add_icon(res.find(f'img/eraser{suf}.png'), self.actionEraser_mask)
# push buttons
self.add_icon(res.find(f'img/photo{suf2}.png'), self.pushButton_show_image)
self.add_icon(res.find(f'img/skel{suf2}.png'), self.pushButton_show_skel)
self.add_icon(res.find(f'img/mask{suf2}.png'), self.pushButton_show_mask)
self.add_icon(res.find(f'img/save_as{suf2}.png'), self.pushButton_export)
self.add_icon(res.find(f'img/view{suf2}.png'), self.pushButton_export_view)
self.add_icon(res.find(f'img/width{suf2}.png'), self.pushButton_show_linemeas)
# general functions __________________________________________
def add_icon(self, img_source, pushButton_object):
"""
Function to add an icon to a pushButton
"""
pushButton_object.setIcon(QIcon(img_source))
def update_progress(self, nb=None, text=''):
self.label_status.setText(text)
if nb is not None:
self.progressBar.setProperty("value", nb)
# hide progress bar when 100%
if nb >= 100:
self.progressBar.setVisible(False)
elif self.progressBar.isHidden():
self.progressBar.setVisible(True)
# export functions
def export_dxf(self):
pass
def export_as_annotation(self):
# launch the option dialog
dialog = ConfigDialog()
if dialog.exec():
# get parameters
mask_img_path = self.output_binary_mask
image_path = self.image_path
dest_folder = dialog.folder
overlap = float(dialog.overlap_factor_edit.text())
w_train = int(dialog.width_edit.text())
h_train = int(dialog.height_edit.text())
prefix = dialog.prefix_edit.text()
i = dialog.annotation_combobox.currentIndex()
if i==0:
as_box = True
else:
as_box = False
save_mask = dialog.save_mask_check.isChecked()
if dialog.split_image_checkbox.isChecked():
# split rgb
seg.split_image(image_path, dest_folder, w_train, h_train, overlap, prefix=prefix)
# split binary
mask_splits, names = seg.split_image(mask_img_path, dest_folder, w_train, h_train, overlap, prefix=prefix+'_mask', save=save_mask)
# save_txt files for each split:
for i, split in enumerate(mask_splits):
txt_path = os.path.join(dest_folder, names[i][:-4] + '.txt')
seg.convert_bin_mask_to_yolo_txt(split, txt_path, as_box=as_box)
# paint and erase __________________________________________
def paint_mask(self):
self.pushButton_show_mask.setChecked(True)
self.pushButton_show_skel.setChecked(False)
self.update_view()
if self.actionPaint_mask.isChecked():
# update status
self.update_progress(text='You can paint on the image. Hold shift and mouse wheel to adapt size')
# change cursor
self.viewer.change_to_brush_cursor()
self.viewer.painting = True
self.viewer.eraser = False
self.viewer.point_selection = False
if self.viewer.hand_drag:
self.viewer.toggleDragMode()
def update_image_mask(self, coords):
if self.has_mask:
image = Image.open(self.output_binary_mask)
image_array = np.array(image)
if not self.viewer.eraser:
binary_image = seg.create_mask_from_paint(image_array, coords)
else:
binary_image = seg.remove_mask_from_paint(image_array, coords)
self.compute_all_outputs_from_binary(binary_image)
self.pushButton_show_mask.setEnabled(True)
self.pushButton_show_skel.setEnabled(True)
self.pushButton_show_mask.setChecked(True)
self.actionMeasure_path.setEnabled(True)
self.update_view()
else:
if not self.viewer.eraser:
image = Image.open(self.image_path)
width, height = image.size
# Create an empty black image (binary) of the same dimension
black_image = np.zeros((height, width), dtype=np.uint8)
# use painted coordinates to create a new mask
binary_image = seg.create_mask_from_paint(black_image, coords)
self.compute_all_outputs_from_binary(binary_image)
self.pushButton_show_mask.setEnabled(True)
self.pushButton_show_skel.setEnabled(True)
self.pushButton_show_mask.setChecked(True)
self.actionMeasure_path.setEnabled(True)
self.actionExport_as_annotation.setEnabled(True)
self.has_mask = True
self.update_view()
def erase_mask(self):
self.pushButton_show_mask.setChecked(True)
self.pushButton_show_skel.setChecked(False)
self.update_view()
if self.actionEraser_mask.isChecked():
# update status
self.update_progress(text='You can remove paint on the image. Hold shift and mouse wheel to adapt size')
# change cursor
self.viewer.change_to_brush_cursor()
self.viewer.painting = True
self.viewer.eraser = True
self.viewer.point_selection = False
if self.viewer.hand_drag:
self.viewer.toggleDragMode()
# line measurement __________________________________________
def toggle_all_meas(self):
if self.pushButton_show_linemeas.isChecked():
self.viewer.add_all_line_measurements(self.line_meas_list)
self.viewer.add_all_path_measurements(self.path_meas_list)
else:
self.viewer.clean_scene_line()
self.viewer.clean_scene_text()
self.viewer.clean_scene_path()
def line_meas(self):
if self.actionMeasure.isChecked():
self.update_progress(text='Click and drag the measure a distance')
self.viewer.setCursor(Qt.ArrowCursor)
# activate drawing tool
self.point_selection = False
self.viewer.painting = False
self.viewer.line_meas = True
self.viewer.toggleDragMode()
def get_line_meas(self, line_obj, text_obj):
self.line_meas_list.append([line_obj, text_obj])
self.pushButton_show_linemeas.setEnabled(True)
self.pushButton_show_linemeas.setChecked(True)
self.hand_pan()
# path measurement __________________________________________
def path_meas(self):
if self.actionMeasure_path.isChecked():
# update status
self.update_progress(text='You can click on a crack segment to measure it')
self.viewer.point_selection = True
self.viewer.painting = False
# change cursor
self.viewer.change_to_brush_cursor()
if self.viewer.hand_drag:
self.viewer.toggleDragMode()
def get_path(self, list):
x = list[1]
y = list[0]
img = cv2.imread(self.output_skeleton)
close_pixel = seg.find_closest_white_pixel(img[:, :, 1], x, y, 30)
if close_pixel is not None:
path = seg.find_path_bis(close_pixel[0], close_pixel[1], self.graph, self.lookup_table)
# highlighted_img = seg.highlight_path(img.shape, path)
self.viewer.add_path_to_scene(path)
def get_path_measurements(self, path_obj, text_obj):
self.path_meas_list.append([path_obj, text_obj])
self.pushButton_show_linemeas.setEnabled(True)
self.pushButton_show_linemeas.setChecked(True)
def hand_pan(self):
# update status
self.update_progress(text='You can drag/pan the image')
# switch back to hand tool
self.viewer.point_selection = False
self.viewer.line_meas = False
self.viewer.painting = False
self.actionHand_selector.setChecked(True)
if not self.viewer.hand_drag:
self.viewer.toggleDragMode()
# viewer __________________________________________
def update_view(self):
if self.pushButton_show_image.isChecked():
if self.pushButton_show_mask.isChecked():
if self.has_mask:
self.viewer.compose_mask_image(self.output_color_mask)
self.viewer.clean_scene()
elif self.pushButton_show_skel.isChecked():
self.viewer.compose_mask_image(self.output_color_skeleton)
# add markers to image
self.viewer.add_nodes(self.junctions, self.endpoints)
else:
self.viewer.clean_scene()
self.viewer.setPhoto(QPixmap(self.image_path))
else:
if self.pushButton_show_mask.isChecked():
if self.has_mask:
self.viewer.compose_mask_image(self.output_color_mask)
self.viewer.clean_scene()
self.viewer.clean_scene()
self.viewer.setPhoto(QPixmap(self.output_color_mask))
elif self.pushButton_show_skel.isChecked():
self.viewer.setPhoto(QPixmap(self.output_color_skeleton))
# add markers to image
self.viewer.add_nodes(self.junctions, self.endpoints)
else:
# Load the existing image
original_image = QImage(self.image_path) # Replace with your image path
# Create a new white image of the same size
white_image = QImage(original_image.size(), QImage.Format_ARGB32)
white_image.fill(Qt.black) # Fill the image with white color
# Convert the white QImage to QPixmap
white_pixmap = QPixmap.fromImage(white_image)
self.viewer.clean_scene()
self.viewer.setPhoto(white_pixmap)
def compute_all_outputs_from_binary(self, binary):
color_mask = seg.binary_to_color_mask(binary)
skel = seg.binary_to_skeleton(binary)
color_skel = seg.binary_to_color_mask(skel)
# save the 4 images
image = Image.fromarray(binary)
image.save(self.output_binary_mask)
image = Image.fromarray(color_mask)
image.save(self.output_color_mask)
image = Image.fromarray(skel)
image.save(self.output_skeleton)
image = Image.fromarray(color_skel)
image.save(self.output_color_skeleton)
# compute junctions from skeleton image
self.junctions, self.endpoints = seg.find_junctions_endpoints(self.output_skeleton)
self.graph = seg.build_graph(self.junctions, self.endpoints, skel)
self.lookup_table = seg.segment_lookup_table(self.graph)
# seg.visualize_graph(self.graph, skel)
# other scientific functions __________________________________________
def set_scale(self):
dialog = ScaleDialog()
if dialog.exec_():
self.resolution = dialog.resolution
print(f'The new resolution is {self.resolution} mm/pixel')
self.viewer.mm_per_pixel = self.resolution
self.toggle_all_meas()
self.toggle_all_meas()
def go_segment(self):
# execute YOLO script
self.hand_pan()
self.update_progress(text="Segmenting image with yolo!", nb=0)
result = seg.get_segmentation_result(self.helper, self.image_path)
binary = seg.create_binary_from_yolo(result)
self.compute_all_outputs_from_binary(binary)
self.has_mask = True
self.update_progress(text="You can now modify the mask!", nb=100)
self.pushButton_show_mask.setEnabled(True)
self.pushButton_show_skel.setEnabled(True)
self.pushButton_show_mask.setChecked(True)
self.actionExport_as_annotation.setEnabled(True)
self.actionMeasure_path.setEnabled(True)
self.update_view()
def update_yolo_steps(self, text):
def extract_numbers(input_string):
# This regex pattern looks for two groups of one or more digits,
# separated by a non-digit character (like '/')
match = re.search(r'(\d+)[^\d]+(\d+)', input_string)
if match:
first_number = int(match.group(1))
second_number = int(match.group(2))
return first_number, second_number
else:
return None, None
i, num_slices = extract_numbers(text)
print(i, num_slices)
self.update_progress(text=f'Segmenting slice {i}/{num_slices}', nb=i / num_slices * 100)
def onOutput(self, text):
if text == 'read':
# Handle 'read' emission here
self.update_progress(text=text)
else:
pass
# load data __________________________________________
def get_image(self):
"""
Get the image path from the user
:return:
"""
try:
img = QFileDialog.getOpenFileName(self, u"Ouverture de fichiers", "",
"Image Files (*.png *.jpg *.JPEG *.bmp *.tif)")
print(f'The following image will be loaded {img[0]}')
except:
pass
if img[0] != '':
# load and show new image
self.load_image(img[0])
def load_image(self, path):
"""
Load the new image and reset the model
:param path:
:return:
"""
# self.reset_points()
self.image_path = path
self.viewer.setPhoto(QPixmap(path), fit_view=True)
self.viewer.set_base_image(path)
self.image_loaded = True
self.hand_pan()
# enable action
self.actionSegment.setEnabled(True)
self.actionMeasure.setEnabled(True)
self.actionSet_scale.setEnabled(True)
self.actionPaint_mask.setEnabled(True)
self.actionEraser_mask.setEnabled(True)
self.actionHand_selector.setEnabled(True)
self.actionHand_selector.setChecked(True)
self.pushButton_show_image.setEnabled(True)
self.pushButton_show_image.setChecked(True)
self.pushButton_export.setEnabled(True)
self.pushButton_export_view.setEnabled(True)
# export data __________________________________________
def export_current_view(self):
# Create a QImage with the size of the viewport
image = QImage(self.viewer.viewport().size(), QImage.Format_ARGB32_Premultiplied)
image.fill(Qt.transparent)
# Paint the QGraphicsView's viewport onto the QImage
painter = QPainter(image)
self.viewer.render(painter)
painter.end()
# Open 'Save As' dialog
file_path, _ = QFileDialog.getSaveFileName(
None, "Save Image", "", "PNG Image (*.png);;JPEG Image (*.jpg *.jpeg *.JPEG)"
)
# Save the image if a file path was provided, using high-quality settings for JPEG
if file_path:
if file_path.lower().endswith('.jpg') or file_path.lower().endswith('.jpeg'):
image.save(file_path, 'JPEG', 100)
else:
image.save(file_path) # PNG is lossless by default
def export_view(self):
# Get the scene from QGraphicsView
scene = self.viewer._scene
# Create a QImage with the size of the scene
image = QImage(scene.sceneRect().size().toSize(), QImage.Format_ARGB32_Premultiplied)
image.fill(Qt.transparent)
# Paint the scene onto the QImage
painter = QPainter(image)
painter.setRenderHints(QPainter.Antialiasing | QPainter.TextAntialiasing, False)
scene.render(painter)
painter.end()
# Open 'Save As' dialog
file_path, _ = QFileDialog.getSaveFileName(
None, "Save Image", "", "PNG Image (*.png);;JPEG Image (*.jpg *.jpeg)"
)
# Save the image if a file path was provided, using high-quality settings for JPEG
if file_path:
if file_path.lower().endswith('.jpg') or file_path.lower().endswith('.jpeg'):
image.save(file_path, 'JPEG', 100)
else:
image.save(file_path) # PNG is lossless by default
def main(argv=None):
"""
Creates the main window for the application and begins the \
QApplication if necessary.
:param argv | [, ...] || None
:return error code
"""
# Define installation path
install_folder = os.path.dirname(__file__)
app = None
# create the application if necessary
if (not QApplication.instance()):
app = QApplication(argv)
app.setStyle('Fusion')
# test if dark theme is used
palette = app.palette()
bg_color = palette.color(QPalette.Window)
is_dark_theme = bg_color.lightness() < 128
print(f'Windows dark theme: {is_dark_theme}')
if is_dark_theme:
app.setStyleSheet("""
QPushButton:checked {
background-color: lightblue;
}
QPushButton:disabled {
background-color: #666;
}
QWidget { background-color: #444; }
QProgressBar {
text-align: center;
color: rgb(240, 240, 240);
border-width: 1px;
border-radius: 10px;
border-color: rgb(230, 230, 230);
border-style: solid;
background-color:rgb(207,207,207);
}
QProgressBar:chunk {
background-color:rgb(50, 156, 179);
border-radius: 10px;
}
""")
# create the main window
print('Launching the application')
window = CrackApp(is_dark_theme)
window.showMaximized()
# run the application if necessary
if (app):
return app.exec_()
# no errors since we're not running our own event loop
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))