-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
72 lines (59 loc) · 2.27 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
from flask import Flask, request, Response,json
from flask_restful import Resource, Api
from json import dumps
from flask import jsonify
from werkzeug.utils import secure_filename
from requests_toolbelt import MultipartEncoder
import os
import subprocess
app = Flask(__name__, static_url_path='/static')
api = Api(app)
class Image(Resource):
def post(self):
file= open('static/result.txt', 'w')
file.write('9')
file.close()
#contain the input image from the http request
image = request.files.to_dict()
#Image Input Name
file_name = image["Input"].filename
#the directory in which the model start its operation on
inputFolder = "OSC-Project-EmoRecog/Input/"
#check IF the Input folder exits
if not os.path.isdir(inputFolder):
os.mkdir(inputFolder)
#saves the Input Image in the Input Folder
image["Input"].save(inputFolder+file_name)
#change directory because i hate python 2
mydir = os.getcwd() # would be the MAIN folder
mydir_tmp = mydir + "/OSC-Project-EmoRecog" # add the testA folder name
os.chdir(mydir_tmp) # change the current working directory
#call For The Model To start processing the Images
subprocess.run("python2 Core.py",shell=True)
#get back to python 2
os.chdir(mydir) # change the current working directory
print("Finished and now in "+ os.getcwd())
#delete The Input Images Form the Imput Folder
os.remove(inputFolder+file_name)
#get the Model Result
with open('static/result.txt') as text:
res = text.readline().split()
if res[0]=='9':
response = app.response_class(
status=422,
mimetype='application/json'
)
return response
#Data to be sent to the App
fields=({'emotion': res[0],
'Data':'static/'+file_name,
'status':200})
response = app.response_class(
response=json.dumps(fields),
status=200,
mimetype='application/json'
)
return response
api.add_resource(Image, '/')
if __name__ == '__main__':
app.run(port='5002')