2
$\begingroup$

I'm trying to extract some particular information from the image(png).

I tried to extract the text using the below code

import cv2
import pytesseract
import os
from PIL import Image
import sys

def get_string(img_path):
    # Read image with opencv
    img = cv2.imread(img_path)

    # Convert to gray
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Apply dilation and erosion to remove some noise
    kernel = np.ones((1, 1), np.uint8)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)

    # Write the image after apply opencv to do some ...
    cv2.imwrite("thres.png", img)
    # Recognize text with tesseract for python
    result = pytesseract.image_to_string(Image.open("invoice.png"))
    os.remove("invoice.png")

    return result

if __name__ == '__main__':
    from sys import argv

    if len(argv)<2:
        print("Usage: python image-to-text.py relative-filepath")
    else:
        print('--- Start recognize text from image ---')
        for i in range(1,len(argv)):
            print(argv[i])
            print(get_string(argv[i]))
            print()
            print()

        print('------ Done -------')

But I want to extract data from particular fields.

Such as

 a) INVOICE NO.
 b) CUSTOMER NO.
 c) SUBTOTAL
 d) TOTAL
 e) DATE

How can I extract the required information from the below image "invoice"?

PFB

enter image description here

$\endgroup$

2 Answers 2

3
$\begingroup$

If all of your images are similar to this one(or have a small set of possible designs), you can simply reference the location (pixel-wise) on the image where this fields are and slice it.

After slicing you can use any OCR algorithm to extract that data.

If your data has more variation than that, you can use OCR on the entire image, which is usually a slow algorithm.

If you have less than 50 or so images to do that, it is more efficient to do this by hand (not worth writing code)

$\endgroup$
1
$\begingroup$

It's a pretty broad question, but maybe this and to a lesser extent this can help you get started.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.