-1

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

boyaronur
  • 521
  • 6
  • 18
  • What exactly is unclear from stack trace you have received? It sounds quite obvious to me, `.DS` is unconvertable to int. There is only one place in your code where you do conversion, last line of `for` loop. Note that filenames prepended with dots are classified as _hidden_ files in most `*nix` systems. – Łukasz Rogalski Jul 27 '17 at 11:51
  • Thanks for your reply, I need it as an integer because faceRecognition.train function needs it like that. So I have to convert it to integer – boyaronur Jul 27 '17 at 11:57

1 Answers1

0

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]))
ZdaR
  • 22,343
  • 7
  • 66
  • 87