Ask Your Question
0

trouble in using detectmultiple command

asked 2020-02-09 23:54:25 -0600

updated 2020-02-10 02:32:44 -0600

LBerger gravatar image

trouble in using detectmultiple command

import cv2
import numpy as np
from matplotlib import pyplot as plt

facepath= "c:\cv2\data\haarcascade_frontalface_default.xml"
facecascade=cv2.CascadeClassifier(facepath)

cap=cv2.VideoCapture(0)
sF=1.05
a=0
while (a!=100):
    ret, img=cap.read()
    gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = facecascade.detectMultiScale(
        gray,
        scaleFactor= sF,
        minNeighbors=8,
        minSize=(55, 55),
        flags=cv2.CASCADE_SCALE_IMAGE
    )
    for(x,y,w,h) in faces:
        detect=cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
    cv2.inshow('face',detect)
    cv2.inwrite('E:\box\face'+str(i)+'.jpg',gray)
    a+=1
    if cv2.waitKey(1) & 0xFF==ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

I tried this code to detect face from video,while compiling I got the following error

gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

can anyone tell me how to overcome this error!!

edit retag flag offensive close merge delete

Comments

read error message : src is empty. please use this tutorials to capture video from a camera

a good idea too is to check that facecascade is not empty : facecascade.empty() ?

LBerger gravatar imageLBerger ( 2020-02-10 02:35:58 -0600 )edit

Since the error is in the cvtColor function, it means that your source image, img is empty. Before calling cvtColor, please check that a valid image has been loaded into img using img.empty().

yogeshmurthy_ gravatar imageyogeshmurthy_ ( 2020-02-10 02:45:55 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-02-10 03:20:44 -0600

mvuori gravatar image

Your facepath is invalid due to single backslashes. Double them or use forward slashes.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-02-09 23:54:25 -0600

Seen: 176 times

Last updated: Feb 10 '20