# %% Cell 1 import os import json from typing import Any from zipfile import ZipFile def export_subset_of_hagrid(output_name: str, count_to_export: int = 1000): image_subset_by_sign: dict[str, list[str]] = {} with ZipFile('hagridv2_512.zip') as zf: for file in zf.namelist(): if file.endswith('.jpg'): sign_category = file.split("/")[1] os.makedirs(f"{output_name}/{sign_category}", exist_ok=True) if len(image_subset_by_sign.get(sign_category, [])) >= count_to_export: continue file_name = file.split('/')[-1] if sign_category not in image_subset_by_sign: image_subset_by_sign[sign_category] = [] image_subset_by_sign[sign_category].append(file_name[:-4]) export_file_name = f"{output_name}/{sign_category}/{file_name}" with open(export_file_name, 'wb') as img_file: img_file.write(zf.read(file)) with open("exported_images.json", 'w') as file: json.dump(image_subset_by_sign, file) # export_subset_of_hagrid('hagrid-10', count_to_export=10) # %% Cell 2 import json def extract_connected_annotations(output_name: str): with ZipFile("annotations.zip") as fa: with open("exported_images.json", 'r') as file: exported_examples = json.load(file) annotations_for_exported_subset: dict[str, dict[str, Any]] = {} minimal_annotations_for_exported_subset: dict[str, dict[str, Any]] = {} for file in fa.namelist()[:]: print(file) if file.endswith('.json'): category = file.split("/")[-1][:-5] if category not in annotations_for_exported_subset: annotations_for_exported_subset[category] = {} minimal_annotations_for_exported_subset[category] = {} if category not in exported_examples: continue with fa.open(file) as json_file: labels = json.load(json_file) print(category) # print(exported_examples[category]) print(set(labels.keys()).intersection(exported_examples[category])) for overlapping_images in set(labels.keys()).intersection(exported_examples[category]): annotations_for_exported_subset[category][overlapping_images] = labels[overlapping_images] minimal_annotations_for_exported_subset[category][overlapping_images] = { "labels": labels[overlapping_images]["labels"], "bboxes": labels[overlapping_images]["bboxes"], "meta": labels[overlapping_images]["meta"], } with open(f"minimal-annotations.json", 'w') as f: json.dump(minimal_annotations_for_exported_subset, f, indent=4) with open(f"annotations.json", 'w') as f: json.dump(annotations_for_exported_subset, f, indent=4) # extract_connected_annotations('hagrid-10') # %% Cell 3 def run_pipeline(output_name: str, count_exported: int): export_subset_of_hagrid(output_name, count_exported) extract_connected_annotations(output_name) annotation_files = [ "minimal-annotations.json", "annotations.json", "exported_images.json" ] output_zip_name = f"{output_name}_{count_exported}_images.zip" with ZipFile(output_zip_name, 'w') as archive: for root, dirs, files in os.walk(output_name): for file in files: file_path = os.path.join(root, file) archive.write(file_path, arcname=os.path.relpath(file_path, start=output_name)) for file_name in annotation_files: full_path = os.path.join(output_name, file_name) if os.path.exists(full_path): archive.write(full_path, arcname=file_name) for root, dirs, files in os.walk(output_name, topdown=False): for name in files: os.remove(os.path.join(root, name)) for name in dirs: os.rmdir(os.path.join(root, name)) os.rmdir(output_name) for file_name in annotation_files: print(file_name) file_to_remove = os.path.join(output_name, file_name) if os.path.exists(file_name): os.remove(file_name) run_pipeline('hagrid-export', 100) run_pipeline('hagrid-export', 500) run_pipeline('hagrid-export', 1000)