Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Q
Question chain chatbot code
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-298
Question chain chatbot code
Commits
970252a0
Commit
970252a0
authored
Oct 08, 2022
by
Vihanga Thathsara Pahalagamage
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
This is web_app.py file
parent
79aa4d32
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
0 deletions
+67
-0
web_app.py
web_app.py
+67
-0
No files found.
web_app.py
0 → 100644
View file @
970252a0
import
mediapipe
as
mp
import
requests
import
json
from
tensorflow.keras.models
import
load_model
import
numpy
as
np
import
cv2
data
=
{
0
:
'Angry'
,
1
:
'Disgust'
,
2
:
'Fear'
,
3
:
'Happy'
,
4
:
'Sad'
,
5
:
'Surprise'
,
6
:
'Neutral'
}
model
=
load_model
(
'emotion/model.h5'
)
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
]],
str
(
classes_x
[
0
])
def
send_data_to_api
(
emotion
):
url
=
"http://127.0.0.1:5000/from_web_app"
data
=
{
'emotion'
:
emotion
}
# sending post request and saving response as response object
headers
=
{
'Content-type'
:
'application/json'
,
'Accept'
:
'text/plain'
}
r
=
requests
.
post
(
url
,
data
=
json
.
dumps
(
data
),
headers
=
headers
)
print
(
'pushed'
)
def
web_camera
():
cap
=
cv2
.
VideoCapture
(
0
)
while
cap
.
isOpened
():
ret
,
frame
=
cap
.
read
()
# BGR 2 RGB
image
=
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_BGR2RGB
)
cv2
.
imshow
(
'Hand Tracking'
,
image
)
gray_img
=
cv2
.
cvtColor
(
image
,
cv2
.
COLOR_BGR2GRAY
)
haar_cascade
=
cv2
.
CascadeClassifier
(
'Haarcascade_frontalface_default.xml'
)
faces_rect
=
haar_cascade
.
detectMultiScale
(
gray_img
,
1.1
,
9
)
if
len
(
faces_rect
)
>
0
:
for
(
x
,
y
,
w
,
h
)
in
faces_rect
:
crop_img
=
image
[
y
:
y
+
h
,
x
:
x
+
w
]
emo
,
emo_index
=
emo_rec
(
crop_img
)
# send_data_to_api(emo_index)
cv2
.
rectangle
(
image
,
(
x
,
y
),
(
x
+
w
,
y
+
h
),
(
0
,
255
,
0
),
2
)
cv2
.
putText
(
image
,
emo
,
(
x
,
y
-
10
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.9
,
(
0
,
255
,
0
),
2
)
if
cv2
.
waitKey
(
10
)
&
0xFF
==
ord
(
'q'
):
break
resized_img
=
cv2
.
resize
(
image
,
(
800
,
600
))
ret
,
buffer
=
cv2
.
imencode
(
'.jpg'
,
resized_img
)
frame
=
buffer
.
tobytes
()
yield
(
b
'--frame
\r\n
'
b
'Content-Type: image/jpeg
\r\n\r\n
'
+
frame
+
b
'
\r\n
'
)
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