-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
47 lines (34 loc) · 1.21 KB
/
server.py
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 Flask, request, send_file
import traceback
import os
app = Flask(__name__)
from patchface import generate_adv_masked_image, patch_input_image
from werkzeug.utils import secure_filename
import cv2 as cv
import logging
@app.route("/")
def hello_world():
return "Hello, World!"
@app.route("/patchface", methods=['POST'])
def patch_uploaded_file():
logging.info(request.files)
try:
f = request.files.get("image")
if f is None:
return "File invalid", 400
file_path = f"/tmp/{secure_filename(f.filename)}"
f.save(file_path)
adv_image = None
if os.environ.get('GENERATE_PATCH') == True:
logging.info('Generating image using fgsm')
adv_image = generate_adv_masked_image(file_path)
else:
adv_image = patch_input_image(file_path)
logging.info(adv_image)
adv_path = f'/tmp/adv-{secure_filename(f.filename)}'
cv.imwrite(adv_path, adv_image)
return send_file(adv_path, 'img/jpeg', attachment_filename=secure_filename(f.filename))
except Exception as e:
tb = traceback.format_exc()
logging.error(tb)
return "Internal Server Error", 500