-
Notifications
You must be signed in to change notification settings - Fork 4
/
step4_detect_assets_from_facades.py
117 lines (68 loc) · 2.91 KB
/
step4_detect_assets_from_facades.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import pandas as pd
import argparse
import os
import subprocess
import streetview as sv
import sys
from tqdm import tqdm
import json
import multiprocessing
import numpy as np
import skimage.io
from options.asset_base_options import AssetBaseOptions
from util import filter_properties
def detect_assets(opt, asset_list, start_point, end_point, core):
f_handler = open(os.path.join(opt.log_folder, str(core) + '.log'), 'w')
std_f = sys.stdout
sys.stdout = f_handler
sys.stdout.write('start is {} number {}, end is {} number {}\n'.format(opt.asset_type, start_point, opt.asset_type,
min(end_point, len(asset_list)) -1))
sys.stdout.flush()
with open(opt.asset_detection_result) as f:
asset_detection_result = json.load(f)
for asset_name in asset_list[start_point:end_point]:
facade_name = asset_detection_result[asset_name]['facade_name']
facade_name_path = os.path.join(opt.facade_folder, facade_name)
if os.path.exists(facade_name_path):
asset_img_path = os.path.join(Asset_folder, asset_name)
if not os.path.exists(asset_img_path):
bbox = asset_detection_result[asset_name]['box']
im = skimage.io.imread(facade_name_path)
result_im = im[int(bbox[1]):int(bbox[1] + bbox[3]), int(bbox[0]):int(bbox[0] + bbox[2]), :]
skimage.io.imsave(asset_img_path, result_im)
print(asset_name + ' has been saved')
else:
print(asset_name + ' has already been saved before')
else:
print(asset_name + ' does not have corresponding facade image')
sys.stdout.flush()
sys.stdout.close()
sys.stdout = std_f
if __name__=='__main__':
opt = AssetBaseOptions().parse()
Asset_folder = opt.asset_folder
if not os.path.exists(Asset_folder):
os.makedirs(Asset_folder)
# Asset_img_folder = Asset_folder
# if not os.path.exists(Asset_img_folder):
# os.makedirs(Asset_img_folder)
Asset_log_folder = os.path.join('logs', os.path.basename(opt.asset_folder))
if not os.path.exists(Asset_log_folder):
os.makedirs(Asset_log_folder)
# df_properties = pd.read_csv(opt.properties_file)
df_properties = filter_properties(opt)
asset_list = df_properties['name'].tolist()
asset_list.sort()
opt.log_folder = Asset_log_folder
print('start')
processing_list = []
step_num = np.int(np.ceil(len(asset_list) / opt.cores))
for i in range(opt.cores):
processing_list.append(
multiprocessing.Process(target=detect_assets,
args=(opt, asset_list, i*step_num, (i+1)*step_num, i)))
for i in range(opt.cores):
processing_list[i].start()
for i in range(opt.cores):
processing_list[i].join()
print('finished')