Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,21 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
import easyocr
|
| 5 |
from ultralytics import YOLO
|
| 6 |
-
from PIL import Image
|
| 7 |
|
| 8 |
# Title of the app
|
| 9 |
st.title("License Plate Recognition System🚗")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Load the YOLO model for license plate detection
|
| 12 |
@st.cache_resource
|
| 13 |
def load_yolo_model():
|
|
@@ -30,69 +40,47 @@ def process_image(image, confidence_threshold=0.5):
|
|
| 30 |
results = yolo_model(image, conf=confidence_threshold)
|
| 31 |
annotated_image = cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
cropped_images = []
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
# Loop through detections and perform OCR
|
| 38 |
for result in results:
|
| 39 |
boxes = result.boxes.xyxy.cpu().numpy().astype(int)
|
| 40 |
-
confidences = result.boxes.conf.cpu().numpy()
|
| 41 |
-
|
| 42 |
if len(boxes) == 0:
|
| 43 |
st.warning("No license plate detected!")
|
| 44 |
-
return
|
| 45 |
|
| 46 |
-
for i,
|
| 47 |
x1, y1, x2, y2 = box
|
| 48 |
-
# Draw bounding box on the annotated image
|
| 49 |
-
cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
| 50 |
-
cv2.putText(annotated_image, f"{conf:.2f}", (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 51 |
-
|
| 52 |
-
# Crop the license plate from the image
|
| 53 |
cropped_image = image[y1:y2, x1:x2]
|
| 54 |
cropped_image_rgb = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Perform OCR on the cropped image
|
| 58 |
text_results = ocr_reader.readtext(cropped_image_rgb, detail=0)
|
| 59 |
detected_text = " ".join(text_results)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
# Sidebar input for file upload
|
| 65 |
uploaded_file = st.file_uploader("Upload an Image or Video", type=["mp4", "avi", "mov", "jpg", "jpeg", "png"])
|
| 66 |
|
| 67 |
if uploaded_file is not None:
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
confidence_threshold = st.sidebar.slider("Confidence Threshold", 0.0, 1.0, 0.5, 0.01)
|
| 71 |
-
|
| 72 |
-
# Create three columns to display the images side by side
|
| 73 |
-
c1, c2, c3 = st.columns(3)
|
| 74 |
-
|
| 75 |
-
with c1:
|
| 76 |
-
# Display uploaded image with bounding boxes
|
| 77 |
-
license_plate_text, cropped_images, annotated_image = process_image(image, confidence_threshold)
|
| 78 |
-
if annotated_image is not None:
|
| 79 |
-
st.image(annotated_image, caption='Uploaded Image with Bounding Boxes', use_container_width=True)
|
| 80 |
-
|
| 81 |
-
with c2:
|
| 82 |
-
# Display cropped license plates
|
| 83 |
-
if cropped_images:
|
| 84 |
-
for i, cropped_image in enumerate(cropped_images):
|
| 85 |
-
st.image(cropped_image, caption=f'Cropped License Plate {i+1}', use_container_width=True)
|
| 86 |
-
else:
|
| 87 |
-
st.write('No License Plate Detected')
|
| 88 |
|
| 89 |
-
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
else:
|
| 95 |
-
st.write('No text detected')
|
| 96 |
|
| 97 |
st.markdown("---")
|
| 98 |
st.info("This application uses Fine Tuned YOLOv8 for detection and EasyOCR for text recognition.")
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import easyocr
|
| 5 |
from ultralytics import YOLO
|
|
|
|
| 6 |
|
| 7 |
# Title of the app
|
| 8 |
st.title("License Plate Recognition System🚗")
|
| 9 |
|
| 10 |
+
# Add background image using custom CSS
|
| 11 |
+
st.markdown("""
|
| 12 |
+
<style>
|
| 13 |
+
.stApp {
|
| 14 |
+
background-image: url("https://i.postimg.cc/zBX9cDZy/bmw-german-luxury-cars-brands-black-shiny-fast-car-in-a-misty-fog-01-11-2024-1730444676-hd-wallpaper.jpg");
|
| 15 |
+
background-size: cover;
|
| 16 |
+
background-position: center center;
|
| 17 |
+
}
|
| 18 |
+
</style>
|
| 19 |
+
""", unsafe_allow_html=True)
|
| 20 |
+
|
| 21 |
# Load the YOLO model for license plate detection
|
| 22 |
@st.cache_resource
|
| 23 |
def load_yolo_model():
|
|
|
|
| 40 |
results = yolo_model(image, conf=confidence_threshold)
|
| 41 |
annotated_image = cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
|
| 42 |
|
| 43 |
+
# Create columns to display the uploaded image, cropped image, and extracted text side by side
|
| 44 |
+
c1, c2 = st.columns(2)
|
|
|
|
| 45 |
|
| 46 |
+
with c1:
|
| 47 |
+
st.image(annotated_image, caption="Detected License Plate(s)", use_container_width=True)
|
| 48 |
+
|
| 49 |
# Loop through detections and perform OCR
|
| 50 |
for result in results:
|
| 51 |
boxes = result.boxes.xyxy.cpu().numpy().astype(int)
|
|
|
|
|
|
|
| 52 |
if len(boxes) == 0:
|
| 53 |
st.warning("No license plate detected!")
|
| 54 |
+
return
|
| 55 |
|
| 56 |
+
for i, box in enumerate(boxes):
|
| 57 |
x1, y1, x2, y2 = box
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
cropped_image = image[y1:y2, x1:x2]
|
| 59 |
cropped_image_rgb = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)
|
| 60 |
+
|
| 61 |
+
with c2:
|
| 62 |
+
st.image(cropped_image_rgb, caption=f"Cropped License Plate {i+1}", use_container_width=True)
|
| 63 |
|
| 64 |
# Perform OCR on the cropped image
|
| 65 |
text_results = ocr_reader.readtext(cropped_image_rgb, detail=0)
|
| 66 |
detected_text = " ".join(text_results)
|
| 67 |
+
|
| 68 |
+
with c2:
|
| 69 |
+
st.write(f"**Extracted Text (Plate {i+1}):** {detected_text}")
|
| 70 |
+
st.write(f"**Confidence Score:** {result.boxes.conf.cpu().numpy()[i]:.2f}")
|
| 71 |
|
| 72 |
# Sidebar input for file upload
|
| 73 |
uploaded_file = st.file_uploader("Upload an Image or Video", type=["mp4", "avi", "mov", "jpg", "jpeg", "png"])
|
| 74 |
|
| 75 |
if uploaded_file is not None:
|
| 76 |
+
# Check if it's an image or video
|
| 77 |
+
file_type = uploaded_file.type
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
if file_type.startswith("image"):
|
| 80 |
+
# Read and process the image
|
| 81 |
+
image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
|
| 82 |
+
confidence_threshold = st.sidebar.slider("Confidence Threshold", 0.0, 1.0, 0.5, 0.01)
|
| 83 |
+
process_image(image, confidence_threshold)
|
|
|
|
|
|
|
| 84 |
|
| 85 |
st.markdown("---")
|
| 86 |
st.info("This application uses Fine Tuned YOLOv8 for detection and EasyOCR for text recognition.")
|