Python and OpenCv implementation for encoding text in the image itself

I have a small python code that detects text from images like:

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) 

Thus, the output is a new image with rectangles above the detected text. I also have a function that encodes text from a static image and shows the encoded result on the console, the function is shown below:

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

So, for example, if I give him an image containing text, he will extract the text, encode it (if we give him the key) and print the encoded line on the console. However, is it possible to encode text on top of the image itself, instead of printing it on the console.

+4
source share
2 answers

, .

, . 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_boxes pytesseract.image_to_data.

, , , , .

+1

( ), , ( ), .

OpenCV PutText:

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

Source: https://habr.com/ru/post/1695409/


All Articles