-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from worldcoin/yichen1-ai-5001-add-sharpness-e…
…stimation-to-open-iris Add sharpness estimation and a validator to reject blurry images
- Loading branch information
Showing
23 changed files
with
305 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/iris/nodes/eye_properties_estimation/sharpness_estimation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from typing import List, Tuple | ||
|
||
import cv2 | ||
import numpy as np | ||
from pydantic import Field, validator | ||
|
||
import iris.io.validators as pydantic_v | ||
from iris.callbacks.callback_interface import Callback | ||
from iris.io.class_configs import Algorithm | ||
from iris.io.dataclasses import NormalizedIris, Sharpness | ||
|
||
|
||
class SharpnessEstimation(Algorithm): | ||
"""Calculate sharpness of the normalized iris. | ||
The goal of this algorithm is to calculate the sharpness of the normalized iris using the variance of Laplacian. | ||
LIMITATIONS: | ||
This method may be biased against dark images with inadequate lighting. | ||
""" | ||
|
||
class Parameters(Algorithm.Parameters): | ||
"""Parameters class for SharpnessEstimation objects. | ||
lap_ksize (int): Laplacian kernel size, must be odd integer no larger than 31. | ||
erosion_ksize (Tuple[int, int]): Mask erosion kernel size, must be odd integers. | ||
""" | ||
|
||
lap_ksize: int = Field(..., gt=0, le=31) | ||
erosion_ksize: Tuple[int, int] = Field(..., gt=0) | ||
|
||
_is_odd0 = validator("lap_ksize", allow_reuse=True)(pydantic_v.is_odd) | ||
_is_odd = validator("erosion_ksize", allow_reuse=True, each_item=True)(pydantic_v.is_odd) | ||
|
||
__parameters_type__ = Parameters | ||
|
||
def __init__( | ||
self, | ||
lap_ksize: int = 11, | ||
erosion_ksize: Tuple[int, int] = (29, 15), | ||
callbacks: List[Callback] = [], | ||
) -> None: | ||
"""Assign parameters. | ||
Args: | ||
lap_ksize (int, optional): kernal size for Laplacian. Defaults to 11. | ||
erosion_ksize (Tuple[int, int], optional): kernal size for mask erosion. Defaults to (29,15). | ||
callbacks (List[Callback]): callbacks list. Defaults to []. | ||
""" | ||
super().__init__(lap_ksize=lap_ksize, erosion_ksize=erosion_ksize, callbacks=callbacks) | ||
|
||
def run(self, normalization_output: NormalizedIris) -> Sharpness: | ||
"""Calculate sharpness of the normalized iris. | ||
Args: | ||
normalization_output (NormalizedIris): Normalized iris. | ||
Returns: | ||
Sharpness: Sharpness object. | ||
""" | ||
output_im = cv2.Laplacian(normalization_output.normalized_image / 255, cv2.CV_32F, ksize=self.params.lap_ksize) | ||
mask_im = cv2.erode( | ||
normalization_output.normalized_mask.astype(np.uint8), kernel=np.ones(self.params.erosion_ksize, np.uint8) | ||
) | ||
sharpness_score = output_im[mask_im == 1].std() if np.sum(mask_im == 1) > 0 else 0.0 | ||
return Sharpness(score=sharpness_score) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
tests/e2e_tests/nodes/eye_properties_estimation/test_e2e_sharpness_estimation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import math | ||
import os | ||
import pickle | ||
from typing import Any | ||
|
||
from iris.nodes.eye_properties_estimation.sharpness_estimation import SharpnessEstimation | ||
|
||
|
||
def load_mock_pickle(name: str) -> Any: | ||
testdir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "iris_response/mocks", "conv_filter_bank") | ||
mock_path = os.path.join(testdir, f"{name}.pickle") | ||
return pickle.load(open(mock_path, "rb")) | ||
|
||
|
||
def test_sharpness_estimation() -> None: | ||
normalized_iris = load_mock_pickle(name="normalized_iris") | ||
|
||
sharpness_obj = SharpnessEstimation() | ||
sharpness = sharpness_obj(normalized_iris) | ||
|
||
assert math.isclose(sharpness.score, 880.9419555664062) | ||
|
||
sharpness_obj = SharpnessEstimation(lap_ksize=7) | ||
sharpness = sharpness_obj(normalized_iris) | ||
|
||
assert math.isclose(sharpness.score, 5.179013252258301) | ||
|
||
sharpness_obj = SharpnessEstimation(erosion_ksize=[13, 7]) | ||
sharpness = sharpness_obj(normalized_iris) | ||
|
||
assert math.isclose(sharpness.score, 1013.1661376953125) |
Binary file modified
BIN
-35 Bytes
(100%)
tests/e2e_tests/nodes/iris_response/mocks/conv_filter_bank/e2e_expected_result.pickle
Binary file not shown.
Binary file modified
BIN
-117 KB
(40%)
tests/e2e_tests/nodes/iris_response/mocks/conv_filter_bank/normalized_iris.pickle
Binary file not shown.
Binary file modified
BIN
-111 Bytes
(100%)
tests/e2e_tests/orchestration/mocks/expected_iris_pipeline_debug_output.pickle
Binary file not shown.
Binary file modified
BIN
+17 Bytes
(100%)
tests/e2e_tests/orchestration/mocks/expected_iris_pipeline_orb_output.pickle
Binary file not shown.
Binary file modified
BIN
-15 Bytes
(100%)
tests/e2e_tests/orchestration/mocks/expected_iris_pipeline_simple_output.pickle
Binary file not shown.
Binary file modified
BIN
-195 Bytes
(100%)
tests/e2e_tests/orchestration/mocks/mock_iris_pipeline_call_trace.pickle
Binary file not shown.
Binary file modified
BIN
+27 Bytes
(100%)
tests/e2e_tests/pipelines/mocks/outputs/expected_iris_debug_pipeline_output.pickle
Binary file not shown.
Binary file modified
BIN
+27 Bytes
(100%)
tests/e2e_tests/pipelines/mocks/outputs/expected_iris_orb_pipeline_output.pickle
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
tests/unit_tests/nodes/eye_properties_estimation/test_sharpness_estimation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from typing import Tuple | ||
|
||
import numpy as np | ||
import pytest | ||
from pydantic import ValidationError | ||
|
||
from iris.nodes.eye_properties_estimation.sharpness_estimation import SharpnessEstimation | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"lap_ksize", | ||
[ | ||
pytest.param(0), | ||
pytest.param("a"), | ||
pytest.param(-10), | ||
pytest.param(33), | ||
pytest.param(2), | ||
pytest.param(np.ones(3)), | ||
], | ||
ids=[ | ||
"lap_ksize should be larger than zero", | ||
"lap_ksize should be int", | ||
"lap_ksize should not be negative", | ||
"lap_ksize should not be larger than 31", | ||
"lap_ksize should be odd number", | ||
"lap_ksize should not be array", | ||
], | ||
) | ||
def test_sharpness_lap_ksize_raises_an_exception(lap_ksize: int) -> None: | ||
with pytest.raises(ValidationError): | ||
_ = SharpnessEstimation(lap_ksize=lap_ksize) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"erosion_ksize", | ||
[ | ||
pytest.param((0, 5)), | ||
pytest.param((1, "a")), | ||
pytest.param((-10, 3)), | ||
pytest.param((30, 5)), | ||
pytest.param(np.ones(3)), | ||
], | ||
ids=[ | ||
"erosion_ksize should all be larger than zero", | ||
"erosion_ksize should all be int", | ||
"erosion_ksize should not be negative", | ||
"erosion_ksize should be odd number", | ||
"erosion_ksize should be a tuple of integer with length 2", | ||
], | ||
) | ||
def test_sharpness_erosion_ksize_raises_an_exception(erosion_ksize: Tuple[int, int]) -> None: | ||
with pytest.raises(ValidationError): | ||
_ = SharpnessEstimation(erosion_ksize=erosion_ksize) |
Oops, something went wrong.