Reputation: 111
images_per_class = 80
fixed_size = tuple((500, 500))
train_path = "dataset/train"
train_labels = os.listdir(train_path)
for training_name in train_labels:
dir = os.path.join(train_path, training_name)
current_label = training_name
for x in range(1,images_per_class+1):
# get the image file name
file = dir + "/" + str(x) + ".jpg"
# read the image and resize it to a fixed-size
image = cv2.imread(file)
image = cv2.resize(image, fixed_size)
when i run this code it appeare this error
error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4052: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
and this warning [ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('dataset/train\Apple/1.jpg'): can't open/read file: check file path/integrity
i dont have probleme with installation of opencv because i use it before andwith another code it fonctionne any help please
Upvotes: 10
Views: 93706
Reputation: 186
In my case, it didn't like the '~' symbol used in Mac to route to Documents with '~/Documents' so I changed it to '../../../Documents'
Upvotes: 1
Reputation: 11
You can add below lines before initializing file_name:
import os
os.chdir(folder_address_which_contain_your_file)
it worked for me
Upvotes: 1
Reputation: 1
Check if such a file really exists. If it gives an error, put it at the same address as the image or the file, for example: the location of your file is C:\Desktop with this name polarbear.jpg. The location of the file where you write the code is C:\Desktop\Files\Python Let it be called kod.py. The code will give an error because your file and your code are not at the same address. Try putting the file and the code at the same address. My file: C:\Desktop\Files\Python\polarbear.jpg My code: C:\Desktop\Files\Python\code.py if you do this solution do not write all of the path just the file name. Example: "polarbear.jpg" Same thing happend to me also I looked for every solution and I couldnt fix it when I was messing around I found the problem. It realy works
Upvotes: 0
Reputation: 44
in my case it was the filename extension that caused problems. Check whether your png/img/bmp is a png/img/bmp or something else ;)
Upvotes: 0
Reputation: 3941
Make sure that the file name does not contain any special characters, OpenCV seems to be quite sensitive here.
For me the problem was that it contained some umlauts (ä, ö, ü).
Upvotes: 4
Reputation: 482
for me, my file path have '\n' in the end. use os.path.exist check, not exist
Upvotes: 0
Reputation: 919
Recently I have faced the same issue, the problem was the file was not exist there. I realise file names could also be a problem.
Upvotes: 0
Reputation: 493
Dears, for me the issue was related to filename extension. I put the extension .JPEG whereas the actual extension was .jpg. Correcting the extension cleared the warning. thanks
Upvotes: 8
Reputation: 138
file = dir + "/" + str(x) + ".jpg"
try to replace this line with :
file = dir + "\" + str(x) + ".jpg"
the / not correct
the correct is \
Upvotes: 0