forked from versae/facestory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
130 lines (121 loc) · 4.92 KB
/
api.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
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
# -*- coding: utf-8 -*-
import io
import os
import re
import uuid
from PIL import Image
from flask import request
from flask.ext import restful
from flask.ext.restful import reqparse
from db import save_user_face, get_user_faces
from utils import (get_face_properties, get_image_faces,
save_file_remote, get_file_remote)
from settings import PROJECT_ROOT
class FaceSimilarity(restful.Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument('photo_id', type=str, required=True,
help='Photo UUID to retrieve')
parser.add_argument('include_data_uri', type=bool, required=False,
default=True, help='Photo UUID to retrieve')
args = parser.parse_args()
photo_id = args["photo_id"]
include_data_uri = args["include_data_uri"]
if photo_id:
faces = get_user_faces(
photo_id,
exclude=["user_agent", "user_ip"]
)
photo_filename = "{}.png".format(photo_id)
image_data_uri = get_file_remote(photo_filename,
data_uri=include_data_uri)
if faces:
return {
"faces": faces,
"message": "OK",
"image_data_uri": image_data_uri,
"image_id": photo_id,
}
return {
"faces": [],
"message": "Invalid photo id",
"image_data_uri": "",
"image_id": ""
}
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('data_uri', type=str, required=True,
help='Data URI with the image of a face')
args = parser.parse_args()
# Get image from request
data_uri = args['data_uri']
image_id = uuid.uuid4().hex
image_name = u"{}.png".format(image_id)
image_path = os.path.join(PROJECT_ROOT, "static", "faces", image_name)
image_str = re.search(r'base64,(.*)', data_uri).group(1)
image_base64 = image_str.decode('base64')
# Save image in disk
message = u"OK"
error = u""
image_url = save_file_remote(image_name, image_base64,
headers={"Content-Type": "image/png"})
if not image_url:
image_url = u"/static/faces/{}".format(image_name)
try:
output = open(image_path, 'wb')
output.write(image_base64)
output.close()
except IOError:
error = u"Error when saving image in disk"
# Create image in memory
image = Image.open(io.BytesIO(image_base64))
image_faces = get_image_faces(io.BytesIO(image_base64))
ages = []
styles = []
urls = []
symmetries = []
if not image_faces or len(image_faces['tags']) == 0:
message = u"No faces found"
else:
for image_face in image_faces['tags']:
face_props = get_face_properties(image, image_face)
ages.append(face_props["painting_age"])
styles.append(face_props["painting_style"])
urls.append(face_props["url"])
symmetries.append(face_props["symmetry"])
# Saving the info to the database
image_width, image_height = image.size
image_face["image_uuid"] = image_id
image_face["image_url"] = image_url
image_face["image_filename"] = image_name
image_face["image_width"] = image_width
image_face["image_height"] = image_height
image_face["painting_age"] = face_props["painting_age"]
image_face["painting_style"] = face_props["painting_style"]
image_face["face_id"] = face_props["id"]
image_face["face_url"] = face_props["url"]
image_face["face_symmetry"] = face_props["symmetry"]
image_face["user_ip"] = request.remote_addr
image_face["user_agent"] = {
"browser": request.user_agent.browser,
"platform": request.user_agent.platform,
"language": request.user_agent.language,
"string": request.user_agent.string,
"version": request.user_agent.version,
}
save_user_face(image_face)
return {
'image_url': image_url,
'image_id': image_id,
'host': request.host,
'message': message,
'error': error,
'faces': image_faces.get("tags", []),
'ages': ages,
'styles': styles,
'urls': urls,
'symmetries': symmetries,
}
def add_api(app):
api = restful.Api(app)
api.add_resource(FaceSimilarity, '/api/similarity')