"""Statistics of the school_notebooks_ru COCO annotations: pages, categories, polygons per category, text presence.""" import json import sys import zipfile from collections import Counter D = "/path/to/TURKIC_OCR_DATA/data/raw/school_notebooks_ru" OUT = "/path/to/TURKIC_OCR_DATA/outputs/r5/linedet/school_stats.json" def main(): res = {} for sp in ("train", "val", "test"): d = json.load(open("%s/annotations_%s.json" % (D, sp), encoding="utf-8")) cats = {c["id"]: c["name"] for c in d["categories"]} cnt = Counter(cats[a["category_id"]] for a in d["annotations"]) with_text = Counter() nseg = Counter() for a in d["annotations"]: tr = (a.get("attributes") or {}).get("translation") if tr: with_text[cats[a["category_id"]]] += 1 nseg[len(a["segmentation"])] += 1 sizes = Counter((i["width"], i["height"]) for i in d["images"]) ex = [a for a in d["annotations"] if a["category_id"] == 4][:2] res[sp] = {"n_images": len(d["images"]), "polygons_by_category": dict(cnt), "with_translation": dict(with_text), "segmentation_list_lengths": dict(nseg), "image_sizes": {"%dx%d" % k: v for k, v in sizes.items()}, "example_text_line_annotations": ex, "first_files": [i["file_name"] for i in d["images"][:3]], "keys_annotation": sorted(d["annotations"][0].keys())} print(sp, json.dumps({k: v for k, v in res[sp].items() if k != "example_text_line_annotations"}, ensure_ascii=False)) z = zipfile.ZipFile(D + "/images.zip") names = z.namelist() res["zip"] = {"n_files": len(names), "first": names[:5]} print("zip", res["zip"]) json.dump(res, open(OUT, "w"), ensure_ascii=False, indent=1) if __name__ == "__main__": main()