-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
157 lines (95 loc) · 3.32 KB
/
app.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import cv2
import argparse
import os
import re
import sys
import tarfile
from shutil import copy
from flask import Flask,render_template, request,redirect, url_for,jsonify
from werkzeug import secure_filename
import _thread
from threading import Thread
import numpy as np
import pickle
from six.moves import urllib
import string
import random
from numpy import genfromtxt
from main import compute
from hist import GetTransformedImage
from feature_extraction import extract_features
FLAGS = None
app =Flask(__name__)
PathForUploads=""
PathForOutputs=""
OUTPUT_FOLDER='static'+'/'+'img'+'/'+'output_Dir'
UPLOAD_FOLDER='uploads'
EmotionalDist=[]
app.config['UPLOAD_FOLDER']=UPLOAD_FOLDER
ALLOWED_EXTENSIONS = set([ 'png', 'jpg', 'jpeg', 'gif'])
target_hlfeat = genfromtxt('input_Dir/' + 'emo6_feat.csv', delimiter=',')
def main(_):
global tmpimage
image = (FLAGS.image_file if FLAGS.image_file else
os.path.join(FLAGS.model_dir, tmpimage))
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def calculate_features_for_target_image(image):
extract_features(UPLOAD_FOLDER,image)
return 1
@app.route('/',methods=['GET'])
def index():
return render_template('index.html')
@app.route('/upload',methods=['POST'])
def UploadFile():
global tmpimage
if 'file' not in request.files:
return jsonify({"success":False})
File = request.files['file']
if File.filename=='':
flash('No selected file')
return jsonify({"success":False})
if File and allowed_file(File.filename):
Extention =File.filename.rsplit('.', 1)[1].lower()
name=id_generator()+'.'+Extention
File.filename=name
PathForUploads=os.path.join(app.config['UPLOAD_FOLDER'],secure_filename(File.filename))
tmpimage=name
File.save(PathForUploads)
return jsonify({"success":True})
else:
return jsonify({"success":False})
@app.route('/my_endpoint', methods=['GET','POST'])
def my_endpoint_handler():
global PathForOutputs
global EmotionalDist
if request.is_json:
my_new_object = request.get_json()
EmotionalDist=my_new_object["x"]
print(EmotionalDist)
def handle_sub_view():
global PathForUploads
global tmpimage
global EmotionalDist
with app.test_request_context():
calculate_features_for_target_image(tmpimage)
des_images,accumu_dist,works=compute(tmpimage,EmotionalDist,target_hlfeat)
image = cv2.imread(UPLOAD_FOLDER+"/"+tmpimage)
if des_images!=None:
GetTransformedImage(image, des_images, accumu_dist, OUTPUT_FOLDER+"/"+tmpimage)
else:
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imwrite(OUTPUT_FOLDER+"/"+tmpimage,gray_image)
# Do the processing here
thread=Thread(target=handle_sub_view)
thread.start()
thread.join()
return jsonify(OUTPUT_FOLDER+"/"+tmpimage)
if __name__ == '__main__':
app.run(threaded=True)