0

I am loading a cascade classifier from a file in OpenCV using python. Since the CascadeClassifier() method requires the filename to be the absolute filename, I have to load the absolute filename.

However, I am deploying and using it on Heroku and I can't seem to get the absolute path of the file. I've tried using os.getcwd() + '\cascader_file.xml', but that still does not work.

I would like to know how to load the classifier on the Heroku deployment

Ace Falobi
  • 741
  • 1
  • 8
  • 25

2 Answers2

0

I figured it out. All I needed to do was use the os.path.abspath() method to convert the relative path to an absolute path

Ace Falobi
  • 741
  • 1
  • 8
  • 25
0

if the file you are looking for is by the side of your script, you can get the directory of the script and then locate the target resource/conf file. That's better practice than using os.getcwd since your current directory maybe not the same directory of your script.

for example, your directory is like this:

-- my_project
  |-- main.py
  |-- cascader_file.xml

to locate cascader_file.xml in script main.py:

d = os.path.dirname(os.path.abspath(__file__))  # your script's dir, my_project
filepath = os.path.join(d, "cascader_file.xml")
filepath = os.path.abspath(filepath) # make it an absolute path

btw, using os.path.join is more secure and more platform compatible than directly concat path parts.

Yun Luo
  • 1,506
  • 1
  • 9
  • 10