簡體   English   中英

Tesseract 未檢測到 Python 上 RGB 圖像上的任何文本

[英]Tesseract not detecting any text on RGB images on Python

嘿,我開始使用 Tesseract OCR,但在從非常簡單的 RGB 圖像中獲取文本時遇到問題。 它適用於 text2image 圖像。 這是我的代碼:

from PIL import Image
import pytesseract
import argparse
import cv2
import os
import sys


class wordExtractor():
    def __init__(self, image_path):
        self.image_path = image_path
        pytesseract.pytesseract.tesseract_cmd = r'/home/yarin/tesseract/bin/debug/tesseract'
        #self.resize_image()

def resize_image(self):
    basewidth = 800
    img = Image.open(self.image_path)
    wpercent = (basewidth/float(img.size[0]))
    hsize = int((float(img.size[1])*float(wpercent)))
    img = img.resize((basewidth,hsize), Image.ANTIALIAS)
    os.remove(self.image_path)
    img.save(self.image_path[:-4] + '.png') 
    self.image_path = self.image_path[:-4] + '.png'



def get_text(self, lang):
    # load the example image and convert it to grayscale
    image = cv2.imread(self.image_path)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # check to see if we should apply thresholding to preprocess the
    # image
    #if args["preprocess"] == "thresh":
    gray = cv2.threshold(gray, 0, 255,
        cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    # make a check to see if median blurring should be done to remove
    # noise
    #elif args["preprocess"] == "blur":
    #   gray = cv2.medianBlur(gray, 3)
    # write the grayscale image to disk as a temporary file so we can
    # apply OCR to it
    filename = "{}.png".format(os.getpid())
    cv2.imwrite(filename, gray)
    #load the image as a PIL/Pillow image, apply OCR, and then delete
    # the temporary file
    text = pytesseract.image_to_string(Image.open(filename), lang='eng')
    os.remove(filename)
    return text
    # show the output images
    #cv2.imshow("Image", image)
    #cv2.imshow("Output", gray)
    #cv2.waitKey(0)

w = wordExtractor('6.png')
print(w.get_text('eng'))

正方體為以下圖像返回空字符串:

在此處輸入圖像描述 在此處輸入圖像描述

請告訴我如何解決這個問題提前謝謝!

在此處輸入圖像描述

閾值化后,您可以使用 findContours 查找每個形狀的輪廓。 然后您可以過濾輪廓並將您感興趣的每個輪廓放入空白圖像中。 屆時,您將獲得信件並准備好使用 tesseract 進行處理。 您可以在下面的代碼中看到詳細信息。

import cv2
import numpy as np
import pytesseract

# img = cv2.imread("dwLFQ.png", cv2.IMREAD_COLOR)
img = cv2.imread("NfwY4.png", cv2.IMREAD_COLOR)
# img = cv2.imread("xTH6s.png", cv2.IMREAD_COLOR)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

items = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contours = items[0] if len(items) == 2 else items[1]

base = np.zeros(thresh.shape, dtype=np.uint8)
base = cv2.bitwise_not(base)

max_area = 0
for i in range(len(contours)):
    x, y, w, h = cv2.boundingRect(contours[i])
    ratio = h / w
    area = cv2.contourArea(contours[i])
    cv2.drawContours(img, [contours[i]], 0, (255, 0, 0), 2)

    if 1 < ratio < 3:
        max_area = max(area, max_area)
        print("area: " + str(area) + ", max area: " + str(max_area) + ", ratio: " + str(ratio))
        # if 1000 < area < max_area / 2:
        if 1000 < area < 40000:
            mask = np.zeros(thresh.shape, dtype=np.uint8)
            cv2.drawContours(mask, [contours[i]], -1, color=255, thickness=-1)
            mean = cv2.mean(thresh, mask=mask)

            segment = np.zeros((h, w), dtype=np.uint8)
            segment[:h, :w] = thresh[y:y + h, x:x + w]

            if mean[0] > 150:
                # white, invert
                segment = cv2.bitwise_not(segment)

            base[y:y + h, x:x + w] = segment[:h, :w]
            cv2.imshow("base", base)

            cv2.drawContours(img, [contours[i]], 0, (255, 0, 0), 2)

            cv2.waitKey(0)

custom_config = r'-l eng --oem 3 --psm 6 -c tessedit_char_whitelist="ABCDEFGHIJKLMNOPQRSTUVWXYZ " '
text = pytesseract.image_to_string(base, config=custom_config)
print("detected: " + text)

cv2.imshow("img", img)
cv2.imshow("base", base)

cv2.waitKey(0)
cv2.destroyAllWindows()

結果

detected: NO
ENTRY

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM