Advertisement
kopyl

Untitled

Jun 14th, 2023 (edited)
962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. import json
  2. import json
  3. from flask import Flask, request, jsonify
  4. import logging
  5. from ultralytics import YOLO
  6.  
  7.  
  8. model = YOLO("best.pt")  # load a pretrained model (recommended for training)
  9.  
  10.  
  11. app = Flask(__name__)
  12. log = logging.getLogger("werkzeug")
  13. log.setLevel(logging.ERROR)
  14.  
  15. class_names = {0: 'apple', 1: 'banana', 2: 'banana'}
  16.  
  17.  
  18.  
  19. def predict_single_image(image_url):
  20.     results = model(image_url)
  21.     processed_resutls = []
  22.  
  23.     for result in results:
  24.         boxes = result.boxes  # Boxes object for bbox outputs
  25.         for box in boxes:
  26.             confidence = box.conf.item()
  27.             name = class_names[int(box.cls.item())]
  28.             processed_resutls.append([name, confidence])
  29.  
  30.     return processed_resutls
  31.  
  32.  
  33. @app.route("/", methods=["POST"])
  34. def home():
  35.     data = request.get_json()
  36.     image_url = data.get("image_url")
  37.  
  38.     if not image_url:
  39.         return jsonify({"result": "No image_url"}), 400
  40.     token = request.headers.get("Authorization")
  41.     if token != "8Jw1kj5Woa4SDHtD%hH!7v4--":
  42.         return jsonify({"result": "Invalid token"}), 401
  43.     print(image_url)
  44.     result = {
  45.         "predictions": predict_single_image(image_url),
  46.     }
  47.  
  48.     if data.get("return_version"):
  49.         result["version"] = "1.0.4"
  50.  
  51.     return jsonify(result), 200
  52.  
  53.  
  54. @app.route("/", methods=["GET"])
  55. def home_get():
  56.     return "Hello World!"
  57.  
  58.  
  59. if __name__ == "__main__":
  60.     app.run(debug=True, host="0.0.0.0", port=80)
  61.     # app.run(debug=True, host="0.0.0.0", port=80, threaded=False, processes=64)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement