File size: 1,880 Bytes
feb85a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
sys.path.append('.')

from flask import Flask, request, jsonify
from time import gmtime, strftime
import os
import base64
import json
import cv2
import numpy as np

from tensorflow.keras.preprocessing import image
from keras_facenet import FaceNet
import pickle

# create a facenet model
embedding_model = FaceNet(key = '20180402-114759',
    use_prebuilt=True,
    cache_folder= os.path.abspath(os.path.dirname(__file__)) + '/facenet_weights').model

clf_model = pickle.load(open(os.path.abspath(os.path.dirname(__file__)) + '/model/clf_mode.sav', 'rb'))

target_shape = (160, 160)

app = Flask(__name__) 

app.config['SITE'] = "http://0.0.0.0:8000/"
app.config['DEBUG'] = False

@app.route('/api/detect_human_face', methods=['POST'])
def detect_human_face():
    file1 = request.files['image1']
    image1 = cv2.imdecode(np.fromstring(file1.read(), np.uint8), cv2.IMREAD_COLOR)
    if image1 is None:
        result = "image1: is null!"
        status = "ok"
        response = jsonify({"status": status, "data": {"result": result}})
        response.status_code = 200
        response.headers["Content-Type"] = "application/json; charset=utf-8"
        return response

    X = np.float32([(np.float32(image1) - 127.5) / 127.5])
    X_ft = embedding_model.predict(X, batch_size=1)
    anomaly_score = clf_model.decision_function(X_ft) * -1

    if anomaly_score > 1:
        result = "Not Human"
    else:
        result = "Human"
  
    status = "ok"
    response = jsonify(
    {
        "status": status, 
        "data": {
            "result": result, 
            "anomaly_score": float(anomaly_score)
            }
    })

    response.status_code = 200
    response.headers["Content-Type"] = "application/json; charset=utf-8"
    return response

if __name__ == '__main__':
    port = int(os.environ.get("PORT", 8000))
    app.run(host='0.0.0.0', port=port)