- first pass as rename and dependency update
[face-privacy-filter.git] / testing / app.py
1 #!/usr/bin/env python3
2 import connexion
3 import logging
4
5 import argparse
6 import json
7 import time
8 import os
9
10 from flask import current_app, make_response
11
12 import pandas as pd
13
14 from acumos.wrapped import load_model
15 from face_privacy_filter.transform_detect import FaceDetectTransform
16 import base64
17
18
19 def generate_image_df(path_image="", bin_stream=b""):
20     # munge stream and mimetype into input sample
21     if path_image and os.path.exists(path_image):
22         bin_stream = open(path_image, 'rb').read()
23     bin_stream = base64.b64encode(bin_stream)
24     if type(bin_stream) == bytes:
25         bin_stream = bin_stream.decode()
26     return pd.DataFrame([['image/jpeg', bin_stream]], columns=[FaceDetectTransform.COL_IMAGE_MIME, FaceDetectTransform.COL_IMAGE_DATA])
27
28
29 def transform(mime_type, base64_data):
30     app = current_app
31     time_start = time.clock()
32     image_read = base64_data.stream.read()
33     X = generate_image_df(bin_stream=image_read)
34     print(X)
35
36     if app.model_detect is not None:
37         pred_out = app.model_detect.transform.from_native(X)
38     if app.model_proc is not None:
39         pred_prior = pred_out
40         #pred_out = app.model_proc.transform.from_msg(pred_prior.as_msg())
41         pred_out = app.model_proc.transform.from_native(pred_prior.as_native())
42     time_stop = time.clock()
43
44     retStr = json.dumps(pred_out.as_native().to_dict(orient='records'), indent=4)
45
46     # formulate response
47     resp = make_response((retStr, 200, { } ))
48     # allow 'localhost' from 'file' or other;
49     # NOTE: DO NOT USE IN PRODUCTION!!!
50     resp.headers['Access-Control-Allow-Origin'] = '*'
51     print(retStr[:min(200, len(retStr))])
52     #print(pred)
53     return resp
54
55
56 if __name__ == '__main__':
57     parser = argparse.ArgumentParser()
58     parser.add_argument('-p', "--port", type=int, default=8884, help='port to launch the simple web server')
59     parser.add_argument('-d', "--modeldir_detect", type=str, default='../model_detect', help='model directory for detection')
60     parser.add_argument('-a', "--modeldir_analyze", type=str, default='../model_pix', help='model directory for detection')
61     pargs = parser.parse_args()
62
63     print("Configuring local application... {:}".format(__name__))
64     logging.basicConfig(level=logging.INFO)
65     app = connexion.App(__name__)
66     app.add_api('swagger.yaml')
67     # example usage:
68     #     curl -F image_binary=@test.jpg -F mime_type="image/jpeg" "http://localhost:8885/transform"
69
70     app.app.model_detect = None
71     if pargs.modeldir_detect:
72         if not os.path.exists(pargs.modeldir_detect):
73             print("Failed loading of detect model '{:}' even though it was specified...".format(pargs.modeldir_detect))
74         else:
75             print("Loading detect model... {:}".format(pargs.modeldir_detect))
76             app.app.model_detect = load_model(pargs.modeldir_detect)
77
78     app.app.model_proc = None
79     if pargs.modeldir_analyze:
80         if not os.path.exists(pargs.modeldir_analyze):
81             print("Failed loading of processing model '{:}' even though it was specified...".format(
82                 pargs.modeldir_analyze))
83         else:
84             print("Loading processing model... {:}".format(pargs.modeldir_analyze))
85             app.app.model_proc = load_model(pargs.modeldir_analyze)
86
87     # run our standalone gevent server
88     app.run(port=pargs.port) #, server='gevent')