-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
69 lines (49 loc) · 1.8 KB
/
cli.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
import os
import requests
from cprint import cprint
from unipath import Path
from genome import VisualGenomeData
PROJECT_ROOT = Path(__file__).absolute().parent
RESULTS_DIR = PROJECT_ROOT.child('results')
visual_genome = VisualGenomeData()
visual_genome.load()
results = visual_genome.fetch_results()
good = 0
i = 0
while good < 10:
i += 1
cprint.info(f"Parsing result {i} / goods {good}...")
test_data = next(results)
response = requests.get(test_data.img_url)
if response.ok:
graph = test_data.get_graph()
img_names = []
for node in graph.nodes():
node_data = graph.nodes[node]
if not node_data:
continue
names = ' - '.join(node_data['names']) + '\n'
img_names.append(names)
rels_desc = []
for subject_id, object_id in graph.edges():
subject = graph.nodes[subject_id]
rel_object = graph.nodes[object_id]
if not (subject and rel_object):
continue
subjects = ' - '.join(subject['names'])
objects = ' - '.join(rel_object['names'])
predicate = graph.edges[subject_id, object_id]['predicate']
rels_desc.append(f"{subjects} {predicate} {objects}\n")
IMG_DIR = RESULTS_DIR.child(str(test_data.img_id))
if not IMG_DIR.exists():
os.mkdir(IMG_DIR)
with open(IMG_DIR.child('results.txt'), 'w') as fd:
fd.writelines(img_names)
fd.write('\n')
fd.write('----------------------\n')
fd.write('\n')
fd.writelines(rels_desc)
format = Path(test_data.img_url).name.split('.')[-1]
with open(IMG_DIR.child(f'{test_data.img_id}.{format}'), 'wb') as fd:
fd.write(response.content)
good += 1