-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestV1.py
197 lines (155 loc) · 4.39 KB
/
testV1.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import os
import sys
from subprocess import Popen, PIPE, STDOUT
from collections import OrderedDict
ver = sys.version_info[0]
def get_config(argv):
for i in range(len(argv) - 1):
if argv[i] == "-c":
return argv[i+1]
return "spcbir.config"
def get_config_param(config, param, default = ""):
f = open(config)
value = default
for line in f.readlines():
line = line.strip()
if line == "#" or line == "":
continue
if line.find("=") >= 0:
if line.startswith(param):
value = line.split("=")[1].strip()
f.close()
return value
def open_exe(argv):
exe_list = argv[1:]
print("running " + " ".join(exe_list) + "...")
return Popen(exe_list, stdout=PIPE, stderr=STDOUT, stdin=PIPE)
def send_input(proc, config):
image_dir = get_config_param(config, "spImagesDirectory")
assert(image_dir != "")
image_prefix = get_config_param(config, "spImagesPrefix")
assert(image_prefix != "")
image_suffix = get_config_param(config, "spImagesSuffix")
assert(image_suffix != "")
numOfImages_str = get_config_param(config, "spNumOfImages")
assert(numOfImages_str != "")
numOfImages = int(numOfImages_str)
for i in range(numOfImages):
line = image_dir + image_prefix + str(i) + image_suffix + '\n'
if ver < 3:
proc.stdin.write(line)
else:
proc.stdin.write(bytes(line, 'UTF-8'))
if ver < 3:
proc.stdin.write('<>')
else:
proc.stdin.write(bytes('<>', 'UTF-8'))
def output_to_dict(out):
if ver >= 3:
out=out.decode('UTF-8')
lines = out.replace("\r\n", "\n").split("\n")
query = ""
closest = []
results = OrderedDict()
for i, line in enumerate(lines):
if line.startswith("Best"):
query = line.split("-")[1].strip()
elif line.startswith("Please") and query != "":
results[query] = closest
query = ""
closest = []
elif query != "":
closest.append(line)
return results
def dict_to_html(config, results):
num_of_similar_images = int(get_config_param(config, "spNumOfSimilarImages", "1"))
html = """
<html>
<head>
<title>Software Project - Final Project - Results</title>
<style>
body {
text-align: center;
}
table {
border 0px;
margin: 10px auto;
padding: 0px;
border-spacing: 0px;
border-collapse: collapse;
}
tr {
margin: 0px;
padding: 0px;
}
td {
border: 1px solid black;
padding: 5px;
text-align: center;
width: 80px;
}
td.query, th.query {
background-color: #fafafa;
border-right: 3px solid black !important;
}
figure {
margin: 0px;
}
img {
height: 70px;
}
</style>
</head>
<body>\n"""
html += '''
<table>
<tr>
<th class="query">query</td>\n'''
for i in range(num_of_similar_images):
html += '\t\t\t<th>no. ' + str(i+1) + '</th>'
html += '''\t\t</tr>\n'''
for query in results:
closest = results[query]
html += '''
<tr>
<td class="query" style="width: 70px;">
<figure>
<img src="''' + query + '''" title="''' + query + '''" />
<figcaption>''' + query + '''</figcaption>
</figure>
</td>\n'''
for image in closest:
html += '''
<td>
<figure>
<img src="''' + image + '''" title="''' + image + '''" />
<figcaption>''' + image + '''</figcaption>
</figure>
</td>\n'''
html += "\t\t</tr>\n"
html += "\t\t</table>\n"
html += """
</body>
</html>"""
return html
def main(argv):
proc = open_exe(argv);
config = get_config(argv)
send_input(proc, config)
results = output_to_dict(proc.communicate()[0])
html = dict_to_html(config, results)
f = open(os.path.splitext(config)[0] + ".html", "w")
f.write(html)
f.close()
if __name__ == "__main__":
if len(sys.argv) > 1:
args = sys.argv
else:
if ver < 3:
command = raw_input("Enter Command line: ").strip().split(" ")
else:
command = input("Enter Command line: ").strip().split(" ")
args = sys.argv + command
print("sarting...")
main(args)
print("done!")