From a7d65a938e877db3b2434b6f6ac9ea04c0c16793 Mon Sep 17 00:00:00 2001 From: flaisch <simon.flaisch@student.reutlingen-university.de> Date: Wed, 3 Apr 2024 00:13:04 +0200 Subject: [PATCH] Haven't fixed the issue with the error messages but I build a html and javascript files which can utilize the results of our analysis. Now we have almost the full architecture prototyped. --- Client.html | 37 +++++++++++++++++++++++++++++++++++++ Consumer.py | 2 +- main.py | 14 ++++++-------- server.js | 27 +++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 Client.html create mode 100644 server.js diff --git a/Client.html b/Client.html new file mode 100644 index 0000000..a858787 --- /dev/null +++ b/Client.html @@ -0,0 +1,37 @@ + +<html> + +<head> + + <style> + + </style> + +</head> + +<body> + +<div id="smileyContainer">No emotion recognized so far</div> + +</body> + +<script> + + const webSocket = new WebSocket('ws://localhost:443/'); + + webSocket.onmessage = (event) => { + let obj = JSON.parse(event.data); + if (obj.emotion === "happy") { + smileyContainer.innerHTML = ":-)"; + } + if (obj.emotion === "neutral") { + smileyContainer.innerHTML = ":-|"; + } + if (obj.emotion === "angry") { + smileyContainer.innerHTML = ":-("; + } + }; + +</script> + +</html> \ No newline at end of file diff --git a/Consumer.py b/Consumer.py index 19c157b..50b523a 100644 --- a/Consumer.py +++ b/Consumer.py @@ -22,4 +22,4 @@ while True: mqttc.loop_stop() print("goodbye!") break - mqttc.loop_stop() +mqttc.loop_stop() diff --git a/main.py b/main.py index 5f03cec..309af80 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ from deepface import DeepFace import threading import paho.mqtt.client as mqtt import json +import os # Initialisiere Text-to-Speech engine = pyttsx3.init('sapi5') @@ -16,8 +17,6 @@ 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") @@ -48,20 +47,21 @@ class Main: self.analysis_results = None self.analysis_thread = None self.speaking_thread = None # Verfolgt den aktuellen Sprech-Thread - self.last_speak_time = 0 # Speichert den Zeitpunkt der letzten Sprachausgabe self.main_time = 0 + self.currentclient = None + self.unacked_publish = set() def speak(self, text): engine.say(text) engine.runAndWait() def speak_in_thread(self, text): - current_time = time.time() - 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 + + if (self.speaking_thread is None or not self.speaking_thread.is_alive()): self.speaking_thread = threading.Thread(target=self.speak, args=(text,)) self.speaking_thread.daemon = True self.speaking_thread.start() + def facial_analysis_thread(self, faceframe): if (self.analysis_thread is None or not self.analysis_thread.is_alive()): self.analysis_thread = threading.Thread(target=self.facial_analysis, args=(faceframe,)) @@ -76,7 +76,6 @@ class Main: self.speak_in_thread('Welcome to the face recognition and prediction tool of the herman hollerith center') def face_recognition(self, mqttc): - last_analysis_time = time.time() while True: ret, frame = video_capture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) @@ -90,7 +89,6 @@ class Main: 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 diff --git a/server.js b/server.js new file mode 100644 index 0000000..b107160 --- /dev/null +++ b/server.js @@ -0,0 +1,27 @@ +const mqtt = require("mqtt"); +const ws = require ("ws"); + +let socketserver = new ws.WebSocketServer({ + port: 443 +}); + + let client = mqtt.connect("mqtt://localhost:1883",{ + username: "standardUser", + password: "GreatHHZ4Ever!" + }); +client.on("connect", function() { + let topicID = "Topic1"; + client.subscribe(topicID); +}); +socketserver.on('connection', ws => { + console.log("new client connected"); + +}); +socketserver.on('close', () => console.log('Client has disconnected!')); + + +client.on("message", function(topic, message){ + socketserver.clients.forEach(client => { + client.send(message.toString()); +})}); + -- GitLab