esssyjr commited on
Commit
5ea748e
Β·
verified Β·
1 Parent(s): e7a154d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -64
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
- # Load models
12
- model = YOLO("plate_segment_best.pt") # Your custom YOLO model for plate detection
13
- ocr = PaddleOCR(use_angle_cls=True, lang='en') # English OCR
14
 
15
- # Detection + OCR pipeline
16
- def detect_and_recognize(image):
17
- results = model(image)[0]
18
- if len(results.boxes) == 0:
19
- return "No plate detected", None
20
 
21
- # Crop the first detected plate
22
- xyxy = results.boxes[0].xyxy[0].cpu().numpy().astype(int)
23
- plate_img = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
24
- plate_array = np.array(plate_img)
25
 
26
- # OCR
27
- result = ocr.ocr(plate_array, cls=True)
28
- if not result or not result[0]:
29
- return "Plate text not recognized", None
30
 
31
- text = ''.join([line[1][0] for line in result[0]])
32
- response = f"Detected Plate: {text}"
 
 
 
 
 
33
 
34
- # Check against police wanted list
35
- if text in wanted_plates:
36
- response += "\n🚨 ALERT: Plate is on the WANTED list!"
 
 
 
37
 
38
- # Check against FRSC info
39
- if text in frsc_data:
40
- driver_info = frsc_data[text]
41
- response += f"\nπŸ“‹ FRSC Info:\nOwner: {driver_info['owner']}\nVehicle: {driver_info['vehicle']}\nLicense Status: {driver_info['status']}"
42
-
43
- return response, plate_img
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  # Add to wanted list
46
- def add_to_wanted(plate):
47
- wanted_plates.add(plate)
48
- return f"{plate} added to the wanted list."
 
 
 
 
 
 
49
 
50
- # Add FRSC info
51
- def add_frsc_info(plate, owner, vehicle, status):
52
- frsc_data[plate] = {"owner": owner, "vehicle": vehicle, "status": status}
53
- return f"FRSC info added for {plate}."
 
 
 
 
54
 
55
- # Gradio interface
56
  with gr.Blocks() as demo:
57
- gr.Markdown("## πŸ‡³πŸ‡¬ Nigeria License Plate Detector (Police & FRSC)")
58
-
59
  with gr.Tab("πŸ” Detect Plate"):
60
- img_input = gr.Image(type="pil")
61
- result_output = gr.Textbox()
62
- cropped_output = gr.Image()
63
  detect_btn = gr.Button("Detect")
64
- detect_btn.click(detect_and_recognize, inputs=[img_input], outputs=[result_output, cropped_output])
65
-
66
- with gr.Tab("🚨 Add to Wanted List"):
67
- plate_input = gr.Textbox(label="Plate Number")
 
 
68
  add_btn = gr.Button("Add")
69
- wanted_output = gr.Textbox()
70
- add_btn.click(add_to_wanted, inputs=[plate_input], outputs=[wanted_output])
71
-
72
- with gr.Tab("πŸš— Add FRSC Info"):
73
- plate_frsc = gr.Textbox(label="Plate Number")
74
- owner_frsc = gr.Textbox(label="Owner Name")
75
- vehicle_frsc = gr.Textbox(label="Vehicle Description")
76
- status_frsc = gr.Textbox(label="License Status (valid/expired)")
77
- frsc_btn = gr.Button("Add Info")
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(share=True)
 
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()