python - OCR : check if letter is in (string) of image (Opencv, Python,超正方体)

标签 python opencv tesseract

这是一个非常棘手的问题。

我正在使用以下代码来检测手写图像中的文本。我不希望它识别字符,在这种情况下,它只会在他找到的每个字符/单词周围创建一个边界框。

o1

这是代码:

import cv2
import tesserocr as tr
from PIL import Image
import numpy as np

img = cv2.imread('1.png')

idx = 0

# since tesserocr accepts PIL images, converting opencv image to pil
pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

# initialize api
api = tr.PyTessBaseAPI()

alphabet_min = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
                'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

alphabet_max = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
                'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

try:
    api.SetImage(pil_img)
    boxes = api.GetComponentImages(tr.RIL.SYMBOL, True)
    text = api.GetUTF8Text()
    print(text)

    for (im, box, _, _) in boxes:
        x, y, w, h = box['x'], box['y'], box['w'], box['h']
        #print(box)

        #if w < 200:
            #cv2.rectangle(img, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=1)

    for letter in text:
        if letter in alphabet_min:
            cv2.rectangle(img, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=-1)

    idx += 1

finally:
    api.End()

cv2.imshow('2', img)
cv2.waitKey(0)

如果你仔细观察,你会看到一个print(text)。这个打印他在图像中找到的文本。但是,作为手工制作的文本,它几乎无法恢复:

Ca) a1 1. s 5305 Fm“. 4 54 0235 166 firm 4 §24630455

但即使是这个输出也能以某种方式帮助我。

在代码上面一点,我做了一个函数:

for letter in text:
        if letter in alphabet_min:
            cv2.rectangle(img, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=-1) 

应该获取print(text) 输出中的每个字符,并将其与代码中的alphabet_min 列表进行比较。 但它不想工作。我不知道为什么?

这样做的目的是:如果你在 print(text) 中找到一个字母,并且这等于 alphabet_min 列表中的一个,则将其覆盖在图像中(使用 cv2.rectangle) 在图像中使用它的对应项。

有什么建议吗?

源图是这样的:

src

编辑

在条件下打印(True),它显示 6 True。这意味着它找到了这封信。唯一的问题是它不会为它们创建边界框。

最佳答案

解决了...

所以,这是新代码:

import cv2
import tesserocr as tr
from PIL import Image
import numpy as np

img = cv2.imread('1.png')

idx = 0

# since tesserocr accepts PIL images, converting opencv image to pil
pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

# initialize api
api = tr.PyTessBaseAPI()

alphabet_min = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
                'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

alphabet_max = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
                'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

t = 0
try:
    api.SetImage(pil_img)
    boxes = api.GetComponentImages(tr.RIL.SYMBOL, True)
    text = api.GetUTF8Text()

    for (im, box, _, _) in boxes:
        x, y, w, h = box['x'], box['y'], box['w'], box['h']
        cv2.rectangle(img, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=1)

        print(text[t])

        for letter in alphabet_min:
            if text[t] in letter:
                cv2.rectangle(img, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=-1)

        t += 1
        cv2.imshow('2', img)
        cv2.waitKey(0)


    idx += 1


finally:
    api.End()

关于python - OCR : check if letter is in (string) of image (Opencv, Python,超正方体),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47395299/

相关文章:

python - 将索引转换为日期时间索引

c++ - 验证基本矩阵

python - "Adding"Tesseract eng.traineddata 的新字体

python - 如何将日期时间列四舍五入到最近的一刻钟

javascript - 从 JS 调用 python 函数

python - 通过PIP在opencv-python中没有最新的(4.3.0。)openCV库

c++ - 错误 LNK2019 无法解析的外部符号 Tesseract OCR C++ 使用 VS 2015

java - tesseract 不使用 java 读取具有背景图像的文本和数字

python - 试图创建一个解决迷宫的程序,但它卡在了特定的路径上

opencv - 是否可以检测到不同类型的梯形