簡體   English   中英

Python + OpenCV + Pytesseract 建議

[英]Python + OpenCV + Pytesseract suggestion

我正在嘗試對這張圖片進行 OCR,它會有所不同(0-4 / 4): 在此處輸入圖像描述

我一直在嘗試使用 Pytesseract,但我沒有得到正確的結果。

這是我到目前為止所擁有的:

screen_crop = cv2.imread(screen)
screen_gray = cv2.cvtColor(screen_crop, cv2.COLOR_BGR2GRAY)
screen_thresh = cv2.threshold(screen_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
screen_noise = cv2.medianBlur(screen_thresh, 1)
cv2.imshow('img', screen_noise)
ocr = pytesseract.image_to_string(screen_noise)
print(ocr)
cv2.waitKey(0)

這是用 OpenCV 處理后的結果: 在此處輸入圖像描述

OCR 正在返回“re”、“res”...

建議(不需要是 pytesseract)? 謝謝!

問題是 Pytesseract 在單詞為黑色且背景為白色時具有更高的准確性。 因此,您應該使用 BINARY_INV 閾值類型而不是 BINARY。
完整代碼:

<!-- language: python -->
import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = 'C:/Users/stevi/AppData/Local/Tesseract-OCR/tesseract.exe'

if __name__ == '__main__':
    screen_crop = cv2.imread('img.png')
    screen_gray = cv2.cvtColor(screen_crop, cv2.COLOR_BGR2GRAY)

    screen_thresh = cv2.threshold(screen_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
    cv2.namedWindow('BINARY', cv2.WINDOW_NORMAL)
    cv2.imshow('BINARY', screen_thresh)

    screen_thresh = cv2.threshold(screen_gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    cv2.namedWindow('BINARY_INV', cv2.WINDOW_NORMAL)
    cv2.imshow('BINARY_INV', screen_thresh)

    screen_noise = cv2.medianBlur(screen_thresh, 1)
    ocr = pytesseract.image_to_string(screen_noise)
    print(ocr)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

結果:

在此處輸入圖像描述

我使用 keras-ocr 而不是 pytesseract 獲得了一些不錯的 OCR 結果。 這是我用於測試的 colab 筆記本的鏈接: https://colab.research.google.com/drive/1ccohrWn98EF4VdAtwl-shs4S5RxDu0Ew

import matplotlib.pyplot as plt
import keras_ocr

# keras-ocr will automatically download pretrained
# weights for the detector and recognizer.
pipeline = keras_ocr.pipeline.Pipeline()

def get_predictions(images, keywords=None, plot=False):
    images = [keras_ocr.tools.read(url) for url in images]
    prediction_groups = pipeline.recognize(images)
    words = [[prediction[0] for prediction in image
              if prediction[0] in (keywords or [])
              or keywords == None]
             for image in prediction_groups]
    if plot:
        # Plot the predictions
        fig, axs = plt.subplots(nrows=len(images), figsize=(20, 20))
        for ax, image, predictions in zip(axs, images, prediction_groups):
            keras_ocr.tools.drawAnnotations(image=image,
                                            predictions=predictions,
                                            ax=ax)
    return words

輸入:

search_images = [
    'https://i.stack.imgur.com/ybpke.png',
    'https://cdn1.egglandsbest.com/assets/images/products/_productFeatureMobi/shell_classic-12over@2x.jpg',
    'https://egglandsbest.coyne-digital.com/wp-content/uploads/2014/08/classic-eggs-MTB.png',
    'https://www.utahsown.org/wp-content/uploads/2017/05/egglands_best_eggs_large_18ct_foam_MT.jpg',
    'https://egglandsbest.coyne-digital.com/wp-content/uploads/2014/08/egglands_best_cage-free_eggs_large_12ct_plastic_MT.jpg',
    'https://cdn1.egglandsbest.com/assets/images/products/_productFeatureMobi/shell_classic-24over@2x.jpg',
]

search_keywords = [
    'egglands',
    'best',
    'extra',
    'large',
    'cage',
    'free',
    'vegetarian',
    '24',
    '12',
    '18',
    '014'
]



predicted_words = get_predictions(search_images)

print(predicted_words)

Output:

[['014'], ['your', 'fresh', 'farm', 'nowi', 'for', 'diet', 'nutritious', 'alits', 'egglands', 'eb', 'best', 'excellent', 'source', 'ofe', 'brandspark', 'vitamins', 'ppro', 'most', 'b5', 'egg', 'b12', 'superior', 'tasting', 'b2', 'americas', 'd', 'e', 'trusted', 'large', 'plus125mg', 'omega', '3', 'grade', 'a', 'eggs', '12', 'saturated', 'fat', '250', 'less', 'american', 'by', 'regular', 'eggs', 'than', 'shoppers', 'fed', 'hens', 'vegetarian', 'per', 'egg', 'lb', 'oz', 'boo', 'colestero', 'coten', 'net', 'wt', '24', 'oz1', 'b', 'facts', 'fon', 'ssee', 'uirmon', 's', 'n', ''], ['farm', 'fresh', 'stays', 'nowi', 'egglands', 'longer', 'fresher', 'best', 'lles', 'vitatnins', 'd', 'biz', 'e', 'zeggse', 'b', 'gradealarge', 'amlne', 'hs', 'ule', 'oe', 'raing', 'doe', 'taltes', 'ce'], ['stays', 'nowi', 'longer', 'eb', 'fresher', 'farm', 'fresh', 'excellent', 'source', 'of', 'eggiands', 'vitamins', 'd', 'brandseer', 'b12', 'e', 'most', 'trusted', 'good', 'best', 'source', 'of', 'soerens', 'vitamins', 'b2', 'b5', 'plusllsmg', 'omega', '3', 'anericas', 'superior', 'tasting', 'egs', '250', 'less', 'saturated', 'fat', '18', 'eggssa', 'large', 'gradea', 'than', 'regular', 'eggs', 'peregg', 'lleg', 'ensizels', 'dibs', 'asia', 'cottn', 'vegetarian', 'fed', 'hens'], ['farm', 'fresh', 'stays', 'nowa', 'le', 'egglands', 'longer', 'free', 'eb', 'fresher', 'best', 'd', 'cage', 'pro', 'excellent', 'source', 'of', 'vitamins', 'd', 'b12', 'e', 'most', 'good', 'source', 'of', 'trusted', 'vitamins', 'b2', 'b5', 'vecetarian', 'plusil', 'fed', 'smess', 'hens', 'omega', '3', '259', '12', 'eggs', 'saturated', 'grade', 'fat', 'ag', 'large', 'brown', 'than', 'regular', 'eggs', 'etranso'], ['your', 'nowhi', 'for', 'diet', 'nutritious', 'eb', 'fresh', 'farm', '0', 'r', 'egglands', 'excellent', 'source', 'of', 'vitamins', 'best', 'b2', 'b12', 'b5', 'd', 'e', 'tasting', 'egg', 'plusi25mg', 'americas', 'superior', 'omega', '3', '250', 'saturated', 'fat', 'less', 'large', 'eggs', 'than', 'regular', 'a', 'grade', 'egg', 'per', 'wuamon', 'icts', 'fon', 'chclesten', 'content', 'sel', '24', 'eggs', 'fed', 'vegetarian', 'hens', 'usda', 'keep', 'refrigerated', 'bandsparl', 'a', 'or', 'below', '45f', 'at', 'most', 'gde', 'trusted', 'wt', '15', 'oz', '3', 'lbsi', '1301', 'net', 'american', 'shofters', 'atons', 'torc', 's']]

您可以指定要在這些圖像上執行 ocr 的 url 列表和(可選)要在這些圖像中查找的單詞列表。 它將返回在每個圖像中找到的單詞列表列表。 您還可以可視化 output 並查看每個檢測的帶注釋的邊界框。

暫無
暫無

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

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