Skip to content

Haseebx162006/Smart-Place-Predictor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Smart Predictor - Mood-Based Place Recommendation System

A microservices-based system that detects user emotions from facial images and recommends nearby places based on their mood.

πŸ—οΈ Architecture

  • Backend: Node.js + Express (Port 3000)
  • ML Service: Python + FastAPI (Port 8000)
  • Cache: Redis (Port 6379)
  • Database: MongoDB
  • Map Data: OpenStreetMap (Overpass API)

🧠 How It Works

  1. User uploads a photo
  2. Backend forwards image to ML service
  3. ML service detects face and predicts emotion
  4. Backend recommends nearby places based on emotion
  5. Results are cached in Redis for 1 hour

Emotion β†’ Place Mapping

Emotion Recommended Places
Happy Parks, Cafes
Sad Parks, Places of Worship
Angry Gyms, Fitness Centres
Neutral Restaurants

πŸš€ Quick Start

Prerequisites

  • Node.js (v16+)
  • Python (v3.8+)
  • MongoDB running locally (or update DB_URL in .env)
  • Redis running locally

Run Locally

1. Start Redis:

redis-server

2. Start ML Service (in a separate terminal):

cd emotion-service
pip install -r requirements.txt
uvicorn app:app --host 0.0.0.0 --port 8000

3. Start Backend (in another terminal):

cd backend
npm install
npm start

Services

πŸ“‘ API Endpoints

Authentication

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"
}

Emotion Detection

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.0479

Response:

{
  "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"
    }
  ]
}

ML Service

Health Check

GET http://localhost:8000/health

Predict Emotion (Direct)

POST http://localhost:8000/predict
Content-Type: multipart/form-data

image: <image_file>

πŸ”§ Configuration

Backend Environment Variables

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

πŸ§ͺ Testing

Test ML Service

# Health check
curl http://localhost:8000/health

# Test emotion detection
curl -X POST http://localhost:8000/predict \
  -F "image=@test_image.jpg"

Test Backend

# 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"

πŸ“ Notes

  • 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

πŸ› οΈ Development

Install Dependencies

Backend:

cd backend
npm install

ML Service:

cd emotion-service
pip install -r requirements.txt

Running Services Locally

Make sure you have MongoDB and Redis running:

Terminal 1 - Redis:

redis-server

Terminal 2 - ML Service:

cd emotion-service
uvicorn app:app --host 0.0.0.0 --port 8000

Terminal 3 - Backend:

cd backend
npm start

πŸ“¦ Project Structure

.
β”œβ”€β”€ 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

πŸ”’ Security

  • 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

🚨 Troubleshooting

MongoDB Connection Error:

  • Ensure MongoDB is running locally
  • Check DB_URL in backend/.env file 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

πŸ“„ License

This project is for educational purposes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published