Skip to content
Snippets Groups Projects
Commit 01b302d9 authored by Simon Flaisch's avatar Simon Flaisch
Browse files

I have threaded the analysis and integrated a mqtt messaging for future usage.

Overall improved the performance but still some parts are buggy. We have to put more time into fixing it.
parent fc950a7f
No related branches found
No related tags found
No related merge requests found
/.venv/
/.idea/
\ No newline at end of file
......@@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv/python_stuff" />
</content>
<orderEntry type="jdk" jdkName="Python 3.8" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.9 (mood-meter)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" />
<component name="Black">
<option name="sdkName" value="Python 3.9 (mood-meter)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (mood-meter)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
FROM eclipse-mosquitto
COPY mosquitto.conf ./mosquitto/config/
COPY mosquitto.passwd ./mosquitto/config/mosquitto.passwd
version: "3.7"
services:
mosquitto:
build: ./
image: uwebreitenbuecher/mosquitto
ports:
- "1883:1883"
- "9001:9001"
\ No newline at end of file
allow_anonymous false
listener 1883
listener 9001
protocol websockets
persistence false
persistence_file mosquitto.db
persistence_location /mosquitto/data/
log_dest stdout
password_file /mosquitto/config/mosquitto.passwd
\ No newline at end of file
standardUser:$7$101$qj8csF6pkG5K5UgI$5L3+LNq5NAzXf5Cb9GXLDuhU1agDFnSYFKc+hBUy0bV9wZeo4QND7YqTaK0m92pDnj2tHjA4BztW4CHHf5q/uQ==
......@@ -5,6 +5,8 @@ warnings.filterwarnings("ignore")
import time
from deepface import DeepFace
import threading
import paho.mqtt.client as mqtt
import json
# Initialisiere Text-to-Speech
engine = pyttsx3.init('sapi5')
......@@ -14,9 +16,42 @@ video_capture = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
class Main:
currentclient = None
unacked_publish = set()
def on_connect(self,client, userdata, flags, reason_code, properties):
print(f"Connected with result code {reason_code}\n")
client.subscribe("Topic1")
def on_publish(self,client, userdata, mid, reason_code, properties):
try:
userdata.remove(mid)
except KeyError:
"Something went wrong. Please check if you have removed the mid from the userdata"
def connectwithmqtt(self, adress:str, targetport:int):
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, userdata=None)
mqttc.on_connect = self.on_connect
mqttc.on_publish = self.on_publish
mqttc.user_data_set(self.unacked_publish)
mqttc.username_pw_set(username="standardUser", password="GreatHHZ4Ever!")
mqttc.connect(host=adress, port=targetport)
while not mqttc.is_connected():
mqttc.loop()
print("Waiting for connection...")
return mqttc
def sendMessage(self, mqttc, topic, message):
mqttc.loop_start()
msg = mqttc.publish(topic, message, qos=1)
self.unacked_publish.add(msg.mid)
while len(self.unacked_publish):
time.sleep(0.1)
msg.wait_for_publish()
def __init__(self):
self.analysis_results = None
self.analysis_thread = None
self.faceframe = None
self.last_analysis_time = 0
self.speaking_thread = None # Verfolgt den aktuellen Sprech-Thread
self.last_speak_time = 0 # Speichert den Zeitpunkt der letzten Sprachausgabe
self.main_time = 0
def speak(self, text):
engine.say(text)
......@@ -27,29 +62,40 @@ class Main:
if (self.speaking_thread is None or not self.speaking_thread.is_alive()) and (current_time - self.last_speak_time > 8):
self.last_speak_time = current_time # Aktualisiere den Zeitpunkt der letzten Sprachausgabe
self.speaking_thread = threading.Thread(target=self.speak, args=(text,))
self.speaking_thread.daemon = True
self.speaking_thread.start()
def facial_analysis_thread(self):
current_time = time.time()
if (self.analysis_thread is None or not self.analysis_thread.is_alive()) and (current_time - self.last_analysis_time > 8):
self.last_analysis_time = current_time
self.analysis_thread = threading.Thread(target=self.facial_analysis)
self.analysis_thread.daemon = True
self.analysis_thread.start()
def facial_analysis(self):
self.analysis_results = DeepFace.analyze(self.faceframe, actions=["emotion", "age", "gender"], enforce_detection=False)
# Welcome Message
def welcome_message(self):
self.speak_in_thread('Welcome to the face recognition and prediction tool of the herman hollerith center')
def face_recognition(self):
def face_recognition(self, mqttc):
last_analysis_time = time.time()
analysis_interval = 8
analyze = None
while True:
ret, frame = video_capture.read()
self.faceframe = frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
current_time = time.time()
if current_time - last_analysis_time > analysis_interval:
try:
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
if current_time - self.main_time > 10:
self.main_time = current_time
self.facial_analysis_thread()
analyze = self.analysis_results[0] if self.analysis_results else None
analysis_results_dict = {"age": analyze.get("age", "N/A"), "gender": analyze.get("dominant_gender", "N/A"), "emotion": analyze.get("dominant_emotion", "N/A")}
self.sendMessage(mqttc=mqttc, topic="Topic1", message=json.dumps(analysis_results_dict))
except Exception as e:
print("Error in DeepFace Analysis:", e)
analyze = None
......@@ -68,6 +114,9 @@ class Main:
cv2.imshow("video_capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
print("Goodbye!")
mqttc.disconnect()
mqttc.loop_stop()
break
video_capture.release()
......@@ -76,4 +125,5 @@ class Main:
if __name__ == '__main__':
main = Main()
main.welcome_message()
main.face_recognition()
main.currentclient =main.connectwithmqtt(adress="localhost", targetport=1883)
main.face_recognition(mqttc=main.currentclient)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment