Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

restapi.py 1.1 KB

You have to be logged in to leave a comment. Sign In
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
  1. """
  2. Run a rest API exposing the yolov5s object detection model
  3. """
  4. import argparse
  5. import io
  6. import torch
  7. from PIL import Image
  8. from flask import Flask, request
  9. app = Flask(__name__)
  10. DETECTION_URL = "/v1/object-detection/yolov5s"
  11. @app.route(DETECTION_URL, methods=["POST"])
  12. def predict():
  13. if not request.method == "POST":
  14. return
  15. if request.files.get("image"):
  16. image_file = request.files["image"]
  17. image_bytes = image_file.read()
  18. img = Image.open(io.BytesIO(image_bytes))
  19. results = model(img, size=640) # reduce size=320 for faster inference
  20. return results.pandas().xyxy[0].to_json(orient="records")
  21. if __name__ == "__main__":
  22. parser = argparse.ArgumentParser(description="Flask API exposing YOLOv5 model")
  23. parser.add_argument("--port", default=5000, type=int, help="port number")
  24. args = parser.parse_args()
  25. model = torch.hub.load("ultralytics/yolov5", "yolov5s", force_reload=True) # force_reload to recache
  26. app.run(host="0.0.0.0", port=args.port) # debug=True causes Restarting with stat
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...