GlitchGhost commited on
Commit
a235a85
ยท
verified ยท
1 Parent(s): 4daefc2

Add some features

Browse files
Files changed (1) hide show
  1. app.py +28 -17
app.py CHANGED
@@ -4,8 +4,8 @@ import torchvision.transforms as T
4
  import cv2
5
  import numpy as np
6
  from PIL import Image
 
7
 
8
- # Custom CSS for amazing UI
9
  st.markdown("""
10
  <style>
11
  .main {
@@ -53,7 +53,6 @@ st.markdown("""
53
  </style>
54
  """, unsafe_allow_html=True)
55
 
56
- # Load model with explicit `weights_only=False`
57
  @st.cache_resource
58
  def load_model():
59
  try:
@@ -66,9 +65,15 @@ def load_model():
66
 
67
  model = load_model()
68
 
69
- def detect_numberplate(image):
 
 
 
 
 
 
70
  if model is None:
71
- return image # Return original image if model fails to load
72
 
73
  transform = T.ToTensor()
74
  img_tensor = transform(image).unsqueeze(0)
@@ -79,24 +84,31 @@ def detect_numberplate(image):
79
  boxes = predictions[0]['boxes']
80
  scores = predictions[0]['scores']
81
 
82
- image_np = np.array(image) # Convert to numpy array
 
83
  for box, score in zip(boxes, scores):
84
- if score > 0.5: # Confidence threshold
85
  x1, y1, x2, y2 = map(int, box.tolist())
86
- # Fancy rectangle with gradient effect
87
  cv2.rectangle(image_np, (x1, y1), (x2, y2), (0, 255, 153), 3)
88
- # Add score label
89
  cv2.putText(image_np, f"{score:.2f}", (x1, y1-10),
90
- cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
 
 
91
 
92
- return Image.fromarray(image_np)
 
 
 
 
 
 
 
 
93
 
94
- # Streamlit UI
95
  st.markdown('<div class="main">', unsafe_allow_html=True)
96
  st.markdown('<p class="title">๐Ÿš— Number Plate Wizard</p>', unsafe_allow_html=True)
97
  st.markdown('<p class="subtitle">Unveil the magic of AI-powered detection!</p>', unsafe_allow_html=True)
98
 
99
- # File uploader with a cool design
100
  with st.container():
101
  st.markdown('<div class="upload-box">', unsafe_allow_html=True)
102
  uploaded_file = st.file_uploader("๐Ÿ“ธ Drop your image here", type=["jpg", "png", "jpeg"],
@@ -106,12 +118,11 @@ with st.container():
106
  if uploaded_file is not None:
107
  image = Image.open(uploaded_file).convert("RGB")
108
 
109
- # Fancy button with spinner
110
  if st.button("โœจ Detect Number Plate"):
111
  with st.spinner("๐Ÿ”ฎ Casting detection spell..."):
112
- result_image = detect_numberplate(image)
 
113
 
114
- # Display results with a sleek layout
115
  st.markdown("<h3 style='color: #ffffff; text-align: center;'>๐ŸŽ‰ Detection Complete!</h3>", unsafe_allow_html=True)
116
  col1, col2 = st.columns(2)
117
  with col1:
@@ -121,10 +132,10 @@ if uploaded_file is not None:
121
  st.image(result_image, caption="Magic Revealed", use_container_width=True)
122
  st.markdown('<p class="image-caption">Number plate spotted!</p>', unsafe_allow_html=True)
123
 
124
- # Fun success message
 
125
  st.success("Boom! The number plate has been summoned! ๐Ÿš˜")
126
 
127
- # Footer
128
  st.markdown('<p style="color: #d1e8ff; text-align: center; margin-top: 20px;">Powered by Abhijeet Singh</p>',
129
  unsafe_allow_html=True)
130
  st.markdown('</div>', unsafe_allow_html=True)
 
4
  import cv2
5
  import numpy as np
6
  from PIL import Image
7
+ import easyocr
8
 
 
9
  st.markdown("""
10
  <style>
11
  .main {
 
53
  </style>
54
  """, unsafe_allow_html=True)
55
 
 
56
  @st.cache_resource
57
  def load_model():
58
  try:
 
65
 
66
  model = load_model()
67
 
68
+ @st.cache_resource
69
+ def load_ocr_reader():
70
+ return easyocr.Reader(['en'], gpu=False)
71
+
72
+ reader = load_ocr_reader()
73
+
74
+ def detect_and_crop_numberplate(image):
75
  if model is None:
76
+ return image, None
77
 
78
  transform = T.ToTensor()
79
  img_tensor = transform(image).unsqueeze(0)
 
84
  boxes = predictions[0]['boxes']
85
  scores = predictions[0]['scores']
86
 
87
+ image_np = np.array(image)
88
+ cropped_plate = None
89
  for box, score in zip(boxes, scores):
90
+ if score > 0.5:
91
  x1, y1, x2, y2 = map(int, box.tolist())
 
92
  cv2.rectangle(image_np, (x1, y1), (x2, y2), (0, 255, 153), 3)
 
93
  cv2.putText(image_np, f"{score:.2f}", (x1, y1-10),
94
+ cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
95
+ cropped_plate = image.crop((x1, y1, x2, y2))
96
+ break
97
 
98
+ return Image.fromarray(image_np), cropped_plate
99
+
100
+ def extract_text(cropped_plate):
101
+ if cropped_plate is None:
102
+ return "No number plate detected"
103
+ cropped_np = np.array(cropped_plate)
104
+ result = reader.readtext(cropped_np)
105
+ text = " ".join([res[1] for res in result])
106
+ return text if text else "Unable to read text"
107
 
 
108
  st.markdown('<div class="main">', unsafe_allow_html=True)
109
  st.markdown('<p class="title">๐Ÿš— Number Plate Wizard</p>', unsafe_allow_html=True)
110
  st.markdown('<p class="subtitle">Unveil the magic of AI-powered detection!</p>', unsafe_allow_html=True)
111
 
 
112
  with st.container():
113
  st.markdown('<div class="upload-box">', unsafe_allow_html=True)
114
  uploaded_file = st.file_uploader("๐Ÿ“ธ Drop your image here", type=["jpg", "png", "jpeg"],
 
118
  if uploaded_file is not None:
119
  image = Image.open(uploaded_file).convert("RGB")
120
 
 
121
  if st.button("โœจ Detect Number Plate"):
122
  with st.spinner("๐Ÿ”ฎ Casting detection spell..."):
123
+ result_image, cropped_plate = detect_and_crop_numberplate(image)
124
+ detected_text = extract_text(cropped_plate)
125
 
 
126
  st.markdown("<h3 style='color: #ffffff; text-align: center;'>๐ŸŽ‰ Detection Complete!</h3>", unsafe_allow_html=True)
127
  col1, col2 = st.columns(2)
128
  with col1:
 
132
  st.image(result_image, caption="Magic Revealed", use_container_width=True)
133
  st.markdown('<p class="image-caption">Number plate spotted!</p>', unsafe_allow_html=True)
134
 
135
+ st.markdown(f"<h4 style='color: #ffffff; text-align: center;'>Detected Text: {detected_text}</h4>", unsafe_allow_html=True)
136
+
137
  st.success("Boom! The number plate has been summoned! ๐Ÿš˜")
138
 
 
139
  st.markdown('<p style="color: #d1e8ff; text-align: center; margin-top: 20px;">Powered by Abhijeet Singh</p>',
140
  unsafe_allow_html=True)
141
  st.markdown('</div>', unsafe_allow_html=True)