diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9bb6253 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 jzhang533 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ab2b002 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# deploy a yolov5 model using pytorch + flask + +## start + +```bash +$ FLASK_ENV=development FLASK_APP=app.py flask run +``` +then, visit http://localhost:5000/ in your browser + +## environments + +- pytorch >= 1.6 +- flask +- pillow + +## reference +- https://github.com/ultralytics/yolov5 +- [Load YOLOv5 from PyTorch Hub ](https://github.com/ultralytics/yolov5/issues/36) +- https://github.com/avinassh/pytorch-flask-api-heroku diff --git a/app.py b/app.py new file mode 100644 index 0000000..955fa83 --- /dev/null +++ b/app.py @@ -0,0 +1,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).autoshape() # for PIL/cv2/np inputs and NMS +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 as results1.jpg, results2.jpg... etc. + os.rename("results0.jpg", "static/results0.jpg") + + full_filename = os.path.join(app.config['RESULT_FOLDER'], 'results0.jpg') + return redirect('static/results0.jpg') + return render_template('index.html') diff --git a/static/pytorch.png b/static/pytorch.png new file mode 100644 index 0000000..a3d4d87 Binary files /dev/null and b/static/pytorch.png differ diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..db5294a --- /dev/null +++ b/static/style.css @@ -0,0 +1,29 @@ +html, +body { + height: 100%; +} + +body { + display: -ms-flexbox; + display: flex; + -ms-flex-align: center; + align-items: center; + padding-top: 40px; + padding-bottom: 40px; + background-color: #f5f5f5; +} + +.form-signin { + width: 100%; + max-width: 330px; + padding: 15px; + margin: auto; +} + +.form-signin .form-control { + position: relative; + box-sizing: border-box; + height: auto; + padding: 10px; + font-size: 16px; +} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..39abaff --- /dev/null +++ b/templates/index.html @@ -0,0 +1,52 @@ + + + + + + + + + + yolov5 object deteection using PyTorch + + +
+ +

Upload any image

+ +
+ +

Built using Pytorch, Flask and Love

+
+ + + + + + diff --git a/templates/result.html b/templates/result.html new file mode 100644 index 0000000..1948c73 --- /dev/null +++ b/templates/result.html @@ -0,0 +1,39 @@ + + + + + + + + + + Image Prediction using PyTorch + + + User Image +
+ +

Prediction

+
Detected Image: {{ class_name }}
+
ImageNet Class ID: {{ class_id }}
+

Built using Pytorch, Flask and Love

+
+ + + + + + + +