+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

检测并分割OCR图像

发布于2021-01-17 17:23     阅读(351)     评论(0)     点赞(18)     收藏(3)


0

1

2

3

4

5

我正在尝试使用OCR标准格式(正反面都对其进行扫描)

在此处输入图片说明 在此处输入图片说明

我只想OCR扫描中的第二张图像(带有文本信息的第二张图像)-是否可以检测和分割它们,并且仅处理正确的图像?对不起,如果我错过了一些重要的事情,那就从头开始。

 import pytesseract as tess
    import os
    from PIL import Image
    import pandas as pd
    import tesserocr



    path = "/Users/oliviervandhuynslager/PycharmProjects/OCR/DC_SCANS_TEST" ##path to directory (folder) where the images are located

    count = 0
    fileName = [] #create empty list that will contain the original filenames
    fullText = [] #create empty list to store the OCR results per file
    for imageName in os.listdir("/Users/oliviervandhuynslager/PycharmProjects/OCR/DC_SCANS_TEST"):
        count = count + 1
        fileName.append(imageName)
        fileName.sort()#generate list from texts.
    #%%
     # APPEND (OCR) text from images TO LIST fullText
    for imageName in os.listdir("/Users/oliviervandhuynslager/PycharmProjects/OCR/DC_SCANS_TEST"):
        inputPath = os.path.join(path, imageName)
        img = Image.open(inputPath)
        text = tess.image_to_string(img, lang="eng")
        fullText.append(text)

解决方案


这是显示图像的工作示例:

import cv2
import numpy as np
import pytesseract

pytesseract.pytesseract.tesseract_cmd=r"C:\Program Files\Tesseract-OCR\tesseract.exe"

img = cv2.imread("BFezy.png", 0)

kernel = np.ones((25, 25), np.uint8)
eroded = cv2.erode(img, kernel, iterations=2)
dilated = cv2.dilate(eroded, kernel, iterations=1)
thresholded = cv2.threshold(dilated, 150, 255, cv2.THRESH_BINARY_INV)[1]
countours = cv2.findContours(th, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]
if len(countours) == 2:
    x, y, w, h = cv2.boundingRect(countours[0])
    crop = img[y:h + y, x:w + x]
    text = pytesseract.image_to_string(crop)
    print(text)

0

1

2

3

4

5

6

7

8

9



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/777387/cd4a19f28aa8c58b7c07/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

18 0
收藏该文
已收藏

评论内容:(最多支持255个字符)