如何在Python中一次对多个图像执行OCR,并将所有数据打印到XLSX文件中?

2024-06-10 01:15:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有大约35张图像,它们的命名约定是rsrc(1.jpg),rsrc(2.jpg,。。。这些图像就像虚拟名片,包含几个人的官方信息

我需要对所有这些一起执行OCR,并将所有这些信息打印在excel文件中,排序到适当的列中。请帮帮我

这是我用于对单个图像执行OCR的代码:

from PIL import Image
import cv2
import numpy as np
import pandas as pd

file_path = '*Path*/rsrc (1).jpg'
im = Image.open(file_path)
im.save('ocr (1).png', dpi=(300, 300))

image = cv2.imread('ocr.png', cv2.IMREAD_COLOR)
image = cv2.resize(image, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
ret, threshold = cv2.threshold(image,127,255,cv2.THRESH_BINARY)

text = pytesseract.image_to_string(threshold)

with open("Output.txt", "w", 5,"utf-8") as text_file:
    text_file.write(text)

这是我正在使用的图像之一: rsrc (1).jpg


Tags: pathtext图像imageimport信息thresholdas
2条回答

improving quality techniques中所述,您可以使用

  • 重新缩放

  • 侵蚀

  • 二值化

  • 页面分割模式

您已经应用了重缩放二值化技术,但输出应有一个空白的白色图像。因此,您没有得到期望的结果。通常,使用OTSU的阈值分割可以获得最佳结果。要使用大津阈值,需要加载图像,将其转换为灰度。结果将是:

enter image description here

通过应用erosion,可以使字体更粗体一些:

enter image description here

现在,如果将OCR设置页面分割模式应用于6(假设图像为单个统一的文本块):

Alec Boere

14 - Associate Partner - ILO

ICEURHOR &
LDSGBLONDON

+44 07816066831

你至少可以正确地得到这个数字

代码:

import cv2
import pytesseract

# Loaf the image
img = cv2.imread("fP8IG.jpg")

# Up-sample
img = cv2.resize(img, (0, 0), fx=2, fy=2)

# Convert to the Gray-scale
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Erode
erd = cv2.erode(gry, None, iterations=1)

# Simple-threshold
thr = cv2.threshold(erd, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# OCR
txt = pytesseract.image_to_string(thr, config=" psm 6")
print(txt)

# Display
cv2.imshow("", thr)
cv2.waitKey(0)

应该是这样的,但我没有运行代码,它只是从代码中复制/粘贴,并围绕代码循环:

results = []
file_paths = ['*Path*/rsrc (1).jpg', '*Path*/rsrc (2).jpg', etc...]

def one_scan(file_path):
    im = Image.open(file_path)
    base = os.path.basename(file_path)
    new_file_name = ps.path.splitext(base)[0]+'.png'
    im.save(new_file_name, dpi=(300, 300))

    image = cv2.imread(new_file_name, cv2.IMREAD_COLOR)
    image = cv2.resize(image, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
    ret, threshold = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)

    return pytesseract.image_to_string(threshold)


for file_path in file_paths:
    result = one_scan(file_path)
    results.append(result)

with open("Output.txt", "w", encoding="utf-8") as text_file:
    text_file.write("\n".join(map(str, results)))

相关问题 更多 >