-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.py
41 lines (31 loc) · 1.06 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
import io
import os
import json
from PIL import Image
import torch
from flask import Flask, jsonify, url_for, render_template, request, redirect
app = Flask(__name__)
RESULT_FOLDER = os.path.join('static')
app.config['RESULT_FOLDER'] = RESULT_FOLDER
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
model.eval()
def get_prediction(img_bytes):
img = Image.open(io.BytesIO(img_bytes))
imgs = [img] # batched list of images
# Inference
results = model(imgs, size=640) # includes NMS
return results
@app.route('/', methods=['GET', 'POST'])
def predict():
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files.get('file')
if not file:
return
img_bytes = file.read()
results = get_prediction(img_bytes)
results.save(save_dir='static')
full_filename = os.path.join(app.config['RESULT_FOLDER'], 'results0.jpg')
return redirect('static/image0.jpg')
return render_template('index.html')