Problem on adressing nested relative directories in server

Viewed 185

I want to run some image processing code on a Linux server.

The code is located in

/q/w/e/r/t

The images are located in

/abc/d/e/f/g # the images are in the g folder

This is the code I'm using:

path = "/abc/d/e/f"

new_path = os.path.join(path, 'g', '001.png')

img1= cv2.imread(new_path)

However, this is the error I'm getting:

[ WARN:0@"a number"] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('/abc/d/e/f/g/001.png'): can't open/read file: check file path/integrity

How can I solve the problem?

4 Answers

You can try the following:

  1. Check whether you have permission to read and write the file. You can simply go to directory that contains all subdirectories and give read and write access to all subdirectories to all users.
  2. Check whether you can access the directory stored in variable path.
  3. Check the spelling of your pathname
  4. Use try and catch to open the files - this way you see whether the problem is only one file or all of them which narrows the source of bug down.
  5. Check the start of your path. Is that correct and does your program know the path?
  6. If you can, download an image and have a look at it. Is it corrupted?

The OpenCV error sounds like certain library paths are not set. Either OpenCV is not properly installed or the path to the library not set. Might be worth to reinstall and set the paths properly.

Check that...

  • the path is indeed correct, that is, /abc/d/e/f/g/001.png exists
  • the process executing the python script has the necessary permissions to read this file
  • the file isn't corrupt (copy it to your local computer and open it in a browser/image viewer) (After reading the opencv source that's probably not it)

You can also "walk" the filesystem so you get all files below a certain directory:

def get_files(path=os.getcwd()):
    files = []
    if not path.endswith(os.sep):
        path += os.sep
    for root, dirs, files in os.walk(path)
        for file in files:
            filepath = os.path.join(root, file)
            files.append(filepath)
    return files
            
files = get_files("/abc/d/e/f")

Also, check your file and path. On the linux system's shell (I assume it is Bash):

$ ls -l <file> # check if it exists and whether the permissions are correct
$ file <file>  # check the filetype (should hint at being an image)
Related