I have the following code I need to files from the folder to be read by opencv.
JavaScript
x
8
1
filename = 'C:/Users/test_image'
2
files = filename
3
for file in files:
4
orig = cv2.imread(file)
5
image = image_utils.load_img(file, target_size=(224,224))
6
image = image_utils.img_to_array(image)
7
8
I am retrieving the below error.
JavaScript
1
10
10
1
test_imagenet.py:26: in <module>
2
image = image_utils.load_img(file, target_size=(224, 224))
3
miniconda3envstensorflowlibsite-packageskeras_preprocessingimageutils.py:110: in .
4
load_img
5
img = pil_image.open(path)
6
miniconda3envstensorflowlibsite-packagesPILImage.py:2809: in open .
7
fp = builtins.open(filename, "rb")
8
E FileNotFoundError: [Errno 2] No such file or directory: 'C'
9
collected 0 items / 1 error
10
Help is highly appreciated. Thanks
Advertisement
Answer
You’re iterating over a string, so it will only get the first character. You need to list the contents of the directory. Use os.listdir like this.
JavaScript
1
8
1
import os
2
filename = 'C:/Users/test_image'
3
files = os.listdir(filename)
4
for file in files:
5
orig = cv2.imread(file)
6
image = image_utils.load_img(file, target_size=(224,224))
7
image = image_utils.img_to_array(image)
8