0
import cv2
import imageio

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

def detect(frame):
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for(x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        gray_face = gray[y:y+h, x:x+w]
        color_face = frame[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(gray_face, 1.1, 3)
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(color_face, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
    return frame

reader = imageio.get_reader('1.mp4')
fps = reader.get_meta_data()['fps']
writer = imageio.get_writer('output.mp4', fps=fps)
for i, frame in enumerate(reader):
    frame = detect(frame)
    writer.append_data(frame)
    print(i)
writer.close()

the error I received is this;

Traceback (most recent call last):
  File "C:/Users/Mustafa Koca/PycharmProjects/mustafakoca/face_det.py", line 23, in <module>
    frame = detect(frame)
  File "C:/Users/Mustafa Koca/PycharmProjects/mustafakoca/face_det.py", line 9, in detect
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Mustafa Koca
  • 5
  • 1
  • 6
  • 1
    Does this answer your question? [Face detection throws error: !empty() in function cv::CascadeClassifier::detectMultiScale](https://stackoverflow.com/questions/30857908/face-detection-throws-error-empty-in-function-cvcascadeclassifierdetectm) – Christoph Rackwitz Sep 10 '22 at 10:41

1 Answers1

4

xml files are not in current folder but in some folder of cv2 module and you have to use full path to load them.

Lucky there is special variabel with folder name - cv2.data.haarcascades

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')

To make sure you can use os.path.join() instead of +

face_cascade = cv2.CascadeClassifier(os.path.join(cv2.data.haarcascades, 'haarcascade_frontalface_default.xml'))
eye_cascade = cv2.CascadeClassifier(os.path.join(cv2.data.haarcascades, 'haarcascade_eye.xml'))

If xml files are loaded then this should gives False

print(face_cascade.empty())
print(eye_cascade.empty())
furas
  • 134,197
  • 12
  • 106
  • 148
  • thank you, it works :) can you explain exactly the goal here? – Mustafa Koca Dec 04 '19 at 15:57
  • as I wrote in first line in answer.: when you use only filename then it searchs this file in your folder with code. But file is in different folder - see `print(cv2.data.haarcascades)`. You can create own file `.xml` or download it from internet and put in folder with your code and then you can use it instead file which is avaliable with module `cv2` and you don't have to use full path. – furas Dec 04 '19 at 15:57
  • you can use `print(cv2.data.haarcascades)` whereevere you want - but after `import cv2`. You don't have to even use it in your code. You can open Python shell and run it `import cv2`, `print(cv2.data.haarcascades)` to check it only once. – furas Dec 04 '19 at 16:03
  • thanks in addition to the code worked, so I took care of the problem, but now the numbers are going to end when it does not give a result again – Mustafa Koca Dec 04 '19 at 16:07
  • in SO is rure - if you have new problem then create new question on new page. This way you will have also more space to describe new problem. – furas Dec 04 '19 at 16:14
  • BTW: `rectangle()` creates new image which you have to assign to variable - `frame = cv2.rectangle(...)` - and save this new image. – furas Dec 04 '19 at 16:18
  • so it gave a different error, but I took care of it :) so how do I transfer it on webcam – Mustafa Koca Dec 04 '19 at 16:26
  • `cam = cv2.VideoCapture(0)` `frame = cam.read()` for built-in cam - you should see it in any tutorial for `cv2`. For other cam you may need other number or url like `cam = cv2.VideoCapture('rtsp://streaming1.osu.edu/media2/ufsap/ufsap.mov')` BTW: it should work also with file `cam = cv2.VideoCapture("1.mp4")` – furas Dec 04 '19 at 17:50
  • `cv2` has also `cv2.VideoWriter('output.mp4')` – furas Dec 04 '19 at 17:57