【问题标题】:pytesseract not recognizing numbers in picture using OCRpytesseract 无法使用 OCR 识别图片中的数字
【发布时间】:2019-03-14 19:33:03
【问题描述】:
我正在尝试使用 Python-tesseract 通过光学字符识别 (OCR) 从中提取数字 (picture)。出于某种原因,pytesseract 无法识别数字,我也不完全理解为什么(数字之间的距离?)。
有人可以帮助我了解如何从这张图片中正确提取数字吗?
下面的代码不打印任何东西
im.save("sudo.png")
text = pytesseract.image_to_string(im)
print(text)
【问题讨论】:
标签:
python-3.x
python-tesseract
【解决方案1】:
一些预处理和使用 ROI 来指定单词的位置会有所帮助。默认情况下,OCR 使用页面布局分析来确定文本块。在这种情况下,图像看起来不像普通的文本页面(例如 PDF 文章)。
为了使 OCR 更容易,首先您可以使用 regionprops 找到单词的位置,然后将单词的位置(作为边界框)传递给 OCR 函数。请参阅下面的代码和结果。它们看起来很准确。您可能需要更多地使用预处理,以使其对不同图像的集合具有鲁棒性。但希望这能让您了解如何进行:
capture = imread('Captura.PNG');
% Increase image size by 3x
my_image = imresize(capture, 3);
figure
imshow(my_image)
% Localize words
BW = imbinarize(rgb2gray(my_image));
BW1 = imdilate(BW,strel('disk',6));
s = regionprops(BW1,'BoundingBox');
bboxes = vertcat(s(:).BoundingBox);
% Sort boxes by image height
[~,ord] = sort(bboxes(:,2));
bboxes = bboxes(ord,:);
% Pre-process image to make letters thicker
BW = imdilate(BW,strel('disk',1));
% Call OCR and pass in location of words. Also, set TextLayout to 'word'
ocrResults = ocr(BW,bboxes,'CharacterSet','.0123456789','TextLayout','word');
words = {ocrResults(:).Text}';
words = deblank(words)