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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
|
| 7 |
+
# Ensure the temp directory exists
|
| 8 |
+
if not os.path.exists("temp"):
|
| 9 |
+
os.makedirs("temp")
|
| 10 |
+
|
| 11 |
+
# HTML/CSS for the header and container styling
|
| 12 |
+
header_html = """
|
| 13 |
+
<header style="background-color: #4CAF50; color: white; padding: 10px 0; text-align: center;">
|
| 14 |
+
<h1>Auto License Plate Detector</h1>
|
| 15 |
+
</header>
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
container_style = """
|
| 19 |
+
<style>
|
| 20 |
+
.container {
|
| 21 |
+
width: 80%;
|
| 22 |
+
margin: 20px auto;
|
| 23 |
+
background-color: white;
|
| 24 |
+
padding: 20px;
|
| 25 |
+
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
| 26 |
+
text-align: center;
|
| 27 |
+
}
|
| 28 |
+
.image-section {
|
| 29 |
+
margin-bottom: 20px;
|
| 30 |
+
}
|
| 31 |
+
.image-section img {
|
| 32 |
+
max-width: 100%;
|
| 33 |
+
height: auto;
|
| 34 |
+
display: white;
|
| 35 |
+
margin: 0 auto;
|
| 36 |
+
}
|
| 37 |
+
</style>
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
# Insert header and style into the Streamlit app
|
| 41 |
+
st.markdown(header_html, unsafe_allow_html=True)
|
| 42 |
+
st.markdown(container_style, unsafe_allow_html=True)
|
| 43 |
+
|
| 44 |
+
# Allow users to upload images or videos
|
| 45 |
+
uploaded_file = st.file_uploader("Upload an image or video",
|
| 46 |
+
type=["jpg", "jpeg", "png", "bmp", "mp4", "avi", "mov", "mkv"])
|
| 47 |
+
|
| 48 |
+
# Load YOLO model
|
| 49 |
+
try:
|
| 50 |
+
model = YOLO('best.pt') # Use the relative path to your trained YOLO model
|
| 51 |
+
except Exception as e:
|
| 52 |
+
st.error(f"Error loading YOLO model: {e}")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def predict_and_save_image(path_test_car, output_image_path):
|
| 56 |
+
"""
|
| 57 |
+
Predicts and saves the bounding boxes on the given test image using the trained YOLO model.
|
| 58 |
+
|
| 59 |
+
Parameters:
|
| 60 |
+
path_test_car (str): Path to the test image file.
|
| 61 |
+
output_image_path (str): Path to save the output image file.
|
| 62 |
+
|
| 63 |
+
Returns:
|
| 64 |
+
str: The path to the saved output image file.
|
| 65 |
+
"""
|
| 66 |
+
try:
|
| 67 |
+
results = model.predict(path_test_car, device='cpu')
|
| 68 |
+
image = cv2.imread(path_test_car)
|
| 69 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 70 |
+
for result in results:
|
| 71 |
+
for box in result.boxes:
|
| 72 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 73 |
+
confidence = box.conf[0]
|
| 74 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 75 |
+
cv2.putText(image, f'{confidence * 100:.2f}%', (x1, y1 - 10),
|
| 76 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
|
| 77 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 78 |
+
cv2.imwrite(output_image_path, image)
|
| 79 |
+
return output_image_path
|
| 80 |
+
except Exception as e:
|
| 81 |
+
st.error(f"Error processing image: {e}")
|
| 82 |
+
return None
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def predict_and_plot_video(video_path, output_path):
|
| 86 |
+
"""
|
| 87 |
+
Predicts and saves the bounding boxes on the given test video using the trained YOLO model.
|
| 88 |
+
|
| 89 |
+
Parameters:
|
| 90 |
+
video_path (str): Path to the test video file.
|
| 91 |
+
output_path (str): Path to save the output video file.
|
| 92 |
+
|
| 93 |
+
Returns:
|
| 94 |
+
str: The path to the saved output video file.
|
| 95 |
+
"""
|
| 96 |
+
try:
|
| 97 |
+
cap = cv2.VideoCapture(video_path)
|
| 98 |
+
if not cap.isOpened():
|
| 99 |
+
st.error(f"Error opening video file: {video_path}")
|
| 100 |
+
return None
|
| 101 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 102 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 103 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 104 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 105 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 106 |
+
while cap.isOpened():
|
| 107 |
+
ret, frame = cap.read()
|
| 108 |
+
if not ret:
|
| 109 |
+
break
|
| 110 |
+
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 111 |
+
results = model.predict(rgb_frame, device='cpu')
|
| 112 |
+
for result in results:
|
| 113 |
+
for box in result.boxes:
|
| 114 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 115 |
+
confidence = box.conf[0]
|
| 116 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 117 |
+
cv2.putText(frame, f'{confidence * 100:.2f}%', (x1, y1 - 10),
|
| 118 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
|
| 119 |
+
out.write(frame)
|
| 120 |
+
cap.release()
|
| 121 |
+
out.release()
|
| 122 |
+
return output_path
|
| 123 |
+
except Exception as e:
|
| 124 |
+
st.error(f"Error processing video: {e}")
|
| 125 |
+
return None
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def process_media(input_path, output_path):
|
| 129 |
+
"""
|
| 130 |
+
Processes the uploaded media file (image or video) and returns the path to the saved output file.
|
| 131 |
+
|
| 132 |
+
Parameters:
|
| 133 |
+
input_path (str): Path to the input media file.
|
| 134 |
+
output_path (str): Path to save the output media file.
|
| 135 |
+
|
| 136 |
+
Returns:
|
| 137 |
+
str: The path to the saved output media file.
|
| 138 |
+
"""
|
| 139 |
+
file_extension = os.path.splitext(input_path)[1].lower()
|
| 140 |
+
if file_extension in ['.mp4', '.avi', '.mov', '.mkv']:
|
| 141 |
+
return predict_and_plot_video(input_path, output_path)
|
| 142 |
+
elif file_extension in ['.jpg', '.jpeg', '.png', '.bmp']:
|
| 143 |
+
return predict_and_save_image(input_path, output_path)
|
| 144 |
+
else:
|
| 145 |
+
st.error(f"Unsupported file type: {file_extension}")
|
| 146 |
+
return None
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
if uploaded_file is not None:
|
| 150 |
+
input_path = os.path.join("temp", uploaded_file.name)
|
| 151 |
+
output_path = os.path.join("temp", f"output_{uploaded_file.name}")
|
| 152 |
+
try:
|
| 153 |
+
with open(input_path, "wb") as f:
|
| 154 |
+
f.write(uploaded_file.getbuffer())
|
| 155 |
+
st.write("Processing...")
|
| 156 |
+
result_path = process_media(input_path, output_path)
|
| 157 |
+
if result_path:
|
| 158 |
+
if input_path.endswith(('.mp4', '.avi', '.mov', '.mkv')):
|
| 159 |
+
st.video(result_path)
|
| 160 |
+
else:
|
| 161 |
+
st.image(result_path)
|
| 162 |
+
except Exception as e:
|
| 163 |
+
st.error(f"Error uploading or processing file: {e}")
|