File size: 6,969 Bytes
677c57e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from flask import Blueprint, request , jsonify
from deepface.api.src.modules.core import service
from deepface.commons.logger import Logger
from deepface.commons.os_path import os_path
import json
import os

logger = Logger(module="api/src/routes.py")

blueprint = Blueprint("routes", __name__)


@blueprint.route("/")
def home():
    return "<h1>Welcome to DeepFace API!</h1>"


@blueprint.route("/represent", methods=["POST"])
def represent():
    input_args = request.get_json()

    if input_args is None:
        return {"message": "empty input set passed"}

    img_path = input_args.get("img") or input_args.get("img_path")
    if img_path is None:
        return {"message": "you must pass img_path input"}

    model_name = input_args.get("model_name", "VGG-Face")
    detector_backend = input_args.get("detector_backend", "opencv")
    enforce_detection = input_args.get("enforce_detection", True)
    align = input_args.get("align", True)

    obj = service.represent(
        img_path=img_path,
        model_name=model_name,
        detector_backend=detector_backend,
        enforce_detection=enforce_detection,
        align=align,
    )

    logger.debug(obj)

    return obj


@blueprint.route("/verify", methods=["POST"])
def verify():
    input_args = request.get_json()

    if input_args is None:
        return {"message": "empty input set passed"}

    img1_path = input_args.get("img1") or input_args.get("img1_path")
    img2_path = input_args.get("img2") or input_args.get("img2_path")

    if img1_path is None:
        return {"message": "you must pass img1_path input"}

    if img2_path is None:
        return {"message": "you must pass img2_path input"}

    model_name = input_args.get("model_name", "VGG-Face")
    detector_backend = input_args.get("detector_backend", "opencv")
    enforce_detection = input_args.get("enforce_detection", True)
    distance_metric = input_args.get("distance_metric", "cosine")
    align = input_args.get("align", True)

    verification = service.verify(
        img1_path=img1_path,
        img2_path=img2_path,
        model_name=model_name,
        detector_backend=detector_backend,
        distance_metric=distance_metric,
        align=align,
        enforce_detection=enforce_detection,
    )

    logger.debug(verification)

    return verification


@blueprint.route("/analyze", methods=["POST"])
def analyze():
    input_args = request.get_json()

    if input_args is None:
        return {"message": "empty input set passed"}

    img_path = input_args.get("img") or input_args.get("img_path")
    if img_path is None:
        return {"message": "you must pass img_path input"}

    detector_backend = input_args.get("detector_backend", "opencv")
    enforce_detection = input_args.get("enforce_detection", True)
    align = input_args.get("align", True)
    actions = input_args.get("actions", ["age", "gender", "emotion", "race"])

    demographies = service.analyze(
        img_path=img_path,
        actions=actions,
        detector_backend=detector_backend,
        enforce_detection=enforce_detection,
        align=align,
    )

    logger.debug(demographies)

    return demographies

@blueprint.route("/find", methods=["POST"])
def find():
    input_args = request.get_json()

    if input_args is None:
        response = jsonify({'error': 'empty input set passed'})
        response.status_code = 500
        return response

    img_name = input_args.get("img") or input_args.get("img_name")
    img_type = input_args.get("img_type")

    if img_name is None:
        response = jsonify({'error': 'you must pass img_name input'})
        response.status_code = 404
        return response

    if img_type == "missing" or img_type == "missing_person" or img_type == "missing_people" or img_type == "missing person" or img_type == "missing people" :
        
        img_path = os.path.join( os_path.get_main_directory() , 'mafqoud' , 'images' , "missing_people" , img_name)
        db_path = os.path.join( os_path.get_main_directory() , 'mafqoud' , 'images' , "founded_people")
        
    elif img_type == "founded" or img_type == "founded_person" or img_type == "founded_people" or img_type == "founded person" or img_type == "founded people" :
    
        img_path = os.path.join( os_path.get_main_directory() , 'mafqoud' , 'images' , "founded_people" , img_name)
        db_path = os.path.join( os_path.get_main_directory() , 'mafqoud' , 'images' , "missing_people")

    else :

        response = jsonify({'error': 'the type of the image is not correct and it should be one of those : ( missing , missing_people , missing_people , missing person , missing people ) or ( founded , founded_people , founded_people , founded person , founded people )'})
        response.status_code = 400
        return response
    
    print(img_path)
    if not os.path.exists(img_path) or not os.path.isfile(img_path):
        # If the image does not exist, return a JSON response with status code 404
        response = jsonify({'error': 'Image not found'})
        response.status_code = 404
        return response
    
        
    model_name = input_args.get("model_name", "Facenet512")
    detector_backend = input_args.get("detector_backend", "mtcnn")
    enforce_detection = input_args.get("enforce_detection", True)
    distance_metric = input_args.get("distance_metric", "euclidean_l2")
    align = input_args.get("align", True)

    if img_name is None:
        return {"message": "you must pass img1_path input"}

    if db_path is None:
        dataset_path = os.path.join(path.get_parent_path(), 'dataset')
        if img_type == "missing_person":
            img_path = os.path.join(dataset_path, 'missing_people', img_name)
            db_path = os.path.join(dataset_path, 'founded_people')
        elif img_type == "founded_people":
            img_path = os.path.join(dataset_path, 'founded_people', img_name)
            db_path = os.path.join(dataset_path, 'missing_people')

    results = service.find(
        img_path=img_path,
        db_path=db_path,
        model_name=model_name,
        detector_backend=detector_backend,
        distance_metric=distance_metric,
        align=align,
        enforce_detection=enforce_detection,
    )

    # Calculate similarity_percentage for each row
    results[0]['similarity_percentage'] =100 - ((results[0]['distance'] / results[0]['threshold']) * 100)

    data = []
    for _, row in results[0].iterrows():
        data.append({
            "identity": row['identity'],
            "similarity_percentage": row['similarity_percentage']
        })

    json_data = json.dumps(data, indent=4)


    logger.debug(json_data)
    return json_data


@blueprint.route("/dataset/sync", methods=["GET"])
def sync_datasets():
    result = service.sync_datasets()
    return jsonify(result)


@blueprint.route("/delete/pkls", methods=["GET"])
def delete_pkls():
    result = service.delete_pkls()
    return jsonify(result)