wko-projekt/ocr.py

43 lines
1.1 KiB
Python

import easyocr
import cv2 as cv
import keras_ocr
import pytesseract
def keras_ocr_func():
pipeline = keras_ocr.pipeline.Pipeline()
images = [
keras_ocr.tools.read(img) for img in ['img0.png', ]
]
prediction_groups = pipeline.recognize(images)
car_numbers = ''
try:
for i in prediction_groups[0]:
car_numbers += i[0]
except:
print('no detection')
return car_numbers
def tesseract_ocr():
img = cv.imread('img0.png')
res = pytesseract.image_to_string(img,
lang='eng',
config='--oem 3 --psm 6 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
return res
def get_text_from_image(img_path, cut=7):
text = ''
image = cv.imread(img_path)
reader = easyocr.Reader(['en'])
ocr_result = reader.readtext((image), paragraph="True", min_size=120, #180 for rgb
allowlist='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
try:
text=ocr_result[0][1].replace(' ', '')[:cut] # cut to 7 symbols
except:
print('too few symbols')
return text