davanstrien HF Staff commited on
Commit
3a5d7ed
·
verified ·
1 Parent(s): d540881

Sync from GitHub via hub-sync

Browse files
Files changed (1) hide show
  1. pp-doclayout.py +21 -7
pp-doclayout.py CHANGED
@@ -195,7 +195,7 @@ def extract_detections(result: Any) -> List[Dict[str, Any]]:
195
  @dataclass
196
  class SourceItem:
197
  key: str # stable identifier per image (used for dedup/resume)
198
- image: Image.Image
199
  extras: Dict[str, Any] # original row fields (only populated for dataset source)
200
 
201
 
@@ -234,16 +234,23 @@ def iter_dataset_images(
234
  total = len(ds)
235
 
236
  def gen() -> Iterator[SourceItem]:
237
- skipped = 0
238
  for i in range(total):
239
  try:
240
  row = ds[i]
241
  image = to_pil(row[image_column])
242
  except (UnidentifiedImageError, OSError) as e:
243
- skipped += 1
 
 
244
  logger.warning(
245
- f"Skipping unreadable image at row {i}: "
246
- f"{type(e).__name__}: {e}"
 
 
 
 
 
247
  )
248
  continue
249
  yield SourceItem(
@@ -251,8 +258,8 @@ def iter_dataset_images(
251
  image=image,
252
  extras={}, # original schema is preserved by the sink via the dataset ref
253
  )
254
- if skipped:
255
- logger.info(f"Skipped {skipped} unreadable image(s) total")
256
 
257
  return gen(), total, ds
258
 
@@ -953,6 +960,13 @@ def main(args: argparse.Namespace) -> None:
953
  if item.key in completed:
954
  skipped += 1
955
  continue
 
 
 
 
 
 
 
956
  try:
957
  arr = pil_to_array(item.image)
958
  results = model.predict(
 
195
  @dataclass
196
  class SourceItem:
197
  key: str # stable identifier per image (used for dedup/resume)
198
+ image: Optional[Image.Image] # None for an unreadable row (placeholder)
199
  extras: Dict[str, Any] # original row fields (only populated for dataset source)
200
 
201
 
 
234
  total = len(ds)
235
 
236
  def gen() -> Iterator[SourceItem]:
237
+ failed = 0
238
  for i in range(total):
239
  try:
240
  row = ds[i]
241
  image = to_pil(row[image_column])
242
  except (UnidentifiedImageError, OSError) as e:
243
+ # Still yield a placeholder so the output row stays aligned with
244
+ # the source row (the dataset sink writes layouts positionally).
245
+ failed += 1
246
  logger.warning(
247
+ f"Unreadable image at row {i}: "
248
+ f"{type(e).__name__}: {e} — writing empty layout"
249
+ )
250
+ yield SourceItem(
251
+ key=f"row-{i:08d}",
252
+ image=None,
253
+ extras={"failed": True},
254
  )
255
  continue
256
  yield SourceItem(
 
258
  image=image,
259
  extras={}, # original schema is preserved by the sink via the dataset ref
260
  )
261
+ if failed:
262
+ logger.info(f"{failed} unreadable image(s) written as empty layouts")
263
 
264
  return gen(), total, ds
265
 
 
960
  if item.key in completed:
961
  skipped += 1
962
  continue
963
+ if item.extras.get("failed") or item.image is None:
964
+ # Unreadable source image — write an empty layout in position so the
965
+ # output stays row-aligned with the source dataset.
966
+ sink.write(item.key, [], {})
967
+ errors += 1
968
+ processed += 1
969
+ continue
970
  try:
971
  arr = pil_to_array(item.image)
972
  results = model.predict(