File size: 2,117 Bytes
f6b56a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from pathlib import Path
import gpmf
import numpy as np
from gyraudio.properties import GYRO_KEY, ACCL_KEY
def extract_imu_blocks(stream, key=GYRO_KEY):
    """ Extract imu data blocks from binary stream

    This is a generator on lists `KVLItem` objects. In
    the GPMF stream, imu data comes into blocks of several
    different data items. For each of these blocks we return a list.

    Parameters
    ----------
    stream: bytes
        The raw GPMF binary stream

    Returns
    -------
    imu_items_generator: generator
        Generator of lists of `KVLItem` objects
    """
    for s in gpmf.parse.filter_klv(stream, "STRM"):
        content = []
        is_imu = False
        for elt in s.value:
            content.append(elt)
            if elt.key == key:
                is_imu = True
        if is_imu:
            yield content


def parse_imu_block(imu_block, key=GYRO_KEY):
    """Turn imu data blocks into `imuData` objects

    Parameters
    ----------
    imu_block: list of KVLItem
        A list of KVLItem corresponding to a imu data block.

    Returns
    -------
    imu_data: imuData
        A imuData object holding the imu information of a block.
    """
    block_dict = {
        s.key: s for s in imu_block
    }

    imu_data = block_dict[key].value * 1.0 / block_dict["SCAL"].value
    return {
        "timestamp": block_dict["TSMP"],
        key: imu_data,
    }


def get_imu_data(pth: Path) -> np.array:
    """Extract imu data from GPMF metadata from a Gopro video file

    Parameters
    ----------
    path: str
        Path to the GPMF file

    Returns
    -------
    imu_data: list of imuData
        List of imuData objects holding the imu information of the file.
    """
    stream = gpmf.io.extract_gpmf_stream(pth)
    imu_data_dict = {}
    for key in [GYRO_KEY, ACCL_KEY]:
        imu_blocks = extract_imu_blocks(stream, key=key)
        imu_data = [parse_imu_block(imu_block, key=key) for imu_block in imu_blocks]
        imu_data_dict[key] = np.vstack([np.array(imu[key]) for imu in imu_data])
    return imu_data_dict[GYRO_KEY], imu_data_dict[ACCL_KEY]