-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
309 lines (259 loc) · 7.87 KB
/
main.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import argparse
import os
import glob
import time
import sys
sys.path.insert(0,'./utils')
from globalVariables import ret_dict,data,res_dict,LABELS_SWORD_COL,_2stream
CHEKPOINT = "./checkpoints"
WEIGHTS = "weights"
LABELS = "classes"
# settings for WampServer
php_webservice = "http://localhost/combine/webservices.php"
wamp_folder = 'C:/wamp64/www/combine/'
def get_sys_info(sys_name):
rgb_dir = None
oflow_dir = None
lstm_dir = None
labels = None
# find which words folder been chosen.
systems = glob.glob(os.path.join(CHEKPOINT,'*'))
systems = list(map(lambda s: s.rsplit(f'{os.sep}',1)[-1],systems))
if not sys_name in systems or len(systems) == 0:
raise ValueError(f"ERROR : could not find {sys_name} in {CHEKPOINT} directory.")
sys_path = os.path.join(CHEKPOINT,sys_name)
# get weights.
sys_weights = glob.glob(os.path.join(sys_path,WEIGHTS,'*.h5'))
if len(sys_weights) == 0:
raise ValueError(f"ERROR : no weights has been found in {WEIGHTS} folder.")
# find rgb,oflow,lstm,lstm_cpu
h5_files = ['rgb','oflow','lstm','cpu']
h5_dirs = {}
for h5_file in h5_files:
h5_dir = [weights for weights in sys_weights if h5_file in weights.lower()]
if len(h5_dir) > 1:
raise ValueError(f"ERROR : In {h5_dir[0].rsplit(os.sep,1)[0]} directory more than one {h5_file} file found.")
h5_dirs[h5_file] = h5_dir[0] if len(h5_dir) > 0 else None
# get labels file
sys_labels = glob.glob(os.path.join(sys_path,LABELS,'*.csv'))
if len(sys_labels) != 1:
raise ValueError(f"ERROR : something wrong with {LABELS} folder.")
return h5_dirs,sys_labels[0]
def print_sys_info(args):
print("running the system with:")
for arg in vars(args):
print(' '*3,f'{arg} = {getattr(args,arg)}')
if __name__ == '__main__' :
parser = argparse.ArgumentParser()
# --run
parser.add_argument(
'-run',
'--run',
dest='run_method',
type=str,
default='webcam',
help='choose a way to test the sign language system.')
parser.add_argument(
'-sys',
'--system',
dest='system_name',
type=str,
default='turkish_10_word',
help='choose which sign language system to run.')
parser.add_argument(
'-use_lstm',
'--use_lstm',
dest='use_lstm',
type=bool,
default=False,
help='add lstm on top of stream network.')
parser.add_argument(
'-rgb',
'--rgb_only',
dest='use_rgb',
type=bool,
default=True,
help='just use rgb stream.')
parser.add_argument(
'-oflow',
'--oflow_only',
dest='use_oflow',
type=bool,
default=False,
help='just use optical flow stream.')
parser.add_argument(
'-on_cpu',
'--use_cpu',
dest='on_cpu',
type=bool,
default=True,
help='run the system on cpu.')
parser.add_argument(
'-pred_type',
'--prediction_type',
dest='pred_type',
type=str,
default='word',
help='define how the system output will be, either word or sentence.')
parser.add_argument(
'-nTop',
'--top_predictions',
dest='nTop',
type=int,
default=3,
help='how many result(output) should the system give.')
parser.add_argument(
'-download',
'--download',
dest='download',
type=bool,
default=False,
help='download weights and classes to checkpoints directory.')
parser.add_argument(
'-mul_oflow',
'--multiprocessing_opticalflow',
dest='mul_oflow',
type=bool,
default=False,
help="faster optical flow calculation with multiprocessing.")
parser.add_argument(
'-oflow_pnum',
'--oflow_process_num',
dest='oflow_pnum',
type=int,
default=4,
help="number of processes to calculate optical flow.")
parser.add_argument(
'-mul_2stream',
'--multiprocessing_two_stream',
dest='mul_2stream',
type=bool,
default=False,
help='run two stream on different processes.')
# CPU OR GPU
# HOW MUCH FRACTION ON GPU DO YOU WANT TO USE
# WHICH GPU TO RUN ON
# WORDS OR SENTENCES
# SINGLE CPU OR MULTIPULE
# use just rgb or just oflow
# don't use lstm
args = parser.parse_args()
# run test script
run_method = args.run_method
use_lstm = args.use_lstm
use_rgb = args.use_rgb
use_oflow = args.use_oflow
on_cpu = args.on_cpu
pred_type = args.pred_type
nTop = args.nTop
download = args.download
mul_oflow = args.mul_oflow
oflow_pnum = args.oflow_pnum
mul_2stream = args.mul_2stream
system_name = args.system_name
# download model weights and labels
if download:
from checkpoints.download import download_sys
Dir = CHEKPOINT+os.sep+system_name
print(f"downloading weights and lables for {system_name} system to {Dir}.")
download_sys(system_name,Dir)
#load checkpoints and labels
models_dir,labels_dir = get_sys_info(system_name)
# informative message
print(f"In {args.system_name} folder:")
for k,v in models_dir.items():
if v is not None:
# informative message
print(f"{' '*4}{k.upper()} WEIGHTS found : {v.rsplit(os.sep,1)[-1]}")
# informative message
print(f"{' '*4}labels : {labels_dir.rsplit(os.sep,1)[-1]}")
# make sure that flags are set properlly
if use_rgb and use_oflow:
raise ValueError("""ERROR : both rgb and oflow flags are on.
trying to use both? set both flag to 'False'""")
if not pred_type == "word" and not pred_type == "sentence":
raise ValueError("ERROR : pred_type should be 'word' or 'sentence'")
con = mul_oflow and not oflow_pnum > 0
#notcon = not mul_oflow and oflow_pnum > 0
if con:
raise ValueError("ERROR : check mul_oflow and oflow_pnum flags.")
if not on_cpu and mul_2stream:
raise ValueError("ERROR : you can't use multiprocessing on streams while the system is running on gpu.")
if (use_rgb or use_oflow) and mul_2stream:
raise ValueError("ERROR : you can't do multiprocessing while using just one stream!.")
# print informative messages for what will be used next
print_sys_info(args)
# create tmp dir
os.makedirs('./tmp', exist_ok=True)
if on_cpu:
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
from collections import defaultdict
models = defaultdict(lambda : None)
from utils.util import load_models,csv_to_dict
from multiprocessing import Manager
from multiprocessing import Process
labels = csv_to_dict(labels_dir,LABELS_SWORD_COL)
if not mul_2stream:
# load labels
print(f"loading labels from {labels_dir}.")
labels = csv_to_dict(labels_dir,LABELS_SWORD_COL)
print(f"{len(labels)} word found in {labels_dir}")
# load models
uploading_time = time.time()
print("Initializing models")
models = load_models(models_dir,
on_cpu,
use_rgb,
use_oflow,
use_lstm,
False)
print(f"Uploading took {round(time.time()-uploading_time,2)} sec")
else:
models['oflow'] = 1
from utils.parallel_streams import nn_work
_2stream.append(Process(target=nn_work, args=('oflow',models_dir,labels_dir,pred_type,nTop,mul_oflow,oflow_pnum)))
_2stream.append(Process(target=nn_work, args=('rgb',models_dir,labels_dir,pred_type,nTop,mul_oflow,oflow_pnum)))
if use_lstm:
_2stream.append(Process(target=nn_work, args=('oflow',models_dir,labels_dir,pred_type,nTop,mul_oflow,oflow_pnum)))
for p in _2stream:
p.start()
print(f"{len(_2stream)} process has been initialized.")
# run some server with flags cpu gpu pred_type nTop
# if wamp
if run_method == "wamp":
print("running wamp server.")
from run.wamp import run_server
if not os.path.exists(wamp_folder):
raise ValueError(f"ERROR : can't find wamp service in {wamp_folder} directory")
# running wamp server
run_server(php_webservice,
wamp_folder,
models,
labels,
pred_type,
nTop,
mul_oflow,
oflow_pnum,
mul_2stream)
elif run_method == "webcam":
print("testing system on webcam, to close webcam press 'q'.")
from run.webcam import test
test(models,
labels,
pred_type,
nTop,
mul_oflow,
oflow_pnum,
mul_2stream)
elif run_method == "REST_API":
print("Initiate REST API server ...")
from run.REST_API import server
server.run(models,
labels,
pred_type,
nTop,
mul_oflow,
oflow_pnum,
mul_2stream,
host="0.0.0.0")