Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
Heal_App
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
TMP-23-365
Heal_App
Commits
5d64f3ee
Commit
5d64f3ee
authored
Nov 01, 2023
by
Yomal-Dulanjana
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
app.py update
parent
e1a6c6d6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
0 deletions
+53
-0
ml/app.py
ml/app.py
+53
-0
No files found.
ml/app.py
View file @
5d64f3ee
from
inference
import
*
from
flask_cors
import
CORS
from
flask
import
Flask
,
Response
,
jsonify
,
request
from
tensorflow.keras.models
import
load_model
from
youtubesearchpython
import
VideosSearch
import
cv2
from
pymongo
import
MongoClient
import
threading
import
requests
import
numpy
as
np
# import schedule
import
time
import
random
from
datetime
import
datetime
from
mongodb_api
import
*
data
=
{
0
:
'Angry'
,
1
:
'Disgust'
,
2
:
'Fear'
,
3
:
'Happy'
,
4
:
'Sad'
,
5
:
'Surprise'
,
6
:
'Neutral'
}
model
=
load_model
(
'weights/model.h5'
)
app
=
Flask
(
__name__
)
CORS
(
app
)
...
...
@@ -41,6 +47,34 @@ def update_diet_plan():
time
.
sleep
(
60
)
def
pre_processing
(
img
):
img
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_BGR2GRAY
)
img
=
cv2
.
equalizeHist
(
img
)
img
=
img
/
255
return
img
def
emo_rec
(
img
):
img
=
np
.
asarray
(
img
)
img
=
cv2
.
resize
(
img
,
(
48
,
48
))
img
=
pre_processing
(
img
)
img
=
img
.
reshape
(
1
,
48
,
48
,
1
)
predict_x
=
model
.
predict
(
img
)
classes_x
=
np
.
argmax
(
predict_x
,
axis
=
1
)
return
data
[
classes_x
[
0
]],
int
(
classes_x
[
0
])
# Convert int64 to int
def
get_music_link
(
emotion
):
search_query
=
f
'{emotion} relaxing music'
videos_search
=
VideosSearch
(
search_query
,
limit
=
10
)
results
=
videos_search
.
result
()
video_links
=
[
video
[
'link'
]
for
video
in
results
[
'result'
]]
if
not
video_links
:
return
None
selected_link
=
random
.
choice
(
video_links
)
return
selected_link
thread1
=
threading
.
Thread
(
target
=
update_risk_level
)
thread1
.
start
()
...
...
@@ -102,6 +136,25 @@ def record():
)
@
app
.
route
(
'/predict'
,
methods
=
[
'POST'
])
def
predict_emotion_and_music
():
if
'image'
not
in
request
.
files
:
return
jsonify
({
'error'
:
'No image part'
})
image
=
request
.
files
[
'image'
]
image_np
=
np
.
fromstring
(
image
.
read
(),
np
.
uint8
)
image_cv2
=
cv2
.
imdecode
(
image_np
,
cv2
.
IMREAD_COLOR
)
emotion_label
,
emotion_class
=
emo_rec
(
image_cv2
)
music_link
=
get_music_link
(
emotion_label
)
if
music_link
is
None
:
return
jsonify
({
'emotion'
:
emotion_label
,
'class'
:
emotion_class
,
'message'
:
'No music found'
})
return
jsonify
({
'emotion'
:
emotion_label
,
'class'
:
emotion_class
,
'music_link'
:
music_link
})
if
__name__
==
"__main__"
:
app
.
run
(
debug
=
True
,
...
...
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