diff --git a/GPMF_gyro.py b/GPMF_gyro.py index 2390bc7c..ff56092c 100644 --- a/GPMF_gyro.py +++ b/GPMF_gyro.py @@ -4,7 +4,8 @@ import gpmf.parse as gpmf_parse from gpmf.extract import get_gpmf_payloads_from_file import sys - +import numpy as np +from matplotlib import pyplot as plt class Extractor: def __init__(self, videopath = "hero5.mp4"): @@ -12,21 +13,30 @@ def __init__(self, videopath = "hero5.mp4"): payloads, parser = get_gpmf_payloads_from_file(videopath) + self.parsed = [] + for gpmf_data, timestamps in payloads: - for element, parents in gpmf_parse.recursive(gpmf_data): - try: - value = gpmf_parse.parse_value(element) - except ValueError: - value = element.data - print("{} {} > {}: {}".format( - timestamps, - ' > '.join([x.decode('ascii') for x in parents]), - element.key.decode('ascii'), - value - )) + self.parsed.append(gpmf_parse.parse_dict(gpmf_data)) + + + + def get_gyro(self): - return 1 + self.gyro = [] + self.scal = 0 + for frame in self.parsed: + for stream in frame["DEVC"]["STRM"]: + if "GYRO" in stream: + self.gyro += stream["GYRO"] + self.scal = stream["SCAL"] + + + + omega = np.array(self.gyro) / self.scal + + plt.plot(omega[:,0]) + plt.show() def get_accl(self): return 1 @@ -36,4 +46,5 @@ def get_video_length(self): if __name__ == "__main__": - testing = Extractor() \ No newline at end of file + testing = Extractor() + testing.get_gyro() \ No newline at end of file diff --git a/gpmf/parse.py b/gpmf/parse.py index d53437a9..98f557b3 100644 --- a/gpmf/parse.py +++ b/gpmf/parse.py @@ -1,6 +1,7 @@ +# Derived from: +# https://github.com/rambo/python-gpmf # The MIT License (MIT) # Copyright (c) 2014 Eero af Heurlin -# https://github.com/rambo/python-gpmf #!/usr/bin/env python3 """Parses the FOURCC data in GPMF stream into fields""" @@ -42,6 +43,9 @@ def parse_value(element): """Parses element value""" + if not element: + return "HELLOWTHERE" + type_parsed = TYPES.parse(bytes([element.type])) #print("DEBUG: type_parsed={}, element.repeat={}, element.size={}, len(element.data): {}".format(type_parsed, element.repeat, element.size, len(element.data))) @@ -123,7 +127,49 @@ def recursive(data, parents=tuple()): yield subyield else: yield (element, parents) + +def parse_list(data, parent = []): + elements = construct.GreedyRange(FOURCC).parse(data) + + out_list = [] + for element in elements: + if element.type == 0: + + out_list.append(parse_list(element.data, out_list)) + else: + try: + value = parse_value(element) + except ValueError: + value = element.data + + out_list.append(value) + print(element.key) + + return out_list + +def parse_dict(data): + """Parse data into a dict recursively + """ + + elements = construct.GreedyRange(FOURCC).parse(data) + new_dict = dict() + + for element in elements: + if element.type == 0: + + if (element.key.decode('ascii') == "STRM"): + new_dict.setdefault("STRM", []).append(parse_dict(element.data)) + else: + new_dict[element.key.decode('ascii')] = parse_dict(element.data) + + else: + try: + value = parse_value(element) + except ValueError: + value = element.data + new_dict[element.key.decode('ascii')] = value + return new_dict if __name__ == '__main__': import sys