Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
22_23-J 53
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
22_23-J 53
22_23-J 53
Commits
b1b27468
Commit
b1b27468
authored
May 11, 2023
by
Harshana Supun Buddhika Abeysinghe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Flask App for the web application
parent
54305b85
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
346 additions
and
0 deletions
+346
-0
app.py
app.py
+346
-0
No files found.
app.py
0 → 100644
View file @
b1b27468
from
ctypes
import
resize
from
flask
import
Flask
,
request
from
flask
import
render_template
from
PIL
import
Image
from
tensorflow.keras.models
import
load_model
import
os
import
cv2
import
imghdr
from
matplotlib
import
pyplot
as
plt
import
tensorflow
as
tf
import
numpy
as
np
import
torch
from
transformers
import
AutoTokenizer
,
AutoModelForSequenceClassification
#--Manoj Libraries
import
pickle
import
nltk
from
datetime
import
datetime
nltk
.
download
(
'punkt'
)
from
keras.preprocessing.text
import
Tokenizer
from
tensorflow.keras.preprocessing.sequence
import
pad_sequences
import
jinja2
body_position_model
=
load_model
(
os
.
path
.
join
(
'models/Harshana'
,
'bodyPositions.h5'
))
eye_emotion_model
=
load_model
(
os
.
path
.
join
(
'models/Harshana'
,
'eyeEmotions.h5'
))
face_emotion_model
=
load_model
(
os
.
path
.
join
(
'models/Harshana'
,
'faceEmotions2.h5'
))
happy_sad_model
=
load_model
(
os
.
path
.
join
(
'models/Harshana'
,
'happysad.h5'
))
emotions_model
=
load_model
(
os
.
path
.
join
(
'models/Pawan'
,
'emotion_model.h5'
))
eye_movement_model
=
load_model
(
os
.
path
.
join
(
'models/Pawan'
,
'eye_moment.h5'
))
gender_detection_model
=
load_model
(
os
.
path
.
join
(
'models/Pawan'
,
'gender_detection.h5'
))
object_detection_model
=
load_model
(
os
.
path
.
join
(
'models/Pawan'
,
'object_detection_model.h5'
))
depression_text_model
=
load_model
(
os
.
path
.
join
(
'models/Manoj'
,
'Bi-LSTM.h5'
))
app
=
Flask
(
__name__
)
@
app
.
route
(
'/'
)
def
home
():
return
render_template
(
'index.html'
)
@
app
.
route
(
'/button-clicked-image'
)
def
upload
():
return
render_template
(
'depression_ic_home.html'
)
@
app
.
route
(
'/button-clicked-text'
)
def
upload2
():
return
render_template
(
'text.html'
)
@
app
.
route
(
'/upload-image'
,
methods
=
[
'POST'
])
def
upload_file
():
image_file
=
request
.
files
[
'image'
]
image
=
Image
.
open
(
image_file
)
image
.
save
(
'uploaded_image.jpg'
)
#uploaded image
uploaded_image
=
cv2
.
imread
(
'uploaded_image.jpg'
)
def
input_prepare
(
image
):
image
=
image
[:,:,:
1
]
image
=
cv2
.
resize
(
image
,
(
64
,
64
))
image
=
image
/
255
# normalize
image
=
image
.
reshape
(
-
1
,
4096
)
# reshaping
return
image
def
input_prepare_suicide
(
image
):
image
=
image
[:,:,
1
]
image
=
cv2
.
resize
(
image
,
(
48
,
48
))
image
=
image
/
255
image
=
image
.
reshape
(
-
1
,
48
,
48
,
1
)
return
image
#-------------------------------------------------------Body Position classification------------------------------------------------------------------
bodyposition_reshape_img
=
input_prepare
(
uploaded_image
)
body_position_answers
=
body_position_model
.
predict
(
bodyposition_reshape_img
)
body_position_answers_max_index
=
np
.
argmax
(
body_position_answers
)
if
body_position_answers_max_index
==
0
:
answer1
=
"Predicted as looking Distracted
\n
"
elif
body_position_answers_max_index
==
1
:
answer1
=
"Predicted as looking Down
\n
"
elif
body_position_answers_max_index
==
2
:
answer1
=
"Predicted as looking Straight
\n
"
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------Eye Emotions classification------------------------------------------------------------------
eyeemotion_reshape_img
=
input_prepare
(
uploaded_image
)
eye_emotion_answers
=
eye_emotion_model
.
predict
(
eyeemotion_reshape_img
)
eye_emotion_answers_max_index
=
np
.
argmax
(
eye_emotion_answers
)
if
eye_emotion_answers_max_index
==
0
:
answer2
=
"Predicted eye emotion as Anger
\n
"
elif
eye_emotion_answers_max_index
==
1
:
answer2
=
"Predicted eye emotion as Disgust
\n
"
elif
eye_emotion_answers_max_index
==
2
:
answer2
=
"Predicted eye emotion as Fear
\n
"
elif
eye_emotion_answers_max_index
==
3
:
answer2
=
"Predicted eye emotion as Happy
\n
"
elif
eye_emotion_answers_max_index
==
4
:
answer2
=
"Predicted eye emotion as Sad
\n
"
elif
eye_emotion_answers_max_index
==
5
:
answer2
=
"Predicted eye emotion as Surprise
\n
"
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------Face Emotions classification----------------------------------------------------------------
face_img
=
uploaded_image
[:,:,:
1
]
face_image_resized
=
tf
.
image
.
resize
(
face_img
,
(
56
,
56
))
face_emotion_answers
=
face_emotion_model
.
predict
(
np
.
expand_dims
(
face_image_resized
/
255
,
0
))
face_emotion_answers_max_index
=
np
.
argmax
(
face_emotion_answers
)
if
face_emotion_answers_max_index
==
0
:
answer3
=
"Predicted face emotion as Angry
\n
"
elif
face_emotion_answers_max_index
==
1
:
answer3
=
"Predicted face emotion Disgust
\n
"
elif
face_emotion_answers_max_index
==
2
:
answer3
=
"Predicted face emotion Fear
\n
"
elif
face_emotion_answers_max_index
==
3
:
answer3
=
"Predicted face emotion as Happy
\n
"
elif
face_emotion_answers_max_index
==
4
:
answer3
=
"Predicted face emotion as Neutral
\n
"
elif
face_emotion_answers_max_index
==
5
:
answer3
=
"Predicted face emotion as Sad
\n
"
elif
face_emotion_answers_max_index
==
6
:
answer3
=
"Predicted face emotion as Surprise
\n
"
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------Happy Sad Image classification----------------------------------------------------------------
happysad_reshape_img
=
tf
.
image
.
resize
(
uploaded_image
,
(
256
,
256
))
happy_sad_answers
=
happy_sad_model
.
predict
(
np
.
expand_dims
(
happysad_reshape_img
/
255
,
0
))
happy_sad_answers
if
happy_sad_answers
>
0.5
:
answer4
=
"Predicted as sad
\n
"
else
:
answer4
=
"Predicted as happy
\n
"
final_answer
=
"Body Position : "
+
answer1
+
"Eye emotions :"
+
answer2
+
"Face Emotions :"
+
answer3
+
"Happy Sad :"
+
answer4
#--------------------------------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------- PAWAN -----------------------------------------------------------------------------------
#------------------------------------------------------------Emotion Detection--------------------------------------------------------------------------
emotion_detection_img
=
uploaded_image
emotion_detection_reshape_img
=
input_prepare_suicide
(
emotion_detection_img
)
emotion_detection_answers
=
emotions_model
.
predict
(
emotion_detection_reshape_img
)
emotion_detection_answer_max
=
np
.
argmax
(
emotion_detection_answers
)
if
emotion_detection_answer_max
==
0
:
answer5
=
"Predicted emotion as Angry
\n
"
elif
emotion_detection_answer_max
==
1
:
answer5
=
"Predicted emotion Disgust
\n
"
elif
emotion_detection_answer_max
==
2
:
answer5
=
"Predicted emotion Fear
\n
"
elif
emotion_detection_answer_max
==
3
:
answer5
=
"Predicted emotion as Happy
\n
"
elif
emotion_detection_answer_max
==
4
:
answer5
=
"Predicted emotion as Neutral
\n
"
elif
emotion_detection_answer_max
==
5
:
answer5
=
"Predicted emotion as Sad
\n
"
elif
emotion_detection_answer_max
==
6
:
answer5
=
"Predicted emotion as Surprise
\n
"
#--------------------------------------------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------Eye Movement Detection--------------------------------------------------------------------------
eye_detection_img
=
uploaded_image
eye_detection_reshape_img
=
input_prepare_suicide
(
eye_detection_img
)
eye_detection_answers
=
eye_movement_model
.
predict
(
eye_detection_reshape_img
)
eye_detection_answer_max
=
np
.
argmax
(
eye_detection_answers
)
if
eye_detection_answer_max
==
0
:
answer6
=
"Predicted as Close Eyes
\n
"
elif
eye_detection_answer_max
==
1
:
answer6
=
"Predicted as Open Eyes
\n
"
#--------------------------------------------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------Gender Detection--------------------------------------------------------------------------
gender_detection_img
=
uploaded_image
gender_detection_reshape_img
=
input_prepare_suicide
(
gender_detection_img
)
gender_detection_answers
=
gender_detection_model
.
predict
(
gender_detection_reshape_img
)
gender_detection_answer_max
=
np
.
argmax
(
gender_detection_answers
)
if
gender_detection_answer_max
==
0
:
answer7
=
"Predicted gender as Man
\n
"
elif
gender_detection_answer_max
==
1
:
answer7
=
"Predicted gender as Woman
\n
"
#--------------------------------------------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------Object Detection--------------------------------------------------------------------------
object_detection_img
=
uploaded_image
object_detection_reshape_img
=
cv2
.
resize
(
object_detection_img
,
(
224
,
224
))
object_detection_reshape_img
=
object_detection_reshape_img
/
255
object_detection_reshape_img
=
object_detection_reshape_img
.
reshape
(
-
1
,
224
,
224
,
3
)
object_detection_answers
=
object_detection_model
.
predict
(
object_detection_reshape_img
)
object_detection_answer_max
=
np
.
argmax
(
object_detection_answers
)
if
object_detection_answer_max
==
0
:
answer8
=
"Predicted the object as Dangerous
\n
"
elif
object_detection_answer_max
==
1
:
answer8
=
"Predicted the object as Safe
\n
"
#--------------------------------------------------------------------------------------------------------------------------------------------------------
final_answer
=
final_answer
+
answer5
+
answer6
+
answer7
+
answer8
return
render_template
(
'result.html'
,
output
=
final_answer
.
replace
(
'
\n
'
,
'<br>'
))
#--------------------------------------------------------------------------------------------------------------------------------------------------------
@
app
.
route
(
'/upload-text'
,
methods
=
[
'POST'
])
def
upload_text
():
#----------------------------------------------------------------------- Ridma -------------------------------------------------------------------------------
text
=
request
.
form
[
'post-content'
]
suicide_text
=
text
tokenizer
=
"google/electra-base-discriminator"
model
=
"models/electra"
def
load_suicide_tokenizer_and_model
():
suicide_tokenizer
=
AutoTokenizer
.
from_pretrained
(
tokenizer
)
suicide_model
=
AutoModelForSequenceClassification
.
from_pretrained
(
model
)
return
suicide_tokenizer
,
suicide_model
def
check_intent
(
text
):
global
suicide_tokenizer
,
suicide_model
suicide_tokenizer
,
suicide_model
=
load_suicide_tokenizer_and_model
()
tokenised_text
=
suicide_tokenizer
.
encode_plus
(
text
,
return_tensors
=
"pt"
)
logits
=
suicide_model
(
**
tokenised_text
)[
0
]
prediction
=
(
torch
.
softmax
(
logits
,
dim
=
1
)
.
tolist
()[
0
][
1
])
return
prediction
final_answer2
=
str
(
check_intent
(
suicide_text
))
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------Manoj----------------------------------------------------------------------------------------
depression_text
=
text
def
is_essay
(
text
):
sentences
=
nltk
.
sent_tokenize
(
text
)
return
len
(
sentences
)
<
4
def
index
():
now
=
datetime
.
now
()
hour
=
now
.
hour
return
hour
text_to_predict
=
is_essay
(
depression_text
)
if
text_to_predict
==
True
:
# load tokenizer
with
open
(
'models/Manoj/tokenizer.pickle'
,
'rb'
)
as
handle
:
tokenizer
=
pickle
.
load
(
handle
)
# Convert the new text to a padded sequence
sequence
=
tokenizer
.
texts_to_sequences
([
text
])
padded_sequence
=
pad_sequences
(
sequence
,
maxlen
=
500
)
# Predict the classification of the new text
prediction
=
depression_text_model
.
predict
(
padded_sequence
)
prediction_score
=
float
(
prediction
)
if
prediction_score
>
0.5
:
hour
=
index
()
if
2
<
hour
<=
3
:
prediction
=
"
\n
Extreme level of having depression
\n
"
elif
12
<
hour
<=
2
:
prediction
=
"
\n
High level of having depression
\n
"
elif
11
<
hour
<=
12
:
prediction
=
"
\n
Mid level of having depression
\n
"
else
:
prediction
=
"
\n
Low level of having depression
\n
"
else
:
prediction
=
"
\n
No depression
\n
"
# Render the result HTML template with the prediction
else
:
prediction
=
"
\n
This is an essay
\n
"
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
final_answer2
=
final_answer2
+
prediction
return
render_template
(
'result.html'
,
output
=
final_answer2
.
replace
(
'
\n
'
,
'<br>'
))
if
__name__
==
'__main__'
:
app
.
debug
=
True
app
.
run
(
host
=
'0.0.0.0'
,
port
=
5000
)
\ No newline at end of file
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