File size: 1,342 Bytes
0213a4c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#!/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)
|