diff --git a/Client.html b/Client.html new file mode 100644 index 0000000000000000000000000000000000000000..a858787d064d62a98598b76a0b9afc8c856ac28c --- /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 19c157b23674bbcaea7acb065f9568c4a160e2d1..50b523a77d76d40220ef689d3747c05b004ce574 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 5f03ceca628d2bda1249699b5dab8f299622ada1..309af803bc33951b9c2f15ff59d03de3871f12f7 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 0000000000000000000000000000000000000000..b1071608dfebf8dad5d9125472c9ada2d8b72167 --- /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()); +})}); +