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
93583f96
Commit
93583f96
authored
Oct 10, 2022
by
Shavin Eeswar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added to flask
parent
7c7dd2e0
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
77 additions
and
0 deletions
+77
-0
app.py
app.py
+77
-0
No files found.
app.py
0 → 100644
View file @
93583f96
from
flask
import
Flask
,
render_template
,
Response
import
cv2
import
numpy
as
np
from
tensorflow.keras.models
import
model_from_json
from
tensorflow.keras.preprocessing
import
image
#load model
model
=
model_from_json
(
open
(
"model.json"
,
"r"
)
.
read
())
#load weights
model
.
load_weights
(
'model.h5'
)
face_haar_cascade
=
cv2
.
CascadeClassifier
(
'haarcascade_frontalface_default.xml'
)
app
=
Flask
(
__name__
)
camera
=
cv2
.
VideoCapture
(
0
)
def
gen_frames
():
# generate frame by frame from camera
while
True
:
# Capture frame by frame
success
,
frame
=
camera
.
read
()
if
not
success
:
break
else
:
gray_img
=
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_BGR2GRAY
)
faces_detected
=
face_haar_cascade
.
detectMultiScale
(
gray_img
,
1.32
,
5
)
for
(
x
,
y
,
w
,
h
)
in
faces_detected
:
print
(
'WORKING'
)
cv2
.
rectangle
(
frame
,(
x
,
y
),(
x
+
w
,
y
+
h
),(
255
,
0
,
0
),
thickness
=
7
)
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
.
expand_dims
(
img_pixels
,
axis
=
0
)
img_pixels
/=
255
print
(
img_pixels
.
shape
)
predictions
=
model
.
predict
(
img_pixels
)
#find max indexed array
max_index
=
np
.
argmax
(
predictions
[
0
])
emotions
=
[
'angry'
,
'disgust'
,
'fear'
,
'happy'
,
'sad'
,
'surprise'
,
'neutral'
]
predicted_emotion
=
emotions
[
max_index
]
print
(
predicted_emotion
)
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
))
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
(
'/'
)
def
index
():
return
render_template
(
'index.html'
)
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