Sourudra commited on
Commit
7526718
·
verified ·
1 Parent(s): 944d1e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
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(image, conf=confidence_threshold)
33
- annotated_image = cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
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 = image[y1:y2, x1:x2]
45
- cropped_image_rgb = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)
46
- st.image(cropped_image_rgb, caption=f"Cropped License Plate {i+1}", use_container_width=True)
47
 
48
  # Perform OCR on the cropped image
49
- text_results = ocr_reader.readtext(cropped_image_rgb, detail=0)
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
- # Read the uploaded image
59
- image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
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("---")