mirror of
https://github.com/StefBuwalda/ProjectIOT.git
synced 2025-10-30 03:09:58 +00:00
refactoring + execution time
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask_migrate import Migrate
|
from flask_migrate import Migrate
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
from pyplatex import ANPR # type: ignore
|
||||||
|
|
||||||
# Web Server
|
# Web Server
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@@ -8,7 +9,11 @@ app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.sqlite"
|
|||||||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||||
app.config["SECRET_KEY"] = "bvjchsygvduycgsyugc"
|
app.config["SECRET_KEY"] = "bvjchsygvduycgsyugc"
|
||||||
|
|
||||||
|
|
||||||
# ORM
|
# ORM
|
||||||
db = SQLAlchemy(app)
|
db = SQLAlchemy(app)
|
||||||
|
|
||||||
migrate = Migrate(app, db)
|
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 application import db, app
|
||||||
from models import AllowedPlate
|
from application.models import AllowedPlate
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.session.add(AllowedPlate("MUN389"))
|
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
|
from ultralytics.nn.tasks import DetectionModel # type: ignore
|
||||||
import os
|
|
||||||
import torch
|
import torch
|
||||||
import asyncio
|
from application import app
|
||||||
from application import app, db
|
from application.api.views import api_blueprint
|
||||||
from application.models import LoggedItem, AllowedPlate
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
torch.serialization.add_safe_globals( # type: ignore
|
torch.serialization.add_safe_globals( # type: ignore
|
||||||
{"DetectionModel": DetectionModel} # type: ignore
|
{"DetectionModel": DetectionModel} # type: ignore
|
||||||
)
|
)
|
||||||
|
|
||||||
anpr = ANPR()
|
app.register_blueprint(api_blueprint)
|
||||||
|
|
||||||
# Saving images locally
|
|
||||||
UPLOAD_FOLDER = "uploads"
|
|
||||||
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
||||||
|
|
||||||
|
|
||||||
# Default app route
|
# Default app route
|
||||||
@@ -25,41 +17,5 @@ def home():
|
|||||||
return "Hello, World!"
|
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__":
|
if __name__ == "__main__":
|
||||||
app.run(host="192.168.137.1", port=2222, debug=True)
|
app.run(host="192.168.137.1", port=2222, debug=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user