-
Notifications
You must be signed in to change notification settings - Fork 10
/
visualize.py
73 lines (60 loc) · 2.09 KB
/
visualize.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Visualize the relations in SpatialSense
import json
from PIL import Image, ImageDraw, ImageFont
import argparse
import random
import os
import pdb
parser = argparse.ArgumentParser(description="Visualize the relations in SpatialSense")
parser.add_argument(
"--num", type=int, default=10, help="The number of relations to visualize"
)
parser.add_argument(
"--dataroot",
default=".",
help="The directory where annotation.js and images reside",
)
parser.add_argument(
"--output",
default="./visualizations",
help="The directory to output visualizations",
)
parser.add_argument("--seed", default=1, help="The random seed")
args = parser.parse_args()
if os.path.exists(args.output):
os.system("rm -r " + args.output)
os.makedirs(args.output)
random.seed(args.seed)
relations = []
data = json.load(open("annotations.json"))
for img in data:
for annot in img["annotations"]:
annot["url"] = img["url"]
annot["width"] = img["width"]
annot["height"] = img["height"]
relations.append(annot)
relations = random.sample(relations, args.num)
def url2path(url):
if url.startswith("http"): # flickr
return os.path.join(args.dataroot, "images", "flickr", url.split("/")[-1])
else: # nyu
return os.path.join(args.dataroot, "images", "nyu", url.split("/")[-1])
for i, rel in enumerate(relations):
img = Image.open(url2path(rel["url"]))
vis = Image.new("RGB", (max(rel["width"], 300), rel["height"] + 40), color="white")
vis.paste(img)
draw = ImageDraw.Draw(vis)
fnt = ImageFont.truetype("arial.ttf", size=25)
s = "%s-%s-%s %s" % (
rel["subject"]["name"],
rel["predicate"],
rel["object"]["name"],
str(rel["label"]),
)
draw.text((10, rel["height"]), s, font=fnt, fill="black")
y0, y1, x0, x1 = rel["subject"]["bbox"]
draw.rectangle([x0, y0, x1, y1], outline="red", width=3)
y0, y1, x0, x1 = rel["object"]["bbox"]
draw.rectangle([x0, y0, x1, y1], outline="red", width=3)
vis.save(os.path.join(args.output, "%05d.jpg" % i))
print("Visualizations saved to", args.output)