Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call ocropy scripts from other python code #284

Open
bugsyb opened this issue Dec 24, 2017 · 2 comments
Open

Call ocropy scripts from other python code #284

bugsyb opened this issue Dec 24, 2017 · 2 comments

Comments

@bugsyb
Copy link

bugsyb commented Dec 24, 2017

Hi All,

What is suggested way to run ocropy scripts from other python code?
The most recommended way is to us "import " but:

  • that searches for .py file,
  • expects to process module and not script (as is ocropy built)

Could scripts be wrapped into standard:
if name == "main":
main()
sys.exit(0)

Some further minor teaks to map args.* to other variables in main code.

The idea is to avoid duplication of code included in scripts, i.e threshold calculation, etc. and have it re-usable.

The other methods considered for today is to use:

  • execfile(),
  • putting init.py file in folder (not sure if that would work at all correctly),

Other options I've looked at are listed here:
https://stackoverflow.com/questions/2349991/python-how-to-import-other-python-files/20749411#20749411

Another option could be one of demonstrated approaches (0,1 or 2):
https://m8051.blogspot.com/2014/02/python-tricks-running-python-scripts.html

        if bDirect == 0:
            sRunPath = sPath
            with open(path.join(sPath,sFile)) as f:
                exec(compile(f.read(), "temp.p", 'exec'), globals())
        elif bDirect == 1:
            tmp = os.getcwd()
            os.chdir(sPath)
            os.system("python " + sFile)
            os.chdir(tmp)
        elif bDirect == 2:
            sys.path.insert(0,sPath)
            tmp = importlib.import_module(sFile.split(".py")[0])
            tmp.run()
sys.path.remove(sPath)
@mittagessen
Copy link

You can't really use ocropy as a library right now because data loading and actual functionality is so intermeshed in the main scripts and most of them also implicitely assume a certain layout of the data on the file system. The last time I tried I just called them using the subprocess module but then went on to refactor everything just because ensuring that all the pseg,nrm,.... files are in the correct location is a pain.

f you want a clean-ish API with mostly the same functionality you might want to look at kraken. The documentation is admittedly lacking but just throwing a PIL image at most functions is a lot nicer than working around each script's peculiarities.

@ChillarAnand
Copy link
Contributor

I have written a simple wrapper function to run ocoropy pipeline. If you are working with lots of images, something like this might be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants