Error saving an image obtained by Opencv

Asked

Viewed 77 times

-1

Directory where I need to save

  Banco-de-Faces\nome 

Directory where he is saving:

  Banco-de-Faces

My code:

  cv2.imwrite("Banco-de-Faces\\" + nome + str(framesObtidos) + ".png", imagemRosto)

My program creates a folder with the name typed, I have to record these images inside that folder, and the name of each image will be called NomeDigitado + (numero do frame).png.

1 answer

3


The problem is that you are not using a separator between the directory name and the file name. What your code does, is join the nome with the framesObtidos to form a single filename. See the example below:

dir1 = "Dir1/"
dir2 = "Dir2"
arquivo = "arquivo.png"
print(dir1 + dir2 + arquivo)   # Saída: Dir1/Dir2arquivo.png

To fix this problem just add "\\" amid nome and framesObtidos thus:

cv2.imwrite("Banco-de-Faces\\" + nome + "\\" + str(framesObtidos) + ".png", imagemRosto)

One improvement you can make in your code is to use the function os.path.join to merge directories with the filename using a suitable separator for your operating system. This way, you avoid conflicts and also make the code more organized and beautiful. Example:

filename = os.path.join("Banco-de-Faces", nome, str(framesObtidos) + ".png")
cv2.imwrite(filename, imagemRosto)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.