1

This is my first attempt to detect faces and eyes in OpenCV 3.1. Here is my code:

import cv2 as cv
import numpy as np

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

cam = cv.VideoCapture(0)

while True:
    tf, img = cam.read()
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:
        img = cv.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

        roi_gray = gray[y:y + h, x:x + w]
        roi_color = img[y:y + h, x:x + w]
        eyes = eye_cascade.detectMultiScalenter code heree(roi_gray)

        for (ex, ey, ew, eh) in eyes:
            cv.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)

        print(tf)
        cv.imshow('my cam', img)
        key = cv.waitKey(1)
        if key  == 27:
            break
cam.release()
cv.destroyAllWindows()

And I got this error:

OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file D:\Build\OpenCV\opencv-3.1.0\modules\objdetect\src\cascadedetect.cpp, line 1639
Traceback (most recent call last):
  File "C:/Users/hedon/PycharmProjects/deneme_1.1/ilk.py", line 13, in <module>
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: D:\Build\OpenCV\opencv-3.1.0\modules\objdetect\src\cascadedetect.cpp:1639: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale

Can anybody told where is my mistake? I've also tried:

gray = cv.cvtColor(img, cv.COLOR_BAYER_GR2GRAY) as PyCharm suggested. Same error:

OpenCV Error: Assertion failed (scn == 1 && dcn == 1) in cv::demosaicing, file D:\Build\OpenCV\opencv-3.1.0\modules\imgproc\src\demosaicing.cpp, line 1630
Traceback (most recent call last):
  File "C:/Users/hedon/PycharmProjects/deneme_1.1/ilk.py", line 11, in <module>
    gray = cv.cvtColor(img, cv.COLOR_BAYER_BG2GRAY)
cv2.error: D:\Build\OpenCV\opencv-3.1.0\modules\imgproc\src\demosaicing.cpp:1630: error: (-215) scn == 1 && dcn == 1 in function cv::demosaicing
limbo
  • 684
  • 9
  • 18
Burak
  • 29
  • 1
  • 9
  • Please edit your title. Titles that read like: "so and so doesn't work", are inappropriate titles. They are ambiguous and give no insight into what your problem is. Always try to ask yourself before creating your title: **"does this summarize my problem in a clear and concise way?"** – Christian Dean Aug 30 '16 at 17:05
  • The first error is because you didn't load correctly the classifier. Check the path to the xml files. The second error is due to the _non-sense_ conversion you're trying to do. And they are two really different errors, and **not** the "same error"! – Miki Aug 30 '16 at 17:11
  • @Miki Ooops, I just confused. They're completely different errors. Thanks. I copied xml files to the same folder with .py file. Isn't it OK? – Burak Aug 30 '16 at 17:27

2 Answers2

1

OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale

is telling you that the classifier is empty, because you didn't load the xml files correctly.

Use the full path to the xml files to be sure to load them properly.

Miki
  • 40,887
  • 13
  • 123
  • 202
0

OpenCV Error: Assertion failed (scn == 1 && dcn == 1) in cv::demosaicing, file D:\Build\OpenCV\opencv-3.1.0\modules\imgproc\src\demosaicing.cpp, line 1630 Traceback (most recent call last): File "C:/Users/hedon/PycharmProjects/deneme_1.1/ilk.py", line 11, in gray = cv.cvtColor(img, cv.COLOR_BAYER_BG2GRAY) cv2.error: D:\Build\OpenCV\opencv-3.1.0\modules\imgproc\src\demosaicing.cpp:1630: error: (-215) scn == 1 && dcn == 1 in function cv::demosaicing

this problem from :

cv2.imread

you should check if the images exist in the path you give , then check the count variable if it have valid number.