1

I'm writing the code to process 6 images in the folder 'test_images'. Their name has been stored in the TestImagesArray.

So print(TestImagesArray) gives me:

['solidYellowCurve.jpg', 'whiteCarLaneSwitch.jpg', 'solidWhiteCurve.jpg', 'solidYellowLeft.jpg', 'solidWhiteRight.jpg', 'solidYellowCurve2.jpg']

In the for loop, I try to read from the first to the 6th

Total 6 images

for i in range(0,2):
    # Add the folder's name before image name
    location = 'test_images/'+TestImagesArray[i];
    image = mpimg.imread(location)
    gray = grayscale(image)
    # Assumption: one kernal size for all images
    ...
    print(i)

When i=0, it works. But when i=1, it returns error.

Traceback (most recent call last): File "p1.py", line 121, in image = mpimg.imread(location) File "/home/cocadas/miniconda3/envs/carnd-term1/lib/python3.5/site-packages/matplotlib/image.py", line 1227, in imread im = pilread(fname) File "/home/cocadas/miniconda3/envs/carnd-term1/lib/python3.5/site-packages/matplotlib/image.py", line 1205, in pilread with Image.open(fname) as image: File "/home/cocadas/miniconda3/envs/carnd-term1/lib/python3.5/site-packages/PIL/Image.py", line 2410, in open fp = builtins.open(filename, "rb") FileNotFoundError: [Errno 2] No such file or directory: 'test_images/whiteCarLaneSwitch.jpg'

In the same directory that runs 'python p1.py'. Verify the location of the file by

cocadas@cocadas-ThinkPad-W540:~/Workspace/carnd/CarND-LaneDection-HT$ ls -al test_images/whiteCarLaneSwitch.jpg
-rw-rw-r-- 1 cocadas cocadas 60676 May 30 13:05 test_images/whiteCarLaneSwitch.jpg

So this tells me that the file is there. It doesn't make sense to me. Do I miss anything?

5
  • 2
    Seems pretty clear-cut: the file isn't there. Are you certain that you got the names right? Commented May 31, 2017 at 20:15
  • 1
    I believe that Python might also be searching for test_images/ relative to where you executed the script, rather than where the script actually is. If you know that the files exist, verify this isn't happening either. Commented May 31, 2017 at 20:34
  • In the same directory that runs 'python p1.py'. Verify the location of the file by cocadas@cocadas-ThinkPad-W540:~/Workspace/carnd/CarND-LaneDection-HT$ ls -al test_images/whiteCarLaneSwitch.jpg -rw-rw-r-- 1 cocadas cocadas 60676 May 30 13:05 test_images/whiteCarLaneSwitch.jpg Commented Jun 1, 2017 at 15:09
  • 1
    A script may reference a different directory that where the script resides. Use full path names. You can add print os.getcwd() to get where the script's current working directory Commented Jun 1, 2017 at 15:15
  • @dawg, thanks for your suggestion. It turns out that one of the imwrite() operation change the directory as a result it can't find the file. Commented Jun 1, 2017 at 15:33

1 Answer 1

1

Look at the end of the stack trace: FileNotFoundError: [Errno 2] No such file or directory: 'test_images/whiteCarLaneSwitch.jpg'. This is Python telling you it can't find the file. Make sure the file is actually on your filesystem, and triple check you've spelled the filename correctly (both in your code and on your filesystem).

You could also (purposely or accidentally) be switching working directories, causing the relative path to fail.

The easiest fix would probably just be to use the full path. In your case (judging by your ls output):

import os
home = os.path.expanduser("~")
workDir = os.path.join(home,'Workspace/carnd/CarND-LaneDection-HT/')
for i in range(0,6):
    location =  = workDir+'test_images/'+TestImagesArray[i];
    image = mpimg.imread(imageLocation)
Sign up to request clarification or add additional context in comments.

5 Comments

In the same directory that runs 'python p1.py'. Verify the location of the file by cocadas@cocadas-ThinkPad-W540:~/Workspace/carnd/CarND-LaneDection-HT$ ls -al test_images/whiteCarLaneSwitch.jpg -rw-rw-r-- 1 cocadas cocadas 60676 May 30 13:05 test_images/whiteCarLaneSwitch.jpg
Are you switching working directories during the computation? A full path might be safer.
I confirmed that imwrite() changes the working directory. Thanks! How to use absolute directory in imread(), do you know by any chance?
I'm not entirely sure. It's pretty unintuitive that it switches directories. I feel as if though full paths are safer 90% of the time. It's good to get into the habit of using them where you can!
thanks! Agree. I updated the code in the answer to reflect what I have heard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.