-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.py
60 lines (47 loc) · 1.59 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
import io
from operator import truediv
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
# finds the model inside your directory automatically - works only if there is one model
def find_model():
for f in os.listdir():
if f.endswith(".pt"):
return f
print("please place a model file in this directory!")
model_name = find_model()
model =torch.hub.load("WongKinYiu/yolov7", 'custom',model_name)
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')
filename = 'image0.jpg'
return render_template('result.html',result_image = filename,model_name = model_name)
return render_template('index.html')
@app.route('/detect', methods=['GET', 'POST'])
def handle_video():
# some code to be implemented later
pass
@app.route('/webcam', methods=['GET', 'POST'])
def web_cam():
# some code to be implemented later
pass