Update app.py
Browse files
app.py
CHANGED
|
@@ -1,128 +1,114 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
-
from
|
| 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 |
-
st.warning("No license plate detected!")
|
| 38 |
-
return
|
| 39 |
-
for i, box in enumerate(boxes):
|
| 40 |
-
x1, y1, x2, y2 = box
|
| 41 |
-
cropped_image = image[y1:y2, x1:x2]
|
| 42 |
-
cropped_image_rgb = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)
|
| 43 |
-
st.image(cropped_image_rgb, caption=f"Cropped License Plate {i+1}", use_container_width=True)
|
| 44 |
-
|
| 45 |
-
# Perform OCR on the cropped image
|
| 46 |
-
text_results = ocr_reader.readtext(cropped_image_rgb, detail=0)
|
| 47 |
-
detected_text = " ".join(text_results)
|
| 48 |
-
st.write(f"**Extracted Text (Plate {i+1}):** {detected_text}")
|
| 49 |
-
st.write(f"**Confidence Score:** {result.boxes.conf.cpu().numpy()[i]:.2f}")
|
| 50 |
-
|
| 51 |
-
# Function to process video and detect license plates
|
| 52 |
-
def process_video(video_path, confidence_threshold=0.5, output_path="output_video.mp4"):
|
| 53 |
-
# Open the video file
|
| 54 |
-
cap = cv2.VideoCapture(video_path)
|
| 55 |
-
|
| 56 |
-
# Get video frame dimensions
|
| 57 |
-
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 58 |
-
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 59 |
-
|
| 60 |
-
# Create VideoWriter object to save the output video
|
| 61 |
-
fourcc = cv2.VideoWriter_fourcc(*"mp4v") # Codec for mp4
|
| 62 |
-
out = cv2.VideoWriter(output_path, fourcc, 20.0, (frame_width, frame_height)) # 20 FPS
|
| 63 |
-
|
| 64 |
-
if not cap.isOpened():
|
| 65 |
-
st.error("Error opening video stream or file")
|
| 66 |
-
return
|
| 67 |
-
|
| 68 |
-
while cap.isOpened():
|
| 69 |
-
ret, frame = cap.read()
|
| 70 |
-
if not ret:
|
| 71 |
-
break
|
| 72 |
-
|
| 73 |
-
results = yolo_model(frame, conf=confidence_threshold)
|
| 74 |
-
annotated_frame = cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
|
| 75 |
-
|
| 76 |
-
# Loop through detections and perform OCR
|
| 77 |
-
for result in results:
|
| 78 |
-
boxes = result.boxes.xyxy.cpu().numpy().astype(int)
|
| 79 |
-
for i, box in enumerate(boxes):
|
| 80 |
-
x1, y1, x2, y2 = box
|
| 81 |
-
cropped_plate = frame[y1:y2, x1:x2]
|
| 82 |
-
cropped_rgb = cv2.cvtColor(cropped_plate, cv2.COLOR_BGR2RGB)
|
| 83 |
-
|
| 84 |
-
# Perform OCR on the cropped image
|
| 85 |
-
text_results = ocr_reader.readtext(cropped_rgb, detail=0)
|
| 86 |
-
detected_text = " ".join(text_results)
|
| 87 |
-
|
| 88 |
-
# Optionally add detected text on the annotated frame
|
| 89 |
-
cv2.putText(annotated_frame, detected_text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 90 |
-
|
| 91 |
-
# Write the annotated frame to the output video
|
| 92 |
-
out.write(cv2.cvtColor(annotated_frame, cv2.COLOR_RGB2BGR))
|
| 93 |
-
|
| 94 |
-
cap.release()
|
| 95 |
-
out.release()
|
| 96 |
-
|
| 97 |
-
st.success(f"Video processing complete. Output video saved to {output_path}")
|
| 98 |
-
|
| 99 |
-
# Provide a download link for the processed video
|
| 100 |
-
with open(output_path, "rb") as f:
|
| 101 |
-
st.download_button(label="Download Processed Video", data=f, file_name=output_path)
|
| 102 |
-
|
| 103 |
-
# Sidebar input for file upload
|
| 104 |
-
uploaded_file = st.file_uploader("Upload an Image or Video", type=["mp4", "avi", "mov", "jpg", "jpeg", "png"])
|
| 105 |
|
|
|
|
| 106 |
if uploaded_file is not None:
|
| 107 |
-
#
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pickle
|
| 3 |
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Load the trained model and other necessary files
|
| 8 |
+
model = pickle.load(open('artifacts/license_plate_model.pkl', 'rb'))
|
| 9 |
+
|
| 10 |
+
# Function to load custom CSS
|
| 11 |
+
def local_css(file_name):
|
| 12 |
+
with open(file_name) as f:
|
| 13 |
+
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
|
| 14 |
+
|
| 15 |
+
# Apply custom CSS for styling
|
| 16 |
+
local_css("style.css")
|
| 17 |
+
|
| 18 |
+
# Page configuration
|
| 19 |
+
st.set_page_config(page_title='License Plate Detection', layout='centered')
|
| 20 |
+
|
| 21 |
+
# Add a fancy header with emojis
|
| 22 |
+
st.markdown("""
|
| 23 |
+
<div class="glass">
|
| 24 |
+
<h1>🚗 License Plate Detection 🚗</h1>
|
| 25 |
+
<p>Upload an image to detect license plates</p>
|
| 26 |
+
</div>
|
| 27 |
+
""", unsafe_allow_html=True)
|
| 28 |
+
|
| 29 |
+
# File uploader for image input
|
| 30 |
+
st.markdown("""
|
| 31 |
+
<div class="glass">
|
| 32 |
+
<p>📸 Upload a car image for license plate detection</p>
|
| 33 |
+
</div>
|
| 34 |
+
""", unsafe_allow_html=True)
|
| 35 |
+
|
| 36 |
+
uploaded_file = st.file_uploader("Choose a file", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# If image is uploaded, process the image and make predictions
|
| 39 |
if uploaded_file is not None:
|
| 40 |
+
# Load the image using PIL
|
| 41 |
+
image = Image.open(uploaded_file)
|
| 42 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 43 |
+
|
| 44 |
+
# Convert image to OpenCV format (for model processing)
|
| 45 |
+
img_array = np.array(image)
|
| 46 |
+
img_gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 47 |
+
|
| 48 |
+
# Use the model to detect license plate (model predictions are made here)
|
| 49 |
+
# Assuming your model is a classifier or detector for the license plate
|
| 50 |
+
plate_detected = model.predict(img_gray) # Modify this depending on your model
|
| 51 |
+
|
| 52 |
+
if plate_detected:
|
| 53 |
+
st.markdown("""
|
| 54 |
+
<div class="glass">
|
| 55 |
+
<p>✔️ License Plate Detected!</p>
|
| 56 |
+
</div>
|
| 57 |
+
""", unsafe_allow_html=True)
|
| 58 |
+
else:
|
| 59 |
+
st.markdown("""
|
| 60 |
+
<div class="glass">
|
| 61 |
+
<p>❌ No License Plate Detected!</p>
|
| 62 |
+
</div>
|
| 63 |
+
""", unsafe_allow_html=True)
|
| 64 |
+
|
| 65 |
+
# Apply custom button styling to Streamlit's default button using CSS
|
| 66 |
+
st.markdown("""
|
| 67 |
+
<style>
|
| 68 |
+
.stButton > button {
|
| 69 |
+
background: linear-gradient(135deg, #8B5E3C, #B8860B); /* Warm vintage gold-brown gradient */
|
| 70 |
+
border: none;
|
| 71 |
+
color: white; /* White text */
|
| 72 |
+
padding: 12px 24px;
|
| 73 |
+
text-align: center;
|
| 74 |
+
text-decoration: none;
|
| 75 |
+
display: inline-block;
|
| 76 |
+
font-size: 16px;
|
| 77 |
+
margin: 10px 5px;
|
| 78 |
+
cursor: pointer;
|
| 79 |
+
border-radius: 12px; /* Rounded corners for a soft vintage touch */
|
| 80 |
+
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.25); /* Soft, deep shadow for antique feel */
|
| 81 |
+
font-family: 'Georgia', serif; /* Classic serif font for the button */
|
| 82 |
+
text-transform: uppercase; /* Uppercase text for a formal touch */
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
.stButton > button:hover {
|
| 86 |
+
background: linear-gradient(135deg, #704214, #B8860B); /* Slightly darker gold-brown */
|
| 87 |
+
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3); /* Stronger shadow for depth on hover */
|
| 88 |
+
transform: translateY(-2px); /* Lift effect */
|
| 89 |
+
}
|
| 90 |
+
</style>
|
| 91 |
+
""", unsafe_allow_html=True)
|
| 92 |
+
|
| 93 |
+
# Optionally, add more interactivity or styling to the button
|
| 94 |
+
if st.button('Detect License Plate'):
|
| 95 |
+
# Trigger the license plate detection when the button is pressed
|
| 96 |
+
pass # Include any additional logic here, if necessary
|
| 97 |
+
|
| 98 |
+
# Glassmorphism styling for text elements
|
| 99 |
+
st.markdown("""
|
| 100 |
+
<style>
|
| 101 |
+
h1, h2, h3, h4 {
|
| 102 |
+
font-family: 'Georgia', serif;
|
| 103 |
+
color: #ffffff;
|
| 104 |
+
text-align: center;
|
| 105 |
+
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
p {
|
| 109 |
+
font-family: 'Georgia', serif;
|
| 110 |
+
color: #ffffff;
|
| 111 |
+
text-align: center;
|
| 112 |
+
}
|
| 113 |
+
</style>
|
| 114 |
+
""", unsafe_allow_html=True)
|