|
|
import streamlit as st |
|
|
import cv2 |
|
|
import numpy as np |
|
|
import easyocr |
|
|
from PIL import Image |
|
|
from tensorflow.keras.models import load_model |
|
|
from tensorflow.keras.preprocessing import image as keras_image |
|
|
|
|
|
|
|
|
model = load_model("Vehicle Number Plates.keras") |
|
|
plate_detector = cv2.CascadeClassifier("haarcascade_russian_plate_number.xml") |
|
|
reader = easyocr.Reader(['en']) |
|
|
|
|
|
|
|
|
def detect_and_predict(img_input): |
|
|
img = np.array(img_input.convert("RGB")) |
|
|
frame = img.copy() |
|
|
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) |
|
|
plates = plate_detector.detectMultiScale(gray, 1.1, 4) |
|
|
|
|
|
plate_text = "Not Detected" |
|
|
confidence = None |
|
|
|
|
|
for x, y, w, h in plates: |
|
|
roi = frame[y:y+h, x:x+w] |
|
|
try: |
|
|
test_img = cv2.resize(roi, (200, 200)) |
|
|
test_img = keras_image.img_to_array(test_img) / 255.0 |
|
|
test_img = np.expand_dims(test_img, axis=0) |
|
|
pred = model.predict(test_img)[0][0] |
|
|
except Exception as e: |
|
|
print(f"Prediction error: {e}") |
|
|
continue |
|
|
|
|
|
if pred < 0.5: |
|
|
result = reader.readtext(roi) |
|
|
if result: |
|
|
plate_text = result[0][1] |
|
|
confidence = result[0][2] |
|
|
label = f"Plate: {plate_text}" |
|
|
else: |
|
|
label = "Plate Detected (No text)" |
|
|
else: |
|
|
label = "Plate Not Detected" |
|
|
|
|
|
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) |
|
|
cv2.putText(frame, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) |
|
|
|
|
|
return frame, confidence, plate_text |
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Vehicle Plate Identifier", layout="centered") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.markdown("<h3 style='text-align: center; color: navy;'>π AI-Powered Vehicle Plate Detection & OCR</h3>", unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
tab1, tab2 = st.tabs([ |
|
|
"πΌοΈ **:blue[Upload Vehicle Image]**", |
|
|
"π· **:green[Use Live Webcam]**" |
|
|
]) |
|
|
|
|
|
|
|
|
with tab1: |
|
|
st.markdown("#### :blue[Upload an image to detect number plate]", unsafe_allow_html=True) |
|
|
uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"]) |
|
|
if uploaded_file: |
|
|
image_input = Image.open(uploaded_file) |
|
|
st.image(image_input, caption="Uploaded Image", width=250) |
|
|
|
|
|
if st.button("π Detect from Upload"): |
|
|
with st.spinner("Processing..."): |
|
|
result_img, confidence, label = detect_and_predict(image_input) |
|
|
|
|
|
st.image(result_img, caption="Detection Result", channels="RGB", width=250) |
|
|
if confidence: |
|
|
st.metric("Confidence", f"{confidence * 100:.2f}%") |
|
|
st.success(f"Detected Text: {label}") |
|
|
else: |
|
|
st.warning("No plate text detected.") |
|
|
|
|
|
|
|
|
with tab2: |
|
|
st.markdown("#### :green[Capture from your webcam to scan a vehicle plate]", unsafe_allow_html=True) |
|
|
camera_image = st.camera_input("π· Take a picture using your webcam") |
|
|
if camera_image: |
|
|
try: |
|
|
image_input = Image.open(camera_image) |
|
|
st.image(image_input, caption="Webcam Snapshot", width=250) |
|
|
|
|
|
with st.spinner("Analyzing..."): |
|
|
result_img, confidence, label = detect_and_predict(image_input) |
|
|
|
|
|
st.image(result_img, caption="Detection Result", channels="RGB", width=250) |
|
|
if confidence is not None: |
|
|
st.metric("Confidence", f"{confidence*100:.2f}%") |
|
|
st.success(f"Detected Text: {label}") |
|
|
else: |
|
|
st.warning("Plate detected but no readable text found.") |
|
|
except Exception as e: |
|
|
st.error(f"β Error: {str(e)}") |
|
|
|