Created
September 30, 2019 09:25
-
-
Save papr/423a4e9dec1c57e05c56ef81dbdb00a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Part 1: Load file and visualize/transform single reference locations | |
| # Requires https://pypi.org/project/msgpack/ | |
| import sys | |
| import msgpack | |
| # Adjust the following variables to your recording | |
| world_video_resolution = (1280, 720) | |
| path_to_reference_locations = "/Volumes/32TB/users/ppr/recordings/2019_09_04/000/offline_data/reference_locations.msgpack" | |
| with open(path_to_reference_locations, "rb") as fh: | |
| reference_locations = msgpack.unpack(fh, raw=False) | |
| assert reference_locations["version"] == 1, "Unknown reference locations format" | |
| data = reference_locations["data"] | |
| if not data: | |
| print("Warning: No reference data found! Aborting.") | |
| sys.exit(1) | |
| # Each reference location consists of: | |
| # Location in pixels `[x, y]` | |
| # Frame index `idx` | |
| # Frame timestamp `ts` | |
| [[x, y], idx, ts] = data[0] # first element | |
| print(f"First reference location:\n\tx={x}\n\ty={y}\n\tidx={idx}\n\tts={ts}") | |
| x_norm = x / world_video_resolution[0] | |
| y_norm = y / world_video_resolution[1] | |
| print(f"Normalized reference location:\n\tx={x_norm}\n\ty={y_norm}") | |
| # Part 2: Load and transform all reference locations with pandas | |
| # Requires: https://pypi.org/project/pandas/ | |
| # In order to load the data with pandas, we need to flatten the data: | |
| # The new data format will be `[x, y, idx, ts]` instead of `[[x, y], idx, ts]` | |
| data = [[*pos, idx, ts] for (pos, idx, ts) in data] | |
| import pandas as pd | |
| data_frame = pd.DataFrame( | |
| data, columns=["pixels_x", "pixels_y", "frame_idx", "frame_ts"] | |
| ) | |
| data_frame["norm_x"] = data_frame["pixels_x"] / world_video_resolution[0] | |
| data_frame["norm_y"] = data_frame["pixels_y"] / world_video_resolution[1] | |
| print("Loaded and transformed reference locations:") | |
| print(data_frame.head()) | |
| # Part 3: Saving result as csv | |
| # Use pathlib for simple path manipulations: | |
| # https://docs.python.org/3/library/pathlib.html#module-pathlib | |
| import pathlib | |
| path_to_reference_locations = pathlib.Path(path_to_reference_locations) | |
| path_to_reference_locations_csv = path_to_reference_locations.with_suffix(".csv") | |
| print(f"Writing result to {path_to_reference_locations_csv}") | |
| data_frame.to_csv(path_to_reference_locations_csv, index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment