Florent Gbelidji commited on
Commit
b556e00
·
verified ·
1 Parent(s): d46c985

Sync DeepSeek OCR HF job code

Browse files
Files changed (1) hide show
  1. ds_batch_ocr/document.py +73 -11
ds_batch_ocr/document.py CHANGED
@@ -19,6 +19,10 @@ GROUNDING_PATTERN = re.compile(
19
  re.DOTALL,
20
  )
21
 
 
 
 
 
22
  FIGURE_MARKDOWN_PATTERN = re.compile(
23
  r"!\[Figure (?P<figure_id>[^\]]+)\]\((?P<path>[^)]+)\)"
24
  )
@@ -47,6 +51,25 @@ def extract_grounding_blocks(text: str) -> List[Dict[str, Any]]:
47
  "span": match.span(),
48
  }
49
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  return matches
51
 
52
 
@@ -55,11 +78,16 @@ def flatten_boxes(coordinates: Any) -> List[List[float]]:
55
  if coordinates is None:
56
  return boxes
57
  if isinstance(coordinates, (list, tuple)):
58
- for item in coordinates:
59
- if isinstance(item, (list, tuple)) and len(item) == 4:
60
- boxes.append([float(value) for value in item])
61
- elif isinstance(item, dict):
62
- boxes.extend(flatten_boxes(item.get("bbox")))
 
 
 
 
 
63
  elif isinstance(coordinates, dict):
64
  boxes.extend(flatten_boxes(coordinates.get("bbox")))
65
  return boxes
@@ -82,10 +110,24 @@ def clamp(value: int, lower: int, upper: int) -> int:
82
  def normalized_to_pixels(box: List[float], width: int, height: int) -> Optional[List[int]]:
83
  if len(box) != 4 or width <= 0 or height <= 0:
84
  return None
85
- x1 = clamp(int(round(box[0] / 999.0 * width)), 0, width)
86
- y1 = clamp(int(round(box[1] / 999.0 * height)), 0, height)
87
- x2 = clamp(int(round(box[2] / 999.0 * width)), 0, width)
88
- y2 = clamp(int(round(box[3] / 999.0 * height)), 0, height)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  if x2 <= x1 or y2 <= y1:
90
  return None
91
  return [x1, y1, x2, y2]
@@ -127,6 +169,21 @@ def save_figure(
127
  if not merged_box:
128
  return None
129
  width, height = image.size
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  pixel_box = normalized_to_pixels(merged_box, width, height)
131
  if not pixel_box:
132
  return None
@@ -142,7 +199,12 @@ def save_figure(
142
  full_path = figures_dir / figure_filename
143
  crop.save(full_path)
144
 
145
- norm_box = [value / 999.0 for value in merged_box]
 
 
 
 
 
146
  bounding_box_norm = {
147
  "x1": norm_box[0],
148
  "y1": norm_box[1],
@@ -157,7 +219,7 @@ def save_figure(
157
  image_path=(Path(sample_id) / figure_relative_doc_path).as_posix(),
158
  document_relative_path=figure_relative_doc_path.as_posix(),
159
  bounding_box_norm=bounding_box_norm,
160
- bounding_box_norm_list=[[float(v) for v in box] for box in boxes],
161
  bounding_box_pixels=bounding_box_pixels,
162
  )
163
 
 
19
  re.DOTALL,
20
  )
21
 
22
+ ALT_GROUNDING_PATTERN = re.compile(
23
+ r"(?P<label>[A-Za-z0-9_]+)\[\[(?P<coords>[^\[\]]+)\]\]",
24
+ )
25
+
26
  FIGURE_MARKDOWN_PATTERN = re.compile(
27
  r"!\[Figure (?P<figure_id>[^\]]+)\]\((?P<path>[^)]+)\)"
28
  )
 
51
  "span": match.span(),
52
  }
53
  )
54
+
55
+ if not matches:
56
+ for match in ALT_GROUNDING_PATTERN.finditer(text):
57
+ label = match.group("label").strip()
58
+ coords_text = match.group("coords").strip()
59
+ coordinates = None
60
+ if coords_text:
61
+ try:
62
+ coordinates = ast.literal_eval(f"[{coords_text}]")
63
+ except Exception:
64
+ coordinates = None
65
+ matches.append(
66
+ {
67
+ "label": label,
68
+ "coordinates": coordinates,
69
+ "raw": match.group(0),
70
+ "span": match.span(),
71
+ }
72
+ )
73
  return matches
74
 
75
 
 
78
  if coordinates is None:
79
  return boxes
80
  if isinstance(coordinates, (list, tuple)):
81
+ if len(coordinates) == 4 and all(
82
+ isinstance(value, (int, float)) for value in coordinates
83
+ ):
84
+ boxes.append([float(value) for value in coordinates])
85
+ else:
86
+ for item in coordinates:
87
+ if isinstance(item, (list, tuple)) and len(item) == 4:
88
+ boxes.append([float(value) for value in item])
89
+ elif isinstance(item, dict):
90
+ boxes.extend(flatten_boxes(item.get("bbox")))
91
  elif isinstance(coordinates, dict):
92
  boxes.extend(flatten_boxes(coordinates.get("bbox")))
93
  return boxes
 
110
  def normalized_to_pixels(box: List[float], width: int, height: int) -> Optional[List[int]]:
111
  if len(box) != 4 or width <= 0 or height <= 0:
112
  return None
113
+
114
+ max_value = max(box)
115
+ min_value = min(box)
116
+
117
+ if max_value <= 1.0:
118
+ scale_x = width
119
+ scale_y = height
120
+ elif max_value <= 999.0:
121
+ scale_x = width / 999.0
122
+ scale_y = height / 999.0
123
+ else:
124
+ scale_x = 1.0
125
+ scale_y = 1.0
126
+
127
+ x1 = clamp(int(round(box[0] * scale_x)), 0, width)
128
+ y1 = clamp(int(round(box[1] * scale_y)), 0, height)
129
+ x2 = clamp(int(round(box[2] * scale_x)), 0, width)
130
+ y2 = clamp(int(round(box[3] * scale_y)), 0, height)
131
  if x2 <= x1 or y2 <= y1:
132
  return None
133
  return [x1, y1, x2, y2]
 
169
  if not merged_box:
170
  return None
171
  width, height = image.size
172
+ converted_boxes: List[List[int]] = []
173
+ normalized_boxes: List[List[float]] = []
174
+ for box in boxes:
175
+ pixels = normalized_to_pixels(box, width, height)
176
+ if pixels:
177
+ converted_boxes.append(pixels)
178
+ normalized_boxes.append(
179
+ [
180
+ pixels[0] / float(width),
181
+ pixels[1] / float(height),
182
+ pixels[2] / float(width),
183
+ pixels[3] / float(height),
184
+ ]
185
+ )
186
+
187
  pixel_box = normalized_to_pixels(merged_box, width, height)
188
  if not pixel_box:
189
  return None
 
199
  full_path = figures_dir / figure_filename
200
  crop.save(full_path)
201
 
202
+ norm_box = [
203
+ pixel_box[0] / float(width),
204
+ pixel_box[1] / float(height),
205
+ pixel_box[2] / float(width),
206
+ pixel_box[3] / float(height),
207
+ ]
208
  bounding_box_norm = {
209
  "x1": norm_box[0],
210
  "y1": norm_box[1],
 
219
  image_path=(Path(sample_id) / figure_relative_doc_path).as_posix(),
220
  document_relative_path=figure_relative_doc_path.as_posix(),
221
  bounding_box_norm=bounding_box_norm,
222
+ bounding_box_norm_list=normalized_boxes or [[float(v) for v in norm_box]],
223
  bounding_box_pixels=bounding_box_pixels,
224
  )
225