-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlurDetection.py
72 lines (52 loc) · 1.92 KB
/
BlurDetection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File : BlurDetection.py
@Time : 2023/06/11 23:23:42
@Author : Li Ruilong
@Version : 1.0
@Contact : [email protected]
@Desc : 人脸模糊判断
实际测试中发现,阈值设置为 100 相对来说比较合适,当然如何数据集很大,可以考虑 提高阈值,当模糊度大于 1000 时,一般为较清晰图片,低于 100 时,图片模糊严重
"""
# here put the import lib
import cv2
from imutils import paths
import os
def calculate_blur(image):
# 计算图像的拉普拉斯梯度
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
laplacian = cv2.Laplacian(gray, cv2.CV_64F).var()
return laplacian
def is_blur_detection(img,Threshold=350):
calculate_blur_ = calculate_blur(img)
if Threshold < calculate_blur_:
return calculate_blur_,True
else:
return calculate_blur_,False
def one_blur_detection(path):
"""
@Time : 2023/06/13 02:17:56
@Author : [email protected]
@Version : 1.0
@Desc : 单个图像输出图像清晰度指标
Args:
Returns:
void
"""
image = cv2.imread(path)
print(calculate_blur(image))
if __name__ == "__main__":
path = "W:\python_code\deepface\\temp\\cf\\cf_795acd36776904f9d96de77bbc9bee614.jpg"
for path in paths.list_images("W:\python_code\deepface\\temp\\cf\\"):
# 加载图像
image = cv2.imread(path)
# 计算图像清晰度指标
blur,boo = is_blur_detection(image,350)
if boo:
composite_img = cv2.putText(image, str(blur), (10, 40), cv2.FONT_HERSHEY_SIMPLEX,
1.0, (255, 255, 255), 5, cv2.LINE_AA, False)
cv2.imwrite(path+".png", composite_img)
os.remove(path)
else:
print(f"图像清晰度指标:{blur}")