Phantom-0-0 commited on
Commit
119aa1c
·
verified ·
1 Parent(s): f3de149

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import cv2
3
+ import numpy as np
4
+ import tensorflow as tf
5
+
6
+ # Load the model
7
+ MODEL_PATH = "engagement_model_89.tflite"
8
+ interpreter = tf.lite.Interpreter(model_path=MODEL_PATH)
9
+ interpreter.allocate_tensors()
10
+ input_details = interpreter.get_input_details()
11
+ output_details = interpreter.get_output_details()
12
+
13
+ def predict_tflite(input_data):
14
+ interpreter.set_tensor(input_details[0]['index'], input_data)
15
+ interpreter.invoke()
16
+ return interpreter.get_tensor(output_details[0]['index'])
17
+
18
+ # Labels
19
+ labels = ["Engaged", "Frustrated", "Bored", "Confused"]
20
+
21
+ # Haar Cascade for face detection
22
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
23
+
24
+ # Initialize Flask app
25
+ app = Flask(__name__)
26
+
27
+ @app.route('/')
28
+ def home():
29
+ return jsonify({
30
+ 'message': 'Engagement Detection API',
31
+ 'endpoints': {
32
+ '/predict': 'POST image for emotion prediction'
33
+ }
34
+ })
35
+
36
+ @app.route('/predict', methods=['POST'])
37
+ def predict():
38
+ file = request.files['image']
39
+ img = np.frombuffer(file.read(), np.uint8)
40
+ frame = cv2.imdecode(img, cv2.IMREAD_COLOR)
41
+
42
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
43
+ faces = face_cascade.detectMultiScale(gray, 1.1, 4)
44
+
45
+ predictions = {"Engaged": 0, "Frustrated": 0, "Bored": 0, "Confused": 0}
46
+
47
+ if len(faces) > 0:
48
+ for (x, y, w, h) in faces:
49
+ face = frame[y:y+h, x:x+w]
50
+ img_resized = cv2.resize(face, (224, 224))
51
+ img_resized = img_resized.astype("float32") / 255.0
52
+ img_resized = np.expand_dims(img_resized, axis=0)
53
+
54
+ # Get model predictions
55
+ preds = predict_tflite(img_resized)[0]
56
+
57
+ # Normalize to sum to 1 (100%)
58
+ preds = preds / np.sum(preds)
59
+
60
+ # Get dominant emotion
61
+ dominant_idx = np.argmax(preds)
62
+ dominant_emotion = labels[dominant_idx]
63
+
64
+ # Debug output
65
+ print(f"Model predictions: {dict(zip(labels, preds))}")
66
+ print(f"Dominant emotion: {dominant_emotion}")
67
+
68
+ # Store results
69
+ for i, label in enumerate(labels):
70
+ predictions[label] = float(preds[i])
71
+ result = dominant_emotion
72
+
73
+ if len(faces) == 0:
74
+ return jsonify({"error": "No face detected", "predictions": None, "result": None})
75
+ return jsonify({"predictions": predictions, "result": dominant_emotion})
76
+
77
+ if __name__ == "__main__":
78
+ import os
79
+ port = int(os.environ.get("PORT", 5000)) # Default to 5000 if not set
80
+ app.run(host='0.0.0.0', port=port, debug=True)