|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- """test_feature_extractors.py - test pipeline feature extractors"""
- import unittest
- import os
- import pytest
- import pipeline.feature_extractors as extractors
-
- from pipeline.utils import Source, SourceMedia # technically makes this an integration test, but...
- class TestSource():
- """Provide utils.Source for testing"""
- def one_colour_silent_audio(self):
- """Provide a source with a silent mono-colour video"""
- TEST_DIR = os.path.dirname(os.path.realpath(__file__))
- SAMPLE_VIDEO = f"{TEST_DIR}/sample_videos/test_video_red_silentaudio.mp4" # silent video definitely has no laughter
- return Source(source=SAMPLE_VIDEO, path=SAMPLE_VIDEO, provider="test")
-
- class TestSourceMedia():
- """Provide utils.SourceMedia for testing"""
- def one_colour_silent_audio(self):
- """Provide a source with a silent mono-colour video"""
- return SourceMedia(sources=[TestSource().one_colour_silent_audio()])
-
- class TestLaughterFeatureExtractor(unittest.TestCase):
- def _mock_laughdetect_callout(self, *args, **kwargs):
- """Mock _laughdetect callout
-
- **kwargs:
- - n : int >=0, number of laughter instances to generate
- Return a list of 2-tuple floats (start, end) representing laughter instances
- """
- laughs = []
- n = kwargs.get("n", 0)
- for i in range(n):
- laughs.append((i, i+1))
-
- return laughs
-
- def _mock_run_get_output(self, *args, **kwargs) -> str:
- """Mock run_get_output callout
-
- kwargs:
- - n : int >=0, number of laughter instances to generate
-
- Return a string of laughter instance of the form:
- instance: (1.234, 5.678)
- """
- # TODO: decide if we want non-"instance" output for testing parsing?
- # (maybe)
- output = []
- n = kwargs.get("n", 0)
- for i in range(n):
- output.append(f"instance: ({i}.{i+1}{i+2}{i+3}, {i+4}.{i+5}{i+6}{i+7})")
- return "\n".join(output)
-
- def _sgo5(self, *args, **kwargs):
- """Mock run_get_output callout"""
- return self._mock_run_get_output(*args, **kwargs, n=5)
-
-
- """Test LaughterFeatureExtractor"""
- def test_init(self):
- test_extractor = extractors.LaughterFeatureExtractor()
- self.assertTrue(test_extractor)
-
- def test_setup_noinput(self):
- """test setup - no input files"""
- test_extractor = extractors.LaughterFeatureExtractor()
- with self.assertRaises(ValueError):
- test_extractor.setup()
- # NB test WITH sources implicitly tested in test_extract
-
- @pytest.mark.slow
- def test_extract_mocked_nolaughs(self):
- """Test extract with mocked laughter detection - no laughs"""
- video_source = TestSource().one_colour_silent_audio()
- test_extractor = extractors.LaughterFeatureExtractor(input_files=[video_source])
- test_extractor._laughdetect = self._mock_laughdetect_callout
- test_extractor.setup()
- test_extractor.run()
- test_extractor.teardown()
- self.assertEqual(len(test_extractor.features), 0)
-
- def test_extract_mocked_run_get_output_none(self):
- """Test extract with mocked laughter detection - no laughs"""
- video_source = TestSource().one_colour_silent_audio()
- test_extractor = extractors.LaughterFeatureExtractor(input_files=[video_source])
- test_extractor._run_get_output = self._mock_run_get_output
- test_extractor.setup()
- test_extractor.run()
- test_extractor.teardown()
- self.assertEqual(len(test_extractor.features), 0)
-
- def test_extract_mocked_run_get_output_5(self):
- """Test extract with mocked laughter detection - 5 laughs"""
- video_source = TestSource().one_colour_silent_audio()
- test_extractor = extractors.LaughterFeatureExtractor(input_files=[video_source])
- test_extractor._run_get_output = self._sgo5
- test_extractor.setup()
- test_extractor.run()
- test_extractor.teardown()
- self.assertEqual(len(test_extractor.features), 5)
-
- def test_run_get_output(self):
- """Test run_get_output"""
- video_source = TestSource().one_colour_silent_audio()
- test_extractor = extractors.LaughterFeatureExtractor(input_files=[video_source])
- test_cmd = ["echo", "foo"]
- test_extractor.setup()
- output = test_extractor._run_get_output(test_cmd)
- self.assertEqual(output, "foo\n")
-
- # TODO: add sample video with laughs to test _laughdetect()
-
- class TestRandomFeatureExtractor(unittest.TestCase):
- """Test RandomFeatureExtractor"""
- def test_init(self):
- test_extractor = extractors.RandomFeatureExtractor()
- self.assertTrue(test_extractor)
-
- def test_setup_noinput(self):
- """test setup - no input files"""
- test_extractor = extractors.RandomFeatureExtractor()
- with self.assertRaises(ValueError):
- test_extractor.setup()
- # NB test WITH sources implicitly tested in test_extract
-
- def test_extract_noinput(self):
- """Test extract with no input files"""
- test_extractor = extractors.RandomFeatureExtractor()
- with self.assertRaises(ValueError):
- test_extractor.run()
-
- def test_extract(self):
- """Test extract with input files"""
- video_source = TestSourceMedia().one_colour_silent_audio()
- test_extractor = extractors.RandomFeatureExtractor(input_files=video_source)
- test_extractor.setup()
- test_extractor.run()
- test_extractor.teardown()
- self.assertTrue(test_extractor.features)
-
- class TestLoudAudioFeatureExtractor(unittest.TestCase):
- """Test LoudAudioFeatureExtractor"""
- def test_init(self):
- video_source = TestSourceMedia().one_colour_silent_audio()
- test_extractor = extractors.LoudAudioFeatureExtractor(input_files=video_source)
- self.assertTrue(test_extractor)
-
- def test_init_noinput(self):
- """test init - no input files"""
- with self.assertRaises(ValueError):
- test_extractor = extractors.LoudAudioFeatureExtractor()
-
- def test_extract(self):
- """Test extract with input files"""
- video_source = TestSourceMedia().one_colour_silent_audio()
- test_extractor = extractors.LoudAudioFeatureExtractor(input_files=video_source)
- test_extractor.setup()
- test_extractor.run()
- test_extractor.teardown()
- self.assertEqual(test_extractor.features, [])
-
- class TestVideoActivityFeatureExtractor(unittest.TestCase):
- """Test VideoActivityFeatureExtractor"""
-
- def test_init(self):
- video_source = TestSourceMedia().one_colour_silent_audio()
- test_extractor = extractors.VideoActivityFeatureExtractor(input_files=video_source)
- self.assertTrue(test_extractor)
-
- def test_init_noinput(self):
- """test init - no input files"""
- with self.assertRaises(ValueError):
- test_extractor = extractors.VideoActivityFeatureExtractor()
-
- def test_extract(self):
- """Test extract with basic input file runs with no errors"""
- video_source = TestSourceMedia().one_colour_silent_audio()
- test_extractor = extractors.VideoActivityFeatureExtractor(input_files=video_source)
- test_extractor.setup()
- test_extractor.run()
- test_extractor.teardown()
- self.assertTrue(test_extractor.features)
-
- # TODO: add sample video with activity to test _activitydetect()
-
|