-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp.py
48 lines (37 loc) · 1.42 KB
/
webapp.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
"""
Simple app to upload an image via a web form
and view the inference results on the image in the browser.
"""
import argparse
import io
import os
from PIL import Image
import datetime
import torch
from flask import Flask, render_template, request, redirect
app = Flask(__name__)
DATETIME_FORMAT = "%Y-%m-%d_%H-%M-%S-%f"
@app.route("/", methods=["GET", "POST"])
def predict():
if request.method == "POST":
if "file" not in request.files:
return redirect(request.url)
file = request.files["file"]
if not file:
return
img_bytes = file.read()
img = Image.open(io.BytesIO(img_bytes))
results = model([img])
results.render() # updates results.imgs with boxes and labels
now_time = datetime.datetime.now().strftime(DATETIME_FORMAT)
img_savename = f"static/{now_time}.png"
Image.fromarray(results.ims[0]).save(img_savename)
return redirect(img_savename)
return render_template("index.html")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Flask app exposing yolov5 models")
parser.add_argument("--port", default=5000, type=int, help="port number")
args = parser.parse_args()
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) # force_reload = recache latest code
model.eval()
app.run(host="0.0.0.0", port=args.port) # debug=True causes Restarting with stat