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|
| 5 |
+
from sklearn.decomposition import PCA
|
| 6 |
+
import io
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
+
|
| 9 |
+
@st.cache_data(show_spinner=False)
|
| 10 |
+
def get_image_info(filter_name):
|
| 11 |
+
# --- FIX: Use API Key directly ---
|
| 12 |
+
<<<<<<< HEAD
|
| 13 |
+
API_KEY = "YOUR_API_KEY_HERE"
|
| 14 |
+
=======
|
| 15 |
+
API_KEY = "AIzaSyCaZqn1vSnAhqxs6OLCNYZjN53YBgdqQFs"
|
| 16 |
+
>>>>>>> d7baaaf (t)
|
| 17 |
+
if API_KEY == "YOUR_API_KEY_HERE":
|
| 18 |
+
st.warning("Please add your Gemini API key to the code to enable this feature.", icon="⚠️")
|
| 19 |
+
return "API key not provided. Please add it to the `get_image_info` function in the code."
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
genai.configure(api_key=API_KEY)
|
| 23 |
+
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 24 |
+
prompt = f"In 2-3 sentences, explain what the '{filter_name}' image processing technique does. Frame it for a user of a photo editing application."
|
| 25 |
+
response = model.generate_content(prompt)
|
| 26 |
+
return response.text
|
| 27 |
+
|
| 28 |
+
except Exception as e:
|
| 29 |
+
st.error(f"Could not connect to Gemini API. Please check your API key. Error: {e}", icon="🔑")
|
| 30 |
+
return "Information could not be retrieved. Please check your API key configuration."
|
| 31 |
+
|
| 32 |
+
def convert_image(img):
|
| 33 |
+
buf = io.BytesIO()
|
| 34 |
+
if img.mode == 'L':
|
| 35 |
+
img.save(buf, format="PNG")
|
| 36 |
+
else:
|
| 37 |
+
img.save(buf, format="PNG")
|
| 38 |
+
byte_im = buf.getvalue()
|
| 39 |
+
return byte_im
|
| 40 |
+
|
| 41 |
+
def process_image(image, operation, **kwargs):
|
| 42 |
+
img_array = np.array(image)
|
| 43 |
+
if len(img_array.shape) == 2: # Grayscale input
|
| 44 |
+
img_array = cv2.cvtColor(img_array, cv2.COLOR_GRAY2RGB)
|
| 45 |
+
elif img_array.shape[2] == 4: # RGBA input
|
| 46 |
+
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGBA2RGB)
|
| 47 |
+
|
| 48 |
+
# --- Image Enhancement ---
|
| 49 |
+
if operation == "Grayscale":
|
| 50 |
+
processed_img = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 51 |
+
elif operation == "Brightness":
|
| 52 |
+
value = kwargs.get('value', 30)
|
| 53 |
+
hsv = cv2.cvtColor(img_array, cv2.COLOR_RGB2HSV)
|
| 54 |
+
h, s, v = cv2.split(hsv)
|
| 55 |
+
v = cv2.add(v, value)
|
| 56 |
+
v[v > 255] = 255
|
| 57 |
+
v[v < 0] = 0
|
| 58 |
+
final_hsv = cv2.merge((h, s, v))
|
| 59 |
+
processed_img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2RGB)
|
| 60 |
+
elif operation == "Contrast":
|
| 61 |
+
alpha = kwargs.get('value', 1.5) # Contrast control
|
| 62 |
+
beta = 0
|
| 63 |
+
processed_img = cv2.convertScaleAbs(img_array, alpha=alpha, beta=beta)
|
| 64 |
+
elif operation == "Gaussian Blur (Low Pass)":
|
| 65 |
+
ksize = kwargs.get('ksize', (15, 15))
|
| 66 |
+
processed_img = cv2.GaussianBlur(img_array, ksize, 0)
|
| 67 |
+
elif operation == "High Pass Filter":
|
| 68 |
+
blurred = cv2.GaussianBlur(img_array, (21, 21), 0)
|
| 69 |
+
processed_img = cv2.addWeighted(img_array, 1.5, blurred, -0.5, 0)
|
| 70 |
+
elif operation == "Invert":
|
| 71 |
+
processed_img = cv2.bitwise_not(img_array)
|
| 72 |
+
|
| 73 |
+
# --- Image Restoration ---
|
| 74 |
+
elif operation == "Median Filter":
|
| 75 |
+
ksize = kwargs.get('ksize', 5)
|
| 76 |
+
processed_img = cv2.medianBlur(img_array, ksize)
|
| 77 |
+
elif operation == "Denoising":
|
| 78 |
+
processed_img = cv2.fastNlMeansDenoisingColored(img_array, None, 10, 10, 7, 21)
|
| 79 |
+
|
| 80 |
+
# --- Image Segmentation ---
|
| 81 |
+
elif operation == "Thresholding":
|
| 82 |
+
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 83 |
+
_, processed_img = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
|
| 84 |
+
elif operation == "Otsu's Binarization":
|
| 85 |
+
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 86 |
+
_, processed_img = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 87 |
+
|
| 88 |
+
# --- Image Compression ---
|
| 89 |
+
elif operation == "JPEG Compression":
|
| 90 |
+
quality = kwargs.get('quality', 90)
|
| 91 |
+
pil_img = Image.fromarray(img_array)
|
| 92 |
+
buf = io.BytesIO()
|
| 93 |
+
pil_img.save(buf, format="JPEG", quality=quality)
|
| 94 |
+
buf.seek(0)
|
| 95 |
+
processed_img_pil = Image.open(buf)
|
| 96 |
+
return processed_img_pil
|
| 97 |
+
|
| 98 |
+
# --- Image Synthesis ---
|
| 99 |
+
elif operation == "Generate Noise":
|
| 100 |
+
noise = np.random.randint(0, 255, img_array.shape, dtype=np.uint8)
|
| 101 |
+
processed_img = cv2.add(img_array, noise)
|
| 102 |
+
|
| 103 |
+
# --- Edge Detection ---
|
| 104 |
+
elif operation == "Canny Edge Detection":
|
| 105 |
+
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 106 |
+
processed_img = cv2.Canny(gray, 100, 200)
|
| 107 |
+
elif operation == "Sobel Edge Detection":
|
| 108 |
+
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 109 |
+
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)
|
| 110 |
+
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5)
|
| 111 |
+
processed_img = cv2.magnitude(sobelx, sobely)
|
| 112 |
+
|
| 113 |
+
# --- PCA ---
|
| 114 |
+
elif operation == "Principal Component Analysis (PCA)":
|
| 115 |
+
n_components = kwargs.get('n_components', 50)
|
| 116 |
+
blue, green, red = cv2.split(img_array)
|
| 117 |
+
pca_b = PCA(n_components=n_components)
|
| 118 |
+
pca_g = PCA(n_components=n_components)
|
| 119 |
+
pca_r = PCA(n_components=n_components)
|
| 120 |
+
trans_pca_b = pca_b.fit_transform(blue)
|
| 121 |
+
trans_pca_g = pca_g.fit_transform(green)
|
| 122 |
+
trans_pca_r = pca_r.fit_transform(red)
|
| 123 |
+
recon_pca_b = pca_b.inverse_transform(trans_pca_b)
|
| 124 |
+
recon_pca_g = pca_g.inverse_transform(trans_pca_g)
|
| 125 |
+
recon_pca_r = pca_r.inverse_transform(trans_pca_r)
|
| 126 |
+
|
| 127 |
+
# --- FIX: Clip values to the valid 0-255 range and convert type ---
|
| 128 |
+
recon_pca_b = np.clip(recon_pca_b, 0, 255)
|
| 129 |
+
recon_pca_g = np.clip(recon_pca_g, 0, 255)
|
| 130 |
+
recon_pca_r = np.clip(recon_pca_r, 0, 255)
|
| 131 |
+
processed_img = cv2.merge((
|
| 132 |
+
recon_pca_b.astype(np.uint8),
|
| 133 |
+
recon_pca_g.astype(np.uint8),
|
| 134 |
+
recon_pca_r.astype(np.uint8)
|
| 135 |
+
))
|
| 136 |
+
|
| 137 |
+
# --- Corner Detection ---
|
| 138 |
+
elif operation == "Harris Corner Detection":
|
| 139 |
+
processed_img = img_array.copy()
|
| 140 |
+
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 141 |
+
gray = np.float32(gray)
|
| 142 |
+
dst = cv2.cornerHarris(gray, 2, 3, 0.04)
|
| 143 |
+
corners = np.argwhere(dst > 0.01 * dst.max())
|
| 144 |
+
for corner in corners:
|
| 145 |
+
y, x = corner
|
| 146 |
+
cv2.circle(processed_img, (x, y), 3, (255, 0, 0), -1)
|
| 147 |
+
elif operation == "Shi-Tomasi Corner Detection":
|
| 148 |
+
processed_img = img_array.copy()
|
| 149 |
+
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 150 |
+
corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
|
| 151 |
+
corners = np.int0(corners)
|
| 152 |
+
for i in corners:
|
| 153 |
+
x, y = i.ravel()
|
| 154 |
+
cv2.circle(processed_img, (x, y), 3, (255, 0, 0), -1)
|
| 155 |
+
|
| 156 |
+
# --- Feature Extraction ---
|
| 157 |
+
elif operation == "SIFT (Scale-Invariant Feature Transform)":
|
| 158 |
+
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 159 |
+
sift = cv2.SIFT_create()
|
| 160 |
+
keypoints, _ = sift.detectAndCompute(gray, None)
|
| 161 |
+
processed_img = cv2.drawKeypoints(gray, keypoints, img_array.copy(), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
|
| 162 |
+
elif operation == "SURF (Speeded-Up Robust Features)":
|
| 163 |
+
try:
|
| 164 |
+
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 165 |
+
surf = cv2.xfeatures2d.SURF_create(400)
|
| 166 |
+
keypoints, _ = surf.detectAndCompute(gray, None)
|
| 167 |
+
processed_img = cv2.drawKeypoints(gray, keypoints, img_array.copy(), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
|
| 168 |
+
except (cv2.error, AttributeError):
|
| 169 |
+
st.error("SURF is not available in your OpenCV version. It's part of the patented algorithms in `opencv-contrib-python`. Please install a compatible version.")
|
| 170 |
+
return image
|
| 171 |
+
else:
|
| 172 |
+
return image
|
| 173 |
+
return Image.fromarray(processed_img)
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
st.set_page_config(page_title="Gemini Image Editor", layout="wide")
|
| 177 |
+
st.title("🖼️ Multi-Page Image Editor")
|
| 178 |
+
st.text("A comprehensive tool for image processing powered by Python and Streamlit.")
|
| 179 |
+
|
| 180 |
+
# --- Sidebar for Navigation and Controls ---
|
| 181 |
+
with st.sidebar:
|
| 182 |
+
st.header("Navigation")
|
| 183 |
+
page = st.radio(
|
| 184 |
+
"Go to",
|
| 185 |
+
[
|
| 186 |
+
"Image Enhancement", "Image Restoration", "Image Segmentation",
|
| 187 |
+
"Image Compression", "Image Synthesis", "Edge Detection",
|
| 188 |
+
"Principal Component Analysis", "Corner Detection", "Feature Extraction"
|
| 189 |
+
]
|
| 190 |
+
)
|
| 191 |
+
st.markdown("---")
|
| 192 |
+
st.header("Upload Image")
|
| 193 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 194 |
+
st.markdown("---")
|
| 195 |
+
st.info("This app uses the Gemini API. Please add your API key directly in the python script to enable the info feature.")
|
| 196 |
+
|
| 197 |
+
# --- Main Page Content ---
|
| 198 |
+
if uploaded_file is not None:
|
| 199 |
+
original_image = Image.open(uploaded_file).convert("RGB")
|
| 200 |
+
if page == "Image Enhancement":
|
| 201 |
+
st.header("✨ Image Enhancement")
|
| 202 |
+
operation = st.selectbox(
|
| 203 |
+
"Choose an enhancement technique",
|
| 204 |
+
["Brightness", "Contrast", "Grayscale", "Gaussian Blur (Low Pass)", "High Pass Filter", "Invert"]
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
kwargs = {}
|
| 208 |
+
if operation == "Brightness":
|
| 209 |
+
kwargs['value'] = st.slider("Brightness Level", -100, 100, 30)
|
| 210 |
+
elif operation == "Contrast":
|
| 211 |
+
kwargs['value'] = st.slider("Contrast Level", 1.0, 3.0, 1.5)
|
| 212 |
+
elif operation == "Gaussian Blur (Low Pass)":
|
| 213 |
+
k_size = st.slider("Kernel Size", 1, 31, 15, step=2)
|
| 214 |
+
kwargs['ksize'] = (k_size, k_size)
|
| 215 |
+
|
| 216 |
+
elif page == "Image Restoration":
|
| 217 |
+
st.header("🔧 Image Restoration")
|
| 218 |
+
operation = st.selectbox(
|
| 219 |
+
"Choose a restoration technique",
|
| 220 |
+
["Median Filter", "Denoising"]
|
| 221 |
+
)
|
| 222 |
+
kwargs = {}
|
| 223 |
+
if operation == "Median Filter":
|
| 224 |
+
k_size = st.slider("Kernel Size", 1, 15, 5, step=2)
|
| 225 |
+
kwargs['ksize'] = k_size
|
| 226 |
+
|
| 227 |
+
elif page == "Image Segmentation":
|
| 228 |
+
st.header("🎨 Image Segmentation")
|
| 229 |
+
operation = st.selectbox(
|
| 230 |
+
"Choose a segmentation technique",
|
| 231 |
+
["Thresholding", "Otsu's Binarization"]
|
| 232 |
+
)
|
| 233 |
+
kwargs = {}
|
| 234 |
+
|
| 235 |
+
elif page == "Image Compression":
|
| 236 |
+
st.header("🗜️ Image Compression")
|
| 237 |
+
operation = "JPEG Compression"
|
| 238 |
+
kwargs = {}
|
| 239 |
+
kwargs['quality'] = st.slider("JPEG Quality", 0, 100, 90)
|
| 240 |
+
|
| 241 |
+
elif page == "Image Synthesis":
|
| 242 |
+
st.header("⚗️ Image Synthesis")
|
| 243 |
+
operation = "Generate Noise"
|
| 244 |
+
kwargs = {}
|
| 245 |
+
|
| 246 |
+
elif page == "Edge Detection":
|
| 247 |
+
st.header("🔪 Edge Detection")
|
| 248 |
+
operation = st.selectbox(
|
| 249 |
+
"Choose an edge detection algorithm",
|
| 250 |
+
["Canny Edge Detection", "Sobel Edge Detection"]
|
| 251 |
+
)
|
| 252 |
+
kwargs = {}
|
| 253 |
+
|
| 254 |
+
elif page == "Principal Component Analysis":
|
| 255 |
+
st.header("📊 Principal Component Analysis (PCA)")
|
| 256 |
+
operation = "Principal Component Analysis (PCA)"
|
| 257 |
+
kwargs = {}
|
| 258 |
+
max_components = min(original_image.size[0], original_image.size[1], 300)
|
| 259 |
+
kwargs['n_components'] = st.slider("Number of Principal Components", 1, max_components, 50)
|
| 260 |
+
|
| 261 |
+
elif page == "Corner Detection":
|
| 262 |
+
st.header("📐 Corner Detection")
|
| 263 |
+
operation = st.selectbox(
|
| 264 |
+
"Choose a corner detection algorithm",
|
| 265 |
+
["Harris Corner Detection", "Shi-Tomasi Corner Detection"]
|
| 266 |
+
)
|
| 267 |
+
kwargs = {}
|
| 268 |
+
|
| 269 |
+
elif page == "Feature Extraction":
|
| 270 |
+
st.header("🌟 Feature Extraction")
|
| 271 |
+
st.warning("Note: SURF may require a specific version of `opencv-contrib-python`.")
|
| 272 |
+
operation = st.selectbox(
|
| 273 |
+
"Choose a feature extraction algorithm",
|
| 274 |
+
["SIFT (Scale-Invariant Feature Transform)", "SURF (Speeded-Up Robust Features)"]
|
| 275 |
+
)
|
| 276 |
+
kwargs = {}
|
| 277 |
+
|
| 278 |
+
else:
|
| 279 |
+
st.error("Page not found!")
|
| 280 |
+
operation = None
|
| 281 |
+
kwargs = {}
|
| 282 |
+
|
| 283 |
+
# --- Display Images and Info ---
|
| 284 |
+
if operation:
|
| 285 |
+
col1, col2 = st.columns(2)
|
| 286 |
+
with col1:
|
| 287 |
+
st.subheader("Original Image")
|
| 288 |
+
st.image(original_image, use_container_width=True)
|
| 289 |
+
|
| 290 |
+
with col2:
|
| 291 |
+
st.subheader("Processed Image")
|
| 292 |
+
with st.spinner("Applying filter..."):
|
| 293 |
+
processed_image = process_image(original_image, operation, **kwargs)
|
| 294 |
+
st.image(processed_image, use_container_width=True)
|
| 295 |
+
|
| 296 |
+
# --- Info Box ---
|
| 297 |
+
st.markdown("---")
|
| 298 |
+
st.subheader(f"ℹ️ About: {operation}")
|
| 299 |
+
with st.expander("Click to learn more", expanded=True):
|
| 300 |
+
with st.spinner("Asking Gemini for info..."):
|
| 301 |
+
info_text = get_image_info(operation)
|
| 302 |
+
st.info(info_text)
|
| 303 |
+
|
| 304 |
+
st.markdown("---")
|
| 305 |
+
st.download_button(
|
| 306 |
+
label="Download Processed Image",
|
| 307 |
+
data=convert_image(processed_image),
|
| 308 |
+
file_name=f"processed_{operation.lower().replace(' ', '_')}.png",
|
| 309 |
+
mime="image/png"
|
| 310 |
+
)
|
| 311 |
+
|
| 312 |
+
else:
|
| 313 |
+
st.info("Please upload an image using the sidebar to get started.")
|
yolo.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytesseract
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# =============================
|
| 9 |
+
# Functions
|
| 10 |
+
# =============================
|
| 11 |
+
|
| 12 |
+
def detect_license_plate_traditional(image_np):
|
| 13 |
+
img_rgb = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
|
| 14 |
+
gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
|
| 15 |
+
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 16 |
+
edges = cv2.Canny(blurred, 100, 200)
|
| 17 |
+
|
| 18 |
+
for contour in cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]:
|
| 19 |
+
approx = cv2.approxPolyDP(contour, 0.02 * cv2.arcLength(contour, True), True)
|
| 20 |
+
if len(approx) == 4:
|
| 21 |
+
x, y, w, h = cv2.boundingRect(approx)
|
| 22 |
+
aspect_ratio = w / float(h)
|
| 23 |
+
if 2 < aspect_ratio < 5 and w > 100 and h > 20:
|
| 24 |
+
return img_rgb, edges, img_rgb[y:y+h, x:x+w]
|
| 25 |
+
return img_rgb, edges, None
|
| 26 |
+
|
| 27 |
+
def detect_license_plate_yolov8(image_np):
|
| 28 |
+
model = YOLO('yolov8n.pt') # Small YOLOv8 model
|
| 29 |
+
results = model(image_np)
|
| 30 |
+
|
| 31 |
+
img_rgb = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
|
| 32 |
+
|
| 33 |
+
for r in results:
|
| 34 |
+
for box in r.boxes:
|
| 35 |
+
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy().astype(int)
|
| 36 |
+
return img_rgb, img_rgb[y1:y2, x1:x2]
|
| 37 |
+
return img_rgb, None
|
| 38 |
+
|
| 39 |
+
def extract_plate_number(license_plate_img):
|
| 40 |
+
gray_plate = cv2.cvtColor(license_plate_img, cv2.COLOR_RGB2GRAY)
|
| 41 |
+
text = pytesseract.image_to_string(gray_plate, config='--psm 8')
|
| 42 |
+
return text.strip()
|
| 43 |
+
|
| 44 |
+
# =============================
|
| 45 |
+
# Streamlit UI
|
| 46 |
+
# =============================
|
| 47 |
+
# --- Streamlit UI ---
|
| 48 |
+
st.title("🚗 License Plate Detection")
|
| 49 |
+
st.write("Upload an image and detect license plates using traditional or YOLOv8 method.")
|
| 50 |
+
|
| 51 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 52 |
+
|
| 53 |
+
if uploaded_file is not None:
|
| 54 |
+
# Convert uploaded file to OpenCV format
|
| 55 |
+
image = np.array(Image.open(uploaded_file).convert('RGB'))
|
| 56 |
+
st.image(image, caption='Uploaded Image', use_container_width=True)
|
| 57 |
+
|
| 58 |
+
method = st.radio("Select Detection Method:", ["Traditional", "YOLOv8"])
|
| 59 |
+
|
| 60 |
+
if st.button("Detect License Plate"):
|
| 61 |
+
if method == "Traditional":
|
| 62 |
+
img_rgb, edges, license_plate = detect_license_plate_traditional(image)
|
| 63 |
+
if license_plate is None:
|
| 64 |
+
st.warning("License Plate Not Found with Traditional Method! Trying YOLOv8...")
|
| 65 |
+
img_rgb, license_plate = detect_license_plate_yolov8(image)
|
| 66 |
+
edges = np.zeros_like(cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY))
|
| 67 |
+
else:
|
| 68 |
+
img_rgb, license_plate = detect_license_plate_yolov8(image)
|
| 69 |
+
edges = np.zeros_like(cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY))
|
| 70 |
+
|
| 71 |
+
plate_number = None
|
| 72 |
+
if license_plate is not None:
|
| 73 |
+
plate_number = extract_plate_number(license_plate)
|
| 74 |
+
st.success(f"Detected License Plate Number: {plate_number}")
|
| 75 |
+
else:
|
| 76 |
+
st.error("License Plate could not be detected!")
|
| 77 |
+
|
| 78 |
+
# Display results
|
| 79 |
+
st.image(img_rgb, caption="Original Image / YOLO Detection", use_container_width=True)
|
| 80 |
+
if method == "Traditional":
|
| 81 |
+
st.image(edges, caption="Edge Detection", use_container_width=True)
|
| 82 |
+
if license_plate is not None:
|
| 83 |
+
st.image(license_plate, caption="Cropped License Plate", use_container_width=True)
|
yolov8n.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f59b3d833e2ff32e194b5bb8e08d211dc7c5bdf144b90d2c8412c47ccfc83b36
|
| 3 |
+
size 6549796
|