LLMMultiLabel / openimages_v7 /processing_openimages.py
DaYuDaYu's picture
Add files using upload-large-folder tool
0213a4c verified
#!/usr/bin/env python
# coding: utf-8
#
# Usage:
# python3 openimages_to_wds.py
import os
import os.path as osp
import json
import shutil
from tqdm import tqdm
root = "/media/data/CC3M/data/openimages_v7"
save_dir = osp.join(root, "openimages_v7_valid")
if not os.path.exists(save_dir):
os.makedirs(save_dir)
'https://storage.googleapis.com/localized-narratives/annotations/open_images_validation_localized_narratives.jsonl'
json_file_path = osp.join(root, "open_images_validation_localized_narratives.jsonl")
sub_fold_cnt = 1
with open(json_file_path, "r") as f:
for i, line in tqdm(enumerate(f)):
line = json.loads(line)
image_id = line["image_id"]
cap = line["caption"]
src = osp.join(root, "valid", f"{image_id}.jpg")
if (i + 1) % 2999 == 0:
sub_fold_cnt += 1
sub_fold = osp.join(save_dir, str(sub_fold_cnt - 1).zfill(5))
if not os.path.exists(sub_fold):
os.makedirs(sub_fold)
img_path = osp.join(root, sub_fold, f"{image_id}.jpg")
txt_path = osp.join(root, sub_fold, f"{image_id}.txt")
jsn_path = osp.join(root, sub_fold, f"{image_id}.json")
shutil.copy(src, img_path)
with open(txt_path, "w") as f:
f.write(cap)
with open(jsn_path, "w") as f:
json.dump(line, f)