boyaronur boyaronur - 2 years ago 133
Python Question

Python OpenCV Invalid Literal Error

I have

ValueError: invalid literal for int() with base 10: '.DS'
error. I am looking for solutions for a day but I couldn't fix it. My directory is not empty. I am trying to train a facerecognizer using OpenCV. I am trying to get photos of faces from my
train
directory but I cannot.

My code :

images = []
labels = []
for file in os.listdir('train/'):
image = cv2.imread('train/'+file, 0)
images.append(image)
labels.append(int(file.split('_')[0]))


Photos in train are named is 1_001.pmg, 1_002.pmg and so on. I am looking for your help for my silly problem. Thanks!

ps. this code is from the answer of @Fabian here how to notify user if there are common faces detected between two directories with python and opencv

Answer Source

The problem is with os.listdir('train/'), as it returns a list of all the files in given directory which may be hidden to you, and '.DS_Store' is also among those files, you need to check if the string in the returned end with .png before passing them to cv::imread.

images = []
labels = []
for file_name in os.listdir('train/'):
    if file_name.endswith(".png"):
        image = cv2.imread('train/'+file_name, 0)
        images.append(image)
        labels.append(int(file_name.split('_')[0]))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download