File size: 5,377 Bytes
4289542 | 1 2 3 4 5 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 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 226 | import streamlit as st
import cv2
import numpy as np
import os
import tempfile
from PIL import Image
from ultralytics import YOLO
import easyocr
import re
# ----------- Page config ----------------
st.set_page_config(
page_title='Number plate',
page_icon='',
layout='wide'
)
st.title("Number plate")
st.write('Upload an image or video to detect helmet')
# -------------- load model detection -----------
@st.cache_resource
def load_model():
return YOLO('best.pt')
@st.cache_resource
def load_ocr():
return easyocr.Reader(['en'])
model = load_model()
ocr_model = load_ocr()
# switching tabs
x,y = st.tabs(['Image Detection','Video Detection'])
# Tabs 1 == Image detection
with x:
st.header('Image Detection')
img_path = st.file_uploader('Please upload an image') # upload option
if img_path is not None:
image = Image.open(img_path)
image_np = np.array(image) # converting in to atrray
# YOLO inference
result = model(image_np,conf=0.4)
annot_img = result[0].plot()
# Convert BGR to RGB
annot_img = cv2.cvtColor(annot_img, cv2.COLOR_BGR2RGB)
# --------------------- number detection ----------------------
detect_number = []
for box in result[0].boxes:
x1,y1,x2,y2 = map(int,box.xyxy[0])
plate_crop = image_np[y1:y2,x1:x2]
gray = cv2.cvtColor(plate_crop,cv2.COLOR_BGR2GRAY)
ocr_result = ocr_model.readtext(gray)
text = ""
for t in ocr_result:
text += t[1] + " "
plate_text = re.sub(r'[^A-Z0-9]', '', text.upper())
if plate_text:
detect_number.append(plate_text)
#st.subheader("Detected Result")
#st.image(annot_img, use_container_width=True)
#st.image(annot_img,width=400) # first image
#st.image(image,width=400) # last image
# to display side by side
ori_img,pre_img = st.columns(2)
with ori_img: # original image
st.markdown('#### ***Original Image***')
st.image(image,width=500)
with pre_img:
st.markdown('#### ***Detected Image***')
st.image(annot_img,width=500)
# Display OCR text
if detect_number:
st.subheader("Detected Number Plate Text")
for num in detect_number:
st.success(num)
else:
st.warning("No number plate text detected")
#------------------------------------------------------------------------
# ---------- For Video detection -------------------------
#------------------------------------------------------------------------
# ---------- For Video detection -------------------------
with y:
st.header("Video Number Plate Detection")
video_file = st.file_uploader(
"Upload a Video",
type=["mp4", "avi", "mov"]
)
if video_file is not None:
temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
temp_video.write(video_file.read())
temp_video.close()
cap = cv2.VideoCapture(temp_video.name)
stframe = st.empty()
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# YOLO inference
results = model(frame, conf=0.4)
annotated_frame = frame.copy()
for box in results[0].boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
plate_crop = frame[y1:y2, x1:x2]
if plate_crop.size == 0:
continue
gray = cv2.cvtColor(plate_crop, cv2.COLOR_BGR2GRAY)
# OCR
ocr_result = ocr_model.readtext(gray)
text = " ".join([t[1] for t in ocr_result])
plate_text = re.sub(r'[^A-Z0-9]', '', text.upper())
# Draw bounding box
cv2.rectangle(
annotated_frame,
(x1, y1),
(x2, y2),
(0, 255, 0),
2
)
# Draw plate text
if plate_text:
cv2.putText(
annotated_frame,
plate_text,
(x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.8,
(0, 255, 0),
2
)
stframe.image(annotated_frame, channels="BGR", width=800)
cap.release()
os.remove(temp_video.name)
st.success("Video processing completed")
st.markdown("""
<br>
<div style='text-align:center; padding:12px; background-color:#111111; border-radius:10px;'>
<span style='color:#AAAAAA; font-size:16px;'>
Designed & Developed by <b style='color:#CCCCCC;'>Yedeedya Injeti</b><br>
Under <b style='color:#B8860B;'>Innomatics Research Labs</b>
</span>
</div>
<br>
""", unsafe_allow_html=True)
|