0

I'm a programmer, but i have no prior experience with Python or any of its libraries, or even OCR/ALPR as a whole. I have a script that i made (basically copying and paste other scripts for all over the web) that I pretend to use to recognize License Plates. But the reality is my code is very bad right now. It can recognize text in images pretty well, but it sucks to capture license plates. Very rarely i can get a License Plate with it.

So I would like some help on how should I change my code to make it better.

In my code, I simply choose an image, convert it to binary and BW, and try to read it.

I ONLY NEED the string (image_to_string); I do NOT NEED THE IMAGE.

Also is important to note that, as I said, I have no expertise with this code or the functions I'm using.

My code:

from PIL import Image
import pytesseract
import numpy as np
import cv2



image = cv2.imread('redcar.jpg')
image=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
se=cv2.getStructuringElement(cv2.MORPH_RECT , (8,8))
bg=cv2.morphologyEx(image, cv2.MORPH_DILATE, se)
out_gray=cv2.divide(image, bg, scale=255)
out_binary=cv2.threshold(out_gray, 0, 255, cv2.THRESH_OTSU )[1] 

#cv2.imshow('binary', out_binary)  
cv2.imwrite('binary.png',out_binary)

#cv2.imshow('gray', out_gray)  
cv2.imwrite('gray.png',out_gray)

filename = 'gray.png'
img1 = np.array(Image.open(filename))




text = pytesseract.image_to_string(filename,config ='--psm 6')
print(text) 

The image I'm using: enter image description here

L. Scott Johnson
  • 4,213
  • 2
  • 17
  • 28
Master
  • 135
  • 1
  • 11

1 Answers1

3

I hope easyocr will be helpfull in such case. You can install easyocr by pip install easyocr with opencv version of opencv-python-4.5.4.60

import easyocr

IMAGE_PATH = 'AQFCB.jpg'
reader = easyocr.Reader(['en'])
result = reader.readtext(IMAGE_PATH)
for detection in result:
    if detection[2] > 0.5:
        print(detection[1])

the output is

HR.26 BR.9044
Berlin Benilo
  • 472
  • 1
  • 12
  • Hi,thanks for the answer. Should i do ONLY this, or should i add this script of yours after mine? – Master May 25 '22 at 17:49
  • You can do only this.. Thats enough – Berlin Benilo May 25 '22 at 18:02
  • Well i really wish i could but i keep getting error "CUDA not available - defaulting to CPU. Note: This module is much faster with a GPU." – Master May 25 '22 at 18:20
  • Yes. The performance is better with GPU and also without any "CUDA not available" warning. If my answer gave the expected result, please accept the answer. thank you – Berlin Benilo May 25 '22 at 18:27