Return 如果面部被检测到至少 3 秒,则为真
Return True if the face is detected at least for 3 seconds
如何让网络摄像头保持打开状态并使用 haar 级联进行人脸检测仅几秒钟?
我有一个函数,这个函数returns如果已经对人脸进行了人脸检测,则为true,但不能一检测到就立即进行,而必须只进行例如,在检测到人脸至少 3 秒后。
如果我使用时间模块并等待,显然这只会减慢我的程序的执行速度,因此也会减慢 cv2.VideoCapture
的执行速度,看到不稳定的网络摄像头。
代码如下:
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")
简单记录检测到人脸的时间,只看到检测到人脸时绘制人脸和当前时间戳是timeout
秒后记录的时间戳。
import cv2
from time import time
def face_detect(timeout):
video_capture = cv2.VideoCapture(0)
start_time = 0 # Temporary value.
face_detected = False # Used to see if we've detected the face.
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
)
if not face_detected and faces:
face_detected = True
start_time = time()
elif not faces:
face_detected = False # Reset if we don't find the faces.
elif face_detected and time() - start_time >= timeout:
# 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(timeout=3)
if detected == True:
print("The face is detected. OK")
else:
print("I'm sorry but I can't detect your face")
如何让网络摄像头保持打开状态并使用 haar 级联进行人脸检测仅几秒钟?
我有一个函数,这个函数returns如果已经对人脸进行了人脸检测,则为true,但不能一检测到就立即进行,而必须只进行例如,在检测到人脸至少 3 秒后。
如果我使用时间模块并等待,显然这只会减慢我的程序的执行速度,因此也会减慢 cv2.VideoCapture
的执行速度,看到不稳定的网络摄像头。
代码如下:
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")
简单记录检测到人脸的时间,只看到检测到人脸时绘制人脸和当前时间戳是timeout
秒后记录的时间戳。
import cv2
from time import time
def face_detect(timeout):
video_capture = cv2.VideoCapture(0)
start_time = 0 # Temporary value.
face_detected = False # Used to see if we've detected the face.
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
)
if not face_detected and faces:
face_detected = True
start_time = time()
elif not faces:
face_detected = False # Reset if we don't find the faces.
elif face_detected and time() - start_time >= timeout:
# 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(timeout=3)
if detected == True:
print("The face is detected. OK")
else:
print("I'm sorry but I can't detect your face")