def test_check_target_type(): stim = ComplexTextStim(join(TEXT_DIR, 'sample_text.txt'), columns='to', default_duration=1) td = SharpnessExtractor() with pytest.raises(TypeError): td.transform(stim)
def test_sharpness_extractor(): pytest.importorskip('cv2') image_dir = join(get_test_data_path(), 'image') stim = ImageStim(join(image_dir, 'apple.jpg')) result = SharpnessExtractor().transform(stim).to_df() sharpness = result['sharpness'][0] assert np.isclose(sharpness, 1.0, 1e-5)
def test_sharpness_extractor(): pytest.importorskip('cv2') stim = ImageStim(join(IMAGE_DIR, 'apple.jpg'), onset=4.2, duration=1) result = SharpnessExtractor().transform(stim).to_df() sharpness = result['sharpness'][0] assert np.isclose(sharpness, 1.0, 1e-5) assert result['onset'][0] == 4.2 assert result['duration'][0] == 1
def test_within_pipeline(): pytest.importorskip('cv2') pytest.importorskip('sklearn') from sklearn.pipeline import Pipeline from sklearn.preprocessing import Normalizer stim = join(get_test_data_path(), 'image', 'apple.jpg') graph = Graph([BrightnessExtractor(), SharpnessExtractor()]) trans = PliersTransformer(graph) normalizer = Normalizer() pipeline = Pipeline([('pliers', trans), ('normalizer', normalizer)]) res = pipeline.fit_transform(stim) assert res.shape == (1, 2) assert np.isclose(res[0][0], 0.66393, 1e-5) assert np.isclose(res[0][1], 0.74780, 1e-5) meta = trans.metadata_ assert 'onset' in meta.columns assert meta['class'][0] == 'ImageStim'
def extract_visual_features(video_file): """ This function extracts luminance, vibrance, saliency, and sharpness from the frames of a video using the pliers library. If you use this function, please cite the pliers library directly: https://github.com/PsychoinformaticsLab/pliers#how-to-cite Parameters ---------- video_file: str Path to video file to analyze. Returns ------- low_level_video_df: DataFrame Pandas dataframe with a column per low-level feature.py (index is time). """ # extract video luminance print('Extracting brightness...') from pliers.extractors import BrightnessExtractor brightext = BrightnessExtractor() brightres = brightext.transform(video_file) brightres_df = pd.DataFrame(columns=brightres[0].to_df().columns) for a, ob in enumerate(brightres): t = ob.to_df() t['order'] = a brightres_df = brightres_df.append(t, ignore_index=True) # extract saliency print('Extracting saliency...') from pliers.extractors import SaliencyExtractor salext = SaliencyExtractor() salres = salext.transform(video_file) salres_df = pd.DataFrame(columns=salres[0].to_df().columns) for a, ob in enumerate(salres): t = ob.to_df() t['order'] = a salres_df = salres_df.append(t, ignore_index=True) # extract sharpness print('Extracting sharpness...') from pliers.extractors import SharpnessExtractor sharpext = SharpnessExtractor() sharpres = sharpext.transform(video_file) sharpres_df = pd.DataFrame(columns=sharpres[0].to_df().columns) for a, ob in enumerate(sharpres): t = ob.to_df() t['order'] = a sharpres_df = sharpres_df.append(t, ignore_index=True) # extract vibrance print('Extracting vibrance...') from pliers.extractors import VibranceExtractor vibext = VibranceExtractor() vibres = vibext.transform(video_file) vibres_df = pd.DataFrame(columns=vibres[0].to_df().columns) for a, ob in enumerate(vibres): t = ob.to_df() t['order'] = a vibres_df = vibres_df.append(t, ignore_index=True) # combine into 1 dataframe print('Combining data...') low_level_video_df = brightres_df.merge(salres_df[salres_df.columns[4:]], left_index=True, right_index=True) low_level_video_df = low_level_video_df.merge( sharpres_df[sharpres_df.columns[4:]], left_index=True, right_index=True) low_level_video_df = low_level_video_df.merge( vibres_df[vibres_df.columns[4:]], left_index=True, right_index=True) low_level_video_df['onset_ms'] = low_level_video_df['onset'] * 1000 low_level_video_df.index = pd.to_datetime(low_level_video_df['onset_ms'], unit='ms') low_level_video_df = low_level_video_df.drop( ['max_saliency', 'max_y', 'max_x', 'onset', 'object_id', 'order'], axis=1) low_level_video_df.index.name = None print('Visual feature extraction complete.') return low_level_video_df