OpenCV error in Python:

0

Good Afternoon Personal, okay?

I'm looking to learn about Face Detection in Python using OpenCV, I installed all the necessary libs, however, when running the file I come across the following error:

import cv2
import numpy as np

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

while True:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for(w,y,w,h) in faces:
        cv2.rectangle(img, (x,y), (w+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.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color, (ex,ey) (ex+ew,ey+eh), (0,255,0),2)

    cv2.imshow('img',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break 
cap.release()
cv2.destroyAllWindows()
error                                     Traceback (most recent call last)
<ipython-input-16-1f0f9c8f21ef> in <module>()
      8 while True:
      9     ret, img = cap.read()
---> 10     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
     11     faces = face_cascade.detectMultiScale(gray, 1.3, 5)
     12     for(w,y,w,h) in faces:

error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

Can anyone help me? Thanks!

    
asked by anonymous 10.09.2018 / 20:43

1 answer

1

You are not loading the Cascade files, try to use the absolute path of the OpenCV library.

Find Path

import os
import cv2
print(os.path.dirname(cv2.__file__))

That will return something of type in Windows: C:\Users\nome_do_usuario\Envs\ambiente_python\lib\site-packages\cv2

Code

Therefore, to correctly load the Cascade files, change to:

face_cascade = cv2.CascadeClassifier('C:\Users\nome_do_usuario\Envs\ambiente_python\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\Users\nome_do_usuario\Envs\ambiente_python\Lib\site-packages\cv2\data\haarcascade_eye.xml')

And as @ederwander said, change line 6 to:

cap = cv2.VideoCapture(0)

On line 12 change to:

for(x,y,w,h) in faces:

On line 16 change to:

eyes = eye_cascade.detectMultiScale(roi_gray)

Among other bugs in your code that have been changed from the official OpenCV tutorials.

Final Code

import cv2
import os


caminho_opencv = os.path.dirname(cv2.__file__)

face_cascade = cv2.CascadeClassifier(caminho_opencv + '\data\haarcascade_frontalface_alt.xml')
eye_cascade = cv2.CascadeClassifier(caminho_opencv + '\data\haarcascade_eye_tree_eyeglasses.xml')
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(
        gray,
        scaleFactor=1.5,
        minNeighbors=5,
        minSize=(20, 20),
        flags=cv2.CASCADE_SCALE_IMAGE
    )
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        roi_color = frame[y:y + h, x:x + w]
        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)

    cv2.imshow('img',frame)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()
cv2.destroyAllWindows()
    
10.09.2018 / 21:24