mirror of
https://github.com/StefBuwalda/ProjectIOT.git
synced 2025-10-29 18:59:57 +00:00
refactoring + execution time
This commit is contained in:
@@ -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.
BIN
application/__pycache__/models.cpython-313.pyc
Normal file
BIN
application/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
BIN
application/api/__pycache__/image_processing.cpython-313.pyc
Normal file
BIN
application/api/__pycache__/image_processing.cpython-313.pyc
Normal file
Binary file not shown.
BIN
application/api/__pycache__/views.cpython-313.pyc
Normal file
BIN
application/api/__pycache__/views.cpython-313.pyc
Normal file
Binary file not shown.
16
application/api/image_processing.py
Normal file
16
application/api/image_processing.py
Normal 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"])
|
||||
@@ -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
BIN
image.jpg
Binary file not shown.
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 18 KiB |
Binary file not shown.
2
seed.py
2
seed.py
@@ -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"))
|
||||
|
||||
52
server.py
52
server.py
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user