File size: 1,585 Bytes
85b1295
 
 
30dbf30
85b1295
 
 
 
f435078
6db03a3
3d15ada
6db03a3
85b1295
2aef184
85b1295
 
 
 
 
b15ad93
85b1295
30dbf30
b15ad93
30dbf30
13be7c6
 
85b1295
 
 
 
 
 
 
 
b15ad93
 
85b1295
b15ad93
85b1295
2aef184
b15ad93
c70bcd8
 
b15ad93
 
 
 
 
 
 
 
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
"""
Run a rest API exposing the yolov5s object detection model
"""

import io
import torch
from flask import Flask, request
from PIL import Image
from waitress import serve
import subprocess
import argparse


#subprocess.run(["export", "FLASK_APP","=","app.py"]) 
app = Flask(__name__)

DETECTION_URL = "/v1/detect"


@app.route(DETECTION_URL,methods=["POST"])
def predict():
    
    #model = torch.hub.load('ultralytics/yolov5', 'custom', path='best2.pt', force_reload=True)  # force_reload to recache
    
    '''if not request.method == "POST":
        return'''

    if request.files.get("image"):
        image_file = request.files["image"]
        image_bytes = image_file.read()

        img = Image.open(io.BytesIO(image_bytes))

        results = model(img, size=640)  # reduce size=320 for faster inference
        results=results.pandas().xyxy[0].to_json(orient="records")
        return f"{results}"

'''
if __name__ == "__main__":

    #subprocess.run(["export","FLASK_ENV","=","development"])
    app.run(host="0.0.0.0", port=7860)  # debug=True causes Restarting with stat
    #serve(app, host="0.0.0.0", port=7860)
'''    
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Flask API exposing YOLOv5 model")
    parser.add_argument("--port", default=7860, type=int, help="port number")
    args = parser.parse_args()

    model = torch.hub.load('ultralytics/yolov5', 'custom', path='best2.pt', force_reload=True)  # force_reload to recache
    app.run(host="0.0.0.0", port=args.port,debug =True)  # debug=True causes Restarting with stat