-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
116 lines (91 loc) · 3.34 KB
/
app.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
#deep learning libraries
from fastai.vision import *
#web frameworks
from starlette.applications import Starlette
from starlette.responses import JSONResponse, HTMLResponse, RedirectResponse
import uvicorn
import aiohttp
import asyncio
import os
import sys
import base64
from PIL import Image
import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox
async def get_bytes(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.read()
app = Starlette()
#path = Path('')
#learner = load_learner(path)
@app.route("/upload", methods = ["POST"])
async def upload(request):
data = await request.form()
bytes = await (data["file"].read())
return predict_image_from_bytes(bytes)
@app.route("/classify-url", methods = ["GET"])
async def classify_url(request):
bytes = await get_bytes(request.query_params["url"])
return predict_image_from_bytes(bytes)
def predict_image_from_bytes(bytes):
# Added ******************
img = io.BytesIO(bytes)
img.seek(0)
file_bytes = np.asarray(bytearray(img.read()), dtype=np.uint8)
frame = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
print('imag from cv2e read')
model="yolov3-tiny"
confidence=0.45
bbox, label, conf = cv.detect_common_objects(frame, confidence=confidence, model=model)
output_image = draw_bbox(frame, bbox, label, conf, write_conf=True)
print('prediction object detection donne')
print(label, conf)
pred_class = label
cv2.imwrite('img_output.jpg', output_image)
img_uri = base64.b64encode(open("img_output.jpg", 'rb').read()).decode('utf-8')
# Added ******************
formatted_outputs = ["{:.1f}%".format(value) for value in [x * 100 for x in conf]]
pred_probs = sorted(zip(label, map(str, formatted_outputs)),
key = lambda p: p[1],
reverse = True
)
return HTMLResponse(
"""
<html>
<body>
<p> Prediction: <b> %s </b> </p>
<p> Confidence: <b> %s </b> </p>
</body>
<figure class = "figure">
<img src="data:image/png;base64, %s" class = "figure-img">
</figure>
</html>
""" %(pred_class, pred_probs, img_uri))
@app.route("/")
def form(request):
return HTMLResponse(
"""
<h1> Deployment of Yolov3.tiny by Data Science and Applications Research Unit </h1>
<p> Object detection </p>
<form action="/upload" method = "post" enctype = "multipart/form-data">
<u> Select picture to upload: </u> <br> <p>
1. <input type="file" name="file"><br><p>
2. <input type="submit" value="Upload">
</form>
<br>
<br>
<u> Submit picture URL </u>
<form action = "/classify-url" method="get">
1. <input type="url" name="url" size="60"><br><p>
2. <input type="submit" value="Upload">
</form>
""")
@app.route("/form")
def redirect_to_homepage(request):
return RedirectResponse("/")
if __name__ == "__main__":
if "serve" in sys.argv:
port = int(os.environ.get("PORT", 8008))
uvicorn.run(app, host = "0.0.0.0", port = port)