-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
132 lines (101 loc) · 4.02 KB
/
main.py
File metadata and controls
132 lines (101 loc) · 4.02 KB
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
from flask import Flask
from flask_restful import Resource, Api, reqparse
from environment import env_config
import os
import sqlalchemy.pool as pool
import MySQLdb
from google.appengine.ext import ndb
import json
import logging
app = Flask(__name__)
api = Api(app)
def connectDB():
env = os.getenv('SERVER_SOFTWARE')
if (env and env.startswith('Google App Engine/')):
# Connecting from App Engine
db = MySQLdb.connect(
unix_socket=env_config['MYSQL_SOCKET_PATH'],
user=env_config['MYSQL_USER'],
passwd=env_config['MYSQL_PASSWORD'])
else:
# Connecting from an external network.
# Make sure your network is whitelisted
db = MySQLdb.connect(
host=env_config['MYSQL_IP'],
port=3306,
user=env_config['MYSQL_USER'],
passwd=env_config['MYSQL_PASSWORD'])
return db
dbpool = pool.QueuePool(connectDB, max_overflow=10, pool_size=5)
class DateCreation(ndb.Expando):
pass
class MKGMetadata(ndb.Expando):
# only structured properties have to be defined
date = ndb.LocalStructuredProperty(DateCreation)
inventory_no = ndb.StringProperty()
record_id = ndb.StringProperty()
class Portrait(ndb.Expando):
mkg_metadata = ndb.LocalStructuredProperty(MKGMetadata)
inventory_no = ndb.StringProperty()
record_id = ndb.StringProperty()
def as_dict(self):
return self.to_dict(exclude=["vision_response"])
class PortraitService():
@staticmethod
def find_by_inventory_no(inventory_no):
return Portrait.query(Portrait.inventory_no == inventory_no).get()
# Config REST API
@app.route('/')
def hello_world():
return 'Hello World, %s' % env_config['MYSQL_DATABASE']
class MetadataForObject(Resource):
def get(self):
try:
parser = reqparse.RequestParser()
parser.add_argument('inventory_no', location='args')
args = parser.parse_args()
_inv_no = args['inventory_no']
portrait = PortraitService.find_by_inventory_no(_inv_no)
if portrait is None:
return {"msg": "No entry found"}, 404
return portrait.as_dict()
except Exception as e:
return {'error': str(e)}, 500
class SimilarHeadRotation(Resource):
def post(self):
try:
parser = reqparse.RequestParser()
parser.add_argument('pan', type=float, help='Left/Right rotation of head')
parser.add_argument('tilt', type=float, help='Up/Down rotation of head')
parser.add_argument('roll', type=float, help='Leaning of head')
args = parser.parse_args()
_pan = args['pan']
_tilt = args['tilt']
_roll = args['roll']
pan_tolerance = 3.0
tilt_tolerance = 3.0
roll_tolerance = 10.0
# query
conn = dbpool.connect()
cursor = conn.cursor()
cursor.execute('SELECT * FROM zeitblick_db.Faces WHERE panAngle < %s AND panAngle > %s AND '
'tiltAngle < %s AND tiltAngle > %s AND '
'rollAngle < %s AND rollAngle > %s',
(_pan + pan_tolerance, _pan - pan_tolerance,
_tilt + tilt_tolerance, _tilt - tilt_tolerance,
_roll + roll_tolerance, _roll - roll_tolerance,))
result = cursor.fetchone()
inventory_no = result[1]
portrait = PortraitService.find_by_inventory_no(inventory_no)
if portrait is None:
return {"msg": "No entry found"}, 404
return portrait.as_dict()
except Exception as e:
return {'error': str(e)}, 500
finally:
cursor.close()
conn.close()
api.add_resource(MetadataForObject, '/MetadataForObject')
api.add_resource(SimilarHeadRotation, '/SimilarHeadRotation')
if __name__ == '__main__':
app.run()