Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
@@ -48,7 +151,9 @@ def detect_and_predict(img_input):
|
|
| 48 |
cv2.putText(frame, label, (x, y - 10),
|
| 49 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# Streamlit App UI
|
| 54 |
st.set_page_config(page_title="License Plate Detection", layout="wide")
|
|
@@ -65,11 +170,12 @@ with tab1:
|
|
| 65 |
with col1:
|
| 66 |
st.image(image_input, caption="Uploaded Image", use_container_width=True)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
with col2:
|
| 72 |
-
st.image(result_img, caption="Detection Result",
|
| 73 |
if confidence:
|
| 74 |
st.metric("Confidence", f"{confidence * 100:.2f}%")
|
| 75 |
st.success(f"Detected Text: {label}")
|
|
@@ -84,18 +190,14 @@ with tab2:
|
|
| 84 |
if camera_image:
|
| 85 |
try:
|
| 86 |
image_input = Image.open(camera_image)
|
| 87 |
-
# with col2:
|
| 88 |
-
# st.image(image_input, caption="Webcam Snapshot", use_container_width=True)
|
| 89 |
-
|
| 90 |
with st.spinner("Analyzing..."):
|
| 91 |
result_img, confidence, label = detect_and_predict(image_input)
|
| 92 |
with col2:
|
| 93 |
-
st.image(result_img, caption="Detection Result",
|
| 94 |
if confidence is not None:
|
| 95 |
st.metric("Confidence", f"{confidence*100:.2f}%")
|
| 96 |
st.success(f"Detected Text: {label}")
|
| 97 |
else:
|
| 98 |
st.warning("Plate detected but no readable text found.")
|
| 99 |
-
|
| 100 |
except Exception as e:
|
| 101 |
st.error(f"β Error: {str(e)}")
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# import cv2
|
| 3 |
+
# import numpy as np
|
| 4 |
+
# import easyocr
|
| 5 |
+
# from PIL import Image
|
| 6 |
+
# from tensorflow.keras.models import load_model
|
| 7 |
+
# from tensorflow.keras.preprocessing import image as keras_image
|
| 8 |
+
|
| 9 |
+
# # Load model and OCR tools
|
| 10 |
+
# model = load_model("Vehicle_number_plate_Detection.keras")
|
| 11 |
+
# plate_detector = cv2.CascadeClassifier("haarcascade_russian_plate_number.xml")
|
| 12 |
+
# reader = easyocr.Reader(['en'])
|
| 13 |
+
|
| 14 |
+
# # Plate Detection Function
|
| 15 |
+
# def detect_and_predict(img_input):
|
| 16 |
+
# img = np.array(img_input.convert("RGB"))
|
| 17 |
+
# frame = img.copy()
|
| 18 |
+
# gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
|
| 19 |
+
# plates = plate_detector.detectMultiScale(gray, 1.1, 4)
|
| 20 |
+
|
| 21 |
+
# plate_text = "Not Detected"
|
| 22 |
+
# confidence = None
|
| 23 |
+
|
| 24 |
+
# for x, y, w, h in plates:
|
| 25 |
+
# roi = frame[y:y+h, x:x+w]
|
| 26 |
+
# try:
|
| 27 |
+
# test_img = cv2.resize(roi, (200, 200))
|
| 28 |
+
# test_img = keras_image.img_to_array(test_img) / 255.0
|
| 29 |
+
# test_img = np.expand_dims(test_img, axis=0)
|
| 30 |
+
# pred = model.predict(test_img)[0][0]
|
| 31 |
+
# except Exception as e:
|
| 32 |
+
# print(f"Prediction error: {e}")
|
| 33 |
+
# continue
|
| 34 |
+
|
| 35 |
+
# if pred < 0.5:
|
| 36 |
+
# result = reader.readtext(roi)
|
| 37 |
+
# if result:
|
| 38 |
+
# plate_text = result[0][1]
|
| 39 |
+
# confidence = result[0][2]
|
| 40 |
+
# label = f"Plate: {plate_text}"
|
| 41 |
+
# else:
|
| 42 |
+
# label = "Plate Detected (No text)"
|
| 43 |
+
# else:
|
| 44 |
+
# label = "Plate Not Detected"
|
| 45 |
+
|
| 46 |
+
# # Draw detection
|
| 47 |
+
# cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 48 |
+
# cv2.putText(frame, label, (x, y - 10),
|
| 49 |
+
# cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 50 |
+
|
| 51 |
+
# return frame, confidence, plate_text
|
| 52 |
+
|
| 53 |
+
# # Streamlit App UI
|
| 54 |
+
# st.set_page_config(page_title="License Plate Detection", layout="wide")
|
| 55 |
+
# st.title("π License Plate Detection App")
|
| 56 |
+
|
| 57 |
+
# tab1, tab2 = st.tabs(["π Upload Image", "π· Webcam Capture"])
|
| 58 |
+
|
| 59 |
+
# # Tab 1 - Upload Image
|
| 60 |
+
# with tab1:
|
| 61 |
+
# col1, col2 = st.columns([1, 2])
|
| 62 |
+
# uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
|
| 63 |
+
# if uploaded_file:
|
| 64 |
+
# image_input = Image.open(uploaded_file)
|
| 65 |
+
# with col1:
|
| 66 |
+
# st.image(image_input, caption="Uploaded Image", use_container_width=True)
|
| 67 |
+
|
| 68 |
+
# if st.button("π Detect from Upload"):
|
| 69 |
+
# with st.spinner("Processing..."):
|
| 70 |
+
# result_img, confidence, label = detect_and_predict(image_input)
|
| 71 |
+
# with col2:
|
| 72 |
+
# st.image(result_img, caption="Detection Result", channels="RGB", use_container_width=True)
|
| 73 |
+
# if confidence:
|
| 74 |
+
# st.metric("Confidence", f"{confidence * 100:.2f}%")
|
| 75 |
+
# st.success(f"Detected Text: {label}")
|
| 76 |
+
# else:
|
| 77 |
+
# st.warning("No plate text detected.")
|
| 78 |
+
|
| 79 |
+
# # Tab 2 - Webcam Input (camera snapshot)
|
| 80 |
+
# with tab2:
|
| 81 |
+
# col1, col2 = st.columns([1, 2])
|
| 82 |
+
# with col1:
|
| 83 |
+
# camera_image = st.camera_input("π· Take a picture using your webcam")
|
| 84 |
+
# if camera_image:
|
| 85 |
+
# try:
|
| 86 |
+
# image_input = Image.open(camera_image)
|
| 87 |
+
# # with col2:
|
| 88 |
+
# # st.image(image_input, caption="Webcam Snapshot", use_container_width=True)
|
| 89 |
+
|
| 90 |
+
# with st.spinner("Analyzing..."):
|
| 91 |
+
# result_img, confidence, label = detect_and_predict(image_input)
|
| 92 |
+
# with col2:
|
| 93 |
+
# st.image(result_img, caption="Detection Result", channels="RGB", use_container_width=True)
|
| 94 |
+
# if confidence is not None:
|
| 95 |
+
# st.metric("Confidence", f"{confidence*100:.2f}%")
|
| 96 |
+
# st.success(f"Detected Text: {label}")
|
| 97 |
+
# else:
|
| 98 |
+
# st.warning("Plate detected but no readable text found.")
|
| 99 |
+
|
| 100 |
+
# except Exception as e:
|
| 101 |
+
# st.error(f"β Error: {str(e)}")
|
| 102 |
+
|
| 103 |
+
|
| 104 |
import streamlit as st
|
| 105 |
import cv2
|
| 106 |
import numpy as np
|
|
|
|
| 151 |
cv2.putText(frame, label, (x, y - 10),
|
| 152 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 153 |
|
| 154 |
+
# Ensure output image is same size as input
|
| 155 |
+
result_img = Image.fromarray(frame)
|
| 156 |
+
return result_img, confidence, plate_text
|
| 157 |
|
| 158 |
# Streamlit App UI
|
| 159 |
st.set_page_config(page_title="License Plate Detection", layout="wide")
|
|
|
|
| 170 |
with col1:
|
| 171 |
st.image(image_input, caption="Uploaded Image", use_container_width=True)
|
| 172 |
|
| 173 |
+
if st.button("π Detect from Upload"):
|
| 174 |
+
with st.spinner("Processing..."):
|
| 175 |
+
result_img, confidence, label = detect_and_predict(image_input)
|
| 176 |
+
|
| 177 |
with col2:
|
| 178 |
+
st.image(result_img, caption="Detection Result", use_container_width=True)
|
| 179 |
if confidence:
|
| 180 |
st.metric("Confidence", f"{confidence * 100:.2f}%")
|
| 181 |
st.success(f"Detected Text: {label}")
|
|
|
|
| 190 |
if camera_image:
|
| 191 |
try:
|
| 192 |
image_input = Image.open(camera_image)
|
|
|
|
|
|
|
|
|
|
| 193 |
with st.spinner("Analyzing..."):
|
| 194 |
result_img, confidence, label = detect_and_predict(image_input)
|
| 195 |
with col2:
|
| 196 |
+
st.image(result_img, caption="Detection Result", use_container_width=True)
|
| 197 |
if confidence is not None:
|
| 198 |
st.metric("Confidence", f"{confidence*100:.2f}%")
|
| 199 |
st.success(f"Detected Text: {label}")
|
| 200 |
else:
|
| 201 |
st.warning("Plate detected but no readable text found.")
|
|
|
|
| 202 |
except Exception as e:
|
| 203 |
st.error(f"β Error: {str(e)}")
|