-
Notifications
You must be signed in to change notification settings - Fork 0
/
caption_image.py
50 lines (39 loc) · 1.19 KB
/
caption_image.py
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
50
import argparse
import json
import logging
import os
import random
from io import open
import math
import sys
from PIL import Image
#import pandas as pd
#import requests
#f#rom time import gmtime, strftime
#from timeit import default_timer as timer
import numpy as np
#from tqdm import tqdm, trange
#import torch
#from torch.utils.data import DataLoader, Dataset, RandomSampler
#from torch.utils.data.distributed import DistributedSampler
#from tensorboardX import SummaryWriter
##
#from pytorch_transformers.tokenization_bert import BertTokenizer
#from pytorch_transformers.optimization import AdamW, WarmupLinearSchedule
#import utils as utils
#import torch.distributed as dist
dataroot = '/projects/sina/vilbert/discourse_project/vilbert-multi-task/data/discoursedata/test/'
caption_path = os.path.join(dataroot, "captions_all_json.json")
with open(caption_path) as json_file:
captions= json.load(json_file)
h = captions.copy()
for k in captions.keys():
img_path = os.path.join(dataroot, "images/{}.jpg".format(str(k)))
try:
img = Image.open(img_path)
except:
print(img_path)
h.pop(k)
print(len(h))
with open(caption_path, 'w') as outfile:
json.dump(h, outfile)