| from flask import Flask, request, jsonify |
| import tensorflow as tf |
| import numpy as np |
| import cv2 |
|
|
| |
| model = tf.keras.models.load_model('Realtime_CNN_Model.h5') |
|
|
| |
| classnames = ["angry", "disgust", "fear", "happy", "neutral", "sad", "surprise"] |
|
|
| app = Flask(__name__) |
|
|
| def detect_depression(sent_out): |
| dc = 0 |
| hc = 0 |
| suc = 0 |
|
|
| for emotion in sent_out: |
| if emotion in ['angry', 'disgust', 'sad', 'fear']: |
| dc += 1 |
| elif emotion in ['happy', 'neutral']: |
| hc += 1 |
| elif emotion == 'surprise': |
| suc += 1 |
|
|
| pos_rate = round((hc / (dc + hc + suc)), 2) * 100 |
| neg_rate = round((dc / (dc + hc + suc)), 2) * 100 |
|
|
| response = "" |
| depression_scale = round(neg_rate / 10, 1) |
| |
| if neg_rate <= 30.0: |
| response = "*** MILD DEPRESSION ***\n" |
| if neg_rate <= 5.0: |
| response += "just chill and relax" |
| elif neg_rate <= 10.0: |
| response += f"Depression Scale Detected: {depression_scale}\nEverything is a-okay! You're probably cuddling a fluffy kitten right now." |
| elif neg_rate <= 20.0: |
| response += f"Depression Scale Detected: {depression_scale}\nYou are a bit frustrated and disappointed but easily distracted and cheered with little effort." |
| elif neg_rate <= 30.0: |
| response += f"Depression Scale Detected: {depression_scale}\nThings are bothering you but you're coping. You might be overtired and hungry." |
| elif neg_rate <= 60.0: |
| response = "*** MODERATE DEPRESSION ***\n" |
| if neg_rate <= 40.0: |
| response += f"Depression Scale Detected: {depression_scale}\nToday is slightly a bad day for you. Use self-care strategies." |
| elif neg_rate <= 50.0: |
| response += f"Depression Scale Detected: {depression_scale}\nYour mental health is starting to impact your everyday life." |
| elif neg_rate <= 60.0: |
| response += f"Depression Scale Detected: {depression_scale}\nYou are struggling with everyday tasks due to your mental health." |
| else: |
| response = "*** SEVERE DEPRESSION ***\n" |
| if neg_rate <= 70.0: |
| response += f"Depression Scale Detected: {depression_scale}\nYou are losing interest in previously enjoyable activities. Seek help." |
| elif neg_rate <= 80.0: |
| response += f"Depression Scale Detected: {depression_scale}\nYour mental health is affecting all parts of your life. You need urgent help." |
| elif neg_rate <= 90.0: |
| response += f"Depression Scale Detected: {depression_scale}\nYou are at a critical point. Seek help immediately." |
| elif neg_rate <= 100.0: |
| response += f"Depression Scale Detected: {depression_scale}\nSevere distress detected. Contact crisis line immediately." |
|
|
| return response |
|
|
| @app.route('/predict', methods=['POST']) |
| def predict(): |
| file = request.files['file'] |
| img = cv2.imdecode(np.fromstring(file.read(), np.uint8), cv2.IMREAD_GRAYSCALE) |
| img = cv2.resize(img, (48, 48)) |
| img = np.array(img).reshape(1, 48, 48, 1) / 255.0 |
|
|
| |
| predict_x = model.predict(img) |
| result = np.argmax(predict_x, axis=1) |
| emotion = classnames[result[0]] |
|
|
| |
| sent_out = [emotion] |
| depression_response = detect_depression(sent_out) |
|
|
| return jsonify({'emotion': emotion, 'depression_analysis': depression_response}) |
|
|
| if __name__ == '__main__': |
| app.run(host='0.0.0.0', port=5000) |
|
|