Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022-169
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
1
Merge Requests
1
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
2022-169
2022-169
Commits
5a81d975
Commit
5a81d975
authored
Nov 13, 2022
by
Rathnayaka R.M.D.S
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Trained Model Prod
parent
81f84197
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
145 additions
and
0 deletions
+145
-0
emotion-detection/prod/app.py
emotion-detection/prod/app.py
+45
-0
emotion-detection/prod/inference.py
emotion-detection/prod/inference.py
+100
-0
emotion-detection/prod/smilef.jpeg
emotion-detection/prod/smilef.jpeg
+0
-0
emotion-detection/prod/weights/emotion-detection.tflite
emotion-detection/prod/weights/emotion-detection.tflite
+0
-0
No files found.
emotion-detection/prod/app.py
0 → 100644
View file @
5a81d975
import
json
from
flask_cors
import
CORS
from
werkzeug.utils
import
secure_filename
from
flask
import
Flask
,
Response
,
request
from
inference
import
*
app
=
Flask
(
__name__
)
CORS
(
app
)
# Emotion Detection
@
app
.
route
(
"/emotion"
,
methods
=
[
"POST"
])
def
Faces
():
try
:
FaceImagefile
=
request
.
files
[
'FaceImage'
]
.
read
()
FaceImage
=
np
.
frombuffer
(
FaceImagefile
,
np
.
uint8
)
FaceImage
=
cv
.
imdecode
(
FaceImage
,
cv
.
IMREAD_COLOR
)
if
FaceImage
.
any
():
emotion
,
proba
=
Inference
(
FaceImage
)
Face_json
=
{
"emotion"
:
f
"{emotion}"
,
"probability"
:
f
"{proba}"
}
return
Response
(
response
=
json
.
dumps
(
Face_json
),
status
=
200
,
mimetype
=
"application/json"
)
except
Exception
as
e
:
return
Response
(
response
=
json
.
dumps
({
"status"
:
"Unscuccessful"
,
"error"
:
str
(
e
)
}),
status
=
500
,
mimetype
=
"application/json"
)
if
__name__
==
'__main__'
:
app
.
run
(
debug
=
True
,
host
=
host
,
port
=
port
)
\ No newline at end of file
emotion-detection/prod/inference.py
0 → 100644
View file @
5a81d975
import
random
import
base64
import
re
,
json
,
os
import
nltk
,
pickle
import
requests
,
os
,
gtts
from
datetime
import
datetime
from
google.cloud
import
speech
import
cv2
as
cv
import
numpy
as
np
import
pandas
as
pd
import
tensorflow
as
tf
from
arcface
import
ArcFace
from
mtcnn.mtcnn
import
MTCNN
from
PIL
import
Image
,
ImageOps
from
sklearn.preprocessing
import
LabelEncoder
from
sklearn.metrics.pairwise
import
cosine_similarity
nltk
.
download
(
'stopwords'
)
nltk
.
download
(
'wordnet'
)
nltk
.
download
(
'punkt'
)
nltk
.
download
(
'omw-1.4'
)
from
nltk.corpus
import
stopwords
from
nltk.stem
import
WordNetLemmatizer
from
nltk.tokenize
import
RegexpTokenizer
#################################### PATHS / VARIABLES #######################################
model_converter
=
'weights/emotion-detection.tflite'
width
=
71
height
=
71
target_size
=
(
width
,
height
)
input_shape
=
(
width
,
height
,
3
)
class_dict
=
{
'happy'
:
1
,
'sad'
:
0
}
class_dict_reverse
=
{
v
:
k
for
k
,
v
in
class_dict
.
items
()}
host
=
'0.0.0.0'
port
=
5000
#################################### MODEL / FILES ###################################
emotion_interpreter
=
tf
.
lite
.
Interpreter
(
model_path
=
model_converter
)
# Load tflite model
emotion_interpreter
.
allocate_tensors
()
# feature_extractor = tf.keras.models.load_model(facenet_weights)
face_rec
=
ArcFace
.
ArcFace
()
def
preprocessing_function
(
img
):
img
=
tf
.
keras
.
applications
.
xception
.
preprocess_input
(
img
)
return
img
def
extract_face
(
filename
,
detector
=
MTCNN
(),
required_size
=
(
160
,
160
)
):
if
isinstance
(
filename
,
str
):
image
=
Image
.
open
(
filename
)
image
=
image
.
convert
(
'RGB'
)
else
:
image
=
Image
.
fromarray
(
filename
)
image
=
ImageOps
.
exif_transpose
(
image
)
pixels
=
np
.
asarray
(
image
)
try
:
results
=
detector
.
detect_faces
(
pixels
)
x1
,
y1
,
width
,
height
=
results
[
0
][
'box'
]
x1
,
y1
=
abs
(
x1
),
abs
(
y1
)
x2
,
y2
=
x1
+
width
,
y1
+
height
face
=
pixels
[
y1
:
y2
,
x1
:
x2
]
image
=
Image
.
fromarray
(
face
)
image
=
image
.
resize
(
required_size
)
face_array
=
np
.
asarray
(
image
)
return
face_array
except
:
pixels
=
cv
.
resize
(
pixels
,
required_size
)
return
pixels
def
Inference
(
image
):
image
=
extract_face
(
image
,
required_size
=
target_size
)
image
=
preprocessing_function
(
image
)
image
=
np
.
expand_dims
(
image
,
axis
=
0
)
input_shape
=
emotion_interpreter
.
get_input_details
()[
0
][
'shape'
]
assert
np
.
array_equal
(
input_shape
,
image
.
shape
),
"Expected Tensor Shape : {} but recieved {}"
.
format
(
input_shape
,
image
.
shape
)
emotion_interpreter
.
set_tensor
(
emotion_interpreter
.
get_input_details
()[
0
][
'index'
],
image
)
emotion_interpreter
.
invoke
()
# set the inference
pred
=
emotion_interpreter
.
get_tensor
(
emotion_interpreter
.
get_output_details
()[
0
][
'index'
])
# Get predictions
pred
=
pred
.
squeeze
()
Label
=
np
.
argmax
(
pred
)
# Get the label
emotion
=
class_dict_reverse
[
Label
]
proba
=
pred
[
Label
]
return
emotion
,
proba
\ No newline at end of file
emotion-detection/prod/smilef.jpeg
0 → 100644
View file @
5a81d975
65.2 KB
emotion-detection/prod/weights/emotion-detection.tflite
0 → 100644
View file @
5a81d975
File added
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