File size: 2,910 Bytes
8096125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import numpy as np

from cv_ops.morphology import apply_morphology
from cv_ops.transforms import reflect, rotate, scale_image, translate
from filters.builtin import BUILTIN_FILTERS
from filters.registry import apply_definition


def sample_image():
    img = np.zeros((20, 30, 3), dtype=np.uint8)
    img[5:15, 10:20] = [200, 100, 50]
    return img


def test_builtin_filter_shapes():
    img = sample_image()
    for name in ["Grayscale", "Sepia", "Invert", "Blur", "Sharpen", "Edge Detection"]:
        out = BUILTIN_FILTERS[name](img)
        assert out.shape == img.shape
        assert out.dtype == np.uint8


def test_transforms_return_matrices():
    img = sample_image()
    out, matrix = translate(img, 2, 3)
    assert out.shape == img.shape
    assert matrix == [[1.0, 0.0, 2.0], [0.0, 1.0, 3.0]]
    assert rotate(img, 15, expand=False)[0].shape == img.shape
    assert scale_image(img, 2, 2)[0].shape[:2] == (40, 60)
    assert reflect(img, "both")[0].shape == img.shape


def test_morphology_binary_output():
    out, kernel = apply_morphology(sample_image(), "Opening", size=3)
    assert out.shape == sample_image().shape
    assert kernel.shape == (3, 3)


def test_pipeline_definition():
    img = sample_image()
    definition = {"type": "pipeline", "steps": [{"operation": "Invert", "params": {}}, {"operation": "Grayscale", "params": {}}]}
    out = apply_definition(img, definition)
    assert out.shape == img.shape


def test_export_image():
    from os.path import exists
    from app import export_image

    assert export_image(None) is None

    img = sample_image()
    filepath = export_image(img)
    assert filepath is not None
    assert isinstance(filepath, str)
    assert exists(filepath)
    assert filepath.endswith(".png")


def test_face_filters():
    from models.face_filters import BUILTIN_AR_FILTERS, apply_face_filter

    img = np.zeros((100, 100, 3), dtype=np.uint8)
    for filter_name in BUILTIN_AR_FILTERS:
        res, msg = apply_face_filter(img, filter_name)
        assert res.shape == img.shape
        assert res.dtype == np.uint8
        assert isinstance(msg, str)


def test_custom_ar_filters():
    from models.face_filters import apply_face_filter, ar_filter_names, delete_ar_filter, save_ar_filter

    custom_def = {
        "elements": [
            {"landmark": "forehead", "shape": "crown", "color": [255, 215, 0], "scale": 1.0},
            {"landmark": "eyes", "shape": "visor", "color": [0, 255, 255], "scale": 1.0},
        ]
    }
    saved = save_ar_filter("Test Crown Visor", custom_def)
    assert saved["name"] == "Test Crown Visor"
    assert "Test Crown Visor" in ar_filter_names(True)

    img = np.zeros((100, 100, 3), dtype=np.uint8)
    res, msg = apply_face_filter(img, "Test Crown Visor")
    assert res.shape == img.shape

    delete_ar_filter("Test Crown Visor")
    assert "Test Crown Visor" not in ar_filter_names(False)