-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
46 lines (40 loc) · 1.87 KB
/
utils.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
import os, time
import tempfile
import redis
# import subprocess
# Helper Functions
def handle_uploaded_image(f):
filename = next(tempfile._get_candidate_names())
current_dirname = os.path.dirname(os.path.abspath(__file__))
filepath = current_dirname + '/images/' + filename
os.mkdir(filepath)
with open(filepath + '/' + f.name, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
# proc = subprocess.Popen('th ' + current_dirname + '/caption_image.lua -model ' + current_dirname + '/neuraltalk2/model_id1-501-1448236541.t7_cpu.t7 -image_folder ' +
# filepath + ' -num_images 1 -gpuid -1 -caption_file_name ' + filename, shell=True, stdout=subprocess.PIPE)
# output = proc.communicate()[0]
# os.system('/home/sanuj/torch/install/bin/th ' + current_dirname + '/caption_image.lua -model ' + current_dirname + '/../model_id1-501-1448236541.t7_cpu.t7 -image_folder ' +
# filepath + ' -num_images 1 -gpuid -1 -caption_file_name ' + filename)
r = redis.StrictRedis(host='localhost', port=6379, db=0)
r.lpush('imagequeue', filepath)
# caption_filepath = current_dirname + '/captions/' + filename + '.txt'
# caption_file = open(caption_filepath, 'r')
# output = caption_file.read()
# caption_file.close()
#
# os.remove(filepath + f.name)
# os.rmdir(filepath)
# os.remove(caption_filepath)
timeout = 10
while r.get(filename) is None and timeout:
time.sleep(0.5)
timeout -= 0.5
os.remove(filepath + '/' + f.name)
os.rmdir(filepath)
if r.get(filename):
return r.get(filename)
else:
return "Error :("
# return 'th ' + current_dirname + '/caption_image.lua -model ' + current_dirname + '/neuraltalk2/model_id1-501-1448236541.t7_cpu.t7 -image_folder ' +
# filepath + ' -num_images 1 -gpuid -1 -caption_file_name ' + filename