emo-bot / src /controllers /predict_controller.py
WaiYanLynn's picture
Upload 10 files
7562203 verified
raw
history blame
No virus
1.28 kB
from flask import request, jsonify, Blueprint
import numpy as np
from deepface import DeepFace
from PIL import Image
import pprint
DeepFace.analyze(
detector_backend='fastmtcnn',
img_path='./download.jpeg', actions=["emotion"])
predicts = Blueprint("predicts", __name__)
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg"}
def allowed_file(filename):
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
@predicts.route("/", methods=["POST"], strict_slashes=False)
def upload_file():
print('predicting')
try:
if "file" not in request.files:
raise ValueError("File not found in the request.")
file = request.files["file"]
if file.filename == "":
raise ValueError("Empty filename in the request.")
if file and allowed_file(file.filename):
objs = DeepFace.analyze(
detector_backend='fastmtcnn',
img_path=np.array(Image.open(file)), actions=["emotion"]
)
# pprint.pp(objs)
return jsonify(objs), 200
else:
raise ValueError("Invalid file type.")
except Exception as e:
print(e)
return f"Error processing file: {str(e)}", 500