textpainting-precompute-chunks / inspect_anytext_errors.py
Leofames's picture
Upload folder using huggingface_hub
f1725f0 verified
Raw History Blame Contribute Delete
2.49 kB
import json
from pathlib import Path
from collections import Counter
BASE_DIR = Path(__file__).resolve().parent
JSON_PATH = BASE_DIR / "train.json"
with open(JSON_PATH, "r", encoding="utf-8") as f:
records = json.load(f)
annotation_types = Counter()
non_list_examples = []
boundary_examples = []
for i, record in enumerate(records):
annotations = record.get("annotations")
annotation_types[type(annotations).__name__] += 1
if not isinstance(annotations, list) and len(non_list_examples) < 10:
non_list_examples.append({
"record_index": i,
"img_name": record.get("img_name"),
"local_image": record.get("local_image"),
"annotations_type": type(annotations).__name__,
"annotations_value": annotations,
})
if isinstance(annotations, list):
for ann_index, ann in enumerate(annotations):
if not isinstance(ann, dict):
continue
polygon = ann.get("polygon", [])
if not isinstance(polygon, list):
continue
for point in polygon:
if (
isinstance(point, (list, tuple))
and len(point) == 2
and all(isinstance(v, (int, float)) for v in point)
and (point[0] >= 512 or point[1] >= 512
or point[0] < 0 or point[1] < 0)
):
if len(boundary_examples) < 10:
boundary_examples.append({
"record_index": i,
"img_name": record.get("img_name"),
"local_image": record.get("local_image"),
"annotation_index": ann_index,
"text": ann.get("text"),
"polygon": polygon,
"image_width": record.get("image_width"),
"image_height": record.get("image_height"),
})
break
print("ANNOTATION VALUE TYPES")
for kind, count in annotation_types.items():
print(f"{kind}: {count}")
print("\nEXAMPLES WHERE ANNOTATIONS IS NOT A LIST")
print(json.dumps(non_list_examples, ensure_ascii=False, indent=2))
print("\nEXAMPLES WITH OUT-OF-BOUNDS COORDINATES")
print(json.dumps(boundary_examples, ensure_ascii=False, indent=2))