Home > OS >  i was making a camera and i kept getting this error in (python) i have given the code
i was making a camera and i kept getting this error in (python) i have given the code

Time:12-02

from types import FrameType
import cv2

cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades   "haarcascade_frontal_default.xml")
bodie_cascade = cv2.CascadeClassifier(cv2.data.haarcascades   "haarcascade_fullbody.xml")

while True:
    _, Frame = cap.read()
    

    gray = cv2.cvtColor(Frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x, y, width, height) in faces:
        cv2.rectangle(Frame, (x, y), (x   width, y   height), (255, 0 , 0), 3)

    cv2.imshow("Camera", Frame)

    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
Traceback (most recent call last):
  File "c:\Users\RITESH BHAKTA\Desktop\py\secuirity.py", line 15, in <module>
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: OpenCV(4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

[ WARN:0] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

CodePudding user response:

You loaded an invalid path to the haarcascades xml file. Try to use an absolute path: C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml:

face_cascade = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_eye.xml')

You can download the haarcascade files here: https://github.com/opencv/opencv/tree/master/data/haarcascades

CodePudding user response:

Here is how to do it.
In line 4.
In "haarcascade_frontal_default.xml" change the frontal into frontalface
Here is an example:

Wrong:
cv2.data.haarcascades   "haarcascade_frontal_default.xml")

Correct:
cv2.data.haarcascades   "haarcascade_frontalface_default.xml")
  • Related