Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
import streamlit as st
|
2 |
-
import cv2
|
3 |
import numpy as np
|
4 |
import torch
|
5 |
from ultralytics import YOLO
|
6 |
import easyocr
|
|
|
7 |
|
8 |
# Title of the app
|
9 |
st.title("License Plate Recognition 🚗")
|
@@ -28,9 +28,12 @@ ocr_reader = load_easyocr_reader()
|
|
28 |
|
29 |
# Function to process the uploaded image
|
30 |
def process_image(image, confidence_threshold=0.5):
|
|
|
|
|
|
|
31 |
# Perform license plate detection
|
32 |
-
results = yolo_model(
|
33 |
-
annotated_image =
|
34 |
st.image(annotated_image, caption="Detected License Plate(s)", use_container_width=True)
|
35 |
|
36 |
# Loop through detections
|
@@ -41,12 +44,12 @@ def process_image(image, confidence_threshold=0.5):
|
|
41 |
return
|
42 |
for i, box in enumerate(boxes):
|
43 |
x1, y1, x2, y2 = box
|
44 |
-
cropped_image =
|
45 |
-
|
46 |
-
st.image(
|
47 |
|
48 |
# Perform OCR on the cropped image
|
49 |
-
text_results = ocr_reader.readtext(
|
50 |
detected_text = " ".join(text_results)
|
51 |
st.write(f"**Extracted Text (Plate {i+1}):** {detected_text}")
|
52 |
|
@@ -55,8 +58,8 @@ confidence_threshold = st.sidebar.slider("Confidence Threshold", 0.0, 1.0, 0.5,
|
|
55 |
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
56 |
|
57 |
if uploaded_file is not None:
|
58 |
-
#
|
59 |
-
image =
|
60 |
process_image(image, confidence_threshold)
|
61 |
|
62 |
st.markdown("---")
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import numpy as np
|
3 |
import torch
|
4 |
from ultralytics import YOLO
|
5 |
import easyocr
|
6 |
+
from PIL import Image
|
7 |
|
8 |
# Title of the app
|
9 |
st.title("License Plate Recognition 🚗")
|
|
|
28 |
|
29 |
# Function to process the uploaded image
|
30 |
def process_image(image, confidence_threshold=0.5):
|
31 |
+
# Convert the PIL image to a numpy array
|
32 |
+
image_np = np.array(image)
|
33 |
+
|
34 |
# Perform license plate detection
|
35 |
+
results = yolo_model(image_np, conf=confidence_threshold)
|
36 |
+
annotated_image = Image.fromarray(results[0].plot())
|
37 |
st.image(annotated_image, caption="Detected License Plate(s)", use_container_width=True)
|
38 |
|
39 |
# Loop through detections
|
|
|
44 |
return
|
45 |
for i, box in enumerate(boxes):
|
46 |
x1, y1, x2, y2 = box
|
47 |
+
cropped_image = image_np[y1:y2, x1:x2]
|
48 |
+
cropped_image_pil = Image.fromarray(cropped_image)
|
49 |
+
st.image(cropped_image_pil, caption=f"Cropped License Plate {i+1}", use_container_width=True)
|
50 |
|
51 |
# Perform OCR on the cropped image
|
52 |
+
text_results = ocr_reader.readtext(np.array(cropped_image_pil), detail=0)
|
53 |
detected_text = " ".join(text_results)
|
54 |
st.write(f"**Extracted Text (Plate {i+1}):** {detected_text}")
|
55 |
|
|
|
58 |
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
59 |
|
60 |
if uploaded_file is not None:
|
61 |
+
# Open the uploaded file as a PIL image
|
62 |
+
image = Image.open(uploaded_file)
|
63 |
process_image(image, confidence_threshold)
|
64 |
|
65 |
st.markdown("---")
|