Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
Better you
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
2022-301
Better you
Commits
2d273540
Commit
2d273540
authored
Nov 13, 2022
by
Shavin Eeswar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update app.py
parent
fad32504
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
103 additions
and
22 deletions
+103
-22
app.py
app.py
+103
-22
No files found.
app.py
View file @
2d273540
from
flask
import
Flask
,
render_template
,
Response
import
os
import
glob
import
cv2
import
numpy
as
np
from
cv2
import
CAP_ANY
,
CAP_DSHOW
from
flask
import
Flask
,
Response
,
redirect
,
render_template
,
request
,
url_for
from
tensorflow.keras.models
import
model_from_json
from
tensorflow.keras.preprocessing
import
image
from
werkzeug.utils
import
secure_filename
#load model
model
=
model_from_json
(
open
(
"model.json"
,
"r"
)
.
read
())
...
...
@@ -14,12 +17,45 @@ model.load_weights('model.h5')
face_haar_cascade
=
cv2
.
CascadeClassifier
(
'haarcascade_frontalface_default.xml'
)
UPLOAD_FOLDER
=
'video'
ALLOWED_EXTENSIONS
=
{
'mp4'
}
app
=
Flask
(
__name__
)
camera
=
cv2
.
VideoCapture
(
0
)
app
.
config
[
'UPLOAD_FOLDER'
]
=
UPLOAD_FOLDER
def
allowed_file
(
filename
):
return
'.'
in
filename
and
\
filename
.
rsplit
(
'.'
,
1
)[
1
]
.
lower
()
in
ALLOWED_EXTENSIONS
@
app
.
route
(
'/gen_frames'
,
methods
=
[
"POST"
])
def
gen_frames
():
file
=
request
.
files
[
'file'
]
# Gives Content type text/html etc
if
file
and
allowed_file
(
file
.
filename
):
filename
=
secure_filename
(
file
.
filename
)
file
.
save
(
os
.
path
.
join
(
app
.
config
[
'UPLOAD_FOLDER'
],
filename
))
# return 'success'
list_of_files
=
glob
.
glob
(
'video/*'
)
# * means all if need specific format then *.csv
latest_file
=
max
(
list_of_files
,
key
=
os
.
path
.
getctime
)
print
(
latest_file
)
camera
=
cv2
.
VideoCapture
(
latest_file
)
emo
=
[]
# def gen_frames(): # generate frame by frame from camera
Angry
=
0
Disgust
=
0
Fear
=
0
Happy
=
0
Neutral
=
0
Sad
=
0
Surprise
=
0
def
gen_frames
():
# generate frame by frame from camera
while
True
:
# Capture frame by frame
success
,
frame
=
camera
.
read
()
...
...
@@ -37,6 +73,7 @@ def gen_frames(): # generate frame by frame from camera
roi_gray
=
gray_img
[
y
:
y
+
w
,
x
:
x
+
h
]
#cropping region of interest i.e. face area from image
roi_gray
=
cv2
.
resize
(
roi_gray
,(
48
,
48
))
img_pixels
=
image
.
img_to_array
(
roi_gray
)
img_pixels
=
np
.
concatenate
((
img_pixels
,)
*
3
,
axis
=
-
1
)
img_pixels
=
np
.
expand_dims
(
img_pixels
,
axis
=
0
)
img_pixels
/=
255
...
...
@@ -47,31 +84,75 @@ def gen_frames(): # generate frame by frame from camera
#find max indexed array
max_index
=
np
.
argmax
(
predictions
[
0
])
mx
=
max
(
max
(
predictions
))
emotions
=
[
'angry'
,
'disgust'
,
'fear'
,
'happy'
,
'sad'
,
'surprise'
,
'neutral'
]
predicted_emotion
=
emotions
[
max_index
]
print
(
predicted_emotion
)
if
(
predicted_emotion
==
'neutral'
):
Neutral
=
Neutral
+
mx
emo
.
append
((
predicted_emotion
,
Neutral
))
elif
(
predicted_emotion
==
'angry'
):
Angry
=
Angry
+
mx
emo
.
append
((
predicted_emotion
,
Angry
))
elif
(
predicted_emotion
==
'disgusted'
):
Disgust
=
Disgust
+
mx
emo
.
append
((
predicted_emotion
,
Disgust
))
elif
(
predicted_emotion
==
'fearful'
):
Fear
=
Fear
+
mx
emo
.
append
((
predicted_emotion
,
Fear
))
elif
(
predicted_emotion
==
'happy'
):
Happy
=
Happy
+
mx
emo
.
append
((
predicted_emotion
,
Happy
))
elif
(
predicted_emotion
==
'sad'
):
Sad
=
Sad
+
mx
emo
.
append
((
predicted_emotion
,
Sad
))
elif
(
predicted_emotion
==
'surprised'
):
Surprise
=
Surprise
+
mx
emo
.
append
((
predicted_emotion
,
Surprise
))
cv2
.
putText
(
frame
,
predicted_emotion
,
(
int
(
x
),
int
(
y
)),
cv2
.
FONT_HERSHEY_SIMPLEX
,
1
,
(
0
,
0
,
255
),
2
)
resized_img
=
cv2
.
resize
(
frame
,
(
1000
,
700
))
# resized_img = cv2.resize(frame, (1000, 700))
# ret, buffer = cv2.imencode('.jpg', frame)
# frame = buffer.tobytes()
# yield (b'--frame\r\n'
# b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result
d
=
dict
(
emo
)
return
d
ret
,
buffer
=
cv2
.
imencode
(
'.jpg'
,
frame
)
frame
=
buffer
.
tobytes
()
yield
(
b
'--frame
\r\n
'
b
'Content-Type: image/jpeg
\r\n\r\n
'
+
frame
+
b
'
\r\n
'
)
# concat frame one by one and show result
# @app.route('/video_feed')
# def video_feed():
# #Video streaming route. Put this in the src attribute of an img tag
# return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@
app
.
route
(
'/video_feed'
)
def
video_feed
():
#Video streaming route. Put this in the src attribute of an img tag
return
Response
(
gen_frames
(),
mimetype
=
'multipart/x-mixed-replace; boundary=frame'
)
# @app.route('/')
# def index():
# return render_template('index.html')
# # @app.route('/gen_frames',methods=["POST"])
@
app
.
route
(
'/'
)
def
index
():
return
render_template
(
'index.html'
)
# @app.route('/emo')
# def emotions():
# d = dict(emo)
# print(d)
# return d
if
__name__
==
'__main__'
:
app
.
run
(
debug
=
True
)
app
.
run
(
host
=
"0.0.0.0"
,
port
=
5000
,
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