import cv2
data_cara = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
"""imagem = cv2.imread('cara.jpg')"""
camera = cv2.VideoCapture(0)
while True:
leitura_frame_sucesso, frame = camera.read()
frame = cv2.flip(frame, 1)
imagem_cinza = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
coordenadas_cara = data_cara.detectMultiScale(imagem_cinza)
for (x, y, w, h) in coordenadas_cara:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 4)
cv2.imshow('Detetor de Face', frame)
tecla = cv2.waitKey(1)
if tecla == 81 or tecla == 113 or tecla == 27:
break
camera.release()
This is my code, I can run it outside vscode but can’t through vscode, this are the erros I get:
[ERROR:[email protected]] global D:aopencv-pythonopencv-pythonopencvmodulescoresrcpersistence.cpp (505) cv::FileStorage::Impl::open Can't open file: 'haarcascade_frontalface_default.xml' in read mode
Traceback (most recent call last):
File "c:UserszyzzcDesktopProgramaçãoPythonDetetor de caraDetetor_Cara.py", line 14, in <module>
coordenadas_cara = data_cara.detectMultiScale(imagem_cinza)
cv2.error: OpenCV(4.6.0) D:aopencv-pythonopencv-pythonopencvmodulesobjdetectsrccascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
2
Answers
Easiest way to solve it is to use full path for file name:
If you do not add a path to the referenced file, it is in the vscode workspace by default, so please ensure that the file exists in the workspace