Skip to content

v1.1

Latest

Choose a tag to compare

@galihru galihru released this 15 Jan 17:05
· 33622 commits to main since this release
410b3e8

Mental Health App

KSKI Logo

This application uses computer vision and machine learning to analyze mental health based on facial expressions. The app includes login system, and real-time mental health analysis through facial landmarks, using OpenCV, Mediapipe, and PyQt5.

New Release

Overview

The Releases App is designed to monitor and predict mental health states based on user inputs. It saves predictions to a database and provides visualizations to track changes over time.

Features

  • Save mental health predictions to a CSV file.
  • Display graphs to monitor mental health predictions.
  • Calculate prediction accuracy.
  • Provide suggestions based on the final prediction.

Code

Save to Database

import datetime
import csv

def save_to_database(prediction):
    filename = "mental_health_predictions.csv"
    current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    with open(filename, mode='a', newline='') as file:
        writer = csv.writer(file)
        writer.writerow([current_time, prediction])

Show Graph Generator

import pandas as pd
import matplotlib.pyplot as plt

def show_graph(date_filter=None):
    # Read data from CSV
    data = pd.read_csv("mental_health_predictions.csv", names=["Time", "Prediction"])
    data["Time"] = pd.to_datetime(data["Time"])

    if date_filter:
        data = data[data["Time"].dt.date == date_filter]

    # Map predictions to numbers for the graph
    predictions_map = {"Neutral": 0, "Fatigue Detected": 1, "Positive Mood": 2, "Stressed/Concerned": 3}
    data["Prediction Value"] = data["Prediction"].map(predictions_map)

    # Create graph with different colors for each prediction
    plt.figure(figsize=(12, 8))
    colors = {'Neutral': 'blue', 'Fatigue Detected': 'red', 'Positive Mood': 'green', 'Stressed/Concerned': 'orange'}
    for prediction, group_data in data.groupby("Prediction"):
        plt.plot_date(group_data["Time"], group_data["Prediction Value"], linestyle='solid', marker='o', color=colors[prediction], label=prediction)

    plt.xlabel("Time")
    plt.ylabel("Prediction")
    plt.title("Mental Health Prediction Monitoring")
    plt.xticks(rotation=45)
    plt.tight_layout()
    plt.legend()

    # Show graph
    plt.show()

    # Calculate prediction accuracy
    total_predictions = len(data)
    correct_predictions = data["Prediction"].value_counts().max()
    accuracy = (correct_predictions / total_predictions) * 100

    print(f"Total Predictions: {total_predictions}")
    print(f"Correct Predictions: {correct_predictions}")
    print(f"Accuracy: {accuracy:.2f}%")

    # Final emotion prediction conclusion
    final_prediction = data["Prediction"].iloc[-1]
    print(f"Final emotion prediction conclusion: {final_prediction}")

    # Suggestions to address or reduce emotions
    suggestions = {
        "Neutral": "Maintain your current condition and continue positive activities.",
        "Fatigue Detected": "Get enough rest, quality sleep, and avoid excessive stress.",
        "Positive Mood": "Continue activities that make you happy and share happiness with others.",
        "Stressed/Concerned": "Do relaxation, meditation, or calming activities. Don't hesitate to seek professional help if needed."
    }
    print(f"Suggestions: {suggestions.get(final_prediction, 'No suggestions available.')}")

    # Prediction accuracy graph
    plt.figure(figsize=(12, 8))
    plt.plot(data["Time"], data["Prediction Value"], label="Detected Emotion", color='blue', linestyle='solid', marker='o')
    plt.plot(data["Time"], data["Prediction Value"].rolling(window=10).mean(), label="Predicted Emotion", color='red', linestyle='dashed')
    plt.xlabel("Time")
    plt.ylabel("Prediction Value")
    plt.title("Detected vs Predicted Emotion")
    plt.xticks(rotation=45)
    plt.tight_layout()
    plt.legend()
    plt.show()

Requirements

Before running the app, make sure you have the following Python packages installed:

  • opencv-python
  • numpy
  • mediapipe
  • selenium
  • PyQt5
  • Matplotlib
  • Datetime
  • csv

You can install missing dependencies by running the following command:

pip install -r requirements.txt