简体   繁体   English

检测和分割 OCR 的图像

[英]detect and split image for OCR

I am trying to OCR standard forms (they are scanned both front and back)我正在尝试 OCR 标准表格(正面和背面都扫描它们)

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

I only want to OCR The second image on the scan (the one with the textual information) - is there a way to detect and split them, and only process the right one?我只想 OCR 扫描的第二个图像(带有文本信息的图像)-有没有办法检测和拆分它们,并且只处理正确的图像? Sorry if I'm missing out on something essential, just starting off.对不起,如果我错过了一些重要的东西,刚刚开始。

 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)

Here is working example for presented images:这是呈现图像的工作示例:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM