Mayo commited on
Commit
04a9c3e
·
unverified ·
1 Parent(s): 2383d72

fix: text box ordering regression

Browse files
.gitignore CHANGED
@@ -49,3 +49,6 @@ temp/
49
  tmp/
50
  playwright-report/
51
  test-results/
 
 
 
 
49
  tmp/
50
  playwright-report/
51
  test-results/
52
+
53
+ # Misc
54
+ AGENTS.md
koharu-ml/src/comic_text_detector/postprocess.rs CHANGED
@@ -1156,7 +1156,7 @@ fn try_merge_text_line(block: &mut CtdBlock, other: &mut CtdBlock, font_size_tol
1156
  true
1157
  }
1158
 
1159
- fn sort_regions(mut blocks: Vec<CtdBlock>) -> Vec<CtdBlock> {
1160
  if blocks.len() < 2 {
1161
  return blocks;
1162
  }
@@ -1166,8 +1166,15 @@ fn sort_regions(mut blocks: Vec<CtdBlock>) -> Vec<CtdBlock> {
1166
  .filter(|block| block.source_direction == TextDirection::Vertical)
1167
  .count();
1168
  let right_to_left = !blocks.is_empty() && vertical_blocks * 2 >= blocks.len();
1169
- blocks.sort_by(|a, b| compare_blocks_for_reading_order(a, b, right_to_left));
1170
- blocks
 
 
 
 
 
 
 
1171
  }
1172
 
1173
  fn compare_blocks_for_reading_order(
@@ -1175,23 +1182,62 @@ fn compare_blocks_for_reading_order(
1175
  b: &CtdBlock,
1176
  right_to_left: bool,
1177
  ) -> std::cmp::Ordering {
1178
- let primary = if right_to_left {
1179
- b.bbox[2].total_cmp(&a.bbox[2])
1180
- } else {
1181
- a.bbox[0].total_cmp(&b.bbox[0])
1182
- };
1183
- let tertiary = if right_to_left {
1184
- b.bbox[0].total_cmp(&a.bbox[0])
 
 
 
 
 
1185
  } else {
1186
- a.bbox[2].total_cmp(&b.bbox[2])
1187
- };
1188
-
1189
- primary
1190
- .then_with(|| a.bbox[1].total_cmp(&b.bbox[1]))
1191
- .then_with(|| tertiary)
1192
- .then_with(|| a.bbox[3].total_cmp(&b.bbox[3]))
1193
  }
1194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1195
  fn dedupe_blocks(blocks: &mut Vec<CtdBlock>) {
1196
  if blocks.len() < 2 {
1197
  return;
@@ -1644,6 +1690,10 @@ fn horizontal_overlap(a: &[f32; 4], b: &[f32; 4]) -> f32 {
1644
  (a[2].min(b[2]) - a[0].max(b[0])).max(0.0)
1645
  }
1646
 
 
 
 
 
1647
  fn mean_mask_score(mask: &GrayImage, bbox: &[f32; 4]) -> f32 {
1648
  let x1 = bbox[0].floor().max(0.0) as u32;
1649
  let y1 = bbox[1].floor().max(0.0) as u32;
@@ -2075,22 +2125,20 @@ mod tests {
2075
  }
2076
 
2077
  #[test]
2078
- fn reading_order_comparator_stays_transitive_across_row_boundaries() {
2079
  let left_lower = test_block([0.0, 9.0, 2.0, 11.0], TextDirection::Horizontal);
2080
  let middle = test_block([1.0, 4.0, 3.0, 6.0], TextDirection::Horizontal);
2081
  let right_upper = test_block([2.0, -1.0, 4.0, 1.0], TextDirection::Horizontal);
 
 
2082
 
2083
  assert_eq!(
2084
- compare_blocks_for_reading_order(&left_lower, &middle, false),
2085
- std::cmp::Ordering::Less
2086
- );
2087
- assert_eq!(
2088
- compare_blocks_for_reading_order(&middle, &right_upper, false),
2089
- std::cmp::Ordering::Less
2090
- );
2091
- assert_eq!(
2092
- compare_blocks_for_reading_order(&left_lower, &right_upper, false),
2093
- std::cmp::Ordering::Less
2094
  );
2095
  }
2096
 
 
1156
  true
1157
  }
1158
 
1159
+ fn sort_regions(blocks: Vec<CtdBlock>) -> Vec<CtdBlock> {
1160
  if blocks.len() < 2 {
1161
  return blocks;
1162
  }
 
1166
  .filter(|block| block.source_direction == TextDirection::Vertical)
1167
  .count();
1168
  let right_to_left = !blocks.is_empty() && vertical_blocks * 2 >= blocks.len();
1169
+ let order = stable_reading_order_indices(&blocks, right_to_left);
1170
+ let mut ordered = Vec::with_capacity(blocks.len());
1171
+ let mut slots = blocks.into_iter().map(Some).collect::<Vec<_>>();
1172
+ for index in order {
1173
+ if let Some(block) = slots[index].take() {
1174
+ ordered.push(block);
1175
+ }
1176
+ }
1177
+ ordered
1178
  }
1179
 
1180
  fn compare_blocks_for_reading_order(
 
1182
  b: &CtdBlock,
1183
  right_to_left: bool,
1184
  ) -> std::cmp::Ordering {
1185
+ let overlap_y = vertical_overlap(&a.bbox, &b.bbox);
1186
+ let row_tolerance = (a.detected_font_size_px.max(b.detected_font_size_px) * 0.6).max(1.0);
1187
+ if overlap_y > 0.0 || (a.center()[1] - b.center()[1]).abs() <= row_tolerance {
1188
+ if right_to_left {
1189
+ b.center()[0]
1190
+ .total_cmp(&a.center()[0])
1191
+ .then_with(|| a.center()[1].total_cmp(&b.center()[1]))
1192
+ } else {
1193
+ a.center()[0]
1194
+ .total_cmp(&b.center()[0])
1195
+ .then_with(|| a.center()[1].total_cmp(&b.center()[1]))
1196
+ }
1197
  } else {
1198
+ a.center()[1]
1199
+ .total_cmp(&b.center()[1])
1200
+ .then_with(|| a.center()[0].total_cmp(&b.center()[0]))
1201
+ }
 
 
 
1202
  }
1203
 
1204
+ fn stable_reading_order_indices(blocks: &[CtdBlock], right_to_left: bool) -> Vec<usize> {
1205
+ let mut edges = vec![Vec::new(); blocks.len()];
1206
+ let mut indegree = vec![0usize; blocks.len()];
1207
+ for left in 0..blocks.len() {
1208
+ for right in (left + 1)..blocks.len() {
1209
+ match compare_blocks_for_reading_order(&blocks[left], &blocks[right], right_to_left) {
1210
+ std::cmp::Ordering::Less => {
1211
+ edges[left].push(right);
1212
+ indegree[right] += 1;
1213
+ }
1214
+ std::cmp::Ordering::Greater => {
1215
+ edges[right].push(left);
1216
+ indegree[left] += 1;
1217
+ }
1218
+ std::cmp::Ordering::Equal => {}
1219
+ }
1220
+ }
1221
+ }
1222
+
1223
+ let mut remaining = (0..blocks.len()).collect::<Vec<_>>();
1224
+ let mut ordered = Vec::with_capacity(blocks.len());
1225
+ while !remaining.is_empty() {
1226
+ let available = remaining
1227
+ .iter()
1228
+ .copied()
1229
+ .filter(|index| indegree[*index] == 0)
1230
+ .collect::<Vec<_>>();
1231
+ let next = available.first().copied().unwrap_or_else(|| remaining[0]);
1232
+ ordered.push(next);
1233
+ remaining.retain(|index| *index != next);
1234
+ for successor in &edges[next] {
1235
+ indegree[*successor] = indegree[*successor].saturating_sub(1);
1236
+ }
1237
+ }
1238
+
1239
+ ordered
1240
+ }
1241
  fn dedupe_blocks(blocks: &mut Vec<CtdBlock>) {
1242
  if blocks.len() < 2 {
1243
  return;
 
1690
  (a[2].min(b[2]) - a[0].max(b[0])).max(0.0)
1691
  }
1692
 
1693
+ fn vertical_overlap(a: &[f32; 4], b: &[f32; 4]) -> f32 {
1694
+ (a[3].min(b[3]) - a[1].max(b[1])).max(0.0)
1695
+ }
1696
+
1697
  fn mean_mask_score(mask: &GrayImage, bbox: &[f32; 4]) -> f32 {
1698
  let x1 = bbox[0].floor().max(0.0) as u32;
1699
  let y1 = bbox[1].floor().max(0.0) as u32;
 
2125
  }
2126
 
2127
  #[test]
2128
+ fn sort_regions_stays_stable_across_row_boundaries() {
2129
  let left_lower = test_block([0.0, 9.0, 2.0, 11.0], TextDirection::Horizontal);
2130
  let middle = test_block([1.0, 4.0, 3.0, 6.0], TextDirection::Horizontal);
2131
  let right_upper = test_block([2.0, -1.0, 4.0, 1.0], TextDirection::Horizontal);
2132
+ let sorted = sort_regions(vec![left_lower, middle, right_upper]);
2133
+ let bboxes = sorted.iter().map(|block| block.bbox).collect::<Vec<_>>();
2134
 
2135
  assert_eq!(
2136
+ bboxes,
2137
+ vec![
2138
+ [0.0, 9.0, 2.0, 11.0],
2139
+ [1.0, 4.0, 3.0, 6.0],
2140
+ [2.0, -1.0, 4.0, 1.0],
2141
+ ]
 
 
 
 
2142
  );
2143
  }
2144