FileNotFoundError: [Errno 2] No such file or directory: 'frame0.png' but actually has

Question:

I have a similar problem to many post about the path problem but I cannot find any solution to fix my problem

So first, I have a function where I create a directory which will store all extracted frames from video

def extract_frame(video,folder):

    os.mkdir(folder)
    vidcap = cv2.VideoCapture(video)
    success,image = vidcap.read()
    fps = vidcap.get(cv2.CAP_PROP_FPS)
    count = 0
    success = True
    while success:  #os.path.join(pathOut,(name+'.png'))
      cv2.imwrite(os.path.join(folder,"frame%d.png" % count), image)         
      success,image = vidcap.read()
      print('Read a new frame: ', success) 
      count += 1

which work pretty well and I want all of the frame to be processed so I wrote

def rm_green(pathOut):   

    for f in os.listdir(pathOut):   
        if f[-3:] == "png":         
            name, ext = os.path.splitext(f)
            im = Image.open(f)
            im = im.convert('RGBA')
            .
            .
            . ## and some other line of code blah blah

then I finally call the function :

extract_frame('vid.mp4', 'test1')
pathIn='./test1/'
rm_green(pathIn)

From here the function extract_frame() work well which it create a folder named ‘test1’ and there are all frames in it. But there is an error

File "C:UsersDELLAnaconda3libsite-packagesspyderutilssitesitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:UsersDELLAnaconda3libsite-packagesspyderutilssitesitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/DELL/Desktop/Senior/video/bg/use this/extract-remove-green-combinevid.py", line 113, in <module>
rm_green(pathIn)

File "C:/Users/DELL/Desktop/Senior/video/bg/use this/extract-remove-green-combinevid.py", line 61, in rm_green
im = Image.open(f)

FileNotFoundError: [Errno 2] No such file or directory: 'frame0.png'

which I don’t know why this happen since there are frames in folder test1. Are there any thing wrong about how I wrote a path? How can it happen since it read ‘frame0.png’ which are in the test1 folder? Or this error is related to the Image.open(f) from PIL library?

Thank you

EDIT:
os.listdir()
Code are from py file named extract-remove-green.. >> Is this os.listdir() work right?

Asked By: emp

||

Answers:

I see that you are trying to use f directly from loop variable. But this will be just a file name rather than a path to file. You might have to do os.abspath(f) to get a complete path to your file and then run the required operation on it.

for f in os.listdir(pathOut):
    file_path = os.path.abspath(os.path.join(pathOut, f))
    if f[-3:] == "png":         
        name, ext = os.path.splitext(f)
        im = Image.open(file_path)
        im = im.convert('RGBA')

Hope this helps you. Thanks.

Answered By: Shreyas

Here is a tip, how to get a full path to adirectory of a file:

import path
script_dir = path.dirname(path.abspath(__file__))

Then you can use join or concatination to get a full path to your file or subdirectory:

file_path = script_dir.join("\the_name_of_file_or_directory") 
# file_path  = script_dir + "\the_name_of_file_or_directory"
Answered By: Viktor Ilyenko
X_train, y_train, train_labels =load_data(TRAIN_DIR, IMG_SIZE)
Answered By: Reham Ali