refactoring + execution time

This commit is contained in:
2025-04-25 16:33:01 +02:00
parent 8d0d9c4321
commit f22ea02e59
11 changed files with 55 additions and 51 deletions

View File

@@ -1,6 +1,7 @@
from flask import Flask
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from pyplatex import ANPR # type: ignore
# Web Server
app = Flask(__name__)
@@ -8,7 +9,11 @@ app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.sqlite"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SECRET_KEY"] = "bvjchsygvduycgsyugc"
# ORM
db = SQLAlchemy(app)
migrate = Migrate(app, db)
# ANPR instance
anpr = ANPR()

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,16 @@
from application import anpr
import io
async def process_image(image: bytes) -> str:
print("Saving file to memory")
image_file = io.BytesIO(image)
print("Processing image")
anpr_info = await anpr.detect(image_file) # type: ignore
if anpr_info is None:
print("Something went wrong with ANPR")
return ""
return str(anpr_info["plate_number"])

View File

@@ -1,3 +1,30 @@
from flask import Blueprint
from flask import Blueprint, request, jsonify
from application import db
from application.models import AllowedPlate, LoggedItem
from application.api.image_processing import process_image
from datetime import datetime
import asyncio
api_blueprint = Blueprint('api', )
api_blueprint = Blueprint("api", __name__, template_folder="templates")
# API to process vehicle
@api_blueprint.route("/api", methods=["POST"])
def data():
data = request.data
np = asyncio.run(process_image(image=data))
# Check if the found plate is allowed to exit
allowed = AllowedPlate.query.filter_by(plate=np).first() is not None
db.session.add( # Log the found numberplate and status
LoggedItem(plate=np, allowed=allowed, datetime=datetime.now())
)
db.session.commit()
# Return decision to Pico
return jsonify(
{
"message": "Image sent succesfully",
"status": allowed,
}
)

BIN
image.jpg

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

View File

@@ -1,5 +1,5 @@
from application import db, app
from models import AllowedPlate
from application.models import AllowedPlate
with app.app_context():
db.session.add(AllowedPlate("MUN389"))

View File

@@ -1,22 +1,14 @@
from flask import request, jsonify
from pyplatex import ANPR # type: ignore
from ultralytics.nn.tasks import DetectionModel # type: ignore
import os
import torch
import asyncio
from application import app, db
from application.models import LoggedItem, AllowedPlate
from datetime import datetime
from application import app
from application.api.views import api_blueprint
torch.serialization.add_safe_globals( # type: ignore
{"DetectionModel": DetectionModel} # type: ignore
)
anpr = ANPR()
# Saving images locally
UPLOAD_FOLDER = "uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
app.register_blueprint(api_blueprint)
# Default app route
@@ -25,41 +17,5 @@ def home():
return "Hello, World!"
# API to process vehicle
@app.route("/api", methods=["POST"])
def data():
data = request.data
with open("image.jpg", "wb") as f:
f.write(data)
status = asyncio.run(process_image(file="image.jpg", anpr=anpr))
return jsonify(
{
"message": "Image sent succesfully",
"status": status,
}
)
async def process_image(file: str, anpr: ANPR) -> bool:
print("Processing image")
anpr_info = await anpr.detect(file) # type: ignore
number_plate = anpr_info["plate_number"]
if number_plate:
allowed = (
AllowedPlate.query.filter_by(plate=number_plate).first()
is not None
)
db.session.add(
LoggedItem(
plate=number_plate, allowed=allowed, datetime=datetime.now()
)
)
db.session.commit()
return allowed
return False
if __name__ == "__main__":
app.run(host="192.168.137.1", port=2222, debug=True)