Reputation: 1
This is the beginning of the code. I am following a face recognition project tutorial using OpenCV and Python. This is meant to take a picture of a face using haar cascade face detection and then storing in a dataset file I have.
import cv2
import os
cam = cv2.VideoCapture(0)
cam.set(3, 640) # set video width
cam.set(4, 480) # set video height
face_detector = cv2.CascadeClassifier('/Users/*********/workspace/Facial Recognition Project/haarcascade_frontalface_default.xml')
# For each person, enter one numeric face id
face_id = input('\n enter user id end press <return> ==> ')
print("\n [INFO] Initializing face capture. Look the camera and wait ...")
# Initialize individual sampling face count
count = 0
while(True):
ret, img = cam.read()
img = cv2.flip(img, -1) # flip video image vertically
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)
count += 1
# Save the captured image into the datasets folder
cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w])
cv2.imshow('image', img)
k = cv2.waitKey(100) & 0xff # Press 'ESC' for exiting video
if k == 27:
break
elif count >= 30: # Take 30 face sample and stop video
break
# Do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cam.release()
cv2.destroyAllWindows()```
This was the error:
cv2.error: OpenCV(4.7.0) /Users/opencv-cn/GHA-OCV-1/_work/opencv-python/opencv-python/opencv/modules/core/src/persistence.cpp:681: error: (-215:Assertion failed) buf in function 'open'
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "/Users/sashuponnaganti/workspace/Facial Recognition Project/01_face_dataset.py", line 9, in face_detector = cv2.CascadeClassifier('/Users/*********/workspace/Facial Recognition Project/haarcascade_frontalface_default.xml') SystemError: <class 'cv2.CascadeClassifier'> returned a result with an exception set
I believe that I am using the cv2.CascadeClassifier function correctly and I know the filepath for the xml file is correct so I am confused why I am getting an error.
Upvotes: 0
Views: 1045
Reputation: 965
I guess you don't need to enforce cv2 to find xml at some particular path... So, try to use
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
insted.
Upvotes: 0