0

When I execute the script below with python3 ocr-test.py, it runs correctly:

from PIL import Image
import pytesseract
# If you don't have tesseract executable in your PATH, include the following:
pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/bin/tesseract'
# Simple image to string
print(pytesseract.image_to_string(Image.open('receipt1.jpg')))

However, when I excute the below script with python3 ocr-test.py process, the process/function does not get called and nothing happens:

from PIL import Image
import pytesseract
def process():
   # If you don't have tesseract executable in your PATH, include the following:
   pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/bin/tesseract'
   # Simple image to string
   print(pytesseract.image_to_string(Image.open('receipt1.jpg')))

Why is this (not) happening?

taylorSeries
  • 505
  • 2
  • 6
  • 18
reallymemorable
  • 882
  • 1
  • 11
  • 28

2 Answers2

3

you need to add

if __name__ == '__main__':
    globals()[sys.argv[1]]()

to the bottom of the file.

as explained here.

no_modules
  • 117
  • 7
Arthur Caccavo
  • 98
  • 1
  • 10
1

Try to add the following code at the very end of your file:

if __name__ == "__main__":
    process()

The reason that process does not execute is because it is probably not called in your file. I have not seen anyone calling a function from command line before, and I do not think it is possible, except for running something like:

python -c "from ocr-test import process;process()" 

but that is not common practice or recommended for that matter. Id stick with the first solution, but its up to you.

no_modules
  • 117
  • 7