yoloserv/modules/paravision/liveness/tests/test_liveness.py

137 lines
4.4 KiB
Python

import os
import pyrealsense2 as rs
import numpy as np
from unittest import TestCase
from ..session import Liveness, CameraParams
from ..types import Rectangle
from ..exceptions import InvalidWindowSizeException
ASSETS_PATH = os.path.join(os.path.dirname(__file__), "assets")
liveness_session = None
class TestSession(TestCase):
@classmethod
def setUpClass(cls):
global liveness_session
liveness_session = Liveness()
def setUp(self):
self.liveness_session = liveness_session
def test_crop_depth_frame(self):
bounding_box = Rectangle(
528.551139831543, 234.36917863815668, 839.0621948242188, 642.2044240250417
)
depth_intrinsic = rs.intrinsics()
depth_intrinsic.width = 1280
depth_intrinsic.height = 720
depth_intrinsic.ppx = 640.387
depth_intrinsic.ppy = 357.513
depth_intrinsic.fx = 635.811
depth_intrinsic.fy = 635.811
depth_intrinsic.model = rs.distortion.brown_conrady
depth_intrinsic.coeffs = [0, 0, 0, 0, 0]
color_intrinsic = rs.intrinsics()
color_intrinsic.width = 1280
color_intrinsic.height = 720
color_intrinsic.ppx = 647.024
color_intrinsic.ppy = 356.927
color_intrinsic.fx = 922.169
color_intrinsic.fy = 922.476
color_intrinsic.model = rs.distortion.inverse_brown_conrady
color_intrinsic.coeffs = [0, 0, 0, 0, 0]
color_depth_extr = rs.extrinsics()
color_depth_extr.rotation = [
0.999945,
0.0103263,
0.00163071,
-0.0103348,
0.999932,
0.00530964,
-0.00157577,
-0.0053262,
0.999985,
]
color_depth_extr.translation = [-0.0147758, -0.000159923, -0.000372309]
camera_params = CameraParams(depth_intrinsic, color_intrinsic, color_depth_extr)
depth_frame = liveness_session.load_depth_data_from_file(
os.path.join(ASSETS_PATH, "depth.txt")
)
cropped_depth_frame = liveness_session.crop_depth_frame(
camera_params, depth_frame, bounding_box
)
expected_cropped_depth_frame = liveness_session.load_depth_data_from_file(
os.path.join(ASSETS_PATH, "cropped_depth.txt")
)
self.assertIsNotNone(cropped_depth_frame, msg="unexpected cropped depth frame")
self.assertTrue(
np.array_equal(cropped_depth_frame, expected_cropped_depth_frame),
msg="invalid cropped depth frame",
)
def test_crop_depth_frame_invalid_camera_params(self):
depth_frame = liveness_session.load_depth_data_from_file(
os.path.join(ASSETS_PATH, "depth.txt")
)
bounding_box = Rectangle(
528.551139831543, 234.36917863815668, 839.0621948242188, 642.2044240250417
)
self.assertRaises(
Exception,
liveness_session.crop_depth_frame,
None,
depth_frame,
bounding_box,
)
def test_crop_depth_frame_invalid_depth_frame(self):
camera_params = CameraParams(None, None, None)
depth_frame = None
bounding_box = Rectangle(1.2, 1.2, 1.2, 1.2)
self.assertRaises(
Exception,
liveness_session.crop_depth_frame,
camera_params,
depth_frame,
bounding_box,
)
def test_crop_depth_frame_invalid_bounding_box(self):
camera_params = CameraParams(None, None, None)
depth_frame = liveness_session.load_depth_data_from_file(
os.path.join(ASSETS_PATH, "depth.txt")
)
bounding_box = None
self.assertRaises(
Exception,
liveness_session.crop_depth_frame,
camera_params,
depth_frame,
bounding_box,
)
def test_engine_invalid_window_size(self):
depth_frame = liveness_session.load_depth_data_from_file(
os.path.join(ASSETS_PATH, "depth.txt")
)
self.assertRaises(
InvalidWindowSizeException,
liveness_session.compute_liveness_probability,
[depth_frame],
)
def test_engine_valid_window_size(self):
depth_frame = liveness_session.load_depth_data_from_file(
os.path.join(ASSETS_PATH, "depth.txt")
)
prob = liveness_session.compute_liveness_probability([depth_frame] * 5)
self.assertTrue(prob >= 0 and prob <= 1)