File size: 4,171 Bytes
90fed93 5d041c5 4c3c504 aec3e22 edeaf63 4c267d5 edeaf63 aec3e22 edeaf63 aec3e22 4c3c504 2f69058 2e60442 edeaf63 2e60442 edeaf63 2e60442 4c3c504 edeaf63 2e60442 edeaf63 4c3c504 edeaf63 129b2b3 4c3c504 edeaf63 4c3c504 2e60442 3b49c35 77e4161 edeaf63 129b2b3 edeaf63 58839a1 4c3c504 9b1dc68 d1053f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | import streamlit as st
import cv2
import numpy as np
import easyocr
from ultralytics import YOLO
# Title of the app with enhanced styling
st.markdown("""
<style>
.stApp {
background-image: url("https://i.postimg.cc/cLpB502d/black-car.png");
background-size: cover;
background-position: center center;
}
h1 {
color: white;
font-size: 3em;
text-shadow: 3px 3px 5px rgba(0, 0, 0, 0.7);
text-align: center;
}
.sidebar .sidebar-content {
background-color: rgba(0, 0, 0, 0.6);
color: white;
}
.stButton>button {
background-color: #4CAF50;
color: white;
font-size: 18px;
padding: 10px 24px;
border-radius: 10px;
border: none;
cursor: pointer;
}
.stButton>button:hover {
background-color: #45a049;
}
.stTextInput>div>div>input {
background-color: #ffffff;
color: black;
}
.stTextInput>div>label {
color: white;
}
.stSlider>div>label {
color: white;
}
.stMarkdown {
color: white;
}
.stInfo {
background-color: rgba(0, 0, 0, 0.6);
color: white;
}
</style>
""", unsafe_allow_html=True)
# Title of the app
st.title("License Plate Recognition System")
# Load the YOLO model for license plate detection
@st.cache_resource
def load_yolo_model():
model_path = "best.pt" # Replace with your model file
model = YOLO(model_path)
return model
# Load EasyOCR reader
@st.cache_resource
def load_easyocr_reader():
return easyocr.Reader(['en'], gpu=False)
# Initialize models
yolo_model = load_yolo_model()
ocr_reader = load_easyocr_reader()
# Function to process image and detect license plates
def process_image(image, confidence_threshold=0.5):
# Perform license plate detection
results = yolo_model(image, conf=confidence_threshold)
annotated_image = cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
# Create columns to display the uploaded image, cropped image, and extracted text side by side
c1, c2 = st.columns(2)
with c1:
st.image(annotated_image, caption="Detected License Plate(s)", use_container_width=True)
# Loop through detections and perform OCR
for result in results:
boxes = result.boxes.xyxy.cpu().numpy().astype(int)
if len(boxes) == 0:
st.warning("No license plate detected!")
return
for i, box in enumerate(boxes):
x1, y1, x2, y2 = box
cropped_image = image[y1:y2, x1:x2]
cropped_image_rgb = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)
with c2:
st.image(cropped_image_rgb, caption=f"Cropped License Plate {i+1}", use_container_width=True)
# Perform OCR on the cropped image
text_results = ocr_reader.readtext(cropped_image_rgb, detail=0)
detected_text = " ".join(text_results)
with c2:
st.write(f"**Extracted Text (Plate {i+1}):** {detected_text}")
st.write(f"**Confidence Score:** {result.boxes.conf.cpu().numpy()[i]:.2f}")
# Sidebar input for file upload
uploaded_file = st.file_uploader("Upload an Image or Video", type=["mp4", "avi", "mov", "jpg", "jpeg", "png"])
if uploaded_file is not None:
# Check if it's an image or video
file_type = uploaded_file.type
if file_type.startswith("image"):
# Read and process the image
image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
confidence_threshold = st.sidebar.slider("Confidence Threshold", 0.0, 1.0, 0.5, 0.01)
process_image(image, confidence_threshold)
st.markdown("---")
st.info("Please use images as input, because processing videos with the CPU may result in longer times.")
st.info("This application uses Fine Tuned YOLOv8 for detection and EasyOCR for text recognition.") |