File size: 2,811 Bytes
2131b5a
 
 
 
 
 
 
 
 
 
d40274e
 
 
 
 
2131b5a
 
 
 
6368107
 
 
 
 
 
d40274e
 
 
 
2131b5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# πŸš€ Streamlit ANPR App using YOLOv8 + EasyOCR
# Upload a car image -> detect number plate -> read plate text -> show result

import cv2
import easyocr
from ultralytics import YOLO
import os
from datetime import datetime
import streamlit as st
import numpy as np
from ultralytics import YOLOvv8

MODEL_PATH = YOLOvv8.from_pretrained("Ultralytics/YOLOv8")
#source = 'http://images.cocodataset.org/val2017/000000039769.jpg'
#model.predict(source=source, save=True)

# Initialize models
#plate_model = YOLO('yolov8n.pt')
# Auto-download yolov8n.pt if not already present
import os
from ultralytics.utils.downloads import attempt_download_asset

if not os.path.exists('yolov8n.pt'):
    attempt_download_asset('yolov8n.pt')

#MODEL_PATH = 'yolov8n.pt'
#if not os.path.exists(MODEL_PATH):
#    from urllib.request import urlretrieve
#    urlretrieve("https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt", MODEL_PATH)

plate_model = YOLO(MODEL_PATH)

reader = easyocr.Reader(['en'])

# Function to detect plate and read text
def detect_plate_number(image_array, output_dir='outputs'):
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # Save uploaded image temporarily
    temp_path = os.path.join(output_dir, 'uploaded_car.jpg')
    cv2.imwrite(temp_path, image_array)

    # Step 1: Detect plate
    results = plate_model(temp_path)
    boxes = results[0].boxes.xyxy.cpu().numpy()

    if len(boxes) == 0:
        return None, "No plate detected."

    # Step 2: Crop plate region
    x1, y1, x2, y2 = map(int, boxes[0])
    plate_img = image_array[y1:y2, x1:x2]

    plate_path = os.path.join(output_dir, f"plate_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg")
    cv2.imwrite(plate_path, plate_img)

    # Step 3: OCR the plate
    result = reader.readtext(plate_path)
    plate_text = result[0][1] if result else "Text not readable"

    return plate_img, plate_text

# πŸ–ΌοΈ Streamlit UI
st.set_page_config(page_title="ANPR App", layout="centered")
st.title("🚘 Automatic Number Plate Recognition")

uploaded_file = st.file_uploader("Upload a car image", type=["jpg", "png", "jpeg"])

if uploaded_file:
    # Convert uploaded image to OpenCV format
    file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
    image = cv2.imdecode(file_bytes, 1)
    st.image(image, caption="Uploaded Car Image", use_column_width=True)

    with st.spinner("Detecting plate and reading text..."):
        plate_img, plate_text = detect_plate_number(image)

    if plate_img is not None:
        st.success(f"βœ… Plate Detected: {plate_text}")
        st.image(plate_img, caption="Detected Plate Region", use_column_width=False, width=300)
    else:
        st.error(plate_text)

st.markdown("---")
st.markdown("Made with ❀️ using YOLOv8 + EasyOCR")