prahalya commited on
Commit
a82a628
Β·
verified Β·
1 Parent(s): 02579dc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("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
+ uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
62
+ if uploaded_file:
63
+ image_input = Image.open(uploaded_file)
64
+ st.image(image_input, caption="Uploaded Image", use_container_width=True)
65
+
66
+ if st.button("πŸ” Detect from Upload"):
67
+ with st.spinner("Processing..."):
68
+ result_img, confidence, label = detect_and_predict(image_input)
69
+
70
+ st.image(result_img, caption="Detection Result", channels="RGB", use_container_width=True)
71
+ if confidence:
72
+ st.metric("Confidence", f"{confidence * 100:.2f}%")
73
+ st.success(f"Detected Text: {label}")
74
+ else:
75
+ st.warning("No plate text detected.")
76
+
77
+ # Tab 2 - Webcam Input (camera snapshot)
78
+ with tab2:
79
+ camera_image = st.camera_input("πŸ“· Take a picture using your webcam")
80
+ if camera_image:
81
+ try:
82
+ image_input = Image.open(camera_image)
83
+ st.image(image_input, caption="Webcam Snapshot", use_container_width=True)
84
+
85
+ with st.spinner("Analyzing..."):
86
+ result_img, confidence, label = detect_and_predict(image_input)
87
+
88
+ st.image(result_img, caption="Detection Result", channels="RGB", use_container_width=True)
89
+ if confidence is not None:
90
+ st.metric("Confidence", f"{confidence*100:.2f}%")
91
+ st.success(f"Detected Text: {label}")
92
+ else:
93
+ st.warning("Plate detected but no readable text found.")
94
+
95
+ except Exception as e:
96
+ st.error(f"❌ Error: {str(e)}")