Opencv Better Way to find contours for lighter text

asked 2018-11-04 11:18:46 -0600

George James gravatar image

I am trying to extract text from ID cards, I want to identify all the text areas, crop it and pass it into tesseract. Below is the code I now have.

class Transformer(object):
    def __init__(self):
        super().__init__()

    def __call__(self, gray, rgba):
        ret, image_binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

        kernel = np.ones((3, 3), np.uint8)
        image_closed = cv2.morphologyEx(image_binary, cv2.MORPH_CLOSE, kernel)
        image_open = cv2.morphologyEx(image_closed, cv2.MORPH_OPEN, kernel)

        kernel = np.ones((2, 2), np.uint8)
        image_dilate = cv2.erode(image_open, kernel, 5)

        image_not = cv2.bitwise_not(image_dilate)
        image_not = cv2.adaptiveThreshold(image_not, 255, cv2.ADAPTIVE_THRESH_MEAN_C,  cv2.THRESH_BINARY, 15, -2)

        image_dilate = cv2.dilate(image_not, np.ones((2, 1)))
        image_dilate = cv2.dilate(image_dilate, np.ones((1, 15)))


        image, contours, heirarchy = cv2.findContours(image_dilate, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        for contour in contours:
            x, y, w, h = cv2.boundingRect(contour)
            # if w > 30 and h > 10:
            cv2.rectangle(rgba, (x, y), (x + w, y + h), (0, 255, 0), 1)

        return image_dilate, rgbamage_dilate, rgba

This transforms image to look like this

image description

This works ok, but I will also like to capture the arabic text and cropped out regions, which is lighter. How do I achieve this.

edit retag flag offensive close merge delete

Comments

You must have this Arabic.traineddata

supra56 gravatar imagesupra56 ( 2018-11-04 20:44:12 -0600 )edit

@supra56, thanks, what I want now is not the conversion to Arabic, what I want is capturing the data in a way that makes sense. The arabic text is currently not properly highlighted and I want it to be

George James gravatar imageGeorge James ( 2018-11-04 23:26:48 -0600 )edit