首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用较少的cv2实现人脸检测

用较少的cv2实现人脸检测
EN

Stack Overflow用户
提问于 2020-11-17 16:34:47
回答 1查看 266关注 0票数 0

对于一个大学项目,我正在编写一个面罩识别程序。为了检测人脸,我使用cv2.CascadeClassifier('face_detector.xml')。正如我注意到的,这个程序占用了太多的CPU,导致视频流帧速率严重混乱。我在一台1.6 i5双核(英特尔酷睿i5)的MacBook Air上运行代码。有人能解释一下我可以做些什么来让它更顺畅吗?或者推荐另一种人脸检测?下面是我的代码:

代码语言:javascript
复制
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()

感谢您的帮助:)

EN

回答 1

Stack Overflow用户

发布于 2020-11-17 20:11:05

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

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

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

对于人脸检测,你可以选择任何方法,如hear,HOG,CNN,并将其放入新的线程中。在主跟踪线程(可以实时运行)中,更新模型、预测边界框并显示边界框。

您可以在这里查找跟踪信息。我推荐基于KCF的方法,因为它是快速和可靠的。

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

只需将检测框rect作为跟踪的输入rect框即可。THen它应该可以直接工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64871768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档