-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtab_qa.py
More file actions
1469 lines (1292 loc) · 62.6 KB
/
tab_qa.py
File metadata and controls
1469 lines (1292 loc) · 62.6 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
import os
import glob
import json
import shutil
import threading
import urllib.request
from concurrent.futures import ThreadPoolExecutor, as_completed
import cv2
import numpy as np
from PIL import Image, ImageOps
from file_utils import find_media_files, get_display_name, IMAGE_EXTS
# Silence MediaPipe / TFLite native-log chatter (xnnpack default, feedback-manager
# disabled, XNNPACK delegate INFO) before the first `import mediapipe`.
os.environ.setdefault("GLOG_minloglevel", "2")
os.environ.setdefault("TF_CPP_MIN_LOG_LEVEL", "2")
try:
import mediapipe as mp
_HAS_MEDIAPIPE = True
except ImportError:
_HAS_MEDIAPIPE = False
from PySide6.QtWidgets import (QWidget, QHBoxLayout, QVBoxLayout, QTextEdit, QSlider, QLabel, QSplitter, QGroupBox,
QLineEdit, QCheckBox, QPushButton, QMessageBox, QFormLayout, QFrame, QSizePolicy, QComboBox,
QProgressBar, QRadioButton, QButtonGroup, QSpinBox, QTreeWidget, QTreeWidgetItem, QHeaderView)
from PySide6.QtCore import Qt, QTimer, Signal, QByteArray, QThread
from PySide6.QtGui import QImage, QPixmap, QFont, QColor, QShortcut, QKeySequence
from gui_widgets import ResizableImageLabel
# ---------------------------------------------------------------------------
# Analysis Worker
# ---------------------------------------------------------------------------
class QAAnalysisWorker(QThread):
progress = Signal(int, int) # current, total
result_ready = Signal(str, dict) # filepath, result dict
log_msg = Signal(str)
finished_analysis = Signal()
BLINK_THRESHOLD = 0.45 # blendshape score above this = eyes closed
def __init__(self, image_files, settings, folder):
super().__init__()
self.image_files = image_files
self.settings = settings
self.folder = folder
self._stop = False
# Per-thread detectors (MediaPipe / YuNet / Haar are not thread-safe)
self._tls = threading.local()
self._landmarkers_lock = threading.Lock()
self._all_landmarkers = [] # track for cleanup
self._person_detectors_lock = threading.Lock()
self._all_person_detectors = [] # track for cleanup
def stop(self):
self._stop = True
# -- blur ---------------------------------------------------------------
@staticmethod
def analyze_blur(cv_gray):
return cv2.Laplacian(cv_gray, cv2.CV_64F).var()
# -- resolution ---------------------------------------------------------
@staticmethod
def analyze_resolution(w, h, threshold):
min_dim = min(w, h)
if min_dim >= threshold:
return 1.0
half = threshold / 2.0
if min_dim <= half:
return 0.0
return (min_dim - half) / (threshold - half)
# -- mask existence -----------------------------------------------------
@staticmethod
def check_mask_exists(img_path, folder):
base = os.path.splitext(os.path.basename(img_path))[0]
# 1. subfolder
if os.path.exists(os.path.join(folder, "masks", f"{base}.png")):
return True
# 2. separate file
base_path = os.path.splitext(img_path)[0]
for suffix in ["-masklabel.png", "-masklabel.jpg", "_masklabel.png", "_masklabel.jpg"]:
if os.path.exists(base_path + suffix):
return True
# 3. alpha channel
try:
pil = Image.open(img_path)
if pil.mode in ("RGBA", "LA"):
alpha = np.array(pil.split()[-1])
if alpha.mean() < 255:
return True
except Exception:
pass
return False
# -- face + eyes (MediaPipe) --------------------------------------------
def _get_face_landmarker(self):
"""Lazy-init a per-thread MediaPipe FaceLandmarker with blendshapes."""
landmarker = getattr(self._tls, "face_landmarker", None)
if landmarker is not None:
return landmarker
if not _HAS_MEDIAPIPE:
return None
model_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "models", "_")
model_path = os.path.join(model_dir, "face_landmarker_v2_with_blendshapes.task")
if not os.path.exists(model_path):
self._download_model(
"https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task",
model_path)
if not os.path.exists(model_path):
return None
try:
options = mp.tasks.vision.FaceLandmarkerOptions(
base_options=mp.tasks.BaseOptions(model_asset_path=model_path),
running_mode=mp.tasks.vision.RunningMode.IMAGE,
num_faces=10,
output_face_blendshapes=True,
min_face_detection_confidence=0.5,
)
landmarker = mp.tasks.vision.FaceLandmarker.create_from_options(options)
self._tls.face_landmarker = landmarker
with self._landmarkers_lock:
self._all_landmarkers.append(landmarker)
except Exception as e:
self.log_msg.emit(f"Failed to create MediaPipe FaceLandmarker: {e}")
return None
return landmarker
def analyze_faces_and_eyes(self, cv_img):
"""Detect faces and eye-blink state in one pass.
Returns (face_count, eyes_closed_count).
Uses MediaPipe FaceLandmarker with blendshapes when available,
falls back to YuNet + Haar cascade otherwise.
"""
landmarker = self._get_face_landmarker()
if landmarker is not None:
return self._analyze_mediapipe(cv_img, landmarker)
return self._analyze_fallback(cv_img)
def _analyze_mediapipe(self, cv_img, landmarker):
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=cv_img)
result = landmarker.detect(mp_image)
face_count = len(result.face_landmarks)
eyes_closed = 0
for blendshapes in (result.face_blendshapes or []):
blink_scores = {}
for bs in blendshapes:
if bs.category_name in ("eyeBlinkLeft", "eyeBlinkRight"):
blink_scores[bs.category_name] = bs.score
left = blink_scores.get("eyeBlinkLeft", 0.0)
right = blink_scores.get("eyeBlinkRight", 0.0)
if left > self.BLINK_THRESHOLD or right > self.BLINK_THRESHOLD:
eyes_closed += 1
return face_count, eyes_closed
# -- person detection (MediaPipe Object Detector) -----------------------
def _get_person_detector(self):
"""Lazy-init a per-thread MediaPipe ObjectDetector restricted to 'person'."""
detector = getattr(self._tls, "person_detector", None)
if detector is not None:
return detector
if not _HAS_MEDIAPIPE:
return None
model_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "models", "_")
model_path = os.path.join(model_dir, "efficientdet_lite2.tflite")
if not os.path.exists(model_path):
self._download_model(
"https://storage.googleapis.com/mediapipe-models/object_detector/efficientdet_lite2/float16/1/efficientdet_lite2.tflite",
model_path)
if not os.path.exists(model_path):
return None
try:
options = mp.tasks.vision.ObjectDetectorOptions(
base_options=mp.tasks.BaseOptions(model_asset_path=model_path),
running_mode=mp.tasks.vision.RunningMode.IMAGE,
category_allowlist=["person"],
score_threshold=0.3,
max_results=50,
)
detector = mp.tasks.vision.ObjectDetector.create_from_options(options)
self._tls.person_detector = detector
with self._person_detectors_lock:
self._all_person_detectors.append(detector)
except Exception as e:
self.log_msg.emit(f"Failed to create MediaPipe ObjectDetector: {e}")
return None
return detector
def analyze_persons(self, cv_img):
"""Detect people in an image. Returns int count (0 if detector unavailable)."""
detector = self._get_person_detector()
if detector is None:
return 0
try:
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=cv_img)
result = detector.detect(mp_image)
return len(result.detections or [])
except Exception:
return 0
# -- fallback: YuNet face + Haar eyes -----------------------------------
def _analyze_fallback(self, cv_img):
faces = self._detect_faces_yunet(cv_img)
face_count = len(faces) if faces is not None else 0
eyes_closed = 0
if face_count > 0:
cv_gray = cv2.cvtColor(cv_img, cv2.COLOR_RGB2GRAY)
for face in faces:
if self._eyes_closed_haar(cv_gray, face):
eyes_closed += 1
return face_count, eyes_closed
def _detect_faces_yunet(self, cv_img):
h, w = cv_img.shape[:2]
detector = getattr(self._tls, "yunet_detector", None)
if detector is None:
model_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "models", "_", "face_detection_yunet_2023mar.onnx")
if not os.path.exists(model_path):
self._download_model(
"https://github.com/opencv/opencv_zoo/raw/main/models/face_detection_yunet/face_detection_yunet_2023mar.onnx",
model_path)
if not os.path.exists(model_path):
return []
detector = cv2.FaceDetectorYN.create(model_path, "", (w, h), 0.7, 0.3, 5000)
self._tls.yunet_detector = detector
detector.setInputSize((w, h))
_, faces = detector.detect(cv_img)
return faces if faces is not None else []
def _eyes_closed_haar(self, cv_gray, face_bbox):
"""Fallback: returns True if eyes appear closed using Haar cascades."""
try:
eye_cascade = getattr(self._tls, "eye_cascade", None)
if eye_cascade is None:
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml")
self._tls.eye_cascade = eye_cascade
img_h, img_w = cv_gray.shape[:2]
if len(face_bbox) >= 8:
fw = int(face_bbox[2])
eye_radius = max(int(fw * 0.15), 15)
eyes_found = 0
for ei in range(2):
ex, ey = int(face_bbox[4 + ei * 2]), int(face_bbox[5 + ei * 2])
x1, y1 = max(0, ex - eye_radius), max(0, ey - eye_radius)
x2, y2 = min(img_w, ex + eye_radius), min(img_h, ey + eye_radius)
patch = cv_gray[y1:y2, x1:x2]
if patch.size == 0:
continue
min_sz = max(int(eye_radius * 0.4), 5)
if len(eye_cascade.detectMultiScale(patch, 1.05, 3, minSize=(min_sz, min_sz))) > 0:
eyes_found += 1
return eyes_found < 2
elif len(face_bbox) >= 4:
x, y, w, h = int(face_bbox[0]), int(face_bbox[1]), int(face_bbox[2]), int(face_bbox[3])
region = cv_gray[y:y + int(h * 0.5), x:x + w]
if region.size == 0:
return True
min_sz = max(int(w * 0.06), 5)
return len(eye_cascade.detectMultiScale(region, 1.05, 3, minSize=(min_sz, min_sz))) < 2
except Exception:
pass
return False
@staticmethod
def _download_model(url, dest_path):
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
try:
urllib.request.urlretrieve(url, dest_path)
except Exception:
pass
# -- per-image analysis (called from thread pool) ------------------------
def _analyze_single(self, img_path):
"""Analyze a single image. Thread-safe via thread-local detectors."""
blur_enabled = self.settings.get("blur_enabled", True)
res_enabled = self.settings.get("resolution_enabled", True)
mask_enabled = self.settings.get("mask_enabled", True)
face_enabled = self.settings.get("face_enabled", True)
eyes_enabled = self.settings.get("eyes_enabled", True)
person_enabled = self.settings.get("person_enabled", True)
res_threshold = self.settings.get("resolution_threshold", 512)
blur_low = self.settings.get("blur_threshold_low", 50)
blur_high = self.settings.get("blur_threshold_high", 500)
result = {
"filepath": img_path,
"blur_variance": 0.0, "blur_score": 1.0,
"width": 0, "height": 0, "min_dimension": 0, "resolution_score": 1.0,
"has_mask": False, "mask_score": 1.0,
"face_count": 0, "face_score": 1.0,
"eyes_closed_count": 0, "eyes_score": 1.0,
"person_count": 0, "person_score": 1.0,
}
try:
pil_img = Image.open(img_path)
pil_img = ImageOps.exif_transpose(pil_img)
cv_img = np.array(pil_img.convert("RGB"))
h, w = cv_img.shape[:2]
result["width"] = w
result["height"] = h
result["min_dimension"] = min(w, h)
cv_gray = cv2.cvtColor(cv_img, cv2.COLOR_RGB2GRAY)
if blur_enabled:
variance = self.analyze_blur(cv_gray)
result["blur_variance"] = round(variance, 2)
if blur_high > blur_low:
result["blur_score"] = round(max(0.0, min(1.0, (variance - blur_low) / (blur_high - blur_low))), 4)
else:
result["blur_score"] = 1.0 if variance >= blur_low else 0.0
if res_enabled:
result["resolution_score"] = round(self.analyze_resolution(w, h, res_threshold), 4)
if mask_enabled:
has_mask = self.check_mask_exists(img_path, self.folder)
result["has_mask"] = has_mask
result["mask_score"] = 1.0 if has_mask else 0.0
if face_enabled:
face_count, eyes_closed = self.analyze_faces_and_eyes(cv_img)
result["face_count"] = face_count
if eyes_enabled and face_count > 0:
result["eyes_closed_count"] = eyes_closed
result["eyes_score"] = 0.0 if eyes_closed > 0 else 1.0
if person_enabled:
result["person_count"] = self.analyze_persons(cv_img)
except Exception as e:
self.log_msg.emit(f"Error analyzing {os.path.basename(img_path)}: {e}")
return img_path, result
# -- main run -----------------------------------------------------------
def run(self):
total = len(self.image_files)
num_workers = min(os.cpu_count() or 4, 8)
completed = 0
with ThreadPoolExecutor(max_workers=num_workers) as executor:
futures = {executor.submit(self._analyze_single, p): p for p in self.image_files}
for future in as_completed(futures):
if self._stop:
for f in futures:
f.cancel()
break
completed += 1
self.progress.emit(completed, total)
try:
img_path, result = future.result()
self.result_ready.emit(img_path, result)
except Exception as e:
img_path = futures[future]
self.log_msg.emit(f"Error analyzing {os.path.basename(img_path)}: {e}")
# Cleanup all per-thread MediaPipe landmarkers
for lm in self._all_landmarkers:
try:
lm.close()
except Exception:
pass
self._all_landmarkers.clear()
# Cleanup all per-thread MediaPipe object detectors
for det in self._all_person_detectors:
try:
det.close()
except Exception:
pass
self._all_person_detectors.clear()
self.progress.emit(total, total)
self.finished_analysis.emit()
# ---------------------------------------------------------------------------
# Tree item with numeric sort
# ---------------------------------------------------------------------------
class _ScoreTreeItem(QTreeWidgetItem):
"""QTreeWidgetItem that sorts score columns numerically."""
def __lt__(self, other):
col = self.treeWidget().sortColumn()
if col == 0:
return self.text(0).lower() < other.text(0).lower()
a = self.data(col, Qt.UserRole)
b = other.data(col, Qt.UserRole)
if a is None: a = -1.0
if b is None: b = -1.0
return a < b
# ---------------------------------------------------------------------------
# QA Tab
# ---------------------------------------------------------------------------
class QATab(QWidget):
log_msg = Signal(str)
SCORE_COLORS = {
"bad": "#e06c75", # red
"medium": "#e5c07b", # yellow
"good": "#98c379", # green
}
def __init__(self):
super().__init__()
self.current_folder = ""
self.recursive = False
self.image_files = [] # all images (unsorted)
self.sorted_files = [] # sorted by score after analysis
self.current_index = -1
self.analysis_results = {} # filepath -> result dict
self.worker = None
self.cv_img_original = None
self.setup_ui()
self.setup_shortcuts()
# -----------------------------------------------------------------------
# UI Setup
# -----------------------------------------------------------------------
def setup_ui(self):
layout = QHBoxLayout(self)
splitter = QSplitter(Qt.Horizontal)
self.main_splitter = splitter
# === LEFT PANEL ===
left_widget = QWidget()
left_layout = QVBoxLayout(left_widget)
left_layout.setContentsMargins(0, 0, 0, 0)
# -- Analysis Settings --
settings_group = QGroupBox("Analysis Settings")
settings_layout = QFormLayout(settings_group)
settings_layout.setContentsMargins(5, 10, 5, 5)
# Blur
self.chk_blur = QCheckBox("Blur Detection")
self.chk_blur.setChecked(True)
self.chk_blur.setToolTip("Detect blurry images using Laplacian variance")
settings_layout.addRow(self.chk_blur)
blur_thresh_layout = QHBoxLayout()
blur_thresh_layout.addWidget(QLabel("Low:"))
self.spin_blur_low = QSpinBox()
self.spin_blur_low.setRange(1, 9999)
self.spin_blur_low.setValue(50)
self.spin_blur_low.setToolTip("Laplacian variance below this = definitely blurry (score 0)")
blur_thresh_layout.addWidget(self.spin_blur_low)
blur_thresh_layout.addWidget(QLabel("High:"))
self.spin_blur_high = QSpinBox()
self.spin_blur_high.setRange(1, 9999)
self.spin_blur_high.setValue(500)
self.spin_blur_high.setToolTip("Laplacian variance above this = definitely sharp (score 1)")
blur_thresh_layout.addWidget(self.spin_blur_high)
settings_layout.addRow(" Thresholds:", blur_thresh_layout)
# Resolution
self.chk_resolution = QCheckBox("Low Resolution")
self.chk_resolution.setChecked(True)
self.chk_resolution.setToolTip("Flag images below a minimum resolution")
settings_layout.addRow(self.chk_resolution)
self.spin_resolution = QSpinBox()
self.spin_resolution.setRange(32, 8192)
self.spin_resolution.setValue(512)
self.spin_resolution.setSuffix(" px")
self.spin_resolution.setToolTip("Minimum acceptable shortest dimension")
settings_layout.addRow(" Min dimension:", self.spin_resolution)
# Mask
self.chk_mask = QCheckBox("Missing Mask")
self.chk_mask.setChecked(True)
self.chk_mask.setToolTip("Check if images have a corresponding mask. Auto-disabled if no images have masks.")
settings_layout.addRow(self.chk_mask)
# Face Detection
self.chk_face = QCheckBox("Face Detection")
self.chk_face.setChecked(True)
self.chk_face.setToolTip("Detect faces using YuNet (OpenCV). Downloads a small model on first use.")
settings_layout.addRow(self.chk_face)
face_mode_layout = QHBoxLayout()
self.radio_face_missing = QRadioButton("Flag no face")
self.radio_face_present = QRadioButton("Flag face found")
self.radio_face_missing.setChecked(True)
self.radio_face_missing.setToolTip("Bad score if no face detected (portrait dataset)")
self.radio_face_present.setToolTip("Bad score if face detected (landscape/object dataset)")
self.face_mode_group = QButtonGroup(self)
self.face_mode_group.addButton(self.radio_face_missing, 0)
self.face_mode_group.addButton(self.radio_face_present, 1)
face_mode_layout.addWidget(self.radio_face_missing)
face_mode_layout.addWidget(self.radio_face_present)
settings_layout.addRow(" Mode:", face_mode_layout)
# Eyes Closed
self.chk_eyes = QCheckBox("Eyes Closed")
self.chk_eyes.setChecked(True)
self.chk_eyes.setToolTip("Detect closed eyes in detected faces (approximate heuristic)")
settings_layout.addRow(self.chk_eyes)
# Person Detection
self.chk_person = QCheckBox("Person Detection")
self.chk_person.setChecked(True)
self.chk_person.setToolTip("Count people using MediaPipe EfficientDet-Lite2 (COCO). "
"Downloads a ~12 MB model on first use.")
if not _HAS_MEDIAPIPE:
self.chk_person.setChecked(False)
self.chk_person.setEnabled(False)
self.chk_person.setToolTip("Requires the 'mediapipe' package.")
settings_layout.addRow(self.chk_person)
person_mode_layout = QHBoxLayout()
self.radio_person_none = QRadioButton("No people")
self.radio_person_any = QRadioButton("Any people")
self.radio_person_multiple = QRadioButton("Multiple")
self.radio_person_multiple.setChecked(True)
self.radio_person_none.setToolTip("Bad score if no people detected")
self.radio_person_any.setToolTip("Bad score if 1+ people detected (only use images without people)")
self.radio_person_multiple.setToolTip("Bad score if 2+ people detected (filter out group shots)")
self.person_mode_group = QButtonGroup(self)
self.person_mode_group.addButton(self.radio_person_none, 0)
self.person_mode_group.addButton(self.radio_person_any, 1)
self.person_mode_group.addButton(self.radio_person_multiple, 2)
person_mode_layout.addWidget(self.radio_person_none)
person_mode_layout.addWidget(self.radio_person_any)
person_mode_layout.addWidget(self.radio_person_multiple)
settings_layout.addRow(" Flag:", person_mode_layout)
left_layout.addWidget(settings_group)
# -- Weights --
weights_group = QGroupBox("Criterion Weights")
weights_layout = QFormLayout(weights_group)
weights_layout.setContentsMargins(5, 10, 5, 5)
self.weight_sliders = {}
for name, default in [("Blur", 10), ("Resolution", 5), ("Mask", 10), ("Face", 7), ("Eyes", 3), ("Person", 5)]:
slider = QSlider(Qt.Horizontal)
slider.setRange(0, 20) # 0.0 to 2.0
slider.setValue(default)
lbl = QLabel(f"{default / 10:.1f}")
lbl.setFixedWidth(28)
slider.valueChanged.connect(lambda v, l=lbl: l.setText(f"{v / 10:.1f}"))
row = QHBoxLayout()
row.addWidget(slider)
row.addWidget(lbl)
weights_layout.addRow(f"{name}:", row)
self.weight_sliders[name.lower()] = slider
left_layout.addWidget(weights_group)
# -- Analyze Button + Progress --
analyze_layout = QHBoxLayout()
self.btn_analyze = QPushButton("Analyze All")
self.btn_analyze.setStyleSheet("background-color: #5d99c4; color: white; font-weight: bold;")
self.btn_analyze.clicked.connect(self.start_analysis)
self.btn_stop = QPushButton("Stop")
self.btn_stop.setEnabled(False)
self.btn_stop.clicked.connect(self.stop_analysis)
analyze_layout.addWidget(self.btn_analyze)
analyze_layout.addWidget(self.btn_stop)
left_layout.addLayout(analyze_layout)
self.progress_bar = QProgressBar()
self.progress_bar.setTextVisible(True)
self.progress_bar.setValue(0)
left_layout.addWidget(self.progress_bar)
# -- Sorted Image Table --
self.COLUMN_KEYS = ["overall_score", "blur_score", "resolution_score", "mask_score", "face_score", "eyes_score", "person_score"]
self.COLUMN_HEADERS = ["File", "Overall", "Blur", "Res", "Mask", "Face", "Eyes", "Person"]
self.tree_widget = QTreeWidget()
self.tree_widget.setHeaderLabels(self.COLUMN_HEADERS)
self.tree_widget.setRootIsDecorated(False)
self.tree_widget.setAllColumnsShowFocus(True)
self.tree_widget.setSortingEnabled(True)
self.tree_widget.setSelectionMode(QTreeWidget.SingleSelection)
self.tree_widget.currentItemChanged.connect(self._on_tree_item_changed)
self.tree_widget.header().sectionClicked.connect(self._on_header_clicked)
# Column sizing
header = self.tree_widget.header()
header.setStretchLastSection(False)
header.setSectionResizeMode(0, QHeaderView.Stretch)
for col in range(1, len(self.COLUMN_HEADERS)):
header.setSectionResizeMode(col, QHeaderView.ResizeToContents)
# Default sort: Overall descending (best first)
self.tree_widget.sortByColumn(1, Qt.DescendingOrder)
self._current_sort_col = 1
self._current_sort_order = Qt.DescendingOrder
left_layout.addWidget(self.tree_widget)
# -- Batch Actions --
batch_group = QGroupBox("Batch Actions")
batch_layout = QVBoxLayout(batch_group)
batch_layout.setContentsMargins(5, 10, 5, 5)
# Caption append text
append_layout = QHBoxLayout()
append_layout.addWidget(QLabel("Append text:"))
self.txt_append = QLineEdit(", low quality, worst quality, blurry")
self.txt_append.setToolTip("Text to append to caption files")
append_layout.addWidget(self.txt_append)
batch_layout.addLayout(append_layout)
preset_layout = QHBoxLayout()
preset_layout.addWidget(QLabel("Presets:"))
self.combo_append_preset = QComboBox()
self.combo_append_preset.addItems([
"Custom",
", low quality, worst quality, jpeg artifacts, blurry",
", blurry, out of focus",
", low resolution, pixelated",
", eyes closed",
])
self.combo_append_preset.currentIndexChanged.connect(self.on_append_preset_changed)
preset_layout.addWidget(self.combo_append_preset)
batch_layout.addLayout(preset_layout)
# Criterion selector for threshold
self.BATCH_CRITERIA = [
("Overall", "overall_score"),
("Blur", "blur_score"),
("Resolution", "resolution_score"),
("Mask", "mask_score"),
("Face", "face_score"),
("Eyes", "eyes_score"),
("Person", "person_score"),
]
criterion_layout = QHBoxLayout()
criterion_layout.addWidget(QLabel("Criterion:"))
self.combo_batch_criterion = QComboBox()
for label, _ in self.BATCH_CRITERIA:
self.combo_batch_criterion.addItem(label)
self.combo_batch_criterion.setToolTip("Which score to compare against the threshold")
self.combo_batch_criterion.currentIndexChanged.connect(lambda: self.on_threshold_changed(self.slider_threshold.value()))
criterion_layout.addWidget(self.combo_batch_criterion)
batch_layout.addLayout(criterion_layout)
# Threshold
thresh_layout = QHBoxLayout()
thresh_layout.addWidget(QLabel("Threshold:"))
self.slider_threshold = QSlider(Qt.Horizontal)
self.slider_threshold.setRange(0, 100)
self.slider_threshold.setValue(50)
self.slider_threshold.valueChanged.connect(self.on_threshold_changed)
self.lbl_threshold = QLabel("0.50")
self.lbl_threshold.setFixedWidth(32)
thresh_layout.addWidget(self.slider_threshold)
thresh_layout.addWidget(self.lbl_threshold)
batch_layout.addLayout(thresh_layout)
self.lbl_below_count = QLabel("0 of 0 images below threshold")
self.lbl_below_count.setStyleSheet("color: #888;")
batch_layout.addWidget(self.lbl_below_count)
# Batch action buttons
batch_btn_layout = QHBoxLayout()
self.btn_batch_append = QPushButton("Append to All Below")
self.btn_batch_append.setToolTip("Append text to captions of all images scoring below the threshold on the selected criterion")
self.btn_batch_append.clicked.connect(lambda: self.apply_batch_action("append"))
self.btn_batch_move = QPushButton("Move All Below to Unused")
self.btn_batch_move.setToolTip("Move all images (+ caption + mask) scoring below the threshold to the unused folder")
self.btn_batch_move.clicked.connect(lambda: self.apply_batch_action("move"))
batch_btn_layout.addWidget(self.btn_batch_append)
batch_btn_layout.addWidget(self.btn_batch_move)
batch_layout.addLayout(batch_btn_layout)
left_layout.addWidget(batch_group)
# === RIGHT PANEL ===
right_widget = QWidget()
right_layout = QVBoxLayout(right_widget)
right_layout.setContentsMargins(0, 0, 0, 0)
# Per-image action buttons (above image for visibility)
action_layout = QHBoxLayout()
self.btn_append_single = QPushButton("Append to Caption [A]")
self.btn_append_single.setToolTip("Append the configured text to this image's caption file (A)")
self.btn_append_single.clicked.connect(self.append_to_current_caption)
self.btn_move_single = QPushButton("Move to Unused [Del]")
self.btn_move_single.setToolTip("Move this image (and its caption/mask) to the unused folder (Del)")
self.btn_move_single.clicked.connect(self.move_current_to_unused)
action_layout.addWidget(self.btn_append_single)
action_layout.addWidget(self.btn_move_single)
right_layout.addLayout(action_layout)
# Image display
self.lbl_image = ResizableImageLabel()
self.lbl_image.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
self.lbl_image.setAlignment(Qt.AlignCenter)
self.lbl_image.setMinimumSize(200, 200)
# Wheel navigation on image
self.lbl_image.wheelEvent = self.image_wheel_event
right_layout.addWidget(self.lbl_image, stretch=1)
# Info bar
self.lbl_info = QLabel("0 / 0")
self.lbl_info.setAlignment(Qt.AlignCenter)
self.lbl_info.setStyleSheet("color: #aaa; padding: 2px;")
right_layout.addWidget(self.lbl_info)
# Navigation slider
self.slider = QSlider(Qt.Horizontal)
self.slider.setRange(0, 0)
self.slider.valueChanged.connect(self.on_slider_changed)
right_layout.addWidget(self.slider)
# Per-image quality details
details_group = QGroupBox("Quality Details")
details_layout = QFormLayout(details_group)
details_layout.setContentsMargins(5, 10, 5, 5)
self.lbl_detail_blur = QLabel("-")
self.lbl_detail_resolution = QLabel("-")
self.lbl_detail_mask = QLabel("-")
self.lbl_detail_face = QLabel("-")
self.lbl_detail_eyes = QLabel("-")
self.lbl_detail_person = QLabel("-")
self.lbl_detail_overall = QLabel("-")
self.lbl_detail_overall.setStyleSheet("font-weight: bold; font-size: 14px;")
details_layout.addRow("Blur:", self.lbl_detail_blur)
details_layout.addRow("Resolution:", self.lbl_detail_resolution)
details_layout.addRow("Mask:", self.lbl_detail_mask)
details_layout.addRow("Face:", self.lbl_detail_face)
details_layout.addRow("Eyes:", self.lbl_detail_eyes)
details_layout.addRow("Person:", self.lbl_detail_person)
sep = QFrame()
sep.setFrameShape(QFrame.HLine)
sep.setStyleSheet("color: #555;")
details_layout.addRow(sep)
details_layout.addRow("Overall Score:", self.lbl_detail_overall)
right_layout.addWidget(details_group)
# Caption preview
caption_group = QGroupBox("Caption")
caption_layout = QVBoxLayout(caption_group)
caption_layout.setContentsMargins(5, 10, 5, 5)
self.txt_caption = QTextEdit()
self.txt_caption.setReadOnly(True)
self.txt_caption.setMaximumHeight(80)
caption_layout.addWidget(self.txt_caption)
right_layout.addWidget(caption_group)
# Assemble splitter
splitter.addWidget(left_widget)
splitter.addWidget(right_widget)
splitter.setStretchFactor(0, 0)
splitter.setStretchFactor(1, 1)
splitter.setSizes([320, 700])
layout.addWidget(splitter)
def setup_shortcuts(self):
QShortcut(QKeySequence(Qt.Key_Left), self, self.navigate_prev)
QShortcut(QKeySequence(Qt.Key_Right), self, self.navigate_next)
QShortcut(QKeySequence(Qt.Key_A), self, self.append_to_current_caption)
QShortcut(QKeySequence(Qt.Key_Delete), self, self.move_current_to_unused)
# -----------------------------------------------------------------------
# Folder / File Discovery
# -----------------------------------------------------------------------
def set_recursive(self, recursive):
self.recursive = recursive
self.refresh_file_list()
def update_folder(self, path, recursive=None):
if path and os.path.isdir(path):
self.current_folder = path
if recursive is not None:
self.recursive = recursive
self.refresh_file_list()
self.load_cache()
def refresh_file_list(self):
if not self.current_folder:
self.tree_widget.clear()
return
self.image_files = find_media_files(self.current_folder, exts=IMAGE_EXTS,
recursive=self.recursive)
self.sorted_files = list(self.image_files)
self._rebuild_list_widget()
def _rebuild_list_widget(self):
self.tree_widget.blockSignals(True)
self.tree_widget.setSortingEnabled(False)
self.tree_widget.clear()
for f in self.sorted_files:
display_name = get_display_name(f, self.current_folder, self.recursive)
item = _ScoreTreeItem(self.tree_widget)
item.setText(0, display_name)
item.setData(0, Qt.UserRole, f) # store full path
if f in self.analysis_results:
r = self.analysis_results[f]
scores = [
r.get("overall_score", -1),
r.get("blur_score", -1),
r.get("resolution_score", -1),
r.get("mask_score", -1),
r.get("face_score", -1),
r.get("eyes_score", -1),
r.get("person_score", -1),
]
for col, score in enumerate(scores, start=1):
if score < 0:
item.setText(col, "-")
else:
item.setText(col, f"{score:.2f}")
item.setForeground(col, QColor(self._score_color(score)))
item.setData(col, Qt.UserRole, score) # for sorting
# Color the filename by overall score
item.setForeground(0, QColor(self._score_color(scores[0])))
else:
for col in range(1, len(self.COLUMN_HEADERS)):
item.setText(col, "-")
item.setData(col, Qt.UserRole, -1.0)
self.tree_widget.setSortingEnabled(True)
self.tree_widget.sortByColumn(self._current_sort_col, self._current_sort_order)
self.tree_widget.blockSignals(False)
# Update slider and select first
self.slider.setRange(0, max(0, len(self.sorted_files) - 1))
if self.tree_widget.topLevelItemCount() > 0:
self.tree_widget.setCurrentItem(self.tree_widget.topLevelItem(0))
# -----------------------------------------------------------------------
# Navigation
# -----------------------------------------------------------------------
def _current_visual_index(self):
"""Return the visual row index of the currently selected tree item."""
item = self.tree_widget.currentItem()
if item is None:
return -1
return self.tree_widget.indexOfTopLevelItem(item)
def _filepath_from_item(self, item):
if item is None:
return None
return item.data(0, Qt.UserRole)
def _on_tree_item_changed(self, current, previous):
f_path = self._filepath_from_item(current)
if f_path is None:
return
row = self._current_visual_index()
self.current_index = row
self.slider.blockSignals(True)
self.slider.setValue(row)
self.slider.blockSignals(False)
self.load_image(f_path)
self.load_caption_preview(f_path)
self.update_detail_panel(f_path)
# Info label
filename = os.path.basename(f_path)
total = self.tree_widget.topLevelItemCount()
res_info = ""
if self.cv_img_original is not None:
h, w = self.cv_img_original.shape[:2]
res_info = f" | {w} x {h}"
score_info = ""
if f_path in self.analysis_results:
score_info = f" | Score: {self.analysis_results[f_path].get('overall_score', 0):.2f}"
self.lbl_info.setText(f"{row + 1} / {total} | {filename}{res_info}{score_info}")
def _on_header_clicked(self, logical_index):
"""Track which column / order the user picked so rebuilds preserve it."""
self._current_sort_col = logical_index
self._current_sort_order = self.tree_widget.header().sortIndicatorOrder()
def on_slider_changed(self, value):
count = self.tree_widget.topLevelItemCount()
if 0 <= value < count:
self.tree_widget.setCurrentItem(self.tree_widget.topLevelItem(value))
def image_wheel_event(self, event):
self.navigate(1 if event.angleDelta().y() < 0 else -1)
def navigate(self, steps):
idx = self._current_visual_index()
new_row = idx + steps
count = self.tree_widget.topLevelItemCount()
if 0 <= new_row < count:
self.tree_widget.setCurrentItem(self.tree_widget.topLevelItem(new_row))
def navigate_prev(self):
self.navigate(-1)
def navigate_next(self):
self.navigate(1)
def set_focus_to_image(self):
self.lbl_image.setFocus()
# -----------------------------------------------------------------------
# Image Display
# -----------------------------------------------------------------------
def load_image(self, f_path):
self.cv_img_original = None
try:
pil_img = Image.open(f_path)
pil_img = ImageOps.exif_transpose(pil_img)
self.cv_img_original = np.array(pil_img.convert("RGB"))
except Exception as e:
self.log_msg.emit(f"Error loading {os.path.basename(f_path)}: {e}")
self.lbl_image.set_image(None)
return
self.update_image_display()
def update_image_display(self):
if self.cv_img_original is None:
self.lbl_image.set_image(None)
return
img = self.cv_img_original
height, width, _ = img.shape
bytes_per_line = 3 * width
q_img = QImage(img.data, width, height, bytes_per_line, QImage.Format_RGB888)
self.lbl_image.current_pixmap = QPixmap.fromImage(q_img)
self.lbl_image.update_view()
def load_caption_preview(self, f_path):
txt_path = os.path.splitext(f_path)[0] + ".txt"
self.txt_caption.blockSignals(True)
if os.path.exists(txt_path):
try:
with open(txt_path, "r", encoding="utf-8") as f:
self.txt_caption.setPlainText(f.read())
except Exception:
self.txt_caption.setPlainText("")
else:
self.txt_caption.setPlainText("")
self.txt_caption.blockSignals(False)
# -----------------------------------------------------------------------
# Detail Panel
# -----------------------------------------------------------------------
def update_detail_panel(self, f_path):
if f_path not in self.analysis_results:
for lbl in [self.lbl_detail_blur, self.lbl_detail_resolution, self.lbl_detail_mask,
self.lbl_detail_face, self.lbl_detail_eyes, self.lbl_detail_person,
self.lbl_detail_overall]:
lbl.setText("-")
return
r = self.analysis_results[f_path]
# Blur
bv = r.get("blur_variance", 0)
bs = r.get("blur_score", 1.0)
self.lbl_detail_blur.setText(f"Score: {bs:.2f} (variance: {bv:.1f})")
self.lbl_detail_blur.setStyleSheet(f"color: {self._score_color(bs)};")
# Resolution
rs = r.get("resolution_score", 1.0)
md = r.get("min_dimension", 0)
self.lbl_detail_resolution.setText(f"Score: {rs:.2f} ({r.get('width', 0)}x{r.get('height', 0)}, min: {md})")
self.lbl_detail_resolution.setStyleSheet(f"color: {self._score_color(rs)};")
# Mask
has_mask = r.get("has_mask", False)
ms = r.get("mask_score", 1.0)
self.lbl_detail_mask.setText(f"{'YES' if has_mask else 'NO'} (score: {ms:.2f})")
self.lbl_detail_mask.setStyleSheet(f"color: {self._score_color(ms)};")
# Face
fc = r.get("face_count", 0)
fs = r.get("face_score", 1.0)
self.lbl_detail_face.setText(f"{fc} face(s) detected (score: {fs:.2f})")
self.lbl_detail_face.setStyleSheet(f"color: {self._score_color(fs)};")
# Eyes
ec = r.get("eyes_closed_count", 0)
es = r.get("eyes_score", 1.0)
if fc > 0:
self.lbl_detail_eyes.setText(f"{ec} with eyes closed (score: {es:.2f})")
else:
self.lbl_detail_eyes.setText("N/A (no faces)")
self.lbl_detail_eyes.setStyleSheet(f"color: {self._score_color(es)};")