写在前面

• 工作中遇到，简单整理
• 人脸识别中，对于模糊程度较高的图像数据，识别率低，错误率高。
• 虽然使用 AdaFace 模型，对低质量人脸表现尤为突出。
• 但是还是需要对 模糊程度高的图像进行丢弃处理
• 当前通过阈值分类，符合要求的进行特性提取
• 实际应用中，可以维护一个质量分数
• 比如由 模糊程度图片字节大小人脸姿态评估(欧拉角)等 算出一个综合质量分，用于人脸归类/聚类
• 理解不足小伙伴帮忙指正

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File    :   detect_blur.py
@Time    :   2023/07/24 22:57:51
@Author  :   Li Ruilong
@Version :   1.0
@Contact :   liruilonger@gmail.com
@Desc    :   图片模糊度检测
"""

# here put the import lib

# import the necessary packages
from imutils import paths
import cv2
import os

def variance_of_laplacian(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# compute the Laplacian of the image and then return the focus
# measure, which is simply the variance of the Laplacian
return cv2.Laplacian(gray, cv2.CV_64F).var()

# loop over the input images
for imagePath in paths.list_images("./res/mh"):
# load the image, convert it to grayscale, and compute the
# focus measure of the image using the Variance of Laplacian
# method
image = cv2.imread(imagePath)
fm = variance_of_laplacian(image)
text = "Not Blurry"
print(fm)
# if the focus measure is less than the supplied threshold,
# then the image should be considered "blurry"
if fm < 100:
text = "Blurry"
# show the image
file_name = os.path.basename(imagePath)
cv2.imwrite(str(fm)+'__' + file_name , image)

cv2.Laplacian(gray, cv2.CV_64F).var()

def variance_of_laplacian(image):
"""
@Time    :   2023/07/25 01:57:44
@Author  :   liruilonger@gmail.com
@Version :   1.0
@Desc    :   模糊度检测
Args:

Returns:
void
"""
numpy_image = np.array(image)
cv2_image = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)
gray = cv2.cvtColor(cv2_image, cv2.COLOR_BGR2GRAY)
# compute the Laplacian of the image and then return the focus
# measure, which is simply the variance of the Laplacian
return cv2.Laplacian(gray, cv2.CV_64F).var()

(AdaFace) C:\Users\liruilong\Documents\GitHub\AdaFace_demo>python detect_blur.py
130.99918569797578
97.54477372302556
70.30346984100659
95.56028915335366
77.70006004883219
107.2065965492792
93.43007114319839
75.44132565995248
127.50238903320515
98.11810838476116
69.49917570127641
132.46578324273048
99.2095025510204
92.97255942246558
93.33812691062155
667.4883318795927