A microservices-based system that detects user emotions from facial images and recommends nearby places based on their mood.
- Backend: Node.js + Express (Port 3000)
- ML Service: Python + FastAPI (Port 8000)
- Cache: Redis (Port 6379)
- Database: MongoDB
- Map Data: OpenStreetMap (Overpass API)
- User uploads a photo
- Backend forwards image to ML service
- ML service detects face and predicts emotion
- Backend recommends nearby places based on emotion
- Results are cached in Redis for 1 hour
| Emotion | Recommended Places |
|---|---|
| Happy | Parks, Cafes |
| Sad | Parks, Places of Worship |
| Angry | Gyms, Fitness Centres |
| Neutral | Restaurants |
- Node.js (v16+)
- Python (v3.8+)
- MongoDB running locally (or update
DB_URLin.env) - Redis running locally
1. Start Redis:
redis-server2. Start ML Service (in a separate terminal):
cd emotion-service
pip install -r requirements.txt
uvicorn app:app --host 0.0.0.0 --port 80003. Start Backend (in another terminal):
cd backend
npm install
npm start- Backend API: http://localhost:3000
- ML Service: http://localhost:8000
- Redis: localhost:6379
Sign Up
POST /api/auth/signUp
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}Sign In
POST /api/auth/signIn
Content-Type: application/json
{
"email": "john@example.com",
"password": "password123"
}Detect Emotion & Get Recommendations
POST /api/emotion/detect
Authorization: Bearer <your_token>
Content-Type: multipart/form-data
image: <image_file>
lat: 33.6844
lng: 73.0479Response:
{
"emotion": "happy",
"confidence": 0.82,
"places": [
{
"name": "Central Park",
"category": "park",
"lat": 33.6850,
"lng": 73.0490,
"mapLink": "https://www.openstreetmap.org/?mlat=33.6850&mlon=73.0490"
}
]
}Health Check
GET http://localhost:8000/healthPredict Emotion (Direct)
POST http://localhost:8000/predict
Content-Type: multipart/form-data
image: <image_file>Create .env file in backend/ directory:
PORT=3000
DB_URL=mongodb://localhost:27017/smartpredictor
# Firebase Admin SDK
project_id=your-project-id
client_email=your-service-account@project.iam.gserviceaccount.com
private_key=-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n
# Firebase Client (for frontend)
apiKey=your-api-key
authDomain=your-project.firebaseapp.com
projectId=your-project-id
storageBucket=your-project.firebasestorage.app
messagingSenderId=123456789
appId=1:123456789:web:abc123# Health check
curl http://localhost:8000/health
# Test emotion detection
curl -X POST http://localhost:8000/predict \
-F "image=@test_image.jpg"# Sign up
curl -X POST http://localhost:3000/api/auth/signUp \
-H "Content-Type: application/json" \
-d '{"name":"Test User","email":"test@test.com","password":"test123"}'
# Sign in (get token)
curl -X POST http://localhost:3000/api/auth/signIn \
-H "Content-Type: application/json" \
-d '{"email":"test@test.com","password":"test123"}'
# Detect emotion
curl -X POST http://localhost:3000/api/emotion/detect \
-H "Authorization: Bearer YOUR_TOKEN" \
-F "image=@test_image.jpg" \
-F "lat=33.6844" \
-F "lng=73.0479"- Images are limited to 5MB
- Supported formats: JPEG, JPG, PNG
- Redis cache expires after 1 hour
- If no face is detected, returns "neutral" with low confidence
- Place recommendations are limited to 5 results within 10km radius
Backend:
cd backend
npm installML Service:
cd emotion-service
pip install -r requirements.txtMake sure you have MongoDB and Redis running:
Terminal 1 - Redis:
redis-serverTerminal 2 - ML Service:
cd emotion-service
uvicorn app:app --host 0.0.0.0 --port 8000Terminal 3 - Backend:
cd backend
npm start.
βββ backend/
β βββ src/
β β βββ configs/
β β β βββ database.js
β β β βββ firebase.js
β β βββ controller/
β β β βββ authcontroller.js
β β β βββ emotioncontroller.js
β β β βββ placeRecommender.js
β β βββ middleware/
β β β βββ upload.js
β β β βββ verify_token.js
β β βββ routes/
β β βββ auth_routes.js
β β βββ emotion_routes.js
β βββ app.js
β βββ server.js
β βββ package.json
βββ emotion-service/
β βββ model/
β β βββ model.h5
β βββ app.py
β βββ haarcascade_frontalface_default.xml
β βββ requirements.txt
βββ README.md
- All emotion detection endpoints require authentication
- JWT tokens are verified using Firebase Admin SDK
- Image uploads are validated for type and size
- Uploaded images are stored temporarily and should be cleaned periodically
MongoDB Connection Error:
- Ensure MongoDB is running locally
- Check
DB_URLinbackend/.envfile matches your MongoDB connection
Redis Connection Error:
- Ensure Redis server is running:
redis-server - Default port is 6379
- Connection errors will be logged but service continues without caching
ML Service Not Responding:
- Check if model file exists:
emotion-service/model/model.h5 - Verify Haar Cascade file exists:
emotion-service/haarcascade_frontalface_default.xml - Ensure Python dependencies are installed:
pip install -r requirements.txt - Check if port 8000 is already in use
No Face Detected:
- Ensure image has a clear, front-facing face
- Try different lighting conditions
- System returns "neutral" emotion with low confidence
This project is for educational purposes.