Opencv error in Python:

Asked

Viewed 1,231 times

2

Good afternoon Everybody, all right?

I’m looking to learn about Face Detection in Python using Opencv, installed all necessary libs, however while 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? Thank you!

  • 1

    cv2.VideoCapture(1) you have two cameras connected ? if you have only one camera you should use index zero cv2.VideoCapture(0) , Error says it is not possible to convert to Gray it’s obvious if he can’t open his cam, switch to index zero and see what happens

  • Thank you Eder!!

1 answer

2


You are not loading the Cascade files, try using the absolute path of the Opencv library.

Find Way

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

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

Code

Therefore, to properly load the Cascade files, switch 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 switch to:

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

On line 16 switch to:

eyes = eye_cascade.detectMultiScale(roi_gray)

Among other errors in your code that have 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()
  • 1

    Thank you Daniel!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.