AI_Lab_CAM / tests /test_cv_ops.py
Aditya7864's picture
Add application file
8096125
Raw
History Blame Contribute Delete
2.91 kB
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)