Spaces:
Runtime error
Runtime error
""" | |
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 | |
app = Flask(__name__) | |
DETECTION_URL = "/v1/detect" | |
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 | |
return results.pandas().xyxy[0].to_json(orient="records") | |
if __name__ == "__main__": | |
#app.run(host="0.0.0.0", port=7860) # debug=True causes Restarting with stat | |
serve(app, host="0.0.0.0", port=7860) | |