使用 pytesseract 从图像中提取字符串

问题描述 投票:0回答:1

我是 OCR 操作和从图像中提取数据的新手。在搜索解决方案后,我确实找到了一些代码,但它不适用于我的用例,它没有正确提取所有字符,最多 2 个。

我想获取这张图片上的字符:

example

我尝试了这个解决方案:

image = cv2.imread('./images/screenshot_2023_11_16_15_41_24.png')

# Assuming 4 characters in a 36x9 image
char_width = image.shape[1] // 4
char_height = image.shape[0]

characters = []
characters_slices = [(0, 9), (9, 18), (18, 27), (27, 36)]  # Adjust based on your image
for start, end in characters_slices:
    char = image[0:char_height, start:end]
    characters.append(char)

# Perform OCR on each character
extracted_text = ""
for char in characters:
    char_text = pytesseract.image_to_string(char, config='--psm 10 --oem 3 -c char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
    extracted_text += char_text.strip() + " "

print("Extracted Text:", extracted_text)

输出为:'H9FA'

谢谢。

python ocr tesseract python-tesseract image-preprocessing
1个回答
0
投票

我使用ImageMagick。您需要一张尺寸合适的黑白图片:

import subprocess
import cv2
import pytesseract

# Image manipulation
# Commands https://imagemagick.org/script/convert.php
mag_img = r'D:\Programme\ImageMagic\magick.exe'
con_bw = r"D:\Programme\ImageMagic\convert.exe" 

in_file = r'alphaNumerical.png'
out_file = r'alphaNumerical_bw.png'

# Play with black and white and contrast for better results
process = subprocess.run([con_bw, in_file, "-resize", "350%","-threshold","80%","-rotate", "-2", out_file])

# Text processing
pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img = cv2.imread(out_file)

# Parameters see tesseract doc 
custom_config = r'--psm 11 --oem 3 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890' 

tex = pytesseract.image_to_string(img, config=custom_config)
print(tex)

with open("cartootn.txt", 'w') as f:
    f.writelines(tex)

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出:

© www.soinside.com 2019 - 2024. All rights reserved.