Tesseract OCR extraction



我正在构建一个OCR模型,我在图像上执行了对象检测。我调用检测函数来检测边界框。我正在裁剪图像的边界框。我面临的挑战是裁剪的图像太小,无法进行数据提取,这影响了精度质量。

# Crop Image
cropped_image = tf.image.crop_to_bounding_box(image, y_min, x_min, y_max - y_min, x_max - x_min)
# write jpg with pillow
img_pil = Image.fromarray(cropped_image.numpy())
score = bscores[idx] * 100
file_name = OUTPUT_PATH + "somefilename"

img_pil = ImageOps.grayscale(img_pil)
img_pil.save(file_name, quality=95, subsampling=0)

我在裁剪的图像上运行超分辨率算法,以便在传递给tesseract之前提高图像质量,但仍然无法达到良好的精度。

# Create an SR object
sr = dnn_superres.DnnSuperResImpl_create()
# Define model path
model_path = os.path.join(base_path, model + ".pb")
# Extract model name, get the text between '/' and '_'
model_name = model_path.split('\')[-1].split('_')[0].lower()
# Extract model scale
model_scale = int(model_path.split('\')[-1].split('_')[1].split('.')[0][1])
# Read the desired model
sr.readModel(model_path)
sr.setModel(model_name, model_scale)

如何解决这些裁剪图像的问题,使数据提取更准确。

您是否尝试过OCRing然后裁剪,而不是相反?这可能需要更长的时间,但它可能会更准确。

我有很多使用ocrmypdf与PDFPlumber和Regex将PDF文档解析为电子表格的经验,这是我通常遵循的过程:

import pandas as pd
import os
import pdfplumber
import re
#OCR PDF
os.system('ocrmypdf --force-ocr --deskew path/to/file.pdf path/to/file.pdf')
text = ''
with pdfplumber.open('path/to/file.pdf'):
for i in range(0, len(pages)):
page = pdf.pages[i]
text = page.extract_text()
pdf_text = pdf_text + 'n' + text
ids = re.findall('id: (.*)', text)
y = pdf_text.split('n')
ds =  []
for i,j in enumerate(ids):
d = {}
try:
id1 = ids[i]
idx1 = [idx for idx, s in enumerate(y) if id1 in s][0]
try:
id2 = ids[i+1]
idx2 = [idx for idx, s in enumerate(y) if id2 in s][0]
z = y[idx1:idx2]
except:
z = y[idx1:]
except:
pass
chunk =  ''
#may need to add if/else or try/except
d['value'] = re.findall('Model name: (.*)', chunk)[0]
#rinse and repeat
ds.append(d)
df = pd.DataFrame(ds)

不确定这有多大帮助,但它可能会给你一些灵感。

最新更新