Home > Back-end >  Can't run code in VSCode but can outside it
Can't run code in VSCode but can outside it

Time:10-12

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:\a\opencv-python\opencv-python\opencv\modules\core\src\persistence.cpp (505) cv::FileStorage::Impl::open Can't open file: 'haarcascade_frontalface_default.xml' in read mode
Traceback (most recent call last):
  File "c:\Users\zyzzc\Desktop\Programação\Python\Detetor de cara\Detetor_Cara.py", line 14, in <module>
    coordenadas_cara = data_cara.detectMultiScale(imagem_cinza)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

CodePudding user response:

Easiest way to solve it is to use full path for file name:

data_cara = cv2.CascadeClassifier('c:\\Users\\zyzzc\\Desktop\\Programação\\Python\\Detetor de cara\\haarcascade_frontalface_default.xml')

CodePudding user response:

data_cara = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

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

  • Related