-
Notifications
You must be signed in to change notification settings - Fork 8
/
v1like_extract_fromcsv.py
157 lines (122 loc) · 4.8 KB
/
v1like_extract_fromcsv.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# TODO: clean + pylint
import optparse
from pprint import pprint
import csv
import sys
import os.path as path
from v1like_extract import v1like_extract
import scipy as sp
from npprogressbar import *
DEFAULT_INPUT_PATH = "./"
DEFAULT_NPROCESSORS = 1
DEFAULT_OVERWRITE = False
DEFAULT_VERBOSE = False
verbose = DEFAULT_VERBOSE
# ------------------------------------------------------------------------------
def pv1like_extract(params):
config_fname, input_fname, output_fname, overwrite = params
return v1like_extract(config_fname,
input_fname,
output_fname,
overwrite = overwrite)
# ------------------------------------------------------------------------------
def v1like_extract_fromcsv(config_fname,
input_csv_fname,
output_suffix,
input_path = DEFAULT_INPUT_PATH,
nprocessors = DEFAULT_NPROCESSORS,
overwrite = DEFAULT_OVERWRITE,
):
assert(nprocessors >= 1)
csvr = csv.reader(open(input_csv_fname))
rows = [ row for row in csvr ]
fnames = sp.array([ row[:-2] for row in rows ]).ravel()
#fnames.sort()
sp.random.shuffle(fnames) # shuffle to enable multiple instances to run in //
nfnames = len(fnames)
# -- set up progress bar
widgets = [RotatingMarker(), " Progress: ", Percentage(), " ",
Bar(left='[',right=']'), ' ',
#" (", FilenameUpdate(fnames), ") ", ETA()]
ETA()]
pbar = ProgressBar(widgets=widgets, maxval=nfnames)
# - should we use multiprocessing ?
if nprocessors > 1:
# -- prepare all parameters
import multiprocessing as mp
pool = mp.Pool(nprocessors)
print "Creating list of parameters for multiprocessing..."
params = []
for fname in fnames:
input_fname = path.join(input_path, fname)
output_fname = input_fname + output_suffix
params += [(config_fname,
input_fname,
output_fname,
overwrite)]
# -- async iterator map
done = pool.imap_unordered(pv1like_extract, params)
# -- update progress bar
print "Processing %d images..." % (nfnames)
i = 1
pbar.start()
for _ in done:
pbar.update(i)
i += 1
else:
# -- process images
pbar.start()
for i, fname in enumerate(fnames):
input_fname = path.join(input_path, fname)
output_fname = input_fname + output_suffix
v1like_extract(config_fname,
input_fname,
output_fname,
overwrite = overwrite)
pbar.update(i+1)
pbar.finish()
print
# ------------------------------------------------------------------------------
def main():
usage = "usage: %prog [options] <config_filename> <input_csv_filename> <output_suffix>"
parser = optparse.OptionParser(usage=usage)
parser.add_option("--input_path", "-i",
default=DEFAULT_INPUT_PATH,
type="str",
metavar="STR",
help="[default=%default]")
parser.add_option("--nprocessors",
default=DEFAULT_NPROCESSORS,
type="int",
metavar="INT",
help="number of processors to use (with multiprocessing if > 1) [default=%default]")
parser.add_option("--overwrite",
default=DEFAULT_OVERWRITE,
action="store_true",
help="overwrite existing file [default=%default]")
parser.add_option("--verbose", "-v" ,
default=DEFAULT_VERBOSE,
action="store_true",
help="[default=%default]")
opts, args = parser.parse_args()
if len(args) != 3:
parser.print_help()
else:
config_fname = args[0]
input_csv_fname = args[1]
output_suffix = args[2]
global verbose
if opts.verbose:
verbose = True
v1like_extract_fromcsv(config_fname,
input_csv_fname,
output_suffix,
input_path = opts.input_path,
nprocessors = opts.nprocessors,
overwrite = opts.overwrite,
)
# ------------------------------------------------------------------------------
if __name__ == "__main__":
main()