-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
2371 lines (2091 loc) · 104 KB
/
Copy pathapp.py
File metadata and controls
2371 lines (2091 loc) · 104 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 gradio as gr
import tempfile, os, shutil
from typing import List, Optional, Tuple
from seg import segmentation_pipeline
from length import load_model, classification_curvature, tube_length_border2border, compute_eye_metrics, compute_eye_diameters, compute_tube_metrics
import openpyxl, io
from openpyxl.drawing.image import Image as ExcelImage
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image as PILImage
import cv2
from scipy.ndimage import distance_transform_edt, gaussian_filter
from skimage.graph import route_through_array
import time
try:
import torch
_HAS_TORCH = True
except Exception:
_HAS_TORCH = False
try:
from scalebar import (detect_scalebar as _detect_scalebar,
draw_scalebar_endpoints as _draw_scalebar_endpoints,
calibrate_from_endpoints as _calibrate_from_endpoints)
_HAS_SCALEBAR = True
except Exception:
_HAS_SCALEBAR = False
MODEL_CACHE = {} # lazy-loaded cache keyed by model filename
# Registry of available segmentation models.
# Each entry: display name -> {target_size, body/eye/edema/swimbladder: (hf_filename, encoder_name, model_type)}
# None for eye/edema/swimbladder means "use the pipeline default" (the 256px SegFormer models).
# Body/eye/edema/swimbladder are all SegFormer (mit_b3) transformer models trained by
# Transformer_Segmentation_train.py, except the Fine-tuned DESY preset, which is untouched.
SEG_MODEL_OPTIONS = {
"Fast & Easy (256 px, ~2s/image)": {
"target_size": 256,
"body": ("best_model_body_256_segformer_mit_b3.pth", "mit_b3", "Segformer"),
"eye": None,
"edema": None,
"swimbladder": None,
},
"Complex & Slower (512 px, ~7s/image)": {
"target_size": 512,
"body": ("best_model_body_512_segformer_mit_b3.pth", "mit_b3", "Segformer"),
"eye": ("best_model_eye_512_segformer_mit_b3.pth", "mit_b3", "Segformer"),
"edema": ("best_model_edema_512_segformer_mit_b3.pth", "mit_b3", "Segformer"),
"swimbladder": ("best_model_swimmbladder_512_segformer_mit_b3.pth", "mit_b3", "Segformer"),
},
"Most Accurate (1024 px, ~35s/image)": {
"target_size": 1024,
"body": ("best_model_body_1024_segformer_mit_b3.pth", "mit_b3", "Segformer"),
"eye": ("best_model_eye_1024_segformer_mit_b3.pth", "mit_b3", "Segformer"),
"edema": ("best_model_edema_1024_segformer_mit_b3.pth", "mit_b3", "Segformer"),
"swimbladder": ("best_model_swimmbladder_1024_segformer_mit_b3.pth", "mit_b3", "Segformer"),
},
"Fine-tuned DESY": {
"target_size": 512,
"body": ("desy_body_512_finetuned.pth", "vgg19", "Unet"),
"eye": ("desy_eye_512_finetuned.pth", "vgg16", "Unet"),
"edema": ("desy_edema_512_finetuned.pth", "vgg19", "Unet"),
"swimbladder": ("desy_swimmbladder_512_finetuned.pth", "vgg19", "FPN"),
},
}
def _ensure_model():
global MODEL_CACHE
key = "classification"
if key not in MODEL_CACHE:
MODEL_CACHE[key] = load_model()
return MODEL_CACHE[key]
def _to_numpy(img):
if img is None:
return None
if _HAS_TORCH and isinstance(img, torch.Tensor):
img = img.detach().cpu().numpy()
if isinstance(img, PILImage.Image):
img = np.array(img)
img = np.asarray(img)
while img.ndim > 2 and img.shape[0] in (1,3) and img.shape[-1] not in (1,3):
if img.ndim == 3:
img = np.transpose(img, (1,2,0))
else:
break
if img.dtype != np.uint8:
img_min = float(img.min()) if img.size else 0.0
img_max = float(img.max()) if img.size else 1.0
if img_max <= 1.0 and img_min >= 0.0:
img = (img * 255.0).clip(0,255).astype(np.uint8)
else:
denom = (img_max - img_min) if (img_max - img_min) != 0 else 1.0
img = ((img - img_min) / denom * 255.0).clip(0,255).astype(np.uint8)
return img
def _make_boxplots_image(fish_lengths, curvatures, ratios, eye_areas=None, edema_areas=None, swim_areas=None, swim_widths=None):
def _clean_numeric(vals):
out = []
for v in (vals or []):
if isinstance(v, (int, float)) and np.isfinite(v):
out.append(float(v))
return out
fish_lengths_clean = _clean_numeric(fish_lengths)
curvatures_clean = _clean_numeric(curvatures)
ratios_clean = _clean_numeric(ratios)
eye_areas_clean = _clean_numeric(eye_areas)
edema_areas_clean = _clean_numeric(edema_areas)
swim_areas_clean = _clean_numeric(swim_areas)
swim_widths_clean = _clean_numeric(swim_widths)
# Count how many plots we need
num_plots = sum([
bool(fish_lengths_clean),
bool(curvatures_clean),
bool(ratios_clean),
bool(eye_areas_clean),
bool(edema_areas_clean),
bool(swim_areas_clean),
bool(swim_widths_clean),
])
if num_plots == 0:
num_plots = 1 # At least one subplot
fig = plt.figure(figsize=(5*num_plots, 5))
plot_idx = 1
if fish_lengths_clean:
plt.subplot(1, num_plots, plot_idx)
plt.boxplot(fish_lengths_clean, vert=True, patch_artist=True)
plt.title("Fish Lengths"); plt.ylabel("Length (µm)")
plot_idx += 1
if curvatures_clean:
plt.subplot(1, num_plots, plot_idx)
plt.boxplot(curvatures_clean, vert=True, patch_artist=True)
plt.title("Curvatures"); plt.ylabel("Curvature")
plot_idx += 1
if ratios_clean:
plt.subplot(1, num_plots, plot_idx)
plt.boxplot(ratios_clean, vert=True, patch_artist=True)
plt.title("Length/Straight Line Ratio"); plt.ylabel("Ratio")
plot_idx += 1
if eye_areas_clean:
plt.subplot(1, num_plots, plot_idx)
plt.boxplot(eye_areas_clean, vert=True, patch_artist=True)
plt.title("Eye Areas"); plt.ylabel("Area (µm²)")
plot_idx += 1
if edema_areas_clean:
plt.subplot(1, num_plots, plot_idx)
plt.boxplot(edema_areas_clean, vert=True, patch_artist=True)
plt.title("Edema Areas"); plt.ylabel("Area (µm²)")
plot_idx += 1
if swim_areas_clean:
plt.subplot(1, num_plots, plot_idx)
plt.boxplot(swim_areas_clean, vert=True, patch_artist=True)
plt.title("Swim Bladder Areas"); plt.ylabel("Area (µm²)")
plot_idx += 1
if swim_widths_clean:
plt.subplot(1, num_plots, plot_idx)
plt.boxplot(swim_widths_clean, vert=True, patch_artist=True)
plt.title("Swim Bladder Widths"); plt.ylabel("Width (µm)")
img_bytes = io.BytesIO()
plt.tight_layout()
plt.savefig(img_bytes, format='png', bbox_inches='tight')
plt.close(fig)
img_bytes.seek(0)
return img_bytes.getvalue()
_EXCEL_FORBIDDEN = str.maketrans('', '', r'/\?*[]:'+"'")
_EXCEL_MAX_SHEET_NAME = 31
_FILENAME_FORBIDDEN = str.maketrans('', '', r'/\?*[]:<>|"')
def _sanitize_sheet_name(name: str, default: str = "Fish Data") -> str:
name = (name or "").strip().translate(_EXCEL_FORBIDDEN)
return name[:_EXCEL_MAX_SHEET_NAME] if name else default
def _sanitize_filename(name: str, default: str = "Fish Data") -> str:
name = (name or "").strip().translate(_FILENAME_FORBIDDEN)
return name if name else default
def write_lengths_to_excel_bytes(
filenames,
fish_lengths,
curvatures,
ratios,
eye_areas,
edema_areas,
threshold_used,
threshold_value,
boxplot_png_bytes,
sheet_name: str = "Fish Data",
exclusions=None,
eye_widths=None,
eye_heights=None,
swim_areas=None,
swim_widths=None,
):
EXCLUDED = "Excluded"
exclusions = exclusions or {}
def _is_included(idx, metric):
return exclusions.get(idx, {}).get(metric, True)
wb = openpyxl.Workbook()
sh = wb.active
sh.title = _sanitize_sheet_name(sheet_name)
header = ["Filename"]
if fish_lengths: header.append("Fish Length (µm)")
if curvatures: header.append("Curvature")
if ratios: header.append("Length/Straight Line Ratio")
if eye_areas: header.append("Eye Area (µm²)")
if eye_widths: header.append("Eye Width / Horizontal Ø (µm)")
if eye_heights: header.append("Eye Height / Vertical Ø (µm)")
if edema_areas: header.append("Edema Area (µm²)")
if swim_areas: header.append("Swim Bladder Area (µm²)")
if swim_widths: header.append("Swim Bladder Width (µm)")
sh.append(header)
for i, fname in enumerate(filenames):
row = [fname]
if fish_lengths:
L = fish_lengths[i] if i < len(fish_lengths) and fish_lengths[i] is not None else "N/A"
row.append(L if _is_included(i, 'fish_length') else EXCLUDED)
if curvatures:
c = curvatures[i] if i < len(curvatures) else None
if c is None:
c = "N/A"
elif c == 5:
c = "Not Classified"
row.append(c if _is_included(i, 'curvature') else EXCLUDED)
if ratios:
r = ratios[i] if i < len(ratios) and ratios[i] is not None else "N/A"
row.append(r if _is_included(i, 'ratio') else EXCLUDED)
if eye_areas:
ea = eye_areas[i] if i < len(eye_areas) and eye_areas[i] is not None else "N/A"
row.append(ea if _is_included(i, 'eye_area') else EXCLUDED)
if eye_widths:
ew = eye_widths[i] if i < len(eye_widths) and eye_widths[i] is not None else "N/A"
row.append(ew if _is_included(i, 'eye_area') else EXCLUDED)
if eye_heights:
eh = eye_heights[i] if i < len(eye_heights) and eye_heights[i] is not None else "N/A"
row.append(eh if _is_included(i, 'eye_area') else EXCLUDED)
if edema_areas:
eda = edema_areas[i] if i < len(edema_areas) and edema_areas[i] is not None else "N/A"
row.append(eda if _is_included(i, 'edema_area') else EXCLUDED)
if swim_areas:
sa = swim_areas[i] if i < len(swim_areas) and swim_areas[i] is not None else "N/A"
row.append(sa if _is_included(i, 'swim_area') else EXCLUDED)
if swim_widths:
sw = swim_widths[i] if i < len(swim_widths) and swim_widths[i] is not None else "N/A"
row.append(sw if _is_included(i, 'swim_area') else EXCLUDED)
sh.append(row)
def _stats(vals, metric_key):
clean_vals = np.array([
float(v) for idx, v in enumerate(vals or [])
if _is_included(idx, metric_key)
and isinstance(v, (int, float)) and np.isfinite(v)
])
if len(clean_vals) == 0:
return ("N/A",) * 5
return (
np.median(clean_vals),
np.percentile(clean_vals, 25),
np.percentile(clean_vals, 75),
np.mean(clean_vals),
np.std(clean_vals),
)
sh.append([])
if threshold_used:
sh.append([f"Threshold used; statistics may be unreliable (threshold: {threshold_value})"])
# Note on excluded metrics
excluded_counts = {}
for metric in ('fish_length', 'curvature', 'ratio', 'eye_area', 'edema_area', 'swim_area'):
excluded_counts[metric] = sum(
1 for i in range(len(filenames)) if not _is_included(i, metric)
)
excl_note_parts = [f"{k.replace('_', ' ')}: {v}" for k, v in excluded_counts.items() if v > 0]
if excl_note_parts:
sh.append(["Excluded from statistics — " + ", ".join(excl_note_parts)])
sh.append(["Statistics (excluded values not counted)"])
if fish_lengths:
medL,p25L,p75L,meanL,stdL = _stats(fish_lengths, 'fish_length')
sh.append(["Median Length (µm)", medL]); sh.append(["25th Percentile Length (µm)", p25L])
sh.append(["75th Percentile Length (µm)", p75L]); sh.append(["Mean Length (µm)", meanL])
sh.append(["Standard Deviation Length (µm)", stdL])
if curvatures:
medC,p25C,p75C,meanC,stdC = _stats(curvatures, 'curvature')
sh.append(["Median Curvature", medC]); sh.append(["25th Percentile Curvature", p25C])
sh.append(["75th Percentile Curvature", p75C]); sh.append(["Mean Curvature", meanC])
sh.append(["Standard Deviation Curvature", stdC])
if ratios:
medR,p25R,p75R,meanR,stdR = _stats(ratios, 'ratio')
sh.append(["Median Ratio", medR]); sh.append(["25th Percentile Ratio", p25R])
sh.append(["75th Percentile Ratio", p75R]); sh.append(["Mean Ratio", meanR])
sh.append(["Standard Deviation Ratio", stdR])
if eye_areas:
medEA,p25EA,p75EA,meanEA,stdEA = _stats(eye_areas, 'eye_area')
sh.append(["Median Eye Area (µm²)", medEA]); sh.append(["25th Percentile Eye Area (µm²)", p25EA])
sh.append(["75th Percentile Eye Area (µm²)", p75EA]); sh.append(["Mean Eye Area (µm²)", meanEA])
sh.append(["Standard Deviation Eye Area (µm²)", stdEA])
if eye_widths:
medEW,p25EW,p75EW,meanEW,stdEW = _stats(eye_widths, 'eye_area')
sh.append(["Median Eye Width (µm)", medEW]); sh.append(["25th Percentile Eye Width (µm)", p25EW])
sh.append(["75th Percentile Eye Width (µm)", p75EW]); sh.append(["Mean Eye Width (µm)", meanEW])
sh.append(["Standard Deviation Eye Width (µm)", stdEW])
if eye_heights:
medEH,p25EH,p75EH,meanEH,stdEH = _stats(eye_heights, 'eye_area')
sh.append(["Median Eye Height (µm)", medEH]); sh.append(["25th Percentile Eye Height (µm)", p25EH])
sh.append(["75th Percentile Eye Height (µm)", p75EH]); sh.append(["Mean Eye Height (µm)", meanEH])
sh.append(["Standard Deviation Eye Height (µm)", stdEH])
if edema_areas:
medEDA,p25EDA,p75EDA,meanEDA,stdEDA = _stats(edema_areas, 'edema_area')
sh.append(["Median Edema Area (µm²)", medEDA]); sh.append(["25th Percentile Edema Area (µm²)", p25EDA])
sh.append(["75th Percentile Edema Area (µm²)", p75EDA]); sh.append(["Mean Edema Area (µm²)", meanEDA])
sh.append(["Standard Deviation Edema Area (µm²)", stdEDA])
if swim_areas:
medSA,p25SA,p75SA,meanSA,stdSA = _stats(swim_areas, 'swim_area')
sh.append(["Median Swim Bladder Area (µm²)", medSA]); sh.append(["25th Percentile Swim Bladder Area (µm²)", p25SA])
sh.append(["75th Percentile Swim Bladder Area (µm²)", p75SA]); sh.append(["Mean Swim Bladder Area (µm²)", meanSA])
sh.append(["Standard Deviation Swim Bladder Area (µm²)", stdSA])
if swim_widths:
medSW,p25SW,p75SW,meanSW,stdSW = _stats(swim_widths, 'swim_area')
sh.append(["Median Swim Bladder Width (µm)", medSW]); sh.append(["25th Percentile Swim Bladder Width (µm)", p25SW])
sh.append(["75th Percentile Swim Bladder Width (µm)", p75SW]); sh.append(["Mean Swim Bladder Width (µm)", meanSW])
sh.append(["Standard Deviation Swim Bladder Width (µm)", stdSW])
sh.append([]); sh.append(["Class Distribution"])
cls_counts = [0,0,0,0,0]
for idx, c in enumerate(curvatures):
if not _is_included(idx, 'curvature') or c is None:
continue
i_cls = 4 if c == 5 else int(c)-1
if 0 <= i_cls < 5:
cls_counts[i_cls] += 1
labels = ["Class 1","Class 2","Class 3","Class 4","Not Classified"]
for i,lbl in enumerate(labels):
sh.append([f"{lbl}", cls_counts[i]])
if boxplot_png_bytes:
img_stream = io.BytesIO(boxplot_png_bytes)
img = ExcelImage(img_stream); sh.add_image(img, "E2")
buf = io.BytesIO(); wb.save(buf); buf.seek(0); return buf
def _normalize_mask(mask: np.ndarray) -> np.ndarray:
m = _to_numpy(mask).astype(np.float32)
if m.ndim == 3 and m.shape[-1] == 3: m = m[...,0]
if m.max() <= 1.0: m = (m > 0.5).astype(np.uint8) * 255
else: m = (m > 127).astype(np.uint8) * 255
return m
GALLERY_MASK_ALPHA = 0.45
MANUAL_MASK_ALPHA = 0.15
MAX_EDITOR_PX = 800 # max display dimension for the mask editor (memory optimisation)
GALLERY_MAX_PX = 900 # max display dimension for gallery thumbnails (browser memory optimisation)
def _make_seg_overlay(original_img, seg_mask, path_points=None, straight_line_points=None, eye_mask=None, edema_mask=None, swimbladder_mask=None, swim_width_line=None, eye_width_line=None, eye_height_line=None, mask_alpha=GALLERY_MASK_ALPHA, draw_eye_diameters=True, max_px=None) -> np.ndarray:
base = _to_numpy(original_img); mask = _normalize_mask(seg_mask)
if base.ndim == 2: base = np.stack([base]*3, axis=-1)
if mask.shape[:2] != base.shape[:2]:
mask = np.array(PILImage.fromarray(mask).resize((base.shape[1], base.shape[0]), resample=PILImage.NEAREST))
overlay = base.copy().astype(np.float32)
# fish mask overlay in yellow
alpha = float(np.clip(mask_alpha, 0.0, 1.0))
yellow = np.zeros_like(overlay)
yellow[..., 0] = 255
yellow[..., 1] = 255
m = (mask > 0)[..., None].astype(np.float32)
overlay = overlay * (1 - alpha * m) + yellow * (alpha * m)
if eye_mask is not None:
eye_norm = _normalize_mask(eye_mask)
if eye_norm.shape[:2] != base.shape[:2]:
eye_norm = np.array(PILImage.fromarray(eye_norm).resize((base.shape[1], base.shape[0]), resample=PILImage.NEAREST))
red = np.zeros_like(overlay)
red[..., 0] = 255
em = (eye_norm > 0)[..., None].astype(np.float32)
overlay = overlay * (1 - 0.35 * em) + red * (0.35 * em)
if edema_mask is not None:
edema_norm = _normalize_mask(edema_mask)
if edema_norm.shape[:2] != base.shape[:2]:
edema_norm = np.array(PILImage.fromarray(edema_norm).resize((base.shape[1], base.shape[0]), resample=PILImage.NEAREST))
blue = np.zeros_like(overlay)
blue[..., 2] = 255
edm = (edema_norm > 0)[..., None].astype(np.float32)
overlay = overlay * (1 - 0.4 * edm) + blue * (0.4 * edm)
if swimbladder_mask is not None:
swim_norm = _normalize_mask(swimbladder_mask)
if swim_norm.shape[:2] != base.shape[:2]:
swim_norm = np.array(PILImage.fromarray(swim_norm).resize((base.shape[1], base.shape[0]), resample=PILImage.NEAREST))
pink = np.zeros_like(overlay)
pink[..., 0] = 255
pink[..., 1] = 105
pink[..., 2] = 180
swm = (swim_norm > 0)[..., None].astype(np.float32)
overlay = overlay * (1 - 0.4 * swm) + pink * (0.4 * swm)
overlay = overlay.clip(0,255).astype(np.uint8)
h_mask, w_mask = _normalize_mask(seg_mask).shape[:2]
h_base, w_base = overlay.shape[:2]
sy = h_base / float(max(1, h_mask))
sx = w_base / float(max(1, w_mask))
if path_points is not None:
try:
p = np.asarray(path_points)
if p.ndim == 2 and p.shape[1] == 2 and len(p) >= 2:
pts = np.stack([
np.clip(np.round(p[:, 1] * sx), 0, w_base - 1),
np.clip(np.round(p[:, 0] * sy), 0, h_base - 1),
], axis=1).astype(np.int32)
# dark outline for contrast, then bright cyan on top
cv2.polylines(overlay, [pts], isClosed=False, color=(0, 0, 0), thickness=6, lineType=cv2.LINE_AA)
cv2.polylines(overlay, [pts], isClosed=False, color=(0, 255, 255), thickness=3, lineType=cv2.LINE_AA)
except Exception:
pass
if straight_line_points is not None:
try:
(r1, c1), (r2, c2) = straight_line_points
p1 = (int(np.clip(round(c1 * sx), 0, w_base - 1)), int(np.clip(round(r1 * sy), 0, h_base - 1)))
p2 = (int(np.clip(round(c2 * sx), 0, w_base - 1)), int(np.clip(round(r2 * sy), 0, h_base - 1)))
# dark outline for contrast, then bright magenta on top
cv2.line(overlay, p1, p2, (0, 0, 0), 6, lineType=cv2.LINE_AA)
cv2.line(overlay, p1, p2, (255, 0, 255), 3, lineType=cv2.LINE_AA)
except Exception:
pass
if draw_eye_diameters:
for line in (eye_width_line, eye_height_line):
if line is None:
continue
try:
(r1, c1), (r2, c2) = line
p1 = (int(np.clip(round(c1 * sx), 0, w_base - 1)), int(np.clip(round(r1 * sy), 0, h_base - 1)))
p2 = (int(np.clip(round(c2 * sx), 0, w_base - 1)), int(np.clip(round(r2 * sy), 0, h_base - 1)))
cv2.line(overlay, p1, p2, (0, 0, 0), 4, lineType=cv2.LINE_AA)
cv2.line(overlay, p1, p2, (0, 255, 0), 2, lineType=cv2.LINE_AA)
except Exception:
pass
if swim_width_line is not None:
try:
(r1, c1), (r2, c2) = swim_width_line
p1 = (int(np.clip(round(c1 * sx), 0, w_base - 1)), int(np.clip(round(r1 * sy), 0, h_base - 1)))
p2 = (int(np.clip(round(c2 * sx), 0, w_base - 1)), int(np.clip(round(r2 * sy), 0, h_base - 1)))
# dark outline for contrast, then bright green on top (measurement-line convention)
cv2.line(overlay, p1, p2, (0, 0, 0), 4, lineType=cv2.LINE_AA)
cv2.line(overlay, p1, p2, (0, 255, 0), 2, lineType=cv2.LINE_AA)
except Exception:
pass
if max_px is not None:
h, w = overlay.shape[:2]
if max(h, w) > max_px:
scale = max_px / max(h, w)
new_w, new_h = max(1, int(round(w * scale))), max(1, int(round(h * scale)))
overlay = cv2.resize(overlay, (new_w, new_h), interpolation=cv2.INTER_AREA)
return overlay # Full resolution unless max_px is given
def _shorten_name(name: str, max_chars: int = 22) -> str:
base = os.path.basename(name)
if len(base) <= max_chars: return base
root, ext = os.path.splitext(base)
keep = max_chars - len(ext) - 3
if keep <= 0: return base[:max(1, max_chars-3)] + '...'
head = keep // 2; tail = keep - head
return f"{root[:head]}...{root[-tail:]}{ext}"
def _stage_inputs(files: Optional[List[gr.File]], folder_input) -> Tuple[str, list, Optional[str]]:
"""
Normalize inputs into a working directory with all images inside, and a
sorted list of filenames (basenames) that match what will be processed.
- If `folder_input` is a list/tuple of paths (Gradio folder upload), copy ALL
of them into a temp dir and return that dir + filenames.
- If `folder_input` is a string path to a directory, enumerate it.
- Otherwise, fall back to `files` (individual uploads) and copy into a temp dir.
Returns (work_dir, filenames, tmpdir_to_clean): the third element is the temp
directory the caller should delete after use, or None when work_dir belongs to
the user (Case 2) and must not be removed.
"""
exts = {'.png', '.jpg', '.jpeg', '.tif', '.tiff', '.bmp'}
# Helper: extract plain file paths from a gradio payload item
def _get_path(x):
if isinstance(x, str):
return x
# Some gradio versions pass objects with `.name`
return getattr(x, "name", None)
# Case 1: Folder upload via list/tuple of paths
if isinstance(folder_input, (list, tuple)) and len(folder_input) > 0:
src_paths = []
for item in folder_input:
p = _get_path(item)
if p and os.path.isfile(p) and os.path.splitext(p)[1].lower() in exts:
src_paths.append(p)
if src_paths:
tmpdir = tempfile.mkdtemp()
basenames = []
for p in src_paths:
bn = os.path.basename(p)
dst = os.path.join(tmpdir, bn)
# If duplicate basenames (rare but possible), disambiguate
if os.path.exists(dst):
root, ext = os.path.splitext(bn)
k = 1
while os.path.exists(dst):
bn = f"{root}_{k}{ext}"
dst = os.path.join(tmpdir, bn)
k += 1
shutil.copy(p, dst)
basenames.append(bn)
basenames.sort()
return tmpdir, basenames, tmpdir
# Case 2: Folder upload as a single directory path (less common)
if isinstance(folder_input, str) and os.path.isdir(folder_input):
names = [n for n in os.listdir(folder_input)
if os.path.splitext(n)[1].lower() in exts]
names.sort()
return folder_input, names, None # user's own folder — do not delete
# Case 3: Individual files upload (UploadButton)
tmpdir = tempfile.mkdtemp()
filenames = []
if files:
for f in files:
p = _get_path(f)
if p and os.path.isfile(p) and os.path.splitext(p)[1].lower() in exts:
bn = os.path.basename(p)
dst = os.path.join(tmpdir, bn)
if os.path.exists(dst):
root, ext = os.path.splitext(bn)
k = 1
while os.path.exists(dst):
bn = f"{root}_{k}{ext}"
dst = os.path.join(tmpdir, bn)
k += 1
shutil.copy(p, dst)
filenames.append(bn)
filenames.sort()
return tmpdir, filenames, tmpdir
def _safe_float(s, default=None):
try:
if s is None: return default
if isinstance(s, (int, float)): return float(s)
s = str(s).strip()
if not s:
return default
# remove common thousands separators/spaces
s = s.replace("\u00A0", "") # non-breaking space
s = s.replace(" ", "")
s = s.replace("_", "")
s = s.replace("'", "")
# Handle locale-specific decimal/thousands separators
if "," in s and "." in s:
# Assume the last separator is the decimal separator
if s.rfind(",") > s.rfind("."):
s = s.replace(".", "")
s = s.replace(",", ".")
else:
s = s.replace(",", "")
elif "," in s:
s = s.replace(",", ".")
return float(s)
except Exception:
return default
def _get_first_image_path(folder_input, files) -> Optional[str]:
"""Return the path to the first image in whichever upload was provided."""
exts = {'.png', '.jpg', '.jpeg', '.tif', '.tiff', '.bmp'}
def _get_path(x):
if isinstance(x, str):
return x
return getattr(x, 'name', None)
# Folder upload (list of file paths)
if isinstance(folder_input, (list, tuple)) and len(folder_input) > 0:
paths = []
for item in folder_input:
p = _get_path(item)
if p and os.path.isfile(p) and os.path.splitext(p)[1].lower() in exts:
paths.append(p)
if paths:
return sorted(paths)[0]
# Folder upload (single directory path)
if isinstance(folder_input, str) and os.path.isdir(folder_input):
names = sorted([n for n in os.listdir(folder_input)
if os.path.splitext(n)[1].lower() in exts])
if names:
return os.path.join(folder_input, names[0])
# Individual file upload
if isinstance(files, (list, tuple)) and len(files) > 0:
for f in files:
p = _get_path(f)
if p and os.path.isfile(p) and os.path.splitext(p)[1].lower() in exts:
return p
return None
def _run_scalebar_detection(folder_input, files, bar_label_um_str=""):
"""
Detect the scale bar line from the first uploaded image and, if the user
has supplied the physical bar length, compute the full calibration.
Returns (preview_update, status_md, bar_px_update, phys_w_update, phys_h_update)
"""
no_img_update = gr.update(visible=False)
first_path = _get_first_image_path(folder_input, files)
if first_path is None:
return (no_img_update,
"Upload images first, then click **Detect Scale Bar**.",
gr.update(), gr.update(), gr.update())
# Load image
try:
img_bgr = cv2.imread(first_path, cv2.IMREAD_COLOR)
if img_bgr is None:
pil = PILImage.open(first_path).convert('RGB')
img_rgb = np.array(pil)
else:
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
except Exception as e:
return (no_img_update, f"⚠ Could not load image: {e}",
gr.update(), gr.update(), gr.update())
if not _HAS_SCALEBAR:
return (gr.update(value=img_rgb, visible=True),
"⚠ `scalebar` module could not be imported.",
gr.update(), gr.update(), gr.update())
label_um = _safe_float(bar_label_um_str, default=None)
result = _detect_scalebar(img_rgb, label_um=label_um)
debug_img = result.get('debug_img') if result.get('debug_img') is not None else img_rgb
bar_px = result.get('bar_length_px')
bar_px_str = str(bar_px) if bar_px is not None else ""
if result['success']:
phys_w = f"{result['phys_width_um']:.1f}"
phys_h = f"{result['phys_height_um']:.1f}"
status = f"✅ {result['message']}"
return (gr.update(value=debug_img, visible=True),
status,
gr.update(value=bar_px_str),
gr.update(value=phys_w),
gr.update(value=phys_h))
elif result['bar_found']:
status = (
f"📏 Scale bar line detected: **{bar_px} px**. "
f"Enter its physical length in the field below, then click **Apply**."
)
return (gr.update(value=debug_img, visible=True),
status,
gr.update(value=bar_px_str),
gr.update(), gr.update())
else:
status = f"⚠ **Detection failed:** {result['message']}"
return (gr.update(value=debug_img, visible=True),
status,
gr.update(value=""),
gr.update(), gr.update())
def _load_manual_scalebar_image(folder_input, files):
"""Load the first uploaded image for manual scale bar endpoint selection."""
first_path = _get_first_image_path(folder_input, files)
if first_path is None:
return None, [], "Upload images first."
try:
img_bgr = cv2.imread(first_path, cv2.IMREAD_COLOR)
if img_bgr is None:
pil = PILImage.open(first_path).convert('RGB')
img_rgb = np.array(pil)
else:
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
except Exception as e:
return None, [], f"Could not load image: {e}"
return img_rgb, [], "Click to set **START** point (one end of scale bar)."
def _record_scalebar_click(evt: gr.SelectData, current_img, sb_points):
"""Record a click for manual scale bar endpoint selection."""
if current_img is None:
return sb_points, current_img, "Click **Load Image** first."
if not (hasattr(evt, 'index') and evt.index is not None):
return sb_points, current_img, "No click coordinates received."
if isinstance(evt.index, (list, tuple)) and len(evt.index) >= 2:
click_x, click_y = int(evt.index[0]), int(evt.index[1])
else:
return sb_points, current_img, "Invalid click coordinates."
if sb_points is None:
sb_points = []
sb_points = list(sb_points)
if len(sb_points) >= 2:
return sb_points, current_img, "⚠ Both endpoints already set. Click **Reset Points** to start over."
sb_points.append((click_x, click_y))
img_with_points = _draw_scalebar_endpoints(current_img, sb_points) if _HAS_SCALEBAR else np.array(current_img).copy()
if len(sb_points) == 2:
cal = _calibrate_from_endpoints(sb_points[0], sb_points[1], np.array(current_img).shape) if _HAS_SCALEBAR else {}
dist_px = cal.get('bar_length_px', 0.0) or 0.0
status = (
f"✓ Both endpoints set ({dist_px:.1f} px apart). "
"Enter the physical length in **Physical length of scale bar (µm)** above, "
"then click **Apply Manual Points**."
)
else:
status = "✓ START point set (green). Now click the other end of the scale bar (END, red)."
return sb_points, img_with_points, status
def _reset_scalebar_points(folder_input, files):
"""Reset manual scale bar points and reload the original image."""
first_path = _get_first_image_path(folder_input, files)
if first_path is None:
return [], None, "Upload images first."
try:
img_bgr = cv2.imread(first_path, cv2.IMREAD_COLOR)
if img_bgr is None:
pil = PILImage.open(first_path).convert('RGB')
img_rgb = np.array(pil)
else:
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
except Exception as e:
return [], None, f"Could not reload image: {e}"
return [], img_rgb, "Points reset. Click to set START point."
def _apply_scalebar_points(sb_points, bar_label_um_str, folder_input, files):
"""Compute µm/px calibration from two manually placed scale bar endpoints."""
if sb_points is None or len(sb_points) != 2:
return gr.update(), "⚠ Need exactly 2 points. Click on the image to set START and END.", gr.update(), gr.update()
# Retrieve image shape for physical size computation
img_shape = (0, 0)
first_path = _get_first_image_path(folder_input, files)
if first_path:
try:
img_bgr = cv2.imread(first_path, cv2.IMREAD_COLOR)
if img_bgr is not None:
img_shape = img_bgr.shape[:2]
else:
pil = PILImage.open(first_path)
img_shape = (pil.size[1], pil.size[0])
except Exception:
pass
label_um = _safe_float(bar_label_um_str, default=None)
if not _HAS_SCALEBAR:
return gr.update(), "⚠ scalebar module unavailable.", gr.update(), gr.update()
result = _calibrate_from_endpoints(sb_points[0], sb_points[1], img_shape, label_um=label_um)
bar_px_str = f"{result['bar_length_px']:.1f}" if result.get('bar_length_px') is not None else ""
if not result['bar_found']:
return gr.update(), f"⚠ {result['message']}", gr.update(), gr.update()
if result['success']:
phys_w = f"{result['phys_width_um']:.1f}"
phys_h = f"{result['phys_height_um']:.1f}"
return gr.update(value=bar_px_str), f"✅ {result['message']}", gr.update(value=phys_w), gr.update(value=phys_h)
else:
return gr.update(value=bar_px_str), f"📏 {result['message']}", gr.update(), gr.update()
def process(folder,
files: Optional[List[gr.File]],
seg_model_choice="General Model",
use_finetuned_desy=False,
process_curvature=True,
process_length=True,
process_ratio=True,
process_eye_size=True,
process_edema=True,
process_swimbladder=True,
use_threshold=False,
threshold_value=0.5,
physical_horizontal_um_str="",
physical_vertical_um_str=""):
t0 = time.perf_counter()
work_dir, filenames, _tmpdir_to_clean = _stage_inputs(files, folder)
# Resolve chosen segmentation model (fine-tuned DESY checkbox overrides the preset radio)
if use_finetuned_desy:
seg_model_choice = "Fine-tuned DESY"
cfg = SEG_MODEL_OPTIONS.get(seg_model_choice, SEG_MODEL_OPTIONS["Fast & Easy (256 px, ~2s/image)"])
model_target_size = cfg["target_size"]
seg_filename, seg_encoder, seg_model_type = cfg["body"]
# Build kwargs for eye/edema/swimbladder models (use pipeline defaults when the entry is None)
eye_kwargs = {}
if cfg["eye"] is not None:
eye_filename, eye_encoder, eye_model_type = cfg["eye"]
eye_kwargs = {
"eye_model_filename": eye_filename,
"eye_encoder_name": eye_encoder,
"eye_model_type": eye_model_type,
}
edema_kwargs = {}
if cfg["edema"] is not None:
edema_filename, edema_encoder, edema_model_type = cfg["edema"]
edema_kwargs = {
"edema_model_filename": edema_filename,
"edema_encoder_name": edema_encoder,
"edema_model_type": edema_model_type,
}
swimbladder_kwargs = {}
if cfg["swimbladder"] is not None:
swimbladder_filename, swimbladder_encoder, swimbladder_model_type = cfg["swimbladder"]
swimbladder_kwargs = {
"swimbladder_model_filename": swimbladder_filename,
"swimbladder_encoder_name": swimbladder_encoder,
"swimbladder_model_type": swimbladder_model_type,
}
# Pass sorted file paths so segmentation results match the sorted filenames list
file_paths_sorted = [os.path.join(work_dir, fn) for fn in filenames]
# Always load eyes for overlay visualization; load edema/swim bladder if requested
try:
pipeline_kwargs = dict(
file_list=file_paths_sorted,
target_size=(model_target_size, model_target_size),
include_eyes=True,
body_model_filename=seg_filename,
body_encoder_name=seg_encoder,
body_model_type=seg_model_type,
**eye_kwargs,
)
if process_edema:
pipeline_kwargs["include_edema"] = True
pipeline_kwargs.update(edema_kwargs)
if process_swimbladder:
pipeline_kwargs["include_swimbladder"] = True
pipeline_kwargs.update(swimbladder_kwargs)
result = segmentation_pipeline(**pipeline_kwargs)
original_images, segmented_images, grown_images, eyes_images = result[:4]
extra = list(result[4:]) # always ordered: [edema?] [swimbladder?]
edema_images = extra.pop(0) if process_edema else [None] * len(original_images)
swimbladder_images = extra.pop(0) if process_swimbladder else [None] * len(original_images)
finally:
if _tmpdir_to_clean:
shutil.rmtree(_tmpdir_to_clean, ignore_errors=True)
model = _ensure_model()
# Parse physical distances (µm) for full image width/height from user
phys_w_um_user = _safe_float(physical_horizontal_um_str, default=None)
phys_h_um_user = _safe_float(physical_vertical_um_str, default=None)
if phys_w_um_user is not None and phys_h_um_user is not None:
y_scale_info = phys_h_um_user / model_target_size
x_scale_info = phys_w_um_user / model_target_size
spacing_info_md = (
f"**Spacing used:** custom input | "
f"y = {y_scale_info:.4f} µm/pixel, x = {x_scale_info:.4f} µm/pixel "
f"(from H={phys_h_um_user:g} µm, W={phys_w_um_user:g} µm over {model_target_size} px)"
)
else:
# No scale bar set: fall back to a 5885 µm reference width, scaling the
# height to match each image's actual pixel aspect ratio. A square
# H=W=5885 assumption would otherwise skew any fish-axis-aligned
# measurement (eye/swim-bladder width & height lines) on non-square
# images, since the model mask is always square (256x256) regardless
# of the original image's shape.
if original_images:
h0, w0 = original_images[0].shape[:2]
x_scale_info = 5885.0 / model_target_size
y_scale_info = x_scale_info * (h0 / w0)
else:
y_scale_info = 5885.0 / model_target_size
x_scale_info = 5885.0 / model_target_size
spacing_info_md = (
f"**Spacing used:** default calibration | "
f"y = {y_scale_info:.4f} µm/pixel, x = {x_scale_info:.4f} µm/pixel "
f"(W=5885 µm, H scaled to match each image's aspect ratio, over {model_target_size} px)"
)
fish_lengths, curvatures, ratios, eye_areas, edema_areas, previews = [], [], [], [], [], []
eye_widths, eye_heights = [], []
swim_areas, swim_widths = [], []
paths, straight_lines = [], [] # stored per-image for gallery overlay regeneration
swim_width_lines = []
eye_width_lines, eye_height_lines = [], []
for i, seg_mask in enumerate(segmented_images):
path_points = None
straight_line_points = None
eye_mask_for_vis = eyes_images[i] if i < len(eyes_images) else None
edema_mask_for_vis = edema_images[i] if i < len(edema_images) else None
swimbladder_mask_for_vis = swimbladder_images[i] if i < len(swimbladder_images) else None
seg_mask_bin = seg_mask > 0
# Per-image pixel scales derived from user-provided physical distances
# Default to pixel units if user did not provide values
if phys_w_um_user is not None and phys_h_um_user is not None:
phys_w_um = phys_w_um_user
phys_h_um = phys_h_um_user
# Calculate spacing for the new function: (dy, dx) in physical units per pixel
y_scale = phys_h_um / model_target_size # physical units per pixel in y direction
x_scale = phys_w_um / model_target_size # physical units per pixel in x direction
else:
# No scale bar set: use a 5885 µm reference width and scale the
# height to this image's actual pixel aspect ratio (the model
# mask is always square, but the source image usually isn't) so
# fish-axis-aligned measurement lines still render at the
# correct angle without requiring calibration.
orig_h, orig_w = original_images[i].shape[:2]
x_scale = 5885.0 / model_target_size
y_scale = x_scale * (orig_h / orig_w)
phys_w_um = 5885.0
phys_h_um = y_scale * model_target_size
if process_length:
# Use the new tube_length_border2border function
try:
eye_mask_for_length = (eye_mask_for_vis > 0) if eye_mask_for_vis is not None else None
spacing = (y_scale, x_scale)
# Use eye mask when available to stabilize head-side start point.
length, straight_length, path_points, straight_line_points = tube_length_border2border(
seg_mask_bin,
spacing=spacing,
return_path=True,
return_straight_line=True,
mask_eye=eye_mask_for_length,
return_eye_info=False,
)
fish_lengths.append(float(length))
# Calculate ratio only if checkbox is enabled
if process_ratio:
# Calculate ratio, avoiding division by zero
if straight_length > 0:
ratio = float(length) / float(straight_length)
else:
ratio = 0.0
ratios.append(ratio)
except Exception as e:
print(f"Error calculating length for image {i}: {e}")
fish_lengths.append(None)
if process_ratio:
ratios.append(None)
eye_width_line = None
eye_height_line = None
if process_eye_size:
try:
eye_mask_for_metrics = (eye_mask_for_vis > 0) if eye_mask_for_vis is not None else None
eye_info = compute_eye_metrics(
eye_mask_for_metrics,
mask_fish=seg_mask_bin,
spacing=(y_scale, x_scale),
)
eye_areas.append(float(eye_info.get("eye_area", 0.0)))
dia = compute_eye_diameters(eye_mask_for_metrics, spacing=(y_scale, x_scale), mask_fish=seg_mask_bin)
eye_widths.append(float(dia.get("eye_width_um", 0.0)))
eye_heights.append(float(dia.get("eye_height_um", 0.0)))
eye_width_line = dia.get("eye_width_line")
eye_height_line = dia.get("eye_height_line")
except Exception as e:
print(f"Error calculating eye metrics for image {i}: {e}")
eye_areas.append(None)
eye_widths.append(None)
eye_heights.append(None)
eye_width_lines.append(eye_width_line)
eye_height_lines.append(eye_height_line)
if process_edema:
try:
edema_mask_bin = (edema_mask_for_vis > 0) if edema_mask_for_vis is not None else None
edema_info = compute_eye_metrics(
edema_mask_bin,
mask_fish=None,
spacing=(y_scale, x_scale),