-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapReader.py
More file actions
765 lines (647 loc) · 27.1 KB
/
MapReader.py
File metadata and controls
765 lines (647 loc) · 27.1 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
import marimo
__generated_with = "0.20.2"
app = marimo.App(width="medium")
@app.cell
def _():
import marimo as mo
import os
import subprocess
import sys
import random
# All third-party dependencies in one place
_deps = {
"numpy": "numpy",
"PIL": "pillow",
"polars": "polars",
"rasterio": "rasterio",
"leafmap": "leafmap",
"localtileserver": "localtileserver",
"plotly": "plotly",
"cv2": "opencv-python",
"easyocr": "easyocr",
"scipy": "scipy",
}
for _import_name, _pip_name in _deps.items():
try:
__import__(_import_name)
except ImportError:
try:
subprocess.run(
[sys.executable, "-m", "pip", "install", _pip_name],
capture_output=True,
)
except Exception:
pass
import numpy as np
from PIL import Image
import polars as pl
import rasterio
from rasterio.enums import Resampling
import leafmap
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import cv2
import easyocr
return (
Image,
Resampling,
cv2,
easyocr,
go,
leafmap,
make_subplots,
mo,
np,
os,
pl,
px,
random,
rasterio,
)
@app.cell
def _(Image, Resampling, np, os, random, rasterio):
def get_random_sample_tiffs(root_dir):
samples = {}
for root, dirs, files in os.walk(root_dir):
tiffs = [f for f in files if f.lower().endswith(('.tif', '.tiff'))]
if tiffs:
selected_tiff = random.choice(tiffs)
rel_path = os.path.relpath(root, root_dir)
samples[rel_path if rel_path != "." else "root"] = os.path.join(root, selected_tiff)
return samples
def downsample_tiff(path, scale_factor=0.05):
with rasterio.open(path) as src:
new_height = int(src.height * scale_factor)
new_width = int(src.width * scale_factor)
data = src.read(
out_shape=(src.count, new_height, new_width),
resampling=Resampling.bilinear
)
if src.count >= 3:
img_data = data[:3].transpose(1, 2, 0)
else:
img_data = data[0]
if img_data.dtype != np.uint8:
img_data = (255 * (img_data - img_data.min()) / (img_data.max() - img_data.min() + 1e-5)).astype(np.uint8)
return Image.fromarray(img_data)
tiffs_to_show = get_random_sample_tiffs("geotiffs")
return downsample_tiff, tiffs_to_show
@app.cell
def _(downsample_tiff, mo, os, tiffs_to_show):
# Display one downsampled image from each directory
visual_previews = []
for label, path in tiffs_to_show.items():
img = downsample_tiff(path)
visual_previews.append(
mo.vstack([
mo.md(f"**Category:** {label}"),
mo.md(f"*File:* {os.path.basename(path)}"),
mo.image(img)
])
)
mo.hstack(visual_previews) if visual_previews else mo.md("No TIFFs found.")
return
@app.cell
def _(os, pl, rasterio):
def audit_geotiff_collection(root_dir):
audit_results = []
for root, dirs, files in os.walk(root_dir):
tiffs = [f for f in files if f.lower().endswith(('.tif', '.tiff'))]
for f in tiffs:
path = os.path.join(root, f)
rel_dir = os.path.relpath(root, root_dir)
try:
with rasterio.open(path) as src:
# Determine color attributes
color_interp = [str(interp.name) for interp in src.colorinterp]
photometric = src.profile.get('photometric', 'N/A')
audit_results.append({
"Category": rel_dir if rel_dir != "." else "root",
"Filename": f,
"Width": src.width,
"Height": src.height,
"Bands": src.count,
"Color Space": photometric,
"Band Interp": ", ".join(color_interp),
"Dtype": src.dtypes[0],
"Projection (CRS)": str(src.crs.to_string()) if src.crs else "Ungeoreferenced",
"Units": src.crs.linear_units if src.crs else "N/A"
})
except Exception as e:
audit_results.append({
"Category": rel_dir,
"Filename": f,
"Error": str(e)
})
return pl.DataFrame(audit_results)
# Execute audit and display as a table
geotiff_audit = audit_geotiff_collection("geotiffs")
geotiff_audit
return
@app.cell
def _(downsample_tiff, leafmap, mo, os, rasterio, tiffs_to_show):
def create_individual_maps_with_images(tiffs_dict):
import base64
from io import BytesIO
map_widgets = {}
for label, path in tiffs_dict.items():
try:
with rasterio.open(path) as src:
# 1. Handle missing CRS
if not src.crs:
map_widgets[label] = mo.vstack([
mo.md(f"### Category: {label}"),
mo.md("⚠️ **No CRS found.** Cannot geolocate on basemap."),
mo.md(f"**Local Bounds:** {src.bounds}")
])
continue
# 2. Calculate WGS84 Bounds
bounds = src.bounds
from rasterio.warp import transform_bounds
wgs_bounds = transform_bounds(src.crs, 'EPSG:4326', *bounds)
# Leaflet uses [[lat_min, lon_min], [lat_max, lon_max]]
# transform_bounds returns (west, south, east, north)
# sw = [lat_min, lon_min], ne = [lat_max, lon_max]
sw = [wgs_bounds[1], wgs_bounds[0]]
ne = [wgs_bounds[3], wgs_bounds[2]]
leaflet_bounds = [sw, ne]
# 3. Create the map centered on the raster
center = [(sw[0] + ne[0]) / 2, (sw[1] + ne[1]) / 2]
m = leafmap.Map(center=center, zoom=11, height="500px")
# 4. Draw the Bounding Box using standard add_marker or similar if available,
# but for rectangles we use add_gdf or add_shape if we want polygons.
# However, for simplicity and compatibility across leafmap backends:
# We'll use add_image and check if it renders.
# 5. Generate and add the Raster Image Overlay
# Using a slightly higher scale factor for better visibility
pil_img = downsample_tiff(path, scale_factor=0.25)
# Convert PIL image to a data URL
buffered = BytesIO()
pil_img.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
data_url = f"data:image/png;base64,{img_str}"
# Add the image overlay
m.add_image(data_url, bounds=leaflet_bounds, layer_name=f"{label} Raster", opacity=0.6)
map_widgets[label] = mo.vstack([
mo.md(f"### Category: {label}"),
mo.md(f"**File:** {os.path.basename(path)}"),
m
])
except Exception as e:
map_widgets[label] = mo.md(f"**{label}**: Error rendering map: {e}")
return mo.tabs(map_widgets)
individual_map_views = create_individual_maps_with_images(tiffs_to_show)
individual_map_views
return
@app.cell
def _(downsample_tiff, make_subplots, mo, px, tiffs_to_show):
# preview current maps
_fig = make_subplots(rows=1, cols=len(tiffs_to_show), subplot_titles=list(tiffs_to_show.keys()))
for _idx, (_label, _path) in enumerate(tiffs_to_show.items()):
_img = downsample_tiff(_path, scale_factor=0.05)
_img_trace = px.imshow(_img).data[0]
_fig.add_trace(_img_trace, row=1, col=_idx + 1)
_fig.update_layout(height=400, title_text="Sample GeoTIFFs (Pan and Zoom)")
_fig.update_xaxes(showticklabels=False)
_fig.update_yaxes(showticklabels=False)
mo.ui.plotly(_fig)
return
@app.cell
def _(
cv2,
downsample_tiff,
go,
make_subplots,
mo,
np,
px,
rasterio,
tiffs_to_show,
):
from scipy.ndimage import map_coordinates
from scipy.signal import find_peaks
from rasterio.warp import transform_bounds, transform
from rasterio.transform import rowcol
# --- Helper functions ---
def detect_neatline(gray_img):
"""Find the map's inner boundary to exclude collar/margins."""
h, w = gray_img.shape
row_means = gray_img.mean(axis=1)
col_means = gray_img.mean(axis=0)
def _find_edge(profile, length):
"""Find strongest gradient in each half of a 1-D intensity profile."""
grad = np.abs(np.diff(profile.astype(float)))
mid = length // 2
lo = int(np.argmax(grad[:mid])) if grad[:mid].max() > 10 else int(length * 0.05)
hi_region = grad[mid:]
hi = mid + int(np.argmax(hi_region)) if hi_region.max() > 10 else int(length * 0.95)
return lo, hi
row_min, row_max = _find_edge(row_means, h)
col_min, col_max = _find_edge(col_means, w)
return row_min, row_max, col_min, col_max
def generate_crs_candidates(path, analysis_scale):
"""Generate candidate graticule lines at standard cartographic intervals."""
try:
with rasterio.open(path) as src:
if not src.crs:
return None
wgs_bounds = transform_bounds(src.crs, "EPSG:4326", *src.bounds)
lon_min, lat_min, lon_max, lat_max = wgs_bounds
lon_span = lon_max - lon_min
lat_span = lat_max - lat_min
full_transform = src.transform
map_crs = src.crs
full_h, full_w = src.height, src.width
except Exception:
return None
intervals = [10, 5, 2, 1, 0.5, 1/4, 1/6, 1/12]
candidates = []
for interval in intervals:
n_meridians = int(lon_span / interval) - 1
n_parallels = int(lat_span / interval) - 1
total = n_meridians + n_parallels
if total < 2 or total > 50:
continue
meridian_curves = []
first_lon = np.ceil(lon_min / interval) * interval
for i in range(n_meridians):
lon = first_lon + i * interval
if lon <= lon_min or lon >= lon_max:
continue
lats = np.linspace(lat_min, lat_max, 100)
lons = np.full_like(lats, lon)
try:
xs, ys = transform("EPSG:4326", map_crs, lons, lats)
rows, cols = rowcol(full_transform, xs, ys)
rows = np.array(rows, dtype=float) * analysis_scale
cols = np.array(cols, dtype=float) * analysis_scale
meridian_curves.append((rows, cols))
except Exception:
continue
parallel_curves = []
first_lat = np.ceil(lat_min / interval) * interval
for i in range(n_parallels):
lat = first_lat + i * interval
if lat <= lat_min or lat >= lat_max:
continue
lons = np.linspace(lon_min, lon_max, 100)
lats = np.full_like(lons, lat)
try:
xs, ys = transform("EPSG:4326", map_crs, lons, lats)
rows, cols = rowcol(full_transform, xs, ys)
rows = np.array(rows, dtype=float) * analysis_scale
cols = np.array(cols, dtype=float) * analysis_scale
parallel_curves.append((rows, cols))
except Exception:
continue
if meridian_curves or parallel_curves:
candidates.append((interval, meridian_curves, parallel_curves))
return candidates if candidates else None
def score_candidate(edge_img, neatline, meridians, parallels):
"""Score how well a candidate interval matches actual image edges."""
row_min, row_max, col_min, col_max = neatline
h, w = edge_img.shape
total_hits = 0
total_samples = 0
all_curves = list(meridians) + list(parallels)
if not all_curves:
return 0.0
for rows, cols in all_curves:
for offset in [-2, -1, 0, 1, 2]:
# For meridians (vertical), offset applies to cols; for parallels, to rows
sample_rows = np.clip(rows + offset * 0.5, 0, h - 1)
sample_cols = np.clip(cols + offset * 0.5, 0, w - 1)
# Only sample within neatline
mask = (sample_rows >= row_min) & (sample_rows <= row_max) & \
(sample_cols >= col_min) & (sample_cols <= col_max)
if mask.sum() == 0:
continue
vals = map_coordinates(edge_img, [sample_rows[mask], sample_cols[mask]], order=0)
total_hits += (vals > 0).sum()
total_samples += mask.sum()
if total_samples == 0:
return 0.0
edge_density = total_hits / total_samples
n_lines = len(all_curves)
coverage = min(1.0, n_lines / 4.0)
return edge_density * coverage
def detect_graticule_pixel_space(gray_img, edge_img, neatline):
"""Fallback for ungeoreferenced TIFFs — find regular spacing via FFT."""
row_min, row_max, col_min, col_max = neatline
cropped = edge_img[row_min:row_max, col_min:col_max]
if cropped.size == 0:
return [], []
# Project edges onto row and column axes
row_profile = cropped.mean(axis=1).astype(float)
col_profile = cropped.mean(axis=0).astype(float)
def _find_spacing(profile):
if len(profile) < 8:
return []
# FFT to find dominant periodic spacing
fft_vals = np.abs(np.fft.rfft(profile - profile.mean()))
# Ignore DC and very low frequencies (spacing > half the profile)
fft_vals[:2] = 0
dominant_freq_idx = np.argmax(fft_vals)
if dominant_freq_idx == 0:
return []
spacing = len(profile) / dominant_freq_idx
if spacing < 10 or spacing > len(profile) / 2:
return []
# Find actual peaks at roughly this spacing
min_dist = int(spacing * 0.5)
peaks, _ = find_peaks(profile, distance=max(1, min_dist), height=profile.mean())
if len(peaks) < 2:
return []
# Validate regularity: reject peaks deviating >30% from median spacing
diffs = np.diff(peaks)
if len(diffs) == 0:
return []
median_sp = np.median(diffs)
if median_sp == 0:
return []
regular = np.abs(diffs - median_sp) / median_sp < 0.3
# Keep only peaks connected by regular intervals
valid = [peaks[0]]
for i, is_reg in enumerate(regular):
if is_reg:
valid.append(peaks[i + 1])
return valid
h_positions = _find_spacing(row_profile)
v_positions = _find_spacing(col_profile)
# Convert from cropped coords back to full image coords
h_positions = [p + row_min for p in h_positions]
v_positions = [p + col_min for p in v_positions]
return h_positions, v_positions
def format_interval(deg):
"""Format degree value as human-readable string."""
minutes = deg * 60
if abs(deg - round(deg)) < 1e-6 and deg >= 1:
return f"{int(round(deg))}\u00b0"
if abs(minutes - round(minutes)) < 0.1:
return f"{int(round(minutes))}'"
return f"{deg:.4f}\u00b0"
# --- Main loop ---
_analysis_scale = 0.15
_display_scale = 0.05
_grid_fig = make_subplots(
rows=len(tiffs_to_show), cols=1,
subplot_titles=[f"Grid: {k}" for k in tiffs_to_show.keys()]
)
_summaries = []
for _idx, (_label, _path) in enumerate(tiffs_to_show.items()):
# Load at analysis and display scales
_img_analysis = np.array(downsample_tiff(_path, scale_factor=_analysis_scale))
_img_display = np.array(downsample_tiff(_path, scale_factor=_display_scale))
# Grayscale + Canny
if len(_img_analysis.shape) == 3:
_gray = cv2.cvtColor(_img_analysis, cv2.COLOR_RGB2GRAY)
else:
_gray = _img_analysis
_edges = cv2.Canny(_gray, 50, 150, apertureSize=3)
# Detect neatline
_neatline = detect_neatline(_gray)
# Try CRS-aware path
_method = None
_interval_str = "N/A"
_draw_curves = []
_line_color = "cyan"
_h_count = 0
_v_count = 0
_candidates = generate_crs_candidates(_path, _analysis_scale)
if _candidates:
_best_score = 0.0
_best = None
for _interval, _meridians, _parallels in _candidates:
_s = score_candidate(_edges, _neatline, _meridians, _parallels)
if _s > _best_score:
_best_score = _s
_best = (_interval, _meridians, _parallels)
if _best is not None and _best_score > 0.02:
_method = "CRS-aware"
_interval_str = format_interval(_best[0])
_line_color = "cyan"
# Collect curves for drawing (meridians + parallels)
for rows, cols in _best[1]:
_draw_curves.append((rows, cols))
_v_count += 1
for rows, cols in _best[2]:
_draw_curves.append((rows, cols))
_h_count += 1
# Pixel-space fallback
if _method is None:
_h_positions, _v_positions = detect_graticule_pixel_space(_gray, _edges, _neatline)
_method = "Pixel-space (FFT)"
_line_color = "red"
_h_count = len(_h_positions)
_v_count = len(_v_positions)
# Convert to curves for uniform drawing
a_h, a_w = _gray.shape
for y in _h_positions:
_draw_curves.append(
(np.array([y, y], dtype=float), np.array([0, a_w - 1], dtype=float))
)
for x in _v_positions:
_draw_curves.append(
(np.array([0, a_h - 1], dtype=float), np.array([x, x], dtype=float))
)
_summaries.append(
f"**{_label}**: {_method} | interval: {_interval_str} | "
f"{_h_count} parallels, {_v_count} meridians"
)
# --- Draw on plotly figure ---
_img_trace = px.imshow(_img_display).data[0]
_grid_fig.add_trace(_img_trace, row=_idx + 1, col=1)
_scale_ratio = _display_scale / _analysis_scale
_line_x = []
_line_y = []
for rows, cols in _draw_curves:
_line_x.extend((cols * _scale_ratio).tolist() + [None])
_line_y.extend((rows * _scale_ratio).tolist() + [None])
if _line_x:
_grid_fig.add_trace(
go.Scatter(
x=_line_x, y=_line_y, mode='lines',
line=dict(color=_line_color, width=2), showlegend=False
),
row=_idx + 1, col=1
)
_grid_fig.update_layout(
height=400 * len(tiffs_to_show),
title_text="CRS-Aware Graticule Detection"
)
_grid_fig.update_xaxes(showticklabels=False)
_grid_fig.update_yaxes(showticklabels=False)
mo.vstack([
mo.md("## Graticule Extraction"),
mo.md("CRS-aware detection scores projected candidate grids against edge evidence. "
"Cyan lines = CRS-aware, Red lines = pixel-space FFT fallback."),
mo.md("\n\n".join(_summaries)),
mo.ui.plotly(_grid_fig)
])
return (
detect_graticule_pixel_space,
detect_neatline,
format_interval,
generate_crs_candidates,
score_candidate,
)
@app.cell
def _(mo, tiffs_to_show):
selected_grids = mo.ui.multiselect(
options=list(tiffs_to_show.keys()),
label="Select the categories/directories with good grid extractions:",
value=[]
)
selected_grids
return (selected_grids,)
@app.cell
def _(
cv2,
detect_graticule_pixel_space,
detect_neatline,
downsample_tiff,
format_interval,
generate_crs_candidates,
go,
mo,
np,
os,
px,
score_candidate,
selected_grids,
):
mo.stop(not selected_grids.value, mo.md("Please select at least one category to process all its maps."))
_batch_analysis_scale = 0.15
_batch_display_scale = 0.05
_batch_results = []
for _label in selected_grids.value:
_dir_path = "geotiffs" if _label == "root" else os.path.join("geotiffs", _label)
_tiffs = [f for f in os.listdir(_dir_path) if f.lower().endswith(('.tif', '.tiff'))]
for _tiff in _tiffs:
_path = os.path.join(_dir_path, _tiff)
# Load for analysis and display
_img_analysis = np.array(downsample_tiff(_path, scale_factor=_batch_analysis_scale))
_img_display = np.array(downsample_tiff(_path, scale_factor=_batch_display_scale))
if len(_img_analysis.shape) == 3:
_gray = cv2.cvtColor(_img_analysis, cv2.COLOR_RGB2GRAY)
else:
_gray = _img_analysis
_edges = cv2.Canny(_gray, 50, 150, apertureSize=3)
_neatline = detect_neatline(_gray)
_method = None
_interval_str = "N/A"
_h_count = 0
_v_count = 0
_draw_curves = []
_line_color = "cyan"
_candidates = generate_crs_candidates(_path, _batch_analysis_scale)
if _candidates:
_best_score = 0.0
_best = None
for _interval, _meridians, _parallels in _candidates:
_s = score_candidate(_edges, _neatline, _meridians, _parallels)
if _s > _best_score:
_best_score = _s
_best = (_interval, _meridians, _parallels)
if _best is not None and _best_score > 0.02:
_method = "CRS-aware"
_interval_str = format_interval(_best[0])
for _rows, _cols in _best[1]:
_draw_curves.append((_rows, _cols))
_v_count += 1
for _rows, _cols in _best[2]:
_draw_curves.append((_rows, _cols))
_h_count += 1
if _method is None:
_h_positions, _v_positions = detect_graticule_pixel_space(_gray, _edges, _neatline)
_method = "Pixel-space (FFT)"
_line_color = "red"
_h_count = len(_h_positions)
_v_count = len(_v_positions)
_a_h, _a_w = _gray.shape
for _y in _h_positions:
_draw_curves.append(
(np.array([_y, _y], dtype=float), np.array([0, _a_w - 1], dtype=float))
)
for _x in _v_positions:
_draw_curves.append(
(np.array([0, _a_h - 1], dtype=float), np.array([_x, _x], dtype=float))
)
# Build summary
_summary_text = f"**{_tiff}** ({_label}): {_method} | {_h_count} parallels, {_v_count} meridians | Interval: {_interval_str}"
# Build plot
_fig_batch = go.Figure()
_img_trace = px.imshow(_img_display).data[0]
_fig_batch.add_trace(_img_trace)
_scale_ratio = _batch_display_scale / _batch_analysis_scale
_line_x = []
_line_y = []
for _rows, _cols in _draw_curves:
_line_x.extend((_cols * _scale_ratio).tolist() + [None])
_line_y.extend((_rows * _scale_ratio).tolist() + [None])
if _line_x:
_fig_batch.add_trace(
go.Scatter(
x=_line_x, y=_line_y, mode='lines',
line=dict(color=_line_color, width=2), showlegend=False
)
)
_fig_batch.update_layout(
height=350,
margin=dict(l=0, r=0, t=10, b=0),
showlegend=False
)
_fig_batch.update_xaxes(showticklabels=False)
_fig_batch.update_yaxes(showticklabels=False)
_batch_results.append(
mo.vstack([
mo.md(_summary_text),
mo.ui.plotly(_fig_batch)
])
)
mo.vstack([
mo.md("### Batch Processing Results"),
mo.vstack(_batch_results)
]) if _batch_results else mo
return
@app.cell
def _(downsample_tiff, easyocr, mo, np, pl, tiffs_to_show):
# Initialize the reader with English and Simplified Chinese
_reader = easyocr.Reader(['en', 'ch_sim'])
_ocr_results = {}
for _ocr_label, _ocr_path in tiffs_to_show.items():
# Read the downsampled image as numpy array for easyocr
_img_pil = downsample_tiff(_ocr_path, scale_factor=0.5)
_img_np = np.array(_img_pil)
# Do OCR
_results = _reader.readtext(_img_np)
_parsed_results = []
for (_bbox, _text, _prob) in _results:
_stripped = _text.replace(" ", "")
# Determine the type of the block
if _stripped.isalpha():
_block_type = "character"
elif _stripped.isdigit():
_block_type = "number"
elif all(not c.isalnum() for c in _stripped) and _stripped:
_block_type = "symbol"
else:
_block_type = "mixed"
_parsed_results.append({
"Original Text": _text,
"Type": _block_type,
"Confidence": round(_prob, 2)
})
_ocr_results[_ocr_label] = pl.DataFrame(_parsed_results) if _parsed_results else pl.DataFrame({"Message": ["No text found"]})
# Display the results
_ocr_views = []
for _label_v, _df_v in _ocr_results.items():
_ocr_views.append(mo.vstack([
mo.md(f"### OCR Results: {_label_v}"),
mo.ui.table(_df_v)
]))
mo.vstack(_ocr_views)
return
if __name__ == "__main__":
app.run()