Advertisement
slik1977

CarPlate Recognition v1.0

Jul 4th, 2022
685
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None
  1. # Import dependencies
  2. import matplotlib
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5.  
  6. import cv2  # This is the OpenCV Python library
  7. import pytesseract  # This is the TesseractOCR Python library
  8.  
  9. # Set Tesseract CMD path to the location of tesseract.exe file
  10. pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
  11.  
  12. # Read car image and convert color to RGB
  13. carplate_img = cv2.imread(r'C:\Users\dellx\Desktop\car.jpg')
  14. carplate_img_rgb = cv2.cvtColor(carplate_img, cv2.COLOR_BGR2RGB)
  15. plt.imshow(carplate_img_rgb)
  16.  
  17. # Import Haar Cascade XML file for Russian car plate numbers
  18. carplate_haar_cascade = cv2.CascadeClassifier(r'C:\Users\dellx\Desktop\haarcascade_russian_plate_number.xml')
  19.  
  20.  
  21. # Setup function to detect car plate
  22. def carplate_detect(image):
  23.     carplate_overlay = image.copy()
  24.     carplate_rects = carplate_haar_cascade.detectMultiScale(carplate_overlay, scaleFactor=1.1, minNeighbors=3)
  25.     for x, y, w, h in carplate_rects:
  26.         cv2.rectangle(carplate_overlay, (x, y), (x + w, y + h), (255, 0, 0), 5)
  27.     return carplate_overlay
  28.  
  29. detected_carplate_img = carplate_detect(carplate_img_rgb)
  30. plt.imshow(detected_carplate_img)
  31.  
  32.  
  33. # Create function to retrieve only the car plate region itself
  34. def carplate_extract(image):
  35.     carplate_rects = carplate_haar_cascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5)
  36.     for x, y, w, h in carplate_rects:
  37.         carplate_img = image[y + 10:y + h - 5,
  38.                    x + 0:x + w - 0]  # Adjusted to extract specific region of interest i.e. car license plate
  39.     return carplate_img
  40.  
  41. # Enlarge image for further processing later on
  42. def enlarge_img(image, scale_percent):
  43.     width = int(image.shape[1] * scale_percent / 100)
  44.     height = int(image.shape[0] * scale_percent / 100)
  45.     dim = (width, height)
  46.     resized_image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
  47.     return resized_image
  48.  
  49. # Display extracted car license plate image
  50. carplate_extract_img = carplate_extract(carplate_img_rgb)
  51. carplate_extract_img = enlarge_img(carplate_extract_img, 150)
  52. plt.imshow(carplate_extract_img);
  53. plt.show()
  54. # Convert image to grayscale
  55. carplate_extract_img_gray = cv2.cvtColor(carplate_extract_img, cv2.COLOR_RGB2GRAY)
  56. plt.axis('off')
  57. plt.imshow(carplate_extract_img_gray, cmap = 'gray');
  58.  
  59. # Apply median blur
  60. carplate_extract_img_gray_blur = cv2.medianBlur(carplate_extract_img_gray,3) # kernel size 3
  61. plt.axis('off')
  62. plt.imshow(carplate_extract_img_gray_blur, cmap = 'gray');
  63.  
  64. # Display the text extracted from the car plate
  65. print(pytesseract.image_to_string(carplate_extract_img_gray_blur,
  66.                                   config = f'--psm 9 --oem 3 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'))
  67.  
  68. # Testing all PSM values
  69. for i in range(3,14):
  70.     print(f'PSM: {i}')
  71.     print(pytesseract.image_to_string(carplate_extract_img_gray_blur,
  72.                                      config = f'--psm {i} --oem 3 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'))
Advertisement
RAW Paste Data Copied
Advertisement