Skip to content

Instantly share code, notes, and snippets.

@papr
Created September 28, 2016 08:44
Show Gist options
  • Save papr/6a9f0517534811e6844a27a1909483e2 to your computer and use it in GitHub Desktop.
Save papr/6a9f0517534811e6844a27a1909483e2 to your computer and use it in GitHub Desktop.
Exports pickled annotations to csv
import pickle, os, sys, csv
def load_object(file_path):
"""Taken from https://github.com/pupil-labs/pupil/blob/master/pupil_src/shared_modules/file_methods.py
Args:
file_path (str): Annotation pickle file
Returns:
list: Annotation dicts
"""
file_path = os.path.expanduser(file_path)
#reading to string and loads is 2.5x faster that using the file handle and load.
with open(file_path,'rb') as fh:
data = fh.read()
return pickle.loads(data)
if len(sys.argv) != 2:
print 'Usage: python export_annotations.py recording_directory'
sys.exit()
rec_dir = os.path.abspath(os.path.expanduser(sys.argv[1]))
annot_file = os.path.join(rec_dir, 'annotations')
export_keys = ['index', 'timestamp', 'label', 'source', 'added_in_player', 'duration']
annotations = load_object(annot_file)
with open(os.path.join(rec_dir, 'annotations.csv'), 'wb') as csvfile:
annot_writer = csv.writer(csvfile)
annot_writer.writerow(export_keys)
for annot in annotations:
annot_writer.writerow([annot[key] for key in export_keys])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment