简体   繁体   English

OCR:检查字母是否在图像的(字符串)中(Opencv,Python,Tesseract)

[英]OCR: check if letter is in (string) of image (Opencv, Python, Tesseract)

This is a really tricky question. 这是一个非常棘手的问题。

I am using the following code to detect text in hand-written images. 我使用以下代码来检测手写图像中的文本。 I don't want it to recognize the char, in this case it only create a bounding box around each character/word he find. 我不希望它识别char,在这种情况下,它只在他找到的每个字符/单词周围创建一个边界框。

O1

This is the code: 这是代码:

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)

If you take a closer look, you can see a print(text) . 如果仔细观察,可以看到print(text) This one print the text he find in the image. 这个打印出他在图像中找到的文字。 But, being this a hand-made text, it recovers near nothing: 但是,作为一个手工制作的文本,它几乎没有恢复:

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

But even this output can help me in some way. 但即使这个输出也可以在某种程度上帮助我。

A little above in the code, I made a function: 在代码中稍微高一点,我做了一个函数:

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

This should take every char in the print(text) output and compare it to the alphabet_min list in the code. 应该print(text)输出中的每个字符,并将其与代码中的alphabet_min列表进行比较。 But it don't wanna work. 但它不想工作。 I don't know why ? 我不知道为什么?

The purpose of this is: if you find a letter in print(text) and this is equal to one of alphabet_min list, then cover it in the image (using cv2.rectangle ) using its correspondent in the image. 这样做的目的是:如果您在print(text)找到一个字母,并且这等于alphabet_min列表中的一个,则使用图像中的对应方将其覆盖在图像中(使用cv2.rectangle )。

Any suggestion ? 有什么建议吗?

Source image is this: 源图像是这样的:

SRC

EDIT 编辑

Doing a print(True) under the condition, it show 6 True . 在条件下进行打印(True),显示6 True This means it finds the letter. 这意味着它找到了这封信。 The only problem is it don't create the bounding box for them.. 唯一的问题是它不为它们创建边界框..

Solved it... 解决了......

So, this is the new code: 所以,这是新代码:

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()

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

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