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!
cv2.VideoCapture(1)
you have two cameras connected ? if you have only one camera you should use index zerocv2.VideoCapture(0)
, Error says it is not possible to convert toGray
it’s obvious if he can’t open his cam, switch to index zero and see what happens– ederwander
Read this answer
– danieltakeshi
Thank you Eder!!
– HV Lopes