-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
42 lines (35 loc) · 1.42 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
# Todo Bottle REST API
from bottle import route, run, Response, request
from time import perf_counter
import os
from src.face_detect import face_detect
import requests
# ENV Variables
IS_PRODUCTION = os.environ.get('ENV', None) == 'production'
PORT = os.environ.get('PORT', 8080)
FACE_DETECT_CONFIDENCE = os.environ.get('FACE_DETECT_CONFIDENCE', 0.8)
# ROUTES
@route('/face-detect', method='post')
def index():
upload = request.files.get('image')
name, ext = os.path.splitext(upload.filename)
print('File extension', upload)
# Request -- TODO ALLOW Fetching from URL instead of uploading
# response = requests.get('http://radenkovic.org/diary/sanda1.jpg', stream=True)
# response.raw.decode_content = True
if ext.lower() not in ('.jpg', '.jpeg'):
Response.status = 400 # set to BadRequest
return { 'success': False, 'message': "Only .jpg and .jpeg fiels are allowed."}
print('image uploaded')
t1_start = perf_counter()
result = face_detect(upload.file, confidence=FACE_DETECT_CONFIDENCE)
t1_stop = perf_counter()
print('face detection took (seconds):', t1_stop - t1_start)
return {'endpoint': '/face-detect', 'success': True, 'data': result}
# Production server
if IS_PRODUCTION:
print('Running in production mode')
run(server='gunicorn', host = 'localhost', port = PORT)
else:
print('Running in development mode')
run(host='localhost', port=PORT)