-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
47 lines (34 loc) · 1.32 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
from starlette.applications import Starlette
from starlette.responses import JSONResponse, HTMLResponse
import uvicorn
import aiohttp
import asyncio
from fastai.vision import open_image, load_learner, defaults, torch, Path
from io import BytesIO
defaults.device = torch.device('cpu')
app = Starlette()
async def get_bytes(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.read()
def predict_image_from_bytes(bytes):
img = open_image(BytesIO(bytes))
learn = load_learner(Path('data/plants'))
pred_class, _, _ = learn.predict(img)
return JSONResponse({"prediction": pred_class.obj})
@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)
@app.route("/")
def form(request):
return HTMLResponse(
"""
Submit an image of either an Aloe, an Anthurium or an Arrowhead plant for classification.
<form action="/classify-url" method="get">
<input type="url" name="url">
<input type="submit" value="Fetch and analyze image">
</form>
""")
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=5000)