Python和OpenCv实现在Image Itself

时间:2018-03-27 10:34:52

标签: python opencv tesseract

我在python中有一个小代码,用于检测来自Images的文本:

import cv2


image = cv2.imread("sample.jpg")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # grayscale
_,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV) # threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))  
dilated = cv2.dilate(thresh,kernel,iterations = 13) # dilate
_, contours, hierarchy =  cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) # get contours

# for each contour found, draw a rectangle around it on original image
for contour in contours:
    # get rectangle bounding contour
    [x,y,w,h] = cv2.boundingRect(contour)

    # discard areas that are too large
    if h>300 and w>300:
        continue

    # discard areas that are too small
    if h<40 or w<40:
        continue

    # draw rectangle around contour on original image
    cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)

    # write original image with added contours to disk  
    cv2.imwrite("contoured.jpg", image) 

因此输出是一个新图像,在检测到的文本上有矩形。 我还有一个函数,它对静态图像中的文本进行编码,并将编码结果显示在控制台上,函数如下所示:

from pytesseract import image_to_string


val_1 = sys.argv[1]
text =  image_to_string(Image.open(''+val_1+''))

def encode(key, string):
    encode = []
    for i in xrange(len(string)):
        key_c = key[i % len(key)]
        encoded_c = chr(ord(string[i]) + ord(key_c) % 256)
        encode.append(encoded_c)
    encoded_string = "".join(encode)
    return base64.urlsafe_b64encode(encoded_string)

encry =  encode(key,text)
#print encry

所以对于eg.If我给它一个包含文本的图像,它将提取文本,编码它(如果我们给它一个键)并将编码的字符串打印到控制台上。但是,可以将文本编码在顶部图像本身而不是在控制台上打印。

2 个答案:

答案 0 :(得分:1)

是的,这是可能的。

您需要包含文本的图像以及带有文本的区域坐标。然后你可以使用OpenCV函数putText()

为此,您必须对您的实施应用一些更改。您有两种不同的选择:

  • 在包含文本的每个矩形中执行OCR,因此您应该执行以下操作:

    import cv2
    from pytesseract import image_to_string
    
    
    # ..various image operations..
    
    # for each contour found, draw a rectangle, perform OCR in that rectangle and write the result on the image
    for contour in contours:
        # get rectangle bounding contour
        [x,y,w,h] = cv2.boundingRect(contour)
    
        # discard areas that are too large
        if h>300 and w>300:
            continue
    
        # discard areas that are too small
        if h<40 or w<40:
            continue
    
        # draw rectangle around contour on original image
        cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)
    
        # get the image area with the text
        text_image = image[y:y+h, x:x+w]
        # perform OCR
        text = image_to_string(text_image)
        # encode the text with your function
        encry = encode(key, text)
        # write the encoded text on the image
        cv2.putText(image, encry, (x,y), cv2.FONT_HERSHEY_SIMPLEX, 4, (255,255,255), 2, cv2.LINE_AA)
    
    • 或者,您可以对整个图像执行一次单独的OCR操作,然后分析识别的文本的坐标。您应该使用pytesseract.image_to_boxespytesseract.image_to_data
    • 之类的内容

我想澄清一下,我没有对代码进行测试,因此可能存在一些不准确之处。

答案 1 :(得分:0)

如果您有文本(您这样做)并且您有一个需要去的位置(您这样做),那么在图像上添加文本就不会太困难。

OpenCV提供PutText function:

https://docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html

以下是一个例子:

font = cv2.FONT_HERSHEY_SIMPLEX
2 cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)