How do I leave the webcam open and do the face detect with haar cascade for only a few seconds?
I have a function and this function returns true if the face detect of a face has been carried out, but it must not do it immediately as soon as it detects it, rather it must do it only after the face has been detected for at least 3 seconds for example.
If I use the time module and do the wait, obviously this will simply slow down the execution of my program and consequently also that of the cv2.VideoCapture
, seeing the jerky webcam.
Here is the code:
import cv2
def face_detect():
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frames = video_capture.read()
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
return True
if __name__ == '__main__':
detected = face_detect()
if detected == True:
print("The face is detected. OK")
else:
print("I'm sorry but I can't detect your face")
in your text. To break the text to a new line, simply separate the text with a new line. – Ted Klein Bergman Dec 17 '21 at 15:04