Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Mood Meter
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
KI-gestütztes Marketing
Backend Development
Mood Meter
Commits
c945a102
Commit
c945a102
authored
Mar 31, 2024
by
Amel Abdic
Browse files
Options
Downloads
Patches
Plain Diff
Used Deepface from main branch and fixed the following: removed TTS and added Timer
parent
7c7f8ae9
No related branches found
No related tags found
No related merge requests found
Changes
2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
haarcascade_frontalface_default.xml
+33314
-0
33314 additions, 0 deletions
haarcascade_frontalface_default.xml
main.py
+51
-40
51 additions, 40 deletions
main.py
with
33365 additions
and
40 deletions
haarcascade_frontalface_default.xml
0 → 100644
+
33314
−
0
View file @
c945a102
This diff is collapsed.
Click to expand it.
main.py
+
51
−
40
View file @
c945a102
#model.h5 is best model
#model.h5 is best model
import
cv2
import
cv2
import
warnings
import
warnings
import
pyttsx3
warnings
.
filterwarnings
(
"
ignore
"
)
warnings
.
filterwarnings
(
"
ignore
"
)
from
keras.models
import
load_model
import
numpy
as
np
import
time
import
time
from
deepface
import
DeepFace
# Intiliazing text to speech
engine
=
pyttsx3
.
init
(
'
sapi5
'
)
# Defining camera and starting video capture
video_capture
=
cv2
.
VideoCapture
(
0
)
# Defining which cascade opencv should use
face_cascade
=
cv2
.
CascadeClassifier
(
'
haarcascade_frontalface_default.xml
'
)
class
Main
:
class
Main
:
def
main
(
self
):
# Welcome Message
face_classifier
=
cv2
.
CascadeClassifier
(
cv2
.
data
.
haarcascades
+
'
haarcascade_frontalface_default.xml
'
)
def
welcome_message
(
self
):
classifier
=
load_model
(
r
'
model.h5
'
)
# Intiliazing text to speech
# Angepasste emotion_labels Liste ohne 'Angry', 'Disgust', 'Fea
r'
engine
.
say
(
'
Welcome to the face recognition and prediction tool of the herman hollerith cente
r
'
)
e
motion_labels
=
[
'
Happy
'
,
'
Neutral
'
,
'
Sad
'
,
'
Surprise
'
]
e
ngine
.
runAndWait
()
cap
=
cv2
.
VideoCapture
(
0
)
def
face_recognition
(
self
):
start_time
=
time
.
time
()
# Intiliazing text to speech
emotion_history
=
[]
engine
=
pyttsx3
.
init
(
'
sapi5
'
)
last_analysis_time
=
time
.
time
()
# Initialisiere die letzte Analysezeit
analysis_interval
=
4
analyze
=
None
while
True
:
while
True
:
_
,
frame
=
cap
.
read
()
ret
,
frame
=
video_capture
.
read
()
gray
=
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_BGR2GRAY
)
gray
=
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_BGR2GRAY
)
faces
=
face_c
l
as
sifier
.
detectMultiScale
(
gray
)
faces
=
face_cas
cade
.
detectMultiScale
(
gray
,
scaleFactor
=
1.1
,
minNeighbors
=
5
)
for
(
x
,
y
,
w
,
h
)
in
faces
:
current_time
=
time
.
time
()
cv2
.
rectangle
(
frame
,
(
x
,
y
),
(
x
+
w
,
y
+
h
),
(
0
,
255
,
255
),
2
)
roi_gray
=
gray
[
y
:
y
+
h
,
x
:
x
+
w
]
roi_gray
=
cv2
.
resize
(
roi_gray
,
(
48
,
48
),
interpolation
=
cv2
.
INTER_AREA
)
if
np
.
sum
([
roi_gray
])
!=
0
:
if
current_time
-
last_analysis_time
>
analysis_interval
:
roi
=
roi_gray
.
astype
(
'
float
'
)
/
255.0
try
:
roi
=
np
.
expand_dims
(
roi
,
axis
=
0
)
# Annahme: DeepFace.analyze gibt eine Liste zurück. Zugriff auf das erste Element.
analysis_results
=
DeepFace
.
analyze
(
frame
,
actions
=
[
"
emotion
"
,
"
age
"
,
"
gender
"
],
enforce_detection
=
False
)
analyze
=
analysis_results
[
0
]
if
analysis_results
else
None
last_analysis_time
=
current_time
except
Exception
as
e
:
print
(
"
Error in DeepFace Analysis:
"
,
e
)
analyze
=
None
# Bei Fehlern wird `analyze` zurückgesetzt
prediction
=
classifier
.
predict
(
roi
)[
0
]
for
(
x
,
y
,
w
,
h
)
in
faces
:
adjusted_prediction
=
np
.
delete
(
prediction
,
[
0
,
1
,
2
])
cv2
.
rectangle
(
frame
,
(
x
,
y
),
(
x
+
w
,
y
+
h
),
(
0
,
0
,
255
),
1
)
max_index
=
np
.
argmax
(
adjusted_prediction
)
if
analyze
:
current_emotion
=
emotion_labels
[
max_index
]
# Zugriff auf die Analyseergebnisse, wenn `analyze` nicht None ist
label_position
=
(
x
,
y
)
cv2
.
putText
(
frame
,
f
'
Approx. Age:
{
analyze
.
get
(
"
age
"
,
"
N/A
"
)
}
'
,
(
x
,
y
-
10
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.9
,
(
0
,
0
,
255
),
2
)
cv2
.
putText
(
frame
,
current_emotion
,
label_position
,
cv2
.
FONT_HERSHEY_SIMPLEX
,
1
,
(
0
,
255
,
0
),
2
)
cv2
.
putText
(
frame
,
f
'
Approx. Gender:
{
analyze
.
get
(
"
dominant_gender
"
,
"
N/A
"
)
}
'
,
(
x
,
y
-
40
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.9
,
(
0
,
255
,
255
),
2
)
cv2
.
putText
(
frame
,
f
'
Current emotion:
{
analyze
.
get
(
"
dominant_emotion
"
,
"
N/A
"
)
}
'
,
(
x
,
y
-
70
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.9
,
(
255
,
255
,
0
),
2
)
cv2
.
imshow
(
"
video_capture
"
,
frame
)
if
cv2
.
waitKey
(
1
)
&
0xFF
==
ord
(
'
q
'
):
break
emotion_history
.
append
(
current_emotion
)
video_capture
.
release
()
cv2
.
destroyAllWindows
()
# Überprüfen, ob 3 Sekunden vergangen sind diese wird dann für die chat gpt api genutzt
if
time
.
time
()
-
start_time
>=
3
:
if
emotion_history
.
count
(
current_emotion
)
==
len
(
emotion_history
):
print
(
f
"
Konstante Emotion für 3 Sekunden:
{
current_emotion
}
"
)
start_time
=
time
.
time
()
if
__name__
==
'
__main__
'
:
emotion_history
.
clear
()
main
=
Main
()
cv2
.
imshow
(
'
Emotion Detector
'
,
frame
)
#main.welcome_message()
if
cv2
.
waitKey
(
1
)
&
0xFF
==
ord
(
'
q
'
):
main
.
face_recognition
()
break
cap
.
release
()
cv2
.
destroyAllWindows
()
if
__name__
==
"
__main__
"
:
main_instance
=
Main
()
main_instance
.
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment