【问题标题】:How to train data using tensorflow ocr?如何使用 tensorflow ocr 训练数据?
【发布时间】:2020-08-05 00:08:58
【问题描述】:

我是 tensorflow 的新手,所以我对 tensorflow 有点困惑,有多个模型可以像执行 OCR 一样

  1. attention_ocr
  2. 街道

我有以下文件,我必须执行 OCR。我尝试使用pytesseract 读取图像但没有给出正确的结果。

我需要上图的以下结果

  • D MANIKANDAN

  • DURAISAMY

  • 16/07/1986

  • BNZPM2501F

请建议我 tensorflow 模态对于执行上述 OCR 很有用。我正在使用下面的代码从pytesseract获取数据

def getData(coordinate, image):
    (y1, y2, x1, x2, classification) = coordinate
    ts = int(time.time())
    height = y2-y1
    width = x2-x1
    crop = image[y1:y1+height, x1:x1+width]
    CROP_IMAGE_URL = EXPORT_PATH +"data.jpg"
    cv2.imwrite(CROP_IMAGE_URL, crop)
    img = cv2.imread(CROP_IMAGE_URL)
    text = pytesseract.image_to_string(img)
    os.remove(CROP_IMAGE_URL)
    return text

【问题讨论】:

  • 投反对票的人请评论投反对票的原因。
  • 我没有投反对票,但我想你也应该发布代码(到目前为止你已经尝试过)
  • 您在哪里遇到检测文本的问题,对吗?如果是,则通过选择 psmoem 值来配置您的 pytessearch

标签: python python-3.x tensorflow tensorflow2.0 tensorflow-datasets


【解决方案1】:

首先您必须创建一个对象检测模型来找到感兴趣的区域,如图所示。然后您可以将 ROI 图像传递给 OCR 模型或 PyTesseract。

【讨论】:

  • 我也做过同样的事情,但有时 PyTesseract 会给出垃圾词......我必须在适当的 tensorflow 中做
  • 你的 pytesseract 配置了吗?
  • 这是 PyTesseract 的主要问题之一。我将 Azure Read API Service 用于 OCR 部分,它提供了非常准确的结果
【解决方案2】:

步骤:

  • 检测轮廓。

  • 根据轮廓提取 ROI 后,使用 tesseract 提取文本。


import cv2
import pytesseract
import matplotlib.pyplot as plt
import matplotlib

img = cv2.imread('pan2.jpg')
image= img.copy()
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = 255 - cv2.threshold(blur, 0,255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# Dilate to combine adjacent text contours
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4,2))
dilate = cv2.dilate(thresh, kernel, iterations=2)

# Find contours, highlight text areas, and extract ROIs
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

ROI_number = 0
ROI_images = []
for c in cnts:
    area = cv2.contourArea(c)
    x,y,w,h = cv2.boundingRect(c)
    if area > 1000 and 12<h<18:
        cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 3)
        ROI = img[y:y+h, x:x+w]
        # cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
        ROI_number += 1
        ROI_images.append(ROI)
plt.subplot(131)
plt.imshow(thresh)
plt.subplot(132)
plt.imshow(dilate)
plt.subplot(133)
plt.imshow(image)
plt.show()


for i in ROI_images:
    text = pytesseract.image_to_string(i,config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
    print("text:",text)
    plt.imshow(i)
    plt.show()

【讨论】:

  • 你要调整if area &gt; 300 and 7&lt;h&lt;20:
  • 我也看过你的其他示例图片。如果您使用上述条件。您还将获得income tax department, govt. of india, permanent account number 等,但您可以丢弃它们,对吗?通过使用正则表达式或简单的字符串匹配来提取你想要的文本。
  • 此代码是否适用于所有类型的图像,例如new-img.patrika.com/upload/mediafiles/2017/07/06/…
  • Dexter 如果你想让它通用,那么你必须只裁剪 PAN 卡并将其转换为笔直,这可以使用 oopencv 完成。
  • ohk..意味着我有裁剪图像到平移卡之后我必须从该裁剪图像中获取文本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
相关资源
最近更新 更多