2

我正在使用 haarcascade 检测面部并使用 OpenCV 使用网络摄像头跟踪它们。我需要保存每张被跟踪的脸。但问题是人们何时移动。在这种情况下,脸部会变得模糊。

我尝试使用以下代码使用 opencv 的 dnn 人脸检测器和 Laplacian 来缓解这个问题:

blob = cv2.dnn.blobFromImage(cropped_face, 1.0, (300, 300), (104.0, 177.0, 123.0))
net.setInput(blob)
detections = net.forward()
confidence = detections[0, 0, 0, 2]
blur = cv2.Laplacian(cropped_face, cv2.CV_64F).var()
if confidence >= confidence_threshold and blur >= blur_threshold:
    cv2.imwrite('less_blurry_image', cropped_face)

在这里,如果由于运动而不是模糊,我尝试将保存的脸限制setting blur_threshold为 500 和confidence_threshold0.98(即 98%)。

但问题是如果我更换相机,我必须再次手动更改阈值。在大多数情况下,设置阈值会忽略大多数人脸。

此外,由于与模糊的面部相比,背景总是清晰的,因此很难检测到。

所以我的问题是如何检测面部的这种运动模糊。我知道我可以训练一个用于面部运动模糊检测的 ML 模型。但这将需要大量处理资源来完成一项小任务。

此外,如果我走这条路,我将需要大量带注释的数据进行训练。这对我这样的学生来说并不容易。

因此,我尝试使用 OpenCV 来检测这一点,与使用 ML 模型进行检测相比,这将占用更少的资源。

有没有什么资源密集型解决方案可以解决这个问题?

4

2 回答 2

1

您可能可以使用傅立叶变换 (FFT) 或离散余弦变换 (DCT) 来确定您的面部有多模糊。图像中的模糊导致高频消失,只剩下低频。

因此,您将拍摄一张脸部图像,将其补零至适合 FFT 或 DCT 的尺寸,然后查看您在较高频率下的频谱功率。

您可能不需要 FFT - DCT 就足够了。DCT 的优点是它产生实值结果(没有虚部)。性能方面,FFT 和 DCT 对于 2 的幂的大小以及只有因子 2、3 和 5 的大小非常快(尽管如果你也有 3 和 5,它会慢一点)。

于 2019-04-16T15:16:41.907 回答
1

正如@PlinyTheElder 所提到的,DCT 信息可以为您提供运动模糊。我在下面附上来自repo的代码片段:

代码在C里面,我不确定是否有 python 绑定libjpeg。否则,您需要创建一个。

/* Fast blur detection using JPEG DCT coefficients
 *
 * Based on "Blur Determination in the Compressed Domain Using DCT
 * Information" by Xavier Marichal, Wei-Ying Ma, and Hong-Jiang Zhang.
 *
 * Tweak MIN_DCT_VALUE and MAX_HISTOGRAM_VALUE to adjust
 * effectiveness.  I reduced these values from those given in the
 * paper because I find the original to be less effective on large
 * JPEGs.
 *
 * Copyright 2010 Julian Squires <julian@cipht.net>
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <jpeglib.h>

static int min_dct_value = 1;   /* -d= */
static float max_histogram_value = 0.005; /* -h= */

static float weights[] = {  /* diagonal weighting */
    8,7,6,5,4,3,2,1,
    1,8,7,6,5,4,3,2,
    2,1,8,7,6,5,4,3,
    3,2,1,8,7,6,5,4,
    4,3,2,1,8,7,6,5,
    5,4,3,2,1,8,7,6,
    6,5,4,3,2,1,8,7,
    7,6,5,4,3,2,1,8
};
static float total_weight = 344;

static inline void update_histogram(JCOEF *block, int *histogram)
{
    for(int k = 0; k < DCTSIZE2; k++, block++)
        if(abs(*block) > min_dct_value) histogram[k]++;
}

static float compute_blur(int *histogram)
{
    float blur = 0.0;
    for(int k = 0; k < DCTSIZE2; k++)
        if(histogram[k] < max_histogram_value*histogram[0])
            blur += weights[k];
    blur /= total_weight;
    return blur;
}


static int operate_on_image(char *path)
{
        struct jpeg_error_mgr jerr;
    struct jpeg_decompress_struct cinfo;
    jvirt_barray_ptr *coeffp;
    JBLOCKARRAY cs;
    FILE *in;
    int histogram[DCTSIZE2] = {0};

        cinfo.err = jpeg_std_error(&jerr);
        jpeg_create_decompress(&cinfo);
    if((in = fopen(path, "rb")) == NULL) {
        fprintf(stderr, "%s: Couldn't open.\n", path);
        jpeg_destroy_decompress(&cinfo);
        return 0;
    }
    jpeg_stdio_src(&cinfo, in);
    jpeg_read_header(&cinfo, TRUE);
    // XXX might be a little faster if we ask for grayscale
    coeffp = jpeg_read_coefficients(&cinfo);

    /* Note: only looking at the luma; assuming it's the first component. */
    for(int i = 0; i < cinfo.comp_info[0].height_in_blocks; i++) {
        cs = cinfo.mem->access_virt_barray((j_common_ptr)&cinfo, coeffp[0], i, 1, FALSE);
        for(int j = 0; j < cinfo.comp_info[0].width_in_blocks; j++)
            update_histogram(cs[0][j], histogram);
    }

    printf("%f\n", compute_blur(histogram));
    // output metadata XXX should be in IPTC etc

    // XXX also need to destroy coeffp?
    jpeg_destroy_decompress(&cinfo);
    return 0;
}

int main(int argc, char **argv)
{
    int status, i;

    for(status = 0, i = 1; i < argc; i++) {
        if(argv[i][0] == '-') {
            if(argv[i][1] == 'd')
                sscanf(argv[i], "-d=%d", &min_dct_value);
            else if(argv[i][1] == 'h')
                sscanf(argv[i], "-h=%f", &max_histogram_value);
            continue;
        }
        status |= operate_on_image(argv[i]);
    }

    return status;
}

编译代码:

gcc -std=c99 blur_detection.c -l jpeg -o blur-detection

运行代码:

./blur-detection <image path>
于 2020-08-13T18:47:11.943 回答