|
| 1 | +import gc |
| 2 | +import json |
| 3 | +import argparse |
| 4 | +from tqdm import tqdm |
| 5 | +from downstream.vcr.data.colormap import color_list |
| 6 | +from PIL import Image |
| 7 | +import PIL.ImageDraw as ImageDraw |
| 8 | + |
| 9 | +TRANSPARENCY = .15 |
| 10 | +OPACITY = int(255 * TRANSPARENCY) |
| 11 | + |
| 12 | + |
| 13 | +parser = argparse.ArgumentParser(description='SCRAPE!') |
| 14 | +parser.add_argument( |
| 15 | + '-fold', |
| 16 | + dest='fold', |
| 17 | + default=0, |
| 18 | + type=int, |
| 19 | + help='which fold we are on' |
| 20 | +) |
| 21 | +parser.add_argument( |
| 22 | + '-num_folds', |
| 23 | + dest='num_folds', |
| 24 | + default=1, |
| 25 | + type=int, |
| 26 | + help='Number of folds (corresponding to both the number of training files and the number of testing files)', |
| 27 | +) |
| 28 | +parser.add_argument( |
| 29 | + '-split', |
| 30 | + dest='split', |
| 31 | + default='train', |
| 32 | + type=str, |
| 33 | +) |
| 34 | +parser.add_argument( |
| 35 | + '-mode', |
| 36 | + dest='mode', |
| 37 | + default='answer', |
| 38 | + type=str, |
| 39 | +) |
| 40 | +args = parser.parse_args() |
| 41 | + |
| 42 | + |
| 43 | +split = args.split |
| 44 | +mode = args.mode |
| 45 | +save_dir = f'bbox/{split}/{mode}' |
| 46 | + |
| 47 | +VCR_DIRECTORY = '' |
| 48 | +items = [json.loads(s) for s in open(f'{VCR_DIRECTORY}/annotation/{split}.jsonl', 'r')] |
| 49 | +img_dir = f'{VCR_DIRECTORY}/vcr1images' |
| 50 | + |
| 51 | +counter = 0 |
| 52 | +for i, item in enumerate(tqdm(items)): |
| 53 | + if i % args.num_folds != args.fold: |
| 54 | + continue |
| 55 | + counter += 1 |
| 56 | + |
| 57 | + mentions = [] |
| 58 | + objects = [] |
| 59 | + |
| 60 | + for word in item["question"]: |
| 61 | + if isinstance(word, list): |
| 62 | + mentions.extend([w for w in word if item["objects"][w] == "person"]) |
| 63 | + objects.extend([w for w in word if item["objects"][w] != "person"]) |
| 64 | + |
| 65 | + for ans in item["answer_choices"]: |
| 66 | + for word in ans: |
| 67 | + if isinstance(word, list): |
| 68 | + mentions.extend([w for w in word if item["objects"][w] == "person"]) |
| 69 | + objects.extend([w for w in word if item["objects"][w] != "person"]) |
| 70 | + |
| 71 | + if mode == 'rationale': |
| 72 | + for rat in item["rationale_choices"]: |
| 73 | + for word in rat: |
| 74 | + if isinstance(word, list): |
| 75 | + mentions.extend([w for w in word if item["objects"][w] == "person"]) |
| 76 | + objects.extend([w for w in word if item["objects"][w] != "person"]) |
| 77 | + |
| 78 | + mentions = list(set(mentions)) |
| 79 | + objects = list(set(objects)) |
| 80 | + |
| 81 | + image = Image.open(f'{img_dir}/{item["img_fn"]}').convert("RGBA") |
| 82 | + meta = json.load(open(f'{img_dir}/{item["metadata_fn"]}', 'r')) |
| 83 | + boxes = meta['boxes'] |
| 84 | + |
| 85 | + for i, box in enumerate(boxes): |
| 86 | + if i in mentions: |
| 87 | + color = color_list[:-1][i % (len(color_list) - 1)] |
| 88 | + elif i in objects: |
| 89 | + color = color_list[-1] |
| 90 | + else: |
| 91 | + continue |
| 92 | + |
| 93 | + box = [int(x) for x in box[:4]] |
| 94 | + x1, y1, x2, y2 = box |
| 95 | + shape = [(x1, y1), (x2, y1), (x2, y2), (x1, y2), (x1, y1)] |
| 96 | + |
| 97 | + overlay = Image.new('RGBA', image.size, tuple(color) + (0,)) |
| 98 | + draw = ImageDraw.Draw(overlay) |
| 99 | + draw.polygon(shape, fill=tuple(color) + (OPACITY,)) |
| 100 | + |
| 101 | + draw = ImageDraw.Draw(image) |
| 102 | + draw.line(shape, fill=tuple(color), width=7) |
| 103 | + |
| 104 | + image = Image.alpha_composite(image, overlay) |
| 105 | + |
| 106 | + image = image.convert("RGB") |
| 107 | + image.save(f'{save_dir}/{item["annot_id"]}.jpg') |
| 108 | + |
| 109 | + gc.collect() |
| 110 | + |
| 111 | +print(f'writing {counter} examples') |
| 112 | + |
0 commit comments