Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2021-005
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Hasini Piumika Alwis
2021-005
Commits
48bad758
Commit
48bad758
authored
Nov 26, 2021
by
Hasini Piumika Alwis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
app.py file
parent
abb135f6
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
85 additions
and
0 deletions
+85
-0
Backend/app.py
Backend/app.py
+85
-0
No files found.
Backend/app.py
0 → 100644
View file @
48bad758
import
cv2
import
os
import
json
import
random
import
io
import
numpy
as
np
from
anaconda_navigator.exceptions
import
exception_handler
from
flask
import
Flask
,
render_template
,
Response
,
request
,
send_from_directory
from
flask_cors
import
CORS
,
cross_origin
from
keras.models
import
load_model
,
model_from_json
from
base64
import
decodestring
from
emotion_detection
import
predict_image
app
=
Flask
(
__name__
)
cors
=
CORS
(
app
)
app
.
config
[
'CORS_HEADERS'
]
=
'Content-Type'
path
=
os
.
getcwd
()
# file Upload
UPLOAD_FOLDER
=
os
.
path
.
join
(
path
,
'uploads'
)
TEMP_FOLDER
=
os
.
path
.
join
(
path
,
'rendered'
)
if
not
os
.
path
.
isdir
(
TEMP_FOLDER
):
os
.
mkdir
(
TEMP_FOLDER
)
if
not
os
.
path
.
isdir
(
UPLOAD_FOLDER
):
os
.
mkdir
(
UPLOAD_FOLDER
)
app
.
config
[
'UPLOAD_FOLDER'
]
=
UPLOAD_FOLDER
app
.
config
[
'TEMP_FOLDER'
]
=
TEMP_FOLDER
ALLOWED_EXTENSIONS
=
set
([
'png'
,
'jpg'
,
'jpeg'
])
def
allowed_file
(
filename
):
return
'.'
in
filename
and
filename
.
rsplit
(
'.'
,
1
)[
1
]
.
lower
()
in
ALLOWED_EXTENSIONS
@
app
.
route
(
'/upload'
,
methods
=
[
'POST'
])
@
cross_origin
()
def
image_upload
():
if
request
.
method
==
'POST'
:
# check if the post request has the file part
if
'file'
not
in
request
.
files
:
# flash('No file part')
return
{
'data'
:
'No file part'
}
file
=
request
.
files
[
'file'
]
if
file
.
filename
==
''
:
# flash('No file selected for uploading')
return
{
'data'
:
'No file selected for uploading'
}
if
file
and
allowed_file
(
file
.
filename
):
upl_path
=
os
.
path
.
join
(
app
.
config
[
'UPLOAD_FOLDER'
],
file
.
filename
)
file
.
save
(
upl_path
)
predicted_image
,
result_image
=
predict_image
(
upl_path
)
_hash
=
str
(
random
.
getrandbits
(
128
))
cv2
.
imwrite
(
'rendered/result_'
+
_hash
+
'.jpg'
,
result_image
)
renders
=
os
.
path
.
join
(
app
.
root_path
,
app
.
config
[
'TEMP_FOLDER'
])
# response = flask.jsonify({'some': 'data'})
# response.headers.add('Access-Control-Allow-Origin', '*')
# print(type(result_image))
# return app.make_response((result_image.blob, 200,{'Access-Control-Allow-Origin':'*', 'Content-Type':'image/jpeg'}))
# response = send_file(io.BytesIO(result_image),
# mimetype='image/jpeg',as_attachment=True,attachment_filename='%s.jpg' % 1000)
# response.headers = {'Access-Control-Allow-Origin':'*', 'type':'image/jpeg'}
# return response
# response.content_type = 'image/jpeg'
return
send_from_directory
(
directory
=
renders
,
filename
=
'result_'
+
_hash
+
'.jpg'
)
else
:
# flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif')
return
{
'data'
:
'Allowed file types are png, jpeg, jpg'
}
@
app
.
route
(
"/test"
,
endpoint
=
'index()'
)
@
exception_handler
def
index
():
pass
if
__name__
==
"__main__"
:
app
.
run
(
host
=
'127.0.0.1'
,
port
=
5000
,
debug
=
True
)
print
(
"OK"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment