Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,8 +5,10 @@ import os
|
|
| 5 |
|
| 6 |
app = Flask(__name__, template_folder="templates", static_folder="static")
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
| 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": "
|
| 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=
|
| 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":
|
| 40 |
-
"scores":
|
| 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__":
|