File size: 1,275 Bytes
7562203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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