Skip to content
Snippets Groups Projects
Commit c945a102 authored by Amel Abdic's avatar Amel Abdic :8ball:
Browse files

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
This diff is collapsed.
#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', 'Fear' engine.say('Welcome to the face recognition and prediction tool of the herman hollerith center')
emotion_labels = ['Happy', 'Neutral', 'Sad', 'Surprise'] engine.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_classifier.detectMultiScale(gray) faces = face_cascade.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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment