【问题标题】:Face Detection with less cpu load cv2 [closed]具有较少 cpu 负载 cv2 的人脸检测 [关闭]
【发布时间】:2021-02-28 11:07:05
【问题描述】:

对于一个大学项目,我正在编写一个面罩识别程序。对于检测人脸,我使用 cv2.CascadeClassifier('face_detector.xml')。正如我所注意到的,该程序占用了过多的 CPU 资源,导致视频流帧速率严重混乱。 我在配备 1.6Hz 双核(英特尔酷睿 i5)的 MacBook Air 上运行代码。 有人可以解释我可以改变什么以使其更流畅吗?或者也许推荐另一个人脸检测? 这是我的代码:

import numpy as np
import os
import tensorflow as tf
import cv2
from matplotlib.pyplot import gray

# Disable tensorflow compilation warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import cv2

# Load the cascade
face_cascade = cv2.CascadeClassifier('face_detector.xml')

# To capture video from webcam.
cap = cv2.VideoCapture(0)
# To use a video file as input
# cap = cv2.VideoCapture('filename.mp4')

model = tf.keras.models.load_model('checkpoint19.ckpt')

i = 0
while True:
    # Read the frame
    _, img = cap.read()

    # Detect the faces
    faces = face_cascade.detectMultiScale(img, 1.3, 4)

    # save each frame as image with PNG format
    image = cv2.imwrite('database/{index}.png'.format(index=i), img)
    i += 1

    # cut out the fragment in the box of the image
    # Draw the rectangle around each face
    for (x, y, w, h) in faces:
        crop_img = img[y:y + h, x:x + w]
        resizedImg = cv2.resize(crop_img, (224, 224))
        gray = cv2.cvtColor(resizedImg, cv2.COLOR_BGR2GRAY)
        imgArrNew = gray.reshape(1, 224, 224, 1)
        prediction = model.predict(imgArrNew)
        print(prediction)
        label = np.argmax(prediction)
        print(label)

        # font
        font = cv2.FONT_HERSHEY_SIMPLEX
        # org
        for (x, y, w, h) in faces:
            org = (x, y+h+30)

        # fontScale
        fontScale = 1
        # Blue color in BGR
        color = (255, 0, 0)
        # Line thickness of 2 px
        thickness = 2
        # output the predicted label/sign on the live-stream frame
        if label == 0:
            color = (0,0,225)
            label_out = "Mask off"
        if label == 1:
            color = (50,205,50)
            label_out = "Mask on"
        if label == 2:
            color = (0,255,225)
            label_out = "incorrect Mask"
        cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
        image1 = cv2.putText(img, label_out, org, font,
                             fontScale, color, thickness, cv2.LINE_AA)
    # Display
    cv2.imshow('Face_Regonition', img)
    # Stop if escape key is pressed
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
# Release the VideoCapture object
cap.release()

感谢您的帮助:)

【问题讨论】:

    标签: python computer-vision cpu-usage face-detection cv2


    【解决方案1】:

    haar 级联分类器很慢。 .对于低端计算设备来说,在每一帧中进行检测是很困难的。

    最简单的方法是使用较低分辨率的图像或较低的 FPS。但它看起来很便宜

    更好的方法是使用检测和跟踪框架,在新线程中以 1hz 的间隔进行检测,而跟踪可以在 30hz 进行,人眼无法区分。

    对于人脸检测,可以选择hear、HOG、CNN等任意一种方法,放到一个新线程中。在主跟踪线程(可以实时运行)中更新模型并预测边界框并显示它。

    您可以从这里查找跟踪。我建议使用基于 KCF 的方法,因为它既快速又可靠。

    https://www.pyimagesearch.com/2018/07/30/opencv-object-tracking/

    只需将检测框矩形作为输入矩形框进行跟踪。那么它应该可以直接工作。

    【讨论】:

    • 在网站上它说:当您需要更快的 FPS 吞吐量但可以处理稍低的对象跟踪精度时使用 KCF 我确实对 haar 级联分类器有一些问题,它并不总是能够检测正确的脸。相反,它宁愿将脚手架检测为一张脸,而不是我的脸……但无论如何我都会尝试。感谢您的快速答复,如果有效,我会通知您
    猜你喜欢
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 2020-04-07
    • 2010-09-21
    • 2011-07-13
    • 2012-06-10
    相关资源
    最近更新 更多