-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
40 lines (33 loc) · 1.02 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
from yolo.yolo_model import YOLO
from utils import detect_image
from flask import Flask, Response, request as req
from flask_restful import Resource, Api
import jsonpickle
import numpy as np
import cv2
from keras import backend as K
def load_model():
return YOLO(0.9, 0.5, 'tiny_yolo.h5')
app = Flask(__name__)
api = Api(app)
class Detection(Resource):
def post(self):
try:
model = load_model()
except:
print("Model Failed to load")
try:
nparr = np.fromstring(req.data, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
except:
print("Image failed to load")
try:
response = detect_image(img, model)
print(response[1:])
response_pickled = jsonpickle.encode(response)
except:
print("meh")
return Response(response=response_pickled, status=200, mimetype="application/json")
api.add_resource(Detection, '/')
if __name__ == '__main__':
app.run(debug = True)