from flask import Flask, request, jsonify import os from pyplatex import ANPR import torch from ultralytics.nn.tasks import DetectionModel import asyncio torch.serialization.add_safe_globals({"DetectionModel": DetectionModel}) # Web Server app = Flask(__name__) # Saving images locally UPLOAD_FOLDER = "uploads" os.makedirs(UPLOAD_FOLDER, exist_ok=True) # Default app route @app.route("/") def home(): return "Hello, World!" # API to process vehicle @app.route("/api", methods=["POST"]) def data(): # Check if image is present if "image" not in request.files: return jsonify({"error": "No file part"}), 400 file = request.files["image"] # get the image from the packet if file.filename == "" or not file.filename: return jsonify({"error": "No selected file"}), 400 # Check if image ends in .jpg if file.filename.lower().endswith(".jpg"): filepath = os.path.join(UPLOAD_FOLDER, file.filename) file.save(filepath) print(asyncio.run(process_image(filepath))) return jsonify( { "message": "File uploaded successfully", "filename": file.filename, "status": True, } ) return jsonify({"error": "Only JPEG files allowed"}), 400 async def process_image(file: str): anpr = ANPR() plates = await anpr.detect(file) return plates if __name__ == "__main__": app.run(host="192.168.137.1", port=2222, debug=True)