2

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:\Users\DELL\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:\Users\DELL\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.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?

emp
  • 602
  • 3
  • 11
  • 22

3 Answers3

3

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.

Shreyas
  • 86
  • 7
  • I got an error 'AttributeError: module 'os' has no attribute 'abspath' ' but I already did 'from os.path import abspath' – emp Sep 20 '18 at 05:20
  • @Jamiewp Sorry it was a type `os.path.abspath(f)` – Shreyas Sep 20 '18 at 05:26
  • I changed but the error changed to be 'FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\DELL\\Desktop\\Senior\\video\\bg\\use this\\frame0.png'' edit: image should be under 'use this/test1/frame0.png – emp Sep 20 '18 at 05:28
  • I changed the folder name to use-this but it still error. As I said the image should be found under test1 which are in use-this – emp Sep 20 '18 at 05:46
  • 1
    **OOPS** `os.path.abspath(..)` [Doc reference](https://docs.python.org/2/library/os.path.html#os.path.abspath) function internally performs a join on the argument that we provide. And here you trying to list all files in `pathOut` and hence we will need to join `pathOut + f` and then find the absolute path to your file. @Jamiewp – Shreyas Sep 20 '18 at 05:47
  • Added `file_path = os.path.abspath(pathOut + f)` and it works! Thank you – emp Sep 20 '18 at 05:58
0

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"
Viktor Ilienko
  • 817
  • 8
  • 15
0
X_train, y_train, train_labels =load_data(TRAIN_DIR, IMG_SIZE)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Reham Ali
  • 1
  • 1