Search code examples
pythonocrtesseractpython-tesseract

Cant run the ocr code by itself. I have to run it from the command prompt


I have the code below which if you run it from the command prompt like:

python first_ocr.py --image pyimagesearch_address.png

it runs just fine. but if you run the code itself it gives the error below: usage: first_ocr.py [-h] -i IMAGE first_ocr.py: error: the following arguments are required: -i/–image

How can I pass the image path to this code to be able to run and debug the code.

# USAGE
# python first_ocr.py --image pyimagesearch_address.png
# python first_ocr.py --image steve_jobs.png

# import the necessary packages
import pytesseract
import argparse
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
                help="path to input image to be OCR'd")
args = vars(ap.parse_args())

# load the input image and convert it from BGR to RGB channel
# ordering
image = cv2.imread(args["image"])
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# use Tesseract to OCR the image
text = pytesseract.image_to_string(image)
print(text)

Solution

  • Try this code out.

    import pytesseract
    import cv2
    path = 'pyimagesearch_address.png'
    image = cv2.imread(path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    text = pytesseract.image_to_string(image)
    print(text)