Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,105 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from paddleocr import PaddleOCR
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import numpy as np
|
| 6 |
-
|
| 7 |
-
# Databases
|
| 8 |
-
wanted_plates = set()
|
| 9 |
-
frsc_data = {}
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
model = YOLO("plate_segment_best.pt") #
|
| 13 |
-
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
return "No plate detected", None
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
return "Plate text not recognized", None
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# Add to wanted list
|
| 46 |
-
def
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
# Add FRSC
|
| 51 |
-
def
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
# Gradio
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
-
gr.Markdown("## π³π¬
|
| 58 |
-
|
| 59 |
with gr.Tab("π Detect Plate"):
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
cropped_output = gr.Image()
|
| 63 |
detect_btn = gr.Button("Detect")
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
add_btn = gr.Button("Add")
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
frsc_output = gr.Textbox()
|
| 79 |
-
frsc_btn.click(add_frsc_info,
|
| 80 |
-
inputs=[plate_frsc, owner_frsc, vehicle_frsc, status_frsc],
|
| 81 |
-
outputs=[frsc_output])
|
| 82 |
|
| 83 |
-
demo.launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
from ultralytics import YOLO
|
| 7 |
from paddleocr import PaddleOCR
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Initialize model and OCR
|
| 10 |
+
model = YOLO("plate_segment_best.pt") # Replace with your actual model path
|
| 11 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
| 12 |
|
| 13 |
+
# Create data folder and files if not exist
|
| 14 |
+
os.makedirs("data", exist_ok=True)
|
| 15 |
+
wanted_file = "data/wanted_list.json"
|
| 16 |
+
frsc_file = "data/frsc_db.json"
|
|
|
|
| 17 |
|
| 18 |
+
# Initialize files
|
| 19 |
+
if not os.path.exists(wanted_file):
|
| 20 |
+
with open(wanted_file, "w") as f:
|
| 21 |
+
json.dump([], f)
|
| 22 |
|
| 23 |
+
if not os.path.exists(frsc_file):
|
| 24 |
+
with open(frsc_file, "w") as f:
|
| 25 |
+
json.dump({}, f)
|
|
|
|
| 26 |
|
| 27 |
+
# Load data
|
| 28 |
+
def load_data():
|
| 29 |
+
with open(wanted_file, "r") as f:
|
| 30 |
+
wanted = json.load(f)
|
| 31 |
+
with open(frsc_file, "r") as f:
|
| 32 |
+
frsc = json.load(f)
|
| 33 |
+
return wanted, frsc
|
| 34 |
|
| 35 |
+
# Detection and OCR
|
| 36 |
+
def detect_plate(image):
|
| 37 |
+
wanted_list, frsc_db = load_data()
|
| 38 |
+
img_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 39 |
+
results = model.predict(source=img_bgr, save=False, conf=0.25)
|
| 40 |
+
output = []
|
| 41 |
|
| 42 |
+
for r in results:
|
| 43 |
+
for box in r.boxes:
|
| 44 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 45 |
+
cropped = img_bgr[y1:y2, x1:x2]
|
| 46 |
+
text = ocr.ocr(cropped, cls=True)
|
| 47 |
+
if text and len(text[0]) > 0:
|
| 48 |
+
detected_plate = text[0][0][1][0].replace(" ", "").upper()
|
| 49 |
+
info = f"Detected Plate: {detected_plate}"
|
| 50 |
+
if detected_plate in wanted_list:
|
| 51 |
+
info += " π¨ This plate is on the WANTED list!"
|
| 52 |
+
elif detected_plate in frsc_db:
|
| 53 |
+
details = frsc_db[detected_plate]
|
| 54 |
+
info += f" β
Registered to {details['driver']} - {details['vehicle']}"
|
| 55 |
+
else:
|
| 56 |
+
info += " β Not found in any database."
|
| 57 |
+
output.append(info)
|
| 58 |
+
return "\n".join(output) if output else "No valid plates detected."
|
| 59 |
|
| 60 |
# Add to wanted list
|
| 61 |
+
def add_wanted(plate):
|
| 62 |
+
plate = plate.strip().upper()
|
| 63 |
+
wanted, _ = load_data()
|
| 64 |
+
if plate not in wanted:
|
| 65 |
+
wanted.append(plate)
|
| 66 |
+
with open(wanted_file, "w") as f:
|
| 67 |
+
json.dump(wanted, f)
|
| 68 |
+
return f"{plate} added to wanted list."
|
| 69 |
+
return f"{plate} already exists in wanted list."
|
| 70 |
|
| 71 |
+
# Add to FRSC database
|
| 72 |
+
def add_frsc(plate, driver, vehicle):
|
| 73 |
+
plate = plate.strip().upper()
|
| 74 |
+
_, frsc = load_data()
|
| 75 |
+
frsc[plate] = {"driver": driver, "vehicle": vehicle}
|
| 76 |
+
with open(frsc_file, "w") as f:
|
| 77 |
+
json.dump(frsc, f)
|
| 78 |
+
return f"{plate} info added to FRSC database."
|
| 79 |
|
| 80 |
+
# Gradio UI
|
| 81 |
with gr.Blocks() as demo:
|
| 82 |
+
gr.Markdown("## π³π¬ Nigerian Plate Number Recognition System")
|
| 83 |
+
|
| 84 |
with gr.Tab("π Detect Plate"):
|
| 85 |
+
with gr.Row():
|
| 86 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
|
|
|
| 87 |
detect_btn = gr.Button("Detect")
|
| 88 |
+
result_output = gr.Textbox(label="Result", lines=5)
|
| 89 |
+
detect_btn.click(detect_plate, inputs=image_input, outputs=result_output)
|
| 90 |
+
|
| 91 |
+
with gr.Tab("π Add to Wanted List"):
|
| 92 |
+
plate_input = gr.Textbox(label="Plate Number (e.g., GWA123AA)")
|
| 93 |
+
result_wanted = gr.Textbox(label="Status")
|
| 94 |
add_btn = gr.Button("Add")
|
| 95 |
+
add_btn.click(add_wanted, inputs=plate_input, outputs=result_wanted)
|
| 96 |
+
|
| 97 |
+
with gr.Tab("π Add FRSC Record"):
|
| 98 |
+
plate_input2 = gr.Textbox(label="Plate Number")
|
| 99 |
+
driver_input = gr.Textbox(label="Driver Name")
|
| 100 |
+
vehicle_input = gr.Textbox(label="Vehicle Details")
|
| 101 |
+
result_frsc = gr.Textbox(label="Status")
|
| 102 |
+
reg_btn = gr.Button("Add Record")
|
| 103 |
+
reg_btn.click(add_frsc, [plate_input2, driver_input, vehicle_input], result_frsc)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
demo.launch()
|