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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
import tempfile
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from ultralytics import YOLO
|
| 8 |
+
import easyocr
|
| 9 |
+
import re
|
| 10 |
+
|
| 11 |
+
# ----------- Page config ----------------
|
| 12 |
+
st.set_page_config(
|
| 13 |
+
page_title='Number plate',
|
| 14 |
+
page_icon='',
|
| 15 |
+
layout='wide'
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
st.title("Number plate")
|
| 21 |
+
st.write('Upload an image or video to detect helmet')
|
| 22 |
+
|
| 23 |
+
# -------------- load model detection -----------
|
| 24 |
+
@st.cache_resource
|
| 25 |
+
def load_model():
|
| 26 |
+
return YOLO('best.pt')
|
| 27 |
+
|
| 28 |
+
@st.cache_resource
|
| 29 |
+
def load_ocr():
|
| 30 |
+
return easyocr.Reader(['en'])
|
| 31 |
+
|
| 32 |
+
model = load_model()
|
| 33 |
+
ocr_model = load_ocr()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# switching tabs
|
| 38 |
+
x,y = st.tabs(['Image Detection','Video Detection'])
|
| 39 |
+
|
| 40 |
+
# Tabs 1 == Image detection
|
| 41 |
+
with x:
|
| 42 |
+
st.header('Image Detection')
|
| 43 |
+
|
| 44 |
+
img_path = st.file_uploader('Please upload an image') # upload option
|
| 45 |
+
|
| 46 |
+
if img_path is not None:
|
| 47 |
+
image = Image.open(img_path)
|
| 48 |
+
image_np = np.array(image) # converting in to atrray
|
| 49 |
+
|
| 50 |
+
# YOLO inference
|
| 51 |
+
result = model(image_np,conf=0.4)
|
| 52 |
+
annot_img = result[0].plot()
|
| 53 |
+
|
| 54 |
+
# Convert BGR to RGB
|
| 55 |
+
annot_img = cv2.cvtColor(annot_img, cv2.COLOR_BGR2RGB)
|
| 56 |
+
|
| 57 |
+
# --------------------- number detection ----------------------
|
| 58 |
+
detect_number = []
|
| 59 |
+
|
| 60 |
+
for box in result[0].boxes:
|
| 61 |
+
x1,y1,x2,y2 = map(int,box.xyxy[0])
|
| 62 |
+
|
| 63 |
+
plate_crop = image_np[y1:y2,x1:x2]
|
| 64 |
+
gray = cv2.cvtColor(plate_crop,cv2.COLOR_BGR2GRAY)
|
| 65 |
+
|
| 66 |
+
ocr_result = ocr_model.readtext(gray)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
text = ""
|
| 70 |
+
for t in ocr_result:
|
| 71 |
+
text += t[1] + " "
|
| 72 |
+
|
| 73 |
+
plate_text = re.sub(r'[^A-Z0-9]', '', text.upper())
|
| 74 |
+
|
| 75 |
+
if plate_text:
|
| 76 |
+
detect_number.append(plate_text)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
#st.subheader("Detected Result")
|
| 81 |
+
#st.image(annot_img, use_container_width=True)
|
| 82 |
+
#st.image(annot_img,width=400) # first image
|
| 83 |
+
#st.image(image,width=400) # last image
|
| 84 |
+
|
| 85 |
+
# to display side by side
|
| 86 |
+
ori_img,pre_img = st.columns(2)
|
| 87 |
+
with ori_img: # original image
|
| 88 |
+
st.markdown('#### ***Original Image***')
|
| 89 |
+
st.image(image,width=500)
|
| 90 |
+
|
| 91 |
+
with pre_img:
|
| 92 |
+
st.markdown('#### ***Detected Image***')
|
| 93 |
+
st.image(annot_img,width=500)
|
| 94 |
+
|
| 95 |
+
# Display OCR text
|
| 96 |
+
if detect_number:
|
| 97 |
+
st.subheader("Detected Number Plate Text")
|
| 98 |
+
for num in detect_number:
|
| 99 |
+
st.success(num)
|
| 100 |
+
else:
|
| 101 |
+
st.warning("No number plate text detected")
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
#------------------------------------------------------------------------
|
| 106 |
+
# ---------- For Video detection -------------------------
|
| 107 |
+
#------------------------------------------------------------------------
|
| 108 |
+
# ---------- For Video detection -------------------------
|
| 109 |
+
with y:
|
| 110 |
+
st.header("Video Number Plate Detection")
|
| 111 |
+
|
| 112 |
+
video_file = st.file_uploader(
|
| 113 |
+
"Upload a Video",
|
| 114 |
+
type=["mp4", "avi", "mov"]
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
if video_file is not None:
|
| 118 |
+
temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 119 |
+
temp_video.write(video_file.read())
|
| 120 |
+
temp_video.close()
|
| 121 |
+
|
| 122 |
+
cap = cv2.VideoCapture(temp_video.name)
|
| 123 |
+
stframe = st.empty()
|
| 124 |
+
|
| 125 |
+
while cap.isOpened():
|
| 126 |
+
ret, frame = cap.read()
|
| 127 |
+
if not ret:
|
| 128 |
+
break
|
| 129 |
+
|
| 130 |
+
# YOLO inference
|
| 131 |
+
results = model(frame, conf=0.4)
|
| 132 |
+
annotated_frame = frame.copy()
|
| 133 |
+
|
| 134 |
+
for box in results[0].boxes:
|
| 135 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 136 |
+
|
| 137 |
+
plate_crop = frame[y1:y2, x1:x2]
|
| 138 |
+
if plate_crop.size == 0:
|
| 139 |
+
continue
|
| 140 |
+
|
| 141 |
+
gray = cv2.cvtColor(plate_crop, cv2.COLOR_BGR2GRAY)
|
| 142 |
+
|
| 143 |
+
# OCR
|
| 144 |
+
ocr_result = ocr_model.readtext(gray)
|
| 145 |
+
text = " ".join([t[1] for t in ocr_result])
|
| 146 |
+
plate_text = re.sub(r'[^A-Z0-9]', '', text.upper())
|
| 147 |
+
|
| 148 |
+
# Draw bounding box
|
| 149 |
+
cv2.rectangle(
|
| 150 |
+
annotated_frame,
|
| 151 |
+
(x1, y1),
|
| 152 |
+
(x2, y2),
|
| 153 |
+
(0, 255, 0),
|
| 154 |
+
2
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# Draw plate text
|
| 158 |
+
if plate_text:
|
| 159 |
+
cv2.putText(
|
| 160 |
+
annotated_frame,
|
| 161 |
+
plate_text,
|
| 162 |
+
(x1, y1 - 10),
|
| 163 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 164 |
+
0.8,
|
| 165 |
+
(0, 255, 0),
|
| 166 |
+
2
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
stframe.image(annotated_frame, channels="BGR", width=800)
|
| 170 |
+
|
| 171 |
+
cap.release()
|
| 172 |
+
os.remove(temp_video.name)
|
| 173 |
+
st.success("Video processing completed")
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
st.markdown("""
|
| 184 |
+
<br>
|
| 185 |
+
<div style='text-align:center; padding:12px; background-color:#111111; border-radius:10px;'>
|
| 186 |
+
<span style='color:#AAAAAA; font-size:16px;'>
|
| 187 |
+
Designed & Developed by <b style='color:#CCCCCC;'>Yedeedya Injeti</b><br>
|
| 188 |
+
Under <b style='color:#B8860B;'>Innomatics Research Labs</b>
|
| 189 |
+
</span>
|
| 190 |
+
</div>
|
| 191 |
+
<br>
|
| 192 |
+
""", unsafe_allow_html=True)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
|
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:71a1ab4efc46650a51a8f878e39c5c166f3915542d3f1a90774e7766c14deb48
|
| 3 |
+
size 22514922
|
download.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
altair==6.0.0
|
| 2 |
+
attrs==25.4.0
|
| 3 |
+
blinker==1.9.0
|
| 4 |
+
cachetools==6.2.6
|
| 5 |
+
certifi==2026.1.4
|
| 6 |
+
charset-normalizer==3.4.4
|
| 7 |
+
click==8.3.1
|
| 8 |
+
colorama==0.4.6
|
| 9 |
+
contourpy==1.3.2
|
| 10 |
+
cycler==0.12.1
|
| 11 |
+
easyocr==1.7.2
|
| 12 |
+
filelock==3.20.3
|
| 13 |
+
fonttools==4.61.1
|
| 14 |
+
fsspec==2026.1.0
|
| 15 |
+
gitdb==4.0.12
|
| 16 |
+
GitPython==3.1.46
|
| 17 |
+
idna==3.11
|
| 18 |
+
ImageIO==2.37.2
|
| 19 |
+
Jinja2==3.1.6
|
| 20 |
+
jsonschema==4.26.0
|
| 21 |
+
jsonschema-specifications==2025.9.1
|
| 22 |
+
kiwisolver==1.4.9
|
| 23 |
+
lazy_loader==0.4
|
| 24 |
+
MarkupSafe==3.0.3
|
| 25 |
+
matplotlib==3.10.8
|
| 26 |
+
mpmath==1.3.0
|
| 27 |
+
narwhals==2.15.0
|
| 28 |
+
networkx==3.4.2
|
| 29 |
+
ninja==1.13.0
|
| 30 |
+
numpy==2.2.6
|
| 31 |
+
opencv-python==4.13.0.90
|
| 32 |
+
opencv-python-headless==4.13.0.90
|
| 33 |
+
packaging==26.0
|
| 34 |
+
pandas==2.3.3
|
| 35 |
+
pi==0.1.2
|
| 36 |
+
pillow==12.1.0
|
| 37 |
+
polars==1.37.1
|
| 38 |
+
polars-runtime-32==1.37.1
|
| 39 |
+
protobuf==6.33.4
|
| 40 |
+
psutil==7.2.1
|
| 41 |
+
pyarrow==23.0.0
|
| 42 |
+
pyclipper==1.4.0
|
| 43 |
+
pydeck==0.9.1
|
| 44 |
+
pyparsing==3.3.2
|
| 45 |
+
python-bidi==0.6.7
|
| 46 |
+
python-dateutil==2.9.0.post0
|
| 47 |
+
pytz==2025.2
|
| 48 |
+
PyYAML==6.0.3
|
| 49 |
+
referencing==0.37.0
|
| 50 |
+
requests==2.32.5
|
| 51 |
+
rpds-py==0.30.0
|
| 52 |
+
scikit-image==0.25.2
|
| 53 |
+
scipy==1.15.3
|
| 54 |
+
shapely==2.1.2
|
| 55 |
+
six==1.17.0
|
| 56 |
+
smmap==5.0.2
|
| 57 |
+
streamlit==1.53.1
|
| 58 |
+
sympy==1.14.0
|
| 59 |
+
tenacity==9.1.2
|
| 60 |
+
tifffile==2025.5.10
|
| 61 |
+
toml==0.10.2
|
| 62 |
+
torch==2.9.1
|
| 63 |
+
torchvision==0.24.1
|
| 64 |
+
tornado==6.5.4
|
| 65 |
+
typing_extensions==4.15.0
|
| 66 |
+
tzdata==2025.3
|
| 67 |
+
ultralytics==8.4.8
|
| 68 |
+
ultralytics-thop==2.0.18
|
| 69 |
+
urllib3==2.6.3
|
| 70 |
+
watchdog==6.0.0
|
yolo26n.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9b09cc8bf347f0fc8a5f7657480587f25db09b34bf33b0652110fb03a8ad4fef
|
| 3 |
+
size 5544453
|