mirror of
https://github.com/StefBuwalda/ProjectIOT.git
synced 2025-10-30 19:29:57 +00:00
123123123
This commit is contained in:
91
server.py
91
server.py
@@ -1,86 +1,69 @@
|
||||
from flask import Flask, request, jsonify
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_migrate import Migrate
|
||||
from datetime import datetime
|
||||
from pyplatex import ANPR
|
||||
from ultralytics.nn.tasks import DetectionModel
|
||||
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 models import LoggedItem, AllowedPlate
|
||||
from datetime import datetime
|
||||
|
||||
torch.serialization.add_safe_globals({"DetectionModel": DetectionModel})
|
||||
torch.serialization.add_safe_globals( # type: ignore
|
||||
{"DetectionModel": DetectionModel} # type: ignore
|
||||
)
|
||||
|
||||
# Web Server
|
||||
app = Flask(__name__)
|
||||
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.sqlite"
|
||||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||
app.config["SECRET_KEY"] = "bvjchsygvduycgsyugc"
|
||||
|
||||
# ORM
|
||||
db = SQLAlchemy()
|
||||
db.init_app(app)
|
||||
|
||||
migrate = Migrate(app, db)
|
||||
|
||||
# Saving images locally
|
||||
UPLOAD_FOLDER = "uploads"
|
||||
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
||||
|
||||
|
||||
# db classes
|
||||
class Plate(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
plate = db.Column(db.String(40), nullable=False)
|
||||
|
||||
|
||||
class LoggedItem(Plate):
|
||||
dateLogged = db.Column(db.DateTime, default=datetime.now)
|
||||
|
||||
|
||||
# Default app route
|
||||
@app.route("/")
|
||||
def home():
|
||||
return "Hello, World!"
|
||||
|
||||
|
||||
i = 0
|
||||
|
||||
|
||||
# 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
|
||||
data = request.data
|
||||
global i
|
||||
|
||||
file = request.files["image"] # get the image from the packet
|
||||
with open(f"image{i}.jpg", "wb") as f:
|
||||
f.write(data)
|
||||
|
||||
if file.filename == "" or not file.filename:
|
||||
return jsonify({"error": "No selected file"}), 400
|
||||
status = asyncio.run(process_image(f"image{i}.jpg"))
|
||||
|
||||
# Check if image ends in .jpg
|
||||
if file.filename.lower().endswith(".jpg"):
|
||||
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
|
||||
file.save(filepath)
|
||||
plate = asyncio.run(process_image(filepath))
|
||||
return jsonify(
|
||||
{
|
||||
"message": "File uploaded successfully",
|
||||
"filename": file.filename,
|
||||
"status": True,
|
||||
"anpr": plate,
|
||||
}
|
||||
)
|
||||
|
||||
return jsonify({"error": "Only JPEG files allowed"}), 400
|
||||
i += 1
|
||||
return jsonify(
|
||||
{
|
||||
"message": "Image sent succesfully",
|
||||
"status": status,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def process_image(file: str):
|
||||
async def process_image(file: str) -> bool:
|
||||
anpr = ANPR()
|
||||
anpr_info = await anpr.detect(file)
|
||||
anpr_info: ... = await anpr.detect(file) # type: ignore
|
||||
number_plate = anpr_info["plate_number"]
|
||||
if number_plate:
|
||||
db.session.add(LoggedItem(plate=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 number_plate
|
||||
return "ERROR"
|
||||
return allowed
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user