fix: don't report a safety issue when a black image is generated
parent
2dd99c183c
commit
24e10f9e5f
@ -0,0 +1,17 @@
|
||||
import cv2
|
||||
|
||||
from imaginairy.img_utils import pillow_img_to_opencv_img
|
||||
|
||||
|
||||
def calculate_blurriness_level(img):
|
||||
img = pillow_img_to_opencv_img(img)
|
||||
|
||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
sharpness = cv2.Laplacian(gray, cv2.CV_64F).var()
|
||||
sharpness = max(sharpness, 0.000001)
|
||||
bluriness = 1 / sharpness
|
||||
return bluriness
|
||||
|
||||
|
||||
def is_blurry(img, threshold=0.91):
|
||||
return calculate_blurriness_level(img) > threshold
|
Binary file not shown.
After Width: | Height: | Size: 6.4 KiB |
Binary file not shown.
After Width: | Height: | Size: 163 KiB |
@ -0,0 +1,19 @@
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
||||
from imaginairy.enhancers.blur_detect import is_blurry
|
||||
from tests import TESTS_FOLDER
|
||||
|
||||
blur_params = [
|
||||
(f"{TESTS_FOLDER}/data/black_square.jpg", True),
|
||||
(f"{TESTS_FOLDER}/data/safety.jpg", False),
|
||||
(f"{TESTS_FOLDER}/data/latent_noise.jpg", False),
|
||||
(f"{TESTS_FOLDER}/data/girl_with_a_pearl_earring.jpg", False),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("img_path,expected", blur_params)
|
||||
def test_calculate_blurriness_level(img_path, expected):
|
||||
img = Image.open(img_path)
|
||||
|
||||
assert is_blurry(img) == expected
|
Loading…
Reference in New Issue