This project implements a real-time emotion detection system using AI models and MQTT communication. It includes:
emotion_demo.py: Core AI script for emotion detection.printResult.py: Script to display results and handle control commands via MQTT.
-
Python Installation Ensure Python (>=3.7) is installed. Download Python.
-
Mosquitto Installation Install Mosquitto MQTT broker version 2.0.12.
Note: The latest versions may not support WebSocket communication. Version 2.0.12 is confirmed to work.
- Windows: Download Mosquitto 2.0.12.
- Linux:
sudo apt update sudo apt install -y mosquitto=2.0.12 mosquitto-clients
- MacOS:
brew install mosquitto@2.0.12
To verify the installed version:
mosquitto -v
-
Dependencies Installation Install the required Python libraries:
pip install -r requirements.txt
Run the Mosquitto MQTT broker with WebSocket support enabled (default port 8083):
mosquittoExecute the emotion_demo.py script to start the emotion detection:
python emotion_demo.pyRun printResult.py to subscribe to the MQTT topic and display results:
python printResult.py-
emotion_demo.py:- Loads a pre-trained model (
emotion_detection.h5) and uses a Haar Cascade classifier (haarcascade_frontalface_default.xml) for face detection. - Publishes results via MQTT.
- Update the absolute path to the model if necessary:
classifier = load_model('C:/Users/manhh/Downloads/emotion detect/emotion_detection.h5')
- Loads a pre-trained model (
-
printResult.py:- Subscribes to two MQTT brokers:
- Local MQTT broker (TCP, port 1883).
- WebSocket MQTT broker (port 8083).
- Dynamically switches between local MQTT and WebSocket brokers based on control commands.
- Control Commands: Received via WebSocket, with key
aito toggle AI mode:ai: true: Switches to local MQTT (1883) for emotion messages.ai: false: Switches back to WebSocket (8083).
- Control Commands: Received via WebSocket, with key
- Forwards messages from WebSocket broker to local MQTT when in WebSocket mode.
- Example processing flow:
- Receives JSON messages containing
emotiondata or control commands. - Decodes and processes messages based on their source.
- Prints processed emotions or logs forwarded messages.
- Receives JSON messages containing
- Subscribes to two MQTT brokers:
-
emotion_detection.h5: Pre-trained model for emotion classification. -
haarcascade_frontalface_default.xml: Haar Cascade XML for face detection. -
requirements.txt: Python dependencies for the project.
-
Mosquitto not running: Ensure Mosquitto version 2.0.12 is installed and running:
mosquitto
-
WebSocket issues: Verify that Mosquitto is configured to listen on port 8083. Check the configuration file (usually located at
/etc/mosquitto/mosquitto.conf) and ensure it includes:listener 8083 protocol websockets -
Missing dependencies: Reinstall the dependencies:
pip install -r requirements.txt
-
FileNotFoundError: Verify all required files (
emotion_detection.h5,haarcascade_frontalface_default.xml) are in the correct directory.
From printResult.py:
[MQTT LOCAL] Message received on emotion_topic: {"emotion": "Happy"}
[MQTT LOCAL] Processed Emotion: Happy
[MQTT WS] Forwarded message to MQTT local: {"emotion": "Neutral"}
[MQTT WS] Switching to MQTT (1883) mode (ai = true).
Feel free to use and modify this file as needed.