-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbearid.py
executable file
·201 lines (181 loc) · 6.89 KB
/
bearid.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
198
199
200
201
#! /usr/bin/python3
import sys
import os
import argparse
# import xml_utils as u
# import datetime
import pdb
import subprocess
# from collections import defaultdict
##------------------------------------------------------------
## can be called like this:
##
## bearid <image_file>
## bearid <image_dir>
##
##------------------------------------------------------------
##------------------------------------------------------------
# How this program works:
#
# create file xml; NOTE: use -r to recursively search for images files
# img = imglab -c also.xml /home/data/bears/imageSource/britishColumbia/melanie_20170828/bc_also
# get faces for those images images
# bearface = bearface ~/dev/bearid-models/bearface_network.dat also.xml
# create chips of faces
# bearchip = bearchip also_faces.xml
# create embeddings for the faces
# bearembed = bearembed --embed ~/dev/bearid-models/bearembed_network.dat also_faces_chips.xml
# classify NOTE: input dat files are hard coded
# bearsvm = bearsvm --infer also_faces_chips_embeds.xml
def main (argv) :
cmds = []
outfiles = []
printfiles = []
# imgs_xml = "imgs.xml"
# faces_xml = "imgs_faces.xml"
# chips_xml = "imgs_faces_chips_xml"
# embeds_xml = "imgs_faces_chips_embed.xml"
outdir = './'
outdir = './result/'
imgs_xml = outdir + "imgs.xml"
faces_xml = outdir + "imgs_faces.xml"
chips_xml = outdir + "imgs_faces_chips.xml"
embeds_xml = outdir + "imgs_faces_chips_embeds.xml"
imglab = "/usr/local/bin/imglab"
dirname = os.path.dirname(__file__)
#--------------------------------------------------
# list all required files here.
#--------------------------------------------------
files_dep = [] # list of files required to exist
files_dep.append (imglab)
bearface = dirname + "/bearface"
files_dep.append (bearface)
bearchip = dirname + "/bearchip"
files_dep.append (bearchip)
bearembed = dirname + "/bearembed"
files_dep.append (bearembed)
bearsvm = dirname + "/bearsvm"
files_dep.append (bearsvm)
bearface_network = dirname + "/bearface_network.dat"
files_dep.append (bearface_network)
bearembed_network = dirname + "/bearembed_network.dat"
files_dep.append (bearembed_network)
bearsvm_network = dirname + "/bearsvm_network.dat"
files_dep.append (bearsvm_network)
bearsvm_ids = dirname + "/bearsvm_network_ids.dat"
files_dep.append (bearsvm_ids)
#--------------------------------------------------
# validate that all those files exist
#--------------------------------------------------
print ('\n... validating files... ')
missing_count = 0
for f in files_dep :
if os.path.exists (f) :
print ('\tfile OK : ', f )
else :
print ('\tfile NOT found : ', f )
missing_count += 1
if not os.path.exists (outdir) :
print ('... creating ', outdir, ' directory.')
os.mkdir (outdir)
if missing_count > 0 :
print ('\n\t----------------------------------------------------------')
print ('\t Unable to continue, above files are required to proceed.')
print ('\t----------------------------------------------------------\n')
return
#--------------------------------------------------
# commands should look something like:
# imglab -c imgs.xml <img/dir/file>
# bearface ~/dev/bearid-models/bearface_network.dat imgs.xml
# bearchip imgs_faces.xml
# bearembed --embed ~/dev/bearid-models/bearembed_network.dat imgs_faces_chips_xml
# bearsvm --infer imgs_faces_chips_embed.xml
#--------------------------------------------------
echo = 'echo '
echo = ''
run = 'test'
run = 'bearid'
args = ''
for a in sys.argv[1:] :
args += ' '
args += a
if run == 'bearid' :
cmds.append (echo + imglab + " -c " + imgs_xml + " " + args)
outfiles.append ("imglab.out")
cmds.append (echo + bearface + " --infer " + bearface_network + " " + imgs_xml)
outfiles.append ("bearface.out")
cmds.append (echo + bearchip + " " + faces_xml)
outfiles.append ("bearchip.out")
cmds.append (echo + bearembed + " --embed " + bearembed_network + " " + chips_xml)
outfiles.append ("bearembed.out")
cmds.append (echo + bearsvm + " --infer " + bearsvm_network + " " + embeds_xml)
outfiles.append ("bearsvm.out")
printfiles.append ("bearsvm.out")
elif run == 'test' :
cmds.append (echo + imglab + " -c " + imgs_xml + " " + args)
outfiles.append ("imglab.out")
cmds.append (echo + bearface + " --infer " + bearface_network + " " + imgs_xml)
outfiles.append ("bearface.out")
cmds.append (echo + bearchip + " " + faces_xml)
outfiles.append ("bearchip.out")
chips_xml = "test_output/chips_validate.xml"
embeds_xml = "test_output/chips_validate_embeds.xml"
cmds.append (echo + bearembed + " --test " + bearembed_network + " " + chips_xml)
outfiles.append ("bearembed.out")
cmds.append (echo + bearsvm + " --test " + bearsvm_network + " " + embeds_xml)
outfiles.append ("bearsvm.out")
printfiles.append ("bearsvm.out")
# for cmd in cmds :
i = 0
cmdlen = len (cmds)
while i < cmdlen :
cmd = cmds[i]
outfile = outfiles[i]
# print ('--------------------------------------------------')
outf = open (outfile, 'w')
print ('\nrunning: ', cmd)
process = subprocess.Popen(cmd.split(), stdout=outf, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if process.returncode != 0 :
print ('return code : ', process.returncode)
# print ('--------------------------------------------------')
i += 1
i = 0
while i < len (printfiles) :
print ('printing content of: ', printfiles[i])
with open (printfiles[i], 'r') as fin :
print (fin.read())
i += 1
# process = subprocess.Popen(cmd.split(),
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE)
# print ('stdout : ', stdout.decode ('utf-8'))
# print ('stderr : ', stderr.decode ('utf-8'))
# parser = argparse.ArgumentParser(description='\nPrint combined stats for all input files/directory.\n \t example: xml_obj_stats -l xxx_*_xml outputdir',
# formatter_class=lambda prog: argparse.HelpFormatter(prog,max_help_position=50))
# parser.formatter.max_help_position = 50
# parser.add_argument ('files', nargs='+')
# parser.add_argument ('-filetype', '--filetype', default="chips",
# help='type of xml file: <chips,faces,pairs> .')
# parser.add_argument ('-write', '--write', default="",
# action="store_true",
# help='write stats into file stats_*_<currentDate> .')
# parser.add_argument ('-l', '--ls', default="",
# action="store_true",
# help='print ordered list of filenames.')
# parser.add_argument ('-v', '--verbosity', type=int, default=1,
# choices=[0, 1, 2, 3], help=argparse.SUPPRESS)
# help="increase output verbosity"
# args = parser.parse_args()
# print "ls : ", args.ls
# print "files: ", args.files
# u.set_verbosity (args.verbosity)
# xml_files = u.generate_xml_file_list (args.files)
# u.get_obj_stats (xml_files, args.ls, args.filetype, args.verbosity, args.write)
if __name__ == "__main__":
main (sys.argv)
##------------------------------------------------------------
#
#
#
##------------------------------------------------------------