1use image::{imageops, imageops::FilterType, Rgb, RgbImage};
13
14pub const REC_HEIGHT: u32 = 48;
16
17pub const REC_MAX_WIDTH: u32 = 16384;
27
28pub const REC_BATCH: usize = 16;
32
33pub struct PrepLine {
36 pub w: usize,
38 pub data: Vec<f32>,
40}
41
42pub fn prep_line(line: &RgbImage) -> Option<PrepLine> {
44 let (w, h) = line.dimensions();
45 if w == 0 || h == 0 {
46 return None;
47 }
48 let new_w = ((w as f32) * REC_HEIGHT as f32 / h as f32)
49 .round()
50 .clamp(8.0, REC_MAX_WIDTH as f32) as u32;
51 let resized = imageops::resize(line, new_w, REC_HEIGHT, FilterType::Triangle);
52 let n = (REC_HEIGHT * new_w) as usize;
53 let mut data = vec![0f32; 3 * n];
55 for (i, px) in resized.pixels().enumerate() {
56 data[i] = px[0] as f32 / 127.5 - 1.0;
57 data[n + i] = px[1] as f32 / 127.5 - 1.0;
58 data[2 * n + i] = px[2] as f32 / 127.5 - 1.0;
59 }
60 Some(PrepLine {
61 w: new_w as usize,
62 data,
63 })
64}
65
66pub fn dict_chars(dict: &str) -> Vec<String> {
69 let mut chars = vec![String::new()]; chars.extend(dict.lines().map(|s| s.to_string()));
71 chars.push(" ".to_string());
72 chars
73}
74
75pub fn decode_row(chars: &[String], probs: &[f32], nc: usize) -> String {
77 decode_row_scored(chars, probs, nc).0
78}
79
80pub fn decode_row_scored(chars: &[String], probs: &[f32], nc: usize) -> (String, f32) {
85 let mut out = String::new();
86 let mut prev = 0usize;
87 let mut conf_sum = 0.0f32;
88 let mut conf_n = 0usize;
89 for row in probs.chunks_exact(nc) {
90 let mut best = 0usize;
91 let mut bestv = row[0];
92 for (c, &v) in row.iter().enumerate().skip(1) {
93 if v > bestv {
94 bestv = v;
95 best = c;
96 }
97 }
98 if best != prev && best != 0 {
99 if let Some(ch) = chars.get(best) {
100 out.push_str(ch);
101 conf_sum += bestv;
102 conf_n += 1;
103 }
104 }
105 prev = best;
106 }
107 let conf = if conf_n == 0 {
108 0.0
109 } else {
110 conf_sum / conf_n as f32
111 };
112 (out, conf)
113}
114
115pub(crate) fn luma(p: &Rgb<u8>) -> f32 {
116 0.299 * p[0] as f32 + 0.587 * p[1] as f32 + 0.114 * p[2] as f32
117}
118
119pub fn segment_lines(crop: &RgbImage) -> Vec<(u32, u32, u32, u32)> {
122 let (w, h) = crop.dimensions();
123 if w == 0 || h == 0 {
124 return Vec::new();
125 }
126 let mean: f32 = crop.pixels().map(luma).sum::<f32>() / (w * h) as f32;
127 let thresh = mean * 0.7; let min_ink = ((w as f32) * 0.005).max(1.0) as u32;
129
130 let mut col_ink = vec![0u32; w as usize];
137 for y in 0..h {
138 for x in 0..w {
139 if luma(crop.get_pixel(x, y)) < thresh {
140 col_ink[x as usize] += 1;
141 }
142 }
143 }
144 let rule_cols = col_ink
149 .iter()
150 .filter(|&&c| c as f32 > 0.9 * h as f32)
151 .count();
152 let mask_rules = (rule_cols as f32) < 0.15 * w as f32;
153 let rule = |x: u32| mask_rules && col_ink[x as usize] as f32 > 0.9 * h as f32;
154
155 let mut profile = vec![0u32; h as usize];
156 for y in 0..h {
157 let mut row = 0u32;
158 for x in 0..w {
159 if !rule(x) && luma(crop.get_pixel(x, y)) < thresh {
160 row += 1;
161 }
162 }
163 profile[y as usize] = row;
164 }
165
166 let mut runs: Vec<(u32, u32)> = Vec::new();
168 let mut start: Option<u32> = None;
169 for y in 0..h {
170 let text = profile[y as usize] >= min_ink;
171 if text && start.is_none() {
172 start = Some(y);
173 } else if !text {
174 if let Some(s) = start.take() {
175 if y - s >= 4 {
176 runs.push((s, y));
177 }
178 }
179 }
180 }
181 if let Some(s) = start {
182 if h - s >= 4 {
183 runs.push((s, h));
184 }
185 }
186
187 runs.into_iter()
189 .map(|(t, b)| {
190 let (mut l, mut r) = (w, 0u32);
191 for y in t..b {
192 for x in 0..w {
193 if luma(crop.get_pixel(x, y)) < thresh {
194 l = l.min(x);
195 r = r.max(x + 1);
196 }
197 }
198 }
199 if l >= r {
200 (0, t, w, b)
201 } else {
202 (l, t, r, b)
203 }
204 })
205 .collect()
206}
207
208pub fn is_text_label(label: &str) -> bool {
210 matches!(
211 label,
212 "text"
213 | "title"
214 | "section_header"
215 | "list_item"
216 | "caption"
217 | "footnote"
218 | "code"
219 | "formula"
220 )
221}
222
223pub type LineBox = (f32, f32, f32, f32);
225
226pub fn prep_region_lines(
232 img: &RgbImage,
233 regions: &[crate::layout::Region],
234 scale: f32,
235) -> (Vec<LineBox>, Vec<PrepLine>) {
236 let (iw, ih) = img.dimensions();
237 let mut bboxes = Vec::new();
238 let mut lines = Vec::new();
239 for region in regions {
240 if !is_text_label(region.label) {
241 continue;
242 }
243 let l = (region.l * scale).max(0.0) as u32;
244 let t = (region.t * scale).max(0.0) as u32;
245 let r = ((region.r * scale).max(0.0) as u32).min(iw);
246 let b = ((region.b * scale).max(0.0) as u32).min(ih);
247 if r <= l || b <= t {
248 continue;
249 }
250 let crop = imageops::crop_imm(img, l, t, r - l, b - t).to_image();
251 for (lx, ly, rx, ry) in segment_lines(&crop) {
252 let line = imageops::crop_imm(&crop, lx, ly, rx - lx, ry - ly).to_image();
253 let Some(pl) = prep_line(&line) else {
254 continue;
255 };
256 bboxes.push((
257 (l + lx) as f32 / scale,
258 (t + ly) as f32 / scale,
259 (l + rx) as f32 / scale,
260 (t + ry) as f32 / scale,
261 ));
262 lines.push(pl);
263 }
264 }
265 (bboxes, lines)
266}
267
268pub fn prep_region_lines_det(
294 img: &RgbImage,
295 regions: &[crate::layout::Region],
296 scale: f32,
297 detected: &[crate::ocr_det::DetBox],
298) -> (Vec<LineBox>, Vec<PrepLine>) {
299 prep_region_lines_det_by(img, regions, scale, detected, |img, _, d| {
300 rotate_crop(img, &d.quad)
301 })
302}
303
304pub fn prep_region_lines_det_boxed(
314 img: &RgbImage,
315 regions: &[crate::layout::Region],
316 scale: f32,
317 detected: &[crate::ocr_det::DetBox],
318) -> (Vec<LineBox>, Vec<PrepLine>) {
319 prep_region_lines_det_by(img, regions, scale, detected, |img, (l, t, r, b), _| {
320 Some(imageops::crop_imm(img, l, t, r - l, b - t).to_image())
321 })
322}
323
324fn prep_region_lines_det_by(
325 img: &RgbImage,
326 regions: &[crate::layout::Region],
327 scale: f32,
328 detected: &[crate::ocr_det::DetBox],
329 crop: impl Fn(&RgbImage, PxRect, &crate::ocr_det::DetBox) -> Option<RgbImage>,
330) -> (Vec<LineBox>, Vec<PrepLine>) {
331 let (iw, ih) = img.dimensions();
332 let mut bboxes = Vec::new();
333 let mut lines = Vec::new();
334 for region in regions {
335 if !is_text_label(region.label) {
336 continue;
337 }
338 let l = (region.l * scale).max(0.0) as u32;
339 let t = (region.t * scale).max(0.0) as u32;
340 let r = ((region.r * scale).max(0.0) as u32).min(iw);
341 let b = ((region.b * scale).max(0.0) as u32).min(ih);
342 if r <= l || b <= t {
343 continue;
344 }
345 let inside = det_boxes_inside(detected, l, t, r, b, iw, ih);
346 if inside.is_empty() {
347 let (bb, pl) = prep_region_lines(img, std::slice::from_ref(region), scale);
348 bboxes.extend(bb);
349 lines.extend(pl);
350 continue;
351 }
352 for ((dl, dt, dr, db), d) in inside {
353 let Some(pl) = crop(img, (dl, dt, dr, db), d).as_ref().and_then(prep_line) else {
354 continue;
355 };
356 bboxes.push((
357 dl as f32 / scale,
358 dt as f32 / scale,
359 dr as f32 / scale,
360 db as f32 / scale,
361 ));
362 lines.push(pl);
363 }
364 }
365 (bboxes, lines)
366}
367
368pub fn segment_words(line: &RgbImage) -> Vec<(u32, u32, u32, u32)> {
375 let (w, h) = line.dimensions();
376 if w == 0 || h == 0 {
377 return Vec::new();
378 }
379 let mean: f32 = line.pixels().map(luma).sum::<f32>() / (w * h) as f32;
380 let thresh = mean * 0.7;
381 let mut col_ink = vec![0u32; w as usize];
382 for y in 0..h {
383 for x in 0..w {
384 if luma(line.get_pixel(x, y)) < thresh {
385 col_ink[x as usize] += 1;
386 }
387 }
388 }
389 let min_gap = ((h as f32) * 0.6).max(4.0) as u32;
390 let mut words = Vec::new();
391 let mut start: Option<u32> = None;
392 let mut last_ink = 0u32;
393 let mut gap = 0u32;
394 for x in 0..w {
395 if col_ink[x as usize] > 0 {
396 if start.is_none() {
397 start = Some(x);
398 }
399 last_ink = x;
400 gap = 0;
401 } else if let Some(s) = start {
402 gap += 1;
403 if gap >= min_gap {
404 words.push((s, 0, last_ink + 1, h));
405 start = None;
406 }
407 }
408 }
409 if let Some(s) = start {
410 words.push((s, 0, last_ink + 1, h));
411 }
412 words
413}
414
415pub fn prep_table_words(
423 img: &RgbImage,
424 regions: &[crate::layout::Region],
425 scale: f32,
426) -> (Vec<LineBox>, Vec<PrepLine>) {
427 let (iw, ih) = img.dimensions();
428 let mut bboxes = Vec::new();
429 let mut lines = Vec::new();
430 for region in regions {
431 if !crate::assemble::is_table_like(region.label) {
432 continue;
433 }
434 let l = (region.l * scale).max(0.0) as u32;
435 let t = (region.t * scale).max(0.0) as u32;
436 let r = ((region.r * scale).max(0.0) as u32).min(iw);
437 let b = ((region.b * scale).max(0.0) as u32).min(ih);
438 if r <= l || b <= t {
439 continue;
440 }
441 let crop = imageops::crop_imm(img, l, t, r - l, b - t).to_image();
442 for (lx, ly, rx, ry) in segment_lines(&crop) {
443 let line = imageops::crop_imm(&crop, lx, ly, rx - lx, ry - ly).to_image();
444 for (wx0, _, wx1, _) in segment_words(&line) {
445 let word = imageops::crop_imm(&line, wx0, 0, wx1 - wx0, ry - ly).to_image();
446 let Some(pl) = prep_line(&word) else {
447 continue;
448 };
449 bboxes.push((
450 (l + lx + wx0) as f32 / scale,
451 (t + ly) as f32 / scale,
452 (l + lx + wx1) as f32 / scale,
453 (t + ry) as f32 / scale,
454 ));
455 lines.push(pl);
456 }
457 }
458 }
459 (bboxes, lines)
460}
461
462pub fn prep_table_words_det(
468 img: &RgbImage,
469 regions: &[crate::layout::Region],
470 scale: f32,
471 detected: &[crate::ocr_det::DetBox],
472) -> (Vec<LineBox>, Vec<PrepLine>) {
473 let (iw, ih) = img.dimensions();
474 let mut bboxes = Vec::new();
475 let mut lines = Vec::new();
476 for region in regions {
477 if !crate::assemble::is_table_like(region.label) {
478 continue;
479 }
480 let l = (region.l * scale).max(0.0) as u32;
481 let t = (region.t * scale).max(0.0) as u32;
482 let r = ((region.r * scale).max(0.0) as u32).min(iw);
483 let b = ((region.b * scale).max(0.0) as u32).min(ih);
484 if r <= l || b <= t {
485 continue;
486 }
487 let inside = det_boxes_inside(detected, l, t, r, b, iw, ih);
488 if inside.is_empty() {
489 let (bb, pl) = prep_table_words(img, std::slice::from_ref(region), scale);
490 bboxes.extend(bb);
491 lines.extend(pl);
492 continue;
493 }
494 for ((dl, dt, dr, db), d) in inside {
497 let Some(pl) = rotate_crop(img, &d.quad).as_ref().and_then(prep_line) else {
498 continue;
499 };
500 bboxes.push((
501 dl as f32 / scale,
502 dt as f32 / scale,
503 dr as f32 / scale,
504 db as f32 / scale,
505 ));
506 lines.push(pl);
507 }
508 }
509 (bboxes, lines)
510}
511
512type PxRect = (u32, u32, u32, u32);
523
524fn det_boxes_inside(
525 detected: &[crate::ocr_det::DetBox],
526 l: u32,
527 t: u32,
528 r: u32,
529 b: u32,
530 iw: u32,
531 ih: u32,
532) -> Vec<(PxRect, &crate::ocr_det::DetBox)> {
533 detected
534 .iter()
535 .filter(|d| {
536 let (cx, cy) = ((d.l + d.r) / 2.0, (d.t + d.b) / 2.0);
537 cx >= l as f32 && cx < r as f32 && cy >= t as f32 && cy < b as f32
538 })
539 .filter_map(|d| det_box_px(d, iw, ih).map(|px| (px, d)))
540 .collect()
541}
542
543fn det_box_px(d: &crate::ocr_det::DetBox, iw: u32, ih: u32) -> Option<PxRect> {
547 let dl = d.l.max(0.0) as u32;
548 let dt = d.t.max(0.0) as u32;
549 let dr = (d.r.max(0.0) as u32).min(iw);
550 let db = (d.b.max(0.0) as u32).min(ih);
551 (dr > dl && db > dt).then_some((dl, dt, dr, db))
552}
553
554pub fn det_box_crop(img: &RgbImage, d: &crate::ocr_det::DetBox) -> Option<RgbImage> {
562 det_box_px(d, img.width(), img.height())?;
567 rotate_crop(img, &d.quad)
568}
569
570pub enum LineSrc {
573 Crop(RgbImage),
574 Prepared(PrepLine),
575}
576
577pub fn region_line_crops_det(
584 img: &RgbImage,
585 regions: &[crate::layout::Region],
586 scale: f32,
587 detected: &[crate::ocr_det::DetBox],
588) -> (Vec<LineBox>, Vec<LineSrc>) {
589 let (iw, ih) = img.dimensions();
590 let mut bboxes = Vec::new();
591 let mut lines = Vec::new();
592 for region in regions {
593 if !is_text_label(region.label) {
594 continue;
595 }
596 let l = (region.l * scale).max(0.0) as u32;
597 let t = (region.t * scale).max(0.0) as u32;
598 let r = ((region.r * scale).max(0.0) as u32).min(iw);
599 let b = ((region.b * scale).max(0.0) as u32).min(ih);
600 if r <= l || b <= t {
601 continue;
602 }
603 let inside = det_boxes_inside(detected, l, t, r, b, iw, ih);
604 if inside.is_empty() {
605 let (bb, pl) = prep_region_lines(img, std::slice::from_ref(region), scale);
606 bboxes.extend(bb);
607 lines.extend(pl.into_iter().map(LineSrc::Prepared));
608 continue;
609 }
610 for ((dl, dt, dr, db), d) in inside {
611 let Some(crop) = rotate_crop(img, &d.quad) else {
612 continue;
613 };
614 bboxes.push((
615 dl as f32 / scale,
616 dt as f32 / scale,
617 dr as f32 / scale,
618 db as f32 / scale,
619 ));
620 lines.push(LineSrc::Crop(crop));
621 }
622 }
623 (bboxes, lines)
624}
625
626pub fn rotate_crop(img: &RgbImage, quad: &[(f32, f32); 4]) -> Option<RgbImage> {
635 let dist = |a: (f32, f32), b: (f32, f32)| ((a.0 - b.0).powi(2) + (a.1 - b.1).powi(2)).sqrt();
636 let w = dist(quad[0], quad[1]).max(dist(quad[2], quad[3])) as u32;
637 let h = dist(quad[0], quad[3]).max(dist(quad[1], quad[2])) as u32;
638 if w == 0 || h == 0 || img.width() == 0 || img.height() == 0 {
639 return None;
640 }
641 let (wf, hf) = (w as f64, h as f64);
642 let map = perspective(
643 [(0.0, 0.0), (wf, 0.0), (wf, hf), (0.0, hf)],
644 quad.map(|(x, y)| (x as f64, y as f64)),
645 )?;
646 let mut out = RgbImage::new(w, h);
647 for (x, y, px) in out.enumerate_pixels_mut() {
648 let (x, y) = (x as f64, y as f64);
649 let d = map[6] * x + map[7] * y + 1.0;
650 if d.abs() < 1e-12 {
651 continue;
652 }
653 let sx = (map[0] * x + map[1] * y + map[2]) / d;
654 let sy = (map[3] * x + map[4] * y + map[5]) / d;
655 *px = bicubic_replicate(img, sx, sy);
656 }
657 Some(if h as f32 / w as f32 >= 1.5 {
658 imageops::rotate270(&out)
659 } else {
660 out
661 })
662}
663
664fn perspective(from: [(f64, f64); 4], to: [(f64, f64); 4]) -> Option<[f64; 8]> {
669 let mut m = [[0f64; 9]; 8];
670 for k in 0..4 {
671 let ((x, y), (u, v)) = (from[k], to[k]);
672 m[2 * k] = [x, y, 1.0, 0.0, 0.0, 0.0, -x * u, -y * u, u];
673 m[2 * k + 1] = [0.0, 0.0, 0.0, x, y, 1.0, -x * v, -y * v, v];
674 }
675 for col in 0..8 {
677 let pivot = (col..8).max_by(|&a, &b| m[a][col].abs().total_cmp(&m[b][col].abs()))?;
678 if m[pivot][col].abs() < 1e-12 {
679 return None;
680 }
681 m.swap(col, pivot);
682 let pivot_row = m[col];
683 for (row, r) in m.iter_mut().enumerate() {
684 let f = r[col] / pivot_row[col];
685 if row != col && f != 0.0 {
686 for (v, p) in r[col..].iter_mut().zip(&pivot_row[col..]) {
687 *v -= f * p;
688 }
689 }
690 }
691 }
692 Some(std::array::from_fn(|i| m[i][8] / m[i][i]))
693}
694
695fn bicubic_replicate(img: &RgbImage, x: f64, y: f64) -> Rgb<u8> {
698 fn weights(t: f64) -> [f64; 4] {
699 const A: f64 = -0.75;
700 let w0 = ((A * (t + 1.0) - 5.0 * A) * (t + 1.0) + 8.0 * A) * (t + 1.0) - 4.0 * A;
701 let w1 = ((A + 2.0) * t - (A + 3.0)) * t * t + 1.0;
702 let w2 = ((A + 2.0) * (1.0 - t) - (A + 3.0)) * (1.0 - t) * (1.0 - t) + 1.0;
703 [w0, w1, w2, 1.0 - w0 - w1 - w2]
704 }
705 let (x0, y0) = (x.floor(), y.floor());
706 let (wx, wy) = (weights(x - x0), weights(y - y0));
707 let (mw, mh) = (img.width() as i64 - 1, img.height() as i64 - 1);
708 let mut acc = [0f64; 3];
709 for (j, wyj) in wy.iter().enumerate() {
710 let yy = (y0 as i64 - 1 + j as i64).clamp(0, mh) as u32;
711 for (i, wxi) in wx.iter().enumerate() {
712 let xx = (x0 as i64 - 1 + i as i64).clamp(0, mw) as u32;
713 let p = img.get_pixel(xx, yy);
714 let w = wxi * wyj;
715 for c in 0..3 {
716 acc[c] += w * p[c] as f64;
717 }
718 }
719 }
720 Rgb(acc.map(|v| v.round().clamp(0.0, 255.0) as u8))
721}
722
723pub fn normalize_polarity(mut img: RgbImage) -> RgbImage {
730 let (w, h) = img.dimensions();
731 if w == 0 || h == 0 {
732 return img;
733 }
734 let mean: f32 = img.pixels().map(luma).sum::<f32>() / (w * h) as f32;
735 if mean < 128.0 {
736 for px in img.pixels_mut() {
737 px.0 = [255 - px.0[0], 255 - px.0[1], 255 - px.0[2]];
738 }
739 }
740 img
741}
742
743pub fn prep_page_lines(img: &RgbImage) -> Vec<PrepLine> {
747 segment_lines(img)
748 .into_iter()
749 .filter_map(|(l, t, r, b)| {
750 let line = imageops::crop_imm(img, l, t, r - l, b - t).to_image();
751 prep_line(&line)
752 })
753 .collect()
754}
755
756pub fn text_score() -> f32 {
763 static SCORE: std::sync::OnceLock<f32> = std::sync::OnceLock::new();
764 *SCORE.get_or_init(|| {
765 text_score_from(docling_core::env::nonempty("DOCLING_RS_OCR_TEXT_SCORE").as_deref())
766 })
767}
768
769fn text_score_from(raw: Option<&str>) -> f32 {
772 raw.and_then(|r| r.trim().parse::<f32>().ok())
773 .filter(|s| s.is_finite() && (0.0..=1.0).contains(s))
774 .unwrap_or(0.5)
775}
776
777pub fn width_batches(lines: &[PrepLine]) -> Vec<(usize, Vec<usize>)> {
781 let mut by_width: std::collections::BTreeMap<usize, Vec<usize>> =
782 std::collections::BTreeMap::new();
783 for (ix, pl) in lines.iter().enumerate() {
784 by_width.entry(pl.w).or_default().push(ix);
785 }
786 let mut out = Vec::new();
787 for (w, ixs) in by_width {
788 for chunk in ixs.chunks(REC_BATCH) {
789 out.push((w, chunk.to_vec()));
790 }
791 }
792 out
793}
794
795pub fn batch_input(w: usize, chunk: &[usize], lines: &[PrepLine]) -> Vec<f32> {
797 let hw = REC_HEIGHT as usize * w;
798 let mut data = vec![0f32; chunk.len() * 3 * hw];
799 for (i, &ix) in chunk.iter().enumerate() {
800 data[i * 3 * hw..(i + 1) * 3 * hw].copy_from_slice(&lines[ix].data);
801 }
802 data
803}
804
805#[cfg(test)]
806mod tests {
807 use super::*;
808
809 fn page() -> RgbImage {
811 let mut img = RgbImage::from_pixel(200, 100, Rgb([255, 255, 255]));
812 for y in 20..30 {
813 for x in 10..190 {
814 img.put_pixel(x, y, Rgb([0, 0, 0]));
815 }
816 }
817 for y in 60..72 {
818 for x in 10..120 {
819 img.put_pixel(x, y, Rgb([0, 0, 0]));
820 }
821 }
822 img
823 }
824
825 #[test]
826 fn segments_and_preps_page_lines() {
827 let lines = prep_page_lines(&page());
828 assert_eq!(lines.len(), 2);
829 for pl in &lines {
830 assert_eq!(pl.data.len(), 3 * REC_HEIGHT as usize * pl.w);
831 }
832 let batches = width_batches(&lines);
834 assert_eq!(batches.len(), 2);
835 let (w0, chunk0) = &batches[0];
836 assert_eq!(
837 batch_input(*w0, chunk0, &lines).len(),
838 3 * REC_HEIGHT as usize * w0
839 );
840 }
841
842 #[test]
849 fn detector_boxes_are_the_region_lines_with_projection_fallback() {
850 let img = page();
851 let region = crate::layout::Region {
852 label: "text",
853 score: 0.9,
854 l: 0.0,
855 t: 0.0,
856 r: 200.0,
857 b: 100.0,
858 };
859 let bx = |l: f32, t: f32, r: f32, b: f32| crate::ocr_det::DetBox::rect(l, t, r, b, 0.9);
860 let det = [bx(8.0, 18.0, 192.0, 32.0), bx(8.0, 58.0, 122.0, 74.0)];
861 let (boxes, lines) = prep_region_lines_det(&img, std::slice::from_ref(®ion), 1.0, &det);
862 assert_eq!(
863 boxes,
864 vec![(8.0, 18.0, 192.0, 32.0), (8.0, 58.0, 122.0, 74.0)]
865 );
866 assert_eq!(lines.len(), 2);
867 let narrow = crate::layout::Region {
870 r: 150.0,
871 ..region.clone()
872 };
873 let det = [bx(-10.0, 18.0, 250.0, 32.0), bx(300.0, 60.0, 400.0, 70.0)];
874 let (boxes, _) = prep_region_lines_det(&img, std::slice::from_ref(&narrow), 1.0, &det);
875 assert_eq!(boxes, vec![(0.0, 18.0, 200.0, 32.0)]);
876 let det = [
878 bx(8.0, 18.0, 192.0, 32.0),
879 bx(8.0, 58.0, 60.0, 74.0),
880 bx(70.0, 58.0, 122.0, 74.0),
881 ];
882 let (boxes, _) = prep_region_lines_det(&img, std::slice::from_ref(®ion), 1.0, &det);
883 assert_eq!(boxes.len(), 3);
884 let (fallback, _) = prep_region_lines_det(&img, std::slice::from_ref(®ion), 1.0, &[]);
886 let (projection, _) = prep_region_lines(&img, std::slice::from_ref(®ion), 1.0);
887 assert_eq!(fallback, projection);
888 assert_eq!(projection.len(), 2);
889 let table = crate::layout::Region {
891 label: "table",
892 ..region.clone()
893 };
894 let det = [bx(8.0, 18.0, 92.0, 32.0), bx(108.0, 18.0, 192.0, 32.0)];
895 let (words, _) = prep_table_words_det(&img, std::slice::from_ref(&table), 1.0, &det);
896 assert_eq!(
897 words,
898 vec![(8.0, 18.0, 92.0, 32.0), (108.0, 18.0, 192.0, 32.0)]
899 );
900 let (fallback, _) = prep_table_words_det(&img, std::slice::from_ref(&table), 1.0, &[]);
901 assert_eq!(
902 fallback,
903 prep_table_words(&img, std::slice::from_ref(&table), 1.0).0
904 );
905 }
906
907 #[test]
912 fn det_box_crop_matches_the_page_ocr_crop() {
913 let img = RgbImage::from_fn(60, 40, |x, y| Rgb([(x * 4) as u8, (y * 6) as u8, 7]));
914 let d = crate::ocr_det::DetBox::rect(10.0, 5.0, 30.0, 25.0, 0.9);
915 let plain = det_box_crop(&img, &d).expect("crop");
916 assert_eq!(plain.dimensions(), (20, 20));
917 assert_eq!(plain.get_pixel(0, 0), img.get_pixel(10, 5));
918 assert_eq!(
919 plain,
920 image::imageops::crop_imm(&img, 10, 5, 20, 20).to_image()
921 );
922 let edge = crate::ocr_det::DetBox::rect(50.7, -3.0, 80.0, 10.0, 0.9);
923 assert_eq!(det_box_crop(&img, &edge).unwrap().dimensions(), (29, 13));
924 let off = crate::ocr_det::DetBox::rect(70.0, 0.0, 90.0, 10.0, 0.9);
925 assert_eq!(det_box_crop(&img, &off), None);
926 let tiny = RgbImage::from_pixel(2, 2, Rgb([255, 255, 255]));
927 let outside = crate::ocr_det::DetBox::rect(3.0, 0.0, 5.0, 2.0, 0.9);
928 assert_eq!(det_box_crop(&tiny, &outside), None);
929 let inside = crate::ocr_det::DetBox::rect(0.0, 0.0, 2.0, 2.0, 0.9);
930 assert_eq!(det_box_crop(&tiny, &inside).unwrap().dimensions(), (2, 2));
931 }
932
933 #[test]
940 fn a_skewed_detected_line_is_warped_upright_not_boxed() {
941 let (cx, cy, hw, hh, a) = (60.0f32, 40.0f32, 30.0f32, 6.0f32, 8.0f32.to_radians());
942 let img = RgbImage::from_fn(120, 80, |x, y| {
943 let (dx, dy) = (x as f32 + 0.5 - cx, y as f32 + 0.5 - cy);
944 let (u, v) = (dx * a.cos() + dy * a.sin(), -dx * a.sin() + dy * a.cos());
945 if u.abs() <= hw && v.abs() <= hh {
946 Rgb([0, 0, 0])
947 } else {
948 Rgb([255, 255, 255])
949 }
950 });
951 let corner = |su: f32, sv: f32| {
952 (
953 cx + su * hw * a.cos() - sv * hh * a.sin(),
954 cy + su * hw * a.sin() + sv * hh * a.cos(),
955 )
956 };
957 let quad = crate::ocr_det::order_corners([
958 corner(-1.0, -1.0),
959 corner(1.0, -1.0),
960 corner(1.0, 1.0),
961 corner(-1.0, 1.0),
962 ]);
963 let (xs, ys): (Vec<f32>, Vec<f32>) = quad.iter().copied().unzip();
964 let (l, t) = (
965 xs.iter().cloned().fold(f32::MAX, f32::min),
966 ys.iter().cloned().fold(f32::MAX, f32::min),
967 );
968 let (r, b) = (
969 xs.iter().cloned().fold(f32::MIN, f32::max),
970 ys.iter().cloned().fold(f32::MIN, f32::max),
971 );
972 let det = crate::ocr_det::DetBox {
973 l,
974 t,
975 r,
976 b,
977 score: 0.9,
978 quad,
979 };
980 let region = crate::layout::Region {
981 label: "text",
982 score: 0.9,
983 l: 0.0,
984 t: 0.0,
985 r: 120.0,
986 b: 80.0,
987 };
988 let (boxes, lines) =
989 prep_region_lines_det(&img, std::slice::from_ref(®ion), 1.0, &[det]);
990 assert_eq!(lines.len(), 1);
991 let bbox_w = (((r.floor() - l.floor()) * 48.0) / (b.floor() - t.floor())).round() as usize;
992 assert!(bbox_w < 160, "bounding box would give {bbox_w}");
993 assert!(
994 (236..=240).contains(&lines[0].w),
995 "warped width {}",
996 lines[0].w
997 );
998 assert_eq!(
1000 boxes,
1001 vec![(
1002 l as u32 as f32,
1003 t as u32 as f32,
1004 r as u32 as f32,
1005 b as u32 as f32
1006 )]
1007 );
1008 let (words, _) = prep_table_words_det(
1009 &img,
1010 &[crate::layout::Region {
1011 label: "table",
1012 score: 0.9,
1013 l: 0.0,
1014 t: 0.0,
1015 r: 120.0,
1016 b: 80.0,
1017 }],
1018 1.0,
1019 &[det],
1020 );
1021 assert_eq!(words, boxes);
1022 }
1023
1024 #[test]
1027 fn text_score_default_is_rapidocrs() {
1028 assert_eq!(text_score_from(None), 0.5);
1029 assert_eq!(text_score_from(Some("0")), 0.0);
1030 assert_eq!(text_score_from(Some("0.7")), 0.7);
1031 for garbage in ["1.5", "-0.1", "NaN", "high", ""] {
1032 assert_eq!(text_score_from(Some(garbage)), 0.5, "{garbage:?}");
1033 }
1034 }
1035
1036 #[test]
1037 fn dark_mode_pages_normalize_to_scan_polarity() {
1038 let mut dark = page();
1043 for px in dark.pixels_mut() {
1044 px.0 = [255 - px.0[0], 255 - px.0[1], 255 - px.0[2]];
1045 }
1046 assert_ne!(segment_lines(&dark), segment_lines(&page()));
1047 let fixed = normalize_polarity(dark);
1048 assert_eq!(segment_lines(&fixed), segment_lines(&page()));
1049 assert_eq!(prep_page_lines(&fixed).len(), 2);
1050 let light = page();
1052 assert_eq!(normalize_polarity(light.clone()), light);
1053 }
1054
1055 #[test]
1056 fn ctc_decode_collapses_repeats_and_blanks() {
1057 let chars = dict_chars("a\nb");
1059 assert_eq!(chars.len(), 4); let probs = [
1061 0.1, 0.8, 0.1, 0.0, 0.1, 0.8, 0.1, 0.0, 0.9, 0.05, 0.05, 0.0, 0.1, 0.1, 0.8, 0.0, 0.1, 0.1, 0.8, 0.0, ];
1067 assert_eq!(decode_row(&chars, &probs, 4), "ab");
1068 }
1069}
1070
1071#[cfg(test)]
1072mod word_segmentation {
1073 use image::{Rgb, RgbImage};
1074
1075 fn line_with_gap(h: u32, gap: u32) -> RgbImage {
1077 let w = 30 + gap + 30 + 10;
1078 let mut img = RgbImage::from_pixel(w, h, Rgb([255, 255, 255]));
1079 for (x0, x1) in [(5u32, 35u32), (35 + gap, 65 + gap)] {
1080 for x in x0..x1.min(w) {
1081 for y in h / 4..(3 * h / 4) {
1082 img.put_pixel(x, y, Rgb([0, 0, 0]));
1083 }
1084 }
1085 }
1086 img
1087 }
1088
1089 #[test]
1096 fn words_split_only_on_gaps_above_six_tenths_of_the_line_height() {
1097 for h in [16u32, 24, 32, 40] {
1098 let split_at = (1..=40u32)
1099 .find(|&gap| super::segment_words(&line_with_gap(h, gap)).len() >= 2)
1100 .expect("some gap splits");
1101 let ratio = split_at as f32 / h as f32;
1102 assert!(
1103 (0.5..=0.65).contains(&ratio),
1104 "h={h}: split at {split_at}px ({ratio:.2} x height)"
1105 );
1106 assert_eq!(
1108 super::segment_words(&line_with_gap(h, split_at - 1)).len(),
1109 1,
1110 "h={h}: a narrower gap must not split"
1111 );
1112 }
1113 }
1114
1115 #[test]
1119 fn rotate_crop_is_the_plain_crop_for_an_upright_box_and_turns_tall_ones() {
1120 let img = RgbImage::from_fn(40, 30, |x, y| Rgb([(x * 6) as u8, (y * 8) as u8, 77]));
1121 let quad = [(5.0, 4.0), (25.0, 4.0), (25.0, 14.0), (5.0, 14.0)];
1122 let warped = super::rotate_crop(&img, &quad).unwrap();
1123 let plain = image::imageops::crop_imm(&img, 5, 4, 20, 10).to_image();
1124 assert_eq!(warped, plain);
1125 let tall = [(10.0, 2.0), (16.0, 2.0), (16.0, 14.0), (10.0, 14.0)];
1127 let turned = super::rotate_crop(&img, &tall).unwrap();
1128 assert_eq!(turned.dimensions(), (12, 6));
1129 assert_eq!(turned.get_pixel(0, 5), img.get_pixel(10, 2));
1130 assert!(super::rotate_crop(&img, &[(1.0, 1.0); 4]).is_none());
1131 }
1132
1133 #[test]
1136 fn rotate_crop_straightens_a_rotated_rectangle() {
1137 let (cx, cy, a) = (50.0f32, 40.0f32, 30f32.to_radians());
1138 let (hw, hh) = (30.0f32, 6.0f32);
1139 let inside = |x: f32, y: f32| {
1140 let (dx, dy) = (x - cx, y - cy);
1141 let u = dx * a.cos() + dy * a.sin();
1142 let v = -dx * a.sin() + dy * a.cos();
1143 u.abs() <= hw && v.abs() <= hh
1144 };
1145 let img = RgbImage::from_fn(100, 80, |x, y| {
1146 if inside(x as f32 + 0.5, y as f32 + 0.5) {
1147 Rgb([0, 0, 0])
1148 } else {
1149 Rgb([255, 255, 255])
1150 }
1151 });
1152 let corner = |su: f32, sv: f32| {
1153 (
1154 cx + su * hw * a.cos() - sv * hh * a.sin(),
1155 cy + su * hw * a.sin() + sv * hh * a.cos(),
1156 )
1157 };
1158 let quad = crate::ocr_det::order_corners([
1159 corner(-1.0, -1.0),
1160 corner(1.0, -1.0),
1161 corner(1.0, 1.0),
1162 corner(-1.0, 1.0),
1163 ]);
1164 let bar = super::rotate_crop(&img, &quad).unwrap();
1165 let (w, h) = bar.dimensions();
1167 assert!(
1168 (59..=60).contains(&w) && (11..=12).contains(&h),
1169 "{w} × {h}"
1170 );
1171 for y in 2..h - 2 {
1173 for x in 2..w - 2 {
1174 assert!(
1175 bar.get_pixel(x, y)[0] < 60,
1176 "({x}, {y}) {:?}",
1177 bar.get_pixel(x, y)
1178 );
1179 }
1180 }
1181 }
1182}