Jul-07-2020, 06:07 AM
Hi! I am currently using a python package (Pliers by tyarkoni on Github: http://tyarkoni.github.io/pliers/results.html) and I am running into errors while trying to get a dataframe output. I am only a beginner coder.
This is my code based on their syntax:
Here is the google.py code where the error happened, in the text below the error line 80 is located in line 31:
This is my code based on their syntax:
from pliers.stimuli import VideoStim from pliers.filters import FrameSamplingFilter from pliers.extractors import GoogleVisionAPIFaceExtractor, merge_results video = VideoStim('/Users/queenknight/OneDrive/Documents/movie100s.mp4') # Sample 1 frames per second sampler = FrameSamplingFilter(hertz=1) frames = sampler.transform(video) ext = GoogleVisionAPIFaceExtractor() results = ext.transform(frames) df = merge_results(results, ) df.to_excel('movie100s_googleface.xlsx', index=False, header=True)Here is the error I got:
Error:638it [47:07, 4.43s/it]
Stim: 638it [00:00, 73165.80it/s]
Traceback (most recent call last):
File "/Users/queenknight/PycharmProjects/Movie/Faceextractor_movie.py", line 13, in <module>
df = merge_results(results, )
File "/Users/queenknight/OneDrive/EDX/lib/python3.8/site-packages/pliers/extractors/base.py", line 271, in merge_results
dfs.append(r.to_df(timing=_timing, metadata=metadata,
File "/Users/queenknight/OneDrive/EDX/lib/python3.8/site-packages/pliers/extractors/base.py", line 114, in to_df
df = self.extractor._to_df(self, **to_df_kwargs)
File "/Users/queenknight/OneDrive/EDX/lib/python3.8/site-packages/pliers/extractors/api/google.py", line 80, in _to_df
name = 'landmark_' + lm['type'] + '_%s'
KeyError: 'type'
Tracing back, I think the error came from line 13 of my code line df = merge_results(results, ). This was based on their website (details in page linked above) so I cannot figure out what went wrong.
Here is the google.py code where the error happened, in the text below the error line 80 is located in line 31:
def _to_df(self, result, handle_annotations=None): ''' Converts a Google API Face JSON response into a Pandas Dataframe. Args: result (ExtractorResult): Result object from which to parse out a Dataframe. handle_annotations (str): How returned face annotations should be handled in cases where there are multiple faces. 'first' indicates to only use the first face JSON object, all other values will default to including every face. ''' annotations = result._data if handle_annotations == 'first': annotations = [annotations[0]] face_results = [] for i, annotation in enumerate(annotations): data_dict = {} for field, val in annotation.items(): if 'Confidence' in field: data_dict['face_' + field] = val elif 'oundingPoly' in field: for j, vertex in enumerate(val['vertices']): for dim in ['x', 'y']: name = '%s_vertex%d_%s' % (field, j+1, dim) val = vertex[dim] if dim in vertex else np.nan data_dict[name] = val elif field == 'landmarks': for lm in val: name = 'landmark_' + lm['type'] + '_%s' lm_pos = {name % k: v for (k, v) in lm['position'].items()} data_dict.update(lm_pos) else: data_dict[field] = val face_results.append(data_dict) return pd.DataFrame(face_results)I also was not able to do
df = results.to_df()
Error:AttributeError: 'list' object has no attribute 'to_df'
For your information, print(type(results))gives
Output:<class 'list'>
I am aware that this question is very specific to the use of an external python package, but will appreciate any help. Let me know if I should provide more code, eg. base.py. Thank you so much!!