检测到人脸 OpenCV 时打印消息

瑞安·庞赛尔

我想在我的实时视频源中检测到人脸时打印一条消息。通过将 Print Message 绑定到在检测到的面部周围绘制矩形的 for 循环,我取得了一定的成功。

现在出现的问题是对于检测到的每一帧都会打印消息。我希望消息每秒打印一次。当我尝试time.sleep(1)在其当前位置使用时,它使馈送断断续续,每秒仅显示 1 帧。

我应该在哪里以及如何放置打印消息代码,以便不影响视频输入和人脸检测?

import cv2 as cv
import numpy as np
import time

# Open Webcam
cap = cv.VideoCapture(0)

# Define ret and size as frame info from webcame
ret, size = cap.read()

# Define rows, cols, and '_' as the return from size.shape
# rows, cols, _ = size.shape

# Print results
# print('Rows', rows)
# print('Cols', cols)

# Face Detection haar_cascade
haar_cascade = cv.CascadeClassifier('haar_face.xml')

# Check if the webcam is opened correctly
if not cap.isOpened():
    raise IOError("Cannot open webcam")

# Do the following when webcam is open
while True:
    ret, frame = cap.read()
    frame = cv.resize(frame, None, fx=1, fy=1, interpolation=cv.INTER_AREA)

    # Divide Frame into Regions of Interest (ROI)
    ROI1 = frame[0:180, 0:320]
    ROI2 = frame[0:180, 320:640]
    ROI3 = frame[180:360, 0:320]
    ROI4 = frame[180:360, 320:640]

    # Detect faces in each ROI
    faces_rect1 = haar_cascade.detectMultiScale(ROI1, scaleFactor=1.1, minNeighbors=5)
    faces_rect2 = haar_cascade.detectMultiScale(ROI2, scaleFactor=1.1, minNeighbors=5)
    faces_rect3 = haar_cascade.detectMultiScale(ROI3, scaleFactor=1.1, minNeighbors=5)
    faces_rect4 = haar_cascade.detectMultiScale(ROI4, scaleFactor=1.1, minNeighbors=5)
    
    # Draw rectangles around detected faces
    for (x, y, w, h) in faces_rect1:
        cv.rectangle(ROI1, (x, y), (x+w,y+h), (0,255,0), thickness=2)
        print('I SEE YOU IN 1')
        time.sleep(1)

    for (x, y, w, h) in faces_rect2:
        cv.rectangle(ROI2, (x, y), (x+w,y+h), (0,255,0), thickness=2)
        print('I SEE YOU IN 2')
        time.sleep(1)

    for (x, y, w, h) in faces_rect3:
        cv.rectangle(ROI3, (x, y), (x+w,y+h), (0,255,0), thickness=2)
        print('I SEE YOU IN 3')
        time.sleep(1)

    for (x, y, w, h) in faces_rect4:
        cv.rectangle(ROI4, (x, y), (x+w,y+h), (0,255,0), thickness=2)
        print('I SEE YOU IN 4')
        time.sleep(1)

    # Show all video feeds
    cv.imshow('Feed', frame)
    cv.imshow('ROI1', ROI1)
    cv.imshow('ROI2', ROI2)
    cv.imshow('ROI3', ROI3)
    cv.imshow('ROI4', ROI4)
    
    # Press ESC to break
    c = cv.waitKey(1)
    if c == 27:
        break

cap.release()
cv.destroyAllWindows()
扎多什特

您可以测量经过的时间并在经过 1 秒后打印消息,而不是停止线程。

例如对于第一个循环:

t1 = time.time()   # start time

while True:
    # ... 
    # ...
    # Draw rectangles around detected faces
    for (x, y, w, h) in faces_rect1:
        cv.rectangle(ROI1, (x, y), (x+w,y+h), (0,255,0), thickness=2)
        t2 = time.time()
        if (t2 - t1) > 1:
            print('I SEE YOU IN 1') 
            t1 = time.time()   # reset start time

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章