Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/python/tests/data/optical_flow.npy
Binary file not shown.
Binary file removed src/python/tests/data/optical_flow.png
Binary file not shown.
32 changes: 11 additions & 21 deletions src/python/tests/optical_flow_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""
Module for testing optical flow computations. Provides test cases for optical flow computation from pose landmarks data.
"""
import tempfile
from unittest import TestCase

from matplotlib.testing.compare import compare_images
import numpy as np

from pose_format.numpy.representation.distance import DistanceRepresentation
from pose_format.pose import Pose
Expand All @@ -18,30 +17,21 @@ class TestOpticalFlow(TestCase):

def test_optical_flow(self):
"""
Tests optical flow from pose landmarks data and
visualize the computed flow using a heatmap.

Compares the generated heatmap with the reference image provided in `data/optical_flow.png` to validate the correctness of the omputed optical flow.

Raises:
AssertionError: If the computed optical flow visualization does not match the reference image.
Tests optical flow from pose landmarks data against a numeric reference.

Compares the computed flow with `data/optical_flow.npy`. The reference is
the raw numeric output (not a rendered image), so the test is independent
of matplotlib/freetype rendering differences across environments.
"""
calculator = OpticalFlowCalculator(fps=30, distance=DistanceRepresentation())

with open('tests/data/mediapipe.pose', 'rb') as f:
pose = Pose.read(f.read())
pose = pose.get_components(["POSE_LANDMARKS", "RIGHT_HAND_LANDMARKS", "LEFT_HAND_LANDMARKS"])

flow = calculator(pose.body.data)
flow = flow.squeeze(axis=1)
print(flow.shape)

# Matplotlib heatmap
import matplotlib.pyplot as plt
plt.imshow(flow.T)
plt.tight_layout()

fp = tempfile.NamedTemporaryFile()
plt.savefig(fp.name, format='png')
flow = calculator(pose.body.data).squeeze(axis=1)

self.assertTrue(compare_images('tests/data/optical_flow.png', fp.name, 0.001) is None)
expected = np.load('tests/data/optical_flow.npy')
self.assertEqual(flow.shape, expected.shape)
self.assertTrue(np.allclose(flow, expected),
"Computed optical flow does not match the reference.")
Loading