# -*- coding: utf-8 -*-
"""Registration Number Extraction.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1NqKaOpmbBP_-LfX5gnRnvFY2FIfIYuOz

# **Using EasyOCR for OCR**
"""

# !pip install ultralytics
# !pip install easyocr

from ultralytics import YOLO
import easyocr
import cv2
from google.colab.patches import cv2_imshow

model = YOLO('/content/number_plate_detector.pt')
reader = easyocr.Reader(['en'])

def process_image(image_path):
    results = model(image_path)
    img = cv2.imread(image_path)
    number = ""

    for result in results:
        boxes = result.boxes.xyxy.cpu().numpy()
        for box in boxes:
            x1, y1, x2, y2 = box.astype(int)

            number_plate_image = img[y1:y2, x1:x2]
            gray_number_plate_image = cv2.cvtColor(number_plate_image, cv2.COLOR_BGR2GRAY)


            ocr_results = reader.readtext(gray_number_plate_image)

            for (bbox, text, prob) in ocr_results:
                number = number + text + " "

    print(f"Detected Number Plate: {number}")

    cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

    (text_width, text_height), baseline = cv2.getTextSize(number, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)

    cv2.rectangle(img, (x1, y1 - text_height - 10), (x1 + text_width, y1), (0, 255, 0), -1)

    cv2.putText(img, number, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)


    cv2_imshow(img)

image_paths = ['/content/a7sp8ji_high-security-number-plates_625x300_12_October_18.webp',
               '/content/images.jpg',
               '/content/why-are-number-plates-yellow-and-white.jpg',
               '/content/download (1).jpg']

for image_path in image_paths:
    process_image(image_path)

"""# **Using Pytesseract for OCR**"""

# !pip install ultralytics
# !apt-get install tesseract-ocr -y
# !pip install pytesseract

from ultralytics import YOLO
import pytesseract
import cv2
from google.colab.patches import cv2_imshow

model = YOLO('/content/number_plate_detector.pt')

def process_image(image_path):
    results = model(image_path)
    img = cv2.imread(image_path)
    number = ""

    for result in results:
        boxes = result.boxes.xyxy.cpu().numpy()
        for box in boxes:
            x1, y1, x2, y2 = box.astype(int)

            number_plate_image = img[y1:y2, x1:x2]
            gray_number_plate_image = cv2.cvtColor(number_plate_image, cv2.COLOR_BGR2GRAY)


            text = pytesseract.image_to_string(gray_number_plate_image, config='--psm 8')
            print(text)

            number = number + text.strip() + " "

    print(f"Detected Number Plate: {number}")

    cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

    (text_width, text_height), baseline = cv2.getTextSize(number, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)

    cv2.rectangle(img, (x1, y1 - text_height - 10), (x1 + text_width, y1), (0, 255, 0), -1)

    cv2.putText(img, number, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)


    cv2_imshow(img)

image_paths = ['/content/a7sp8ji_high-security-number-plates_625x300_12_October_18.webp',
               '/content/images.jpg',
               '/content/why-are-number-plates-yellow-and-white.jpg',
               '/content/download (1).jpg']

for image_path in image_paths:
    process_image(image_path)