Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
  2. """
  3. Run a Flask REST API exposing a YOLOv5s model
  4. """
  5. import argparse
  6. import io
  7. import torch
  8. from flask import Flask, request
  9. from PIL import Image
  10. app = Flask(__name__)
  11. DETECTION_URL = "/v1/object-detection/yolov5s"
  12. @app.route(DETECTION_URL, methods=["POST"])
  13. def predict():
  14. if not request.method == "POST":
  15. return
  16. if request.files.get("image"):
  17. # Method 1
  18. # with request.files["image"] as f:
  19. # im = Image.open(io.BytesIO(f.read()))
  20. # Method 2
  21. im_file = request.files["image"]
  22. im_bytes = im_file.read()
  23. im = Image.open(io.BytesIO(im_bytes))
  24. results = model(im, size=640) # reduce size=320 for faster inference
  25. return results.pandas().xyxy[0].to_json(orient="records")
  26. if __name__ == "__main__":
  27. parser = argparse.ArgumentParser(description="Flask API exposing YOLOv5 model")
  28. parser.add_argument("--port", default=5000, type=int, help="port number")
  29. opt = parser.parse_args()
  30. # Fix known issue urllib.error.HTTPError 403: rate limit exceeded https://github.com/ultralytics/yolov5/pull/7210
  31. torch.hub._validate_not_a_forked_repo = lambda a, b, c: True
  32. model = torch.hub.load("ultralytics/yolov5", "yolov5s", force_reload=True) # force_reload to recache
  33. app.run(host="0.0.0.0", port=opt.port) # debug=True causes Restarting with stat