Gagan0141 commited on
Commit
f68ab71
·
verified ·
1 Parent(s): 0716bf9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -5,8 +5,10 @@ import os
5
 
6
  app = Flask(__name__, template_folder="templates", static_folder="static")
7
 
8
- # PRELOAD MODELS (only once at startup)
9
- emotion_model = DeepFace.build_model("Emotion")
 
 
10
 
11
  @app.route("/")
12
  def index():
@@ -15,7 +17,7 @@ def index():
15
  @app.route("/detect_live", methods=["POST"])
16
  def detect_live():
17
  if "frame" not in request.files:
18
- return jsonify({"error": "missing frame"}), 400
19
 
20
  file = request.files["frame"]
21
  tmp = tempfile.NamedTemporaryFile(delete=False)
@@ -23,9 +25,9 @@ def detect_live():
23
 
24
  try:
25
  result = DeepFace.analyze(
26
- tmp.name,
27
  actions=["emotion"],
28
- models={"emotion": emotion_model},
29
  detector_backend="mediapipe",
30
  enforce_detection=False
31
  )
@@ -35,9 +37,12 @@ def detect_live():
35
 
36
  os.remove(tmp.name)
37
 
 
 
 
38
  return jsonify({
39
- "emotion": result[0]["dominant_emotion"],
40
- "scores": result[0]["emotion"]
41
  })
42
 
43
  if __name__ == "__main__":
 
5
 
6
  app = Flask(__name__, template_folder="templates", static_folder="static")
7
 
8
+ # preload emotion model once
9
+ models_cache = {
10
+ "emotion": DeepFace.build_model("Emotion")
11
+ }
12
 
13
  @app.route("/")
14
  def index():
 
17
  @app.route("/detect_live", methods=["POST"])
18
  def detect_live():
19
  if "frame" not in request.files:
20
+ return jsonify({"error": "no frame uploaded"}), 400
21
 
22
  file = request.files["frame"]
23
  tmp = tempfile.NamedTemporaryFile(delete=False)
 
25
 
26
  try:
27
  result = DeepFace.analyze(
28
+ img_path=tmp.name,
29
  actions=["emotion"],
30
+ models=models_cache,
31
  detector_backend="mediapipe",
32
  enforce_detection=False
33
  )
 
37
 
38
  os.remove(tmp.name)
39
 
40
+ dominant = result[0]["dominant_emotion"]
41
+ scores = result[0]["emotion"]
42
+
43
  return jsonify({
44
+ "emotion": dominant,
45
+ "scores": scores
46
  })
47
 
48
  if __name__ == "__main__":