2024-11-12 21:36:37 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Visualise tracking data\n",
"\n",
"This notebook is an adapted version of `process.py` as it is included in the Trajectron++ package. It can be used to \n",
"\n",
"* Visualise the recorded trajectories, which are normally parsed to a Trajectron++ Node"
]
},
{
"cell_type": "code",
2024-11-14 17:01:32 +01:00
"execution_count": 1,
2024-11-12 21:36:37 +01:00
"metadata": {},
2024-11-14 17:01:32 +01:00
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/ruben/suspicion/trap/.venv/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
2024-11-12 21:36:37 +01:00
"source": [
"import sys\n",
"import os\n",
"import numpy as np\n",
"import pandas as pd\n",
"import dill\n",
"import tqdm\n",
"import matplotlib.pyplot as plt\n",
"\n",
"#sys.path.append(\"../../\")\n",
"from trajectron.environment import Environment, Scene, Node\n",
2024-11-12 21:37:20 +01:00
"from trajectron.environment import derivative_of\n",
"\n",
"from trap.tracker import Smoother"
2024-11-12 21:36:37 +01:00
]
},
{
"cell_type": "code",
2024-11-14 17:01:32 +01:00
"execution_count": 2,
2024-11-12 21:36:37 +01:00
"metadata": {},
"outputs": [],
"source": [
2024-11-12 21:37:20 +01:00
"smoothing = True\n",
2024-11-12 21:36:37 +01:00
"desired_max_time = 100\n",
"pred_indices = [2, 3]\n",
"state_dim = 6\n",
"frame_diff = 10\n",
"desired_frame_diff = 1\n",
"dt = 0.1\n",
2024-11-14 17:01:32 +01:00
"min_track_length = 20\n",
2024-11-12 21:36:37 +01:00
"\n",
"standardization = {\n",
" 'PEDESTRIAN': {\n",
" 'position': {\n",
" 'x': {'mean': 0, 'std': 1},\n",
" 'y': {'mean': 0, 'std': 1}\n",
" },\n",
" 'velocity': {\n",
" 'x': {'mean': 0, 'std': 2},\n",
" 'y': {'mean': 0, 'std': 2}\n",
" },\n",
" 'acceleration': {\n",
" 'x': {'mean': 0, 'std': 1},\n",
" 'y': {'mean': 0, 'std': 1}\n",
" }\n",
" }\n",
"}\n",
"\n",
"data_columns = pd.MultiIndex.from_product([['position', 'velocity', 'acceleration'], ['x', 'y']])\n",
"\n",
"\n",
"# desired_source = 'EXPERIMENTS/raw/hof-meter-maskrcnn2'\n",
"# desired_source = 'EXPERIMENTS/20240424-hof-meter-maskrcnn2'\n",
"# desired_source = 'EXPERIMENTS/20240426-hof-yolo'\n",
2024-12-10 15:43:30 +01:00
"desired_source = 'EXPERIMENTS/raw/hof3'\n"
2024-11-12 21:36:37 +01:00
]
},
{
"cell_type": "code",
2024-11-14 17:01:32 +01:00
"execution_count": 3,
2024-11-12 21:36:37 +01:00
"metadata": {},
"outputs": [],
"source": [
"# run some tests\n",
"\n",
"if not os.path.exists(desired_source):\n",
" raise FileNotFoundError(f\"Path does not exist {desired_source=}\")"
]
},
{
"cell_type": "code",
2024-12-10 15:43:30 +01:00
"execution_count": null,
2024-11-12 21:36:37 +01:00
"metadata": {},
"outputs": [],
"source": [
"from matplotlib.axes import Axes\n",
"from trap.frame_emitter import DetectionState\n",
"\n",
2024-11-12 21:37:20 +01:00
"if smoothing:\n",
" smoother = Smoother(window_len=12, convolution=False)\n",
"\n",
2024-11-12 21:36:37 +01:00
"def parse_txt(data_path, dt, axes: Axes, axes2: Axes) -> dict[str, pd.DataFrame]:\n",
" skipped_for_error = 0\n",
" created = 0\n",
"\n",
" data = pd.read_csv(data_path, sep='\\t', index_col=False, header=None)\n",
" data.columns = ['frame_id', 'track_id', 'l','t', 'w','h', 'pos_x', 'pos_y', 'state']\n",
" # data['frame_id'] = pd.to_numeric(data['frame_id'], downcast='integer')\n",
" data['track_id'] = pd.to_numeric(data['track_id'], downcast='integer')\n",
"\n",
" data['frame_id'] = data['frame_id'] // 10\n",
" data['frame_id'] -= data['frame_id'].min()\n",
"\n",
" data['node_type'] = 'PEDESTRIAN'\n",
" data['node_id'] = data['track_id'].astype(str)\n",
" data['state'] = data['state'].apply(lambda x: (eval(x) if type(x) is str else DetectionState(x)).value)\n",
" data.sort_values('frame_id', inplace=True)\n",
"\n",
" # Mean Position\n",
"\n",
" print(\"Means: x:\", data['pos_x'].mean(), \"y:\", data['pos_y'].mean())\n",
" data['pos_x'] = data['pos_x'] - data['pos_x'].mean()\n",
" data['pos_y'] = data['pos_y'] - data['pos_y'].mean()\n",
"\n",
" max_timesteps = data['frame_id'].max()\n",
"\n",
" nodes = {}\n",
"\n",
" # only keep tentative and confirmed detections\n",
" # print(data)\n",
" data = data.loc[data['state'] != DetectionState.Lost.value]\n",
"\n",
2024-11-12 21:37:20 +01:00
" # print(data[['track_id', 'node_id', 'state']])\n",
2024-11-12 21:36:37 +01:00
"\n",
" for node_id in tqdm.tqdm(pd.unique(data['node_id'])):\n",
" node_df = data[data['node_id'] == node_id]\n",
" if not np.all(np.diff(node_df['frame_id']) == 1):\n",
" # print(f\"Interval in {node_id} not always 1\")\n",
" # print(node_df['frame_id'])\n",
" # print(np.diff(node_df['frame_id']) != 1)\n",
" # mask=np.append(False, np.diff(node_df['frame_id']) != 1)\n",
" # print(node_df[mask]['frame_id'])\n",
" skipped_for_error += 1\n",
" continue\n",
"\n",
"\n",
" node_values = node_df[['pos_x', 'pos_y']].values\n",
"\n",
2024-11-12 21:37:20 +01:00
" if node_values.shape[0] <= min_track_length:\n",
2024-11-12 21:36:37 +01:00
" continue\n",
"\n",
" new_first_idx = node_df['frame_id'].iloc[0]\n",
"\n",
2024-11-12 21:37:20 +01:00
" x = smoother.smooth(node_values[:, 0])\n",
2024-12-10 15:43:30 +01:00
" y = smoother.smooth(-node_values[:, 1]) # OpenCV a right-handed one (y looking down) coordinate system\n",
2024-11-12 21:36:37 +01:00
" vx = derivative_of(x, dt)\n",
" vy = derivative_of(y, dt)\n",
" ax = derivative_of(vx, dt)\n",
" ay = derivative_of(vy, dt)\n",
"\n",
" \n",
" axes.plot(x, y, alpha=.3)\n",
" axes.scatter(x, y, marker='x', alpha=.3)\n",
"\n",
" nv = node_df[['l','t', 'w', 'h']].values\n",
2024-11-12 21:37:20 +01:00
" x = smoother.smooth(nv[:, 0] + .5*nv[:, 2])\n",
" y = smoother.smooth(nv[:, 1] + nv[:, 3])\n",
2024-11-12 21:36:37 +01:00
" axes2.plot(x, y, alpha=.3)\n",
" axes2.scatter(x, y, marker='x', alpha=.3)\n",
"\n",
" nodes[node_id] = [x,y]\n",
" # data_dict = {'node_id': node_id, ('position', 'x'): x,\n",
" # ('position', 'y'): y,\n",
" # ('velocity', 'x'): vx,\n",
" # ('velocity', 'y'): vy,\n",
" # ('acceleration', 'x'): ax,\n",
" # ('acceleration', 'y'): ay}\n",
" \n",
" # node_data = pd.DataFrame(data_dict, columns=data_columns)\n",
" # nodes[node_id] = node_data\n",
" return nodes"
]
},
{
"cell_type": "code",
2024-12-10 15:43:30 +01:00
"execution_count": 9,
2024-11-12 21:36:37 +01:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-12-10 15:43:30 +01:00
"tracked.txt\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Means: x: 11.879941019858627 y: 7.810144445001718\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 3232/3232 [00:58<00:00, 55.53it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"tracked.txt\n",
"Means: x: 12.897854652926158 y: 8.370203861117504\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 519/519 [00:02<00:00, 242.70it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"tracked.txt\n",
"Means: x: 12.625057777854769 y: 8.071677215100557\n"
2024-11-12 21:36:37 +01:00
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
2024-12-10 15:43:30 +01:00
"100%|██████████| 291/291 [00:01<00:00, 257.23it/s]\n"
2024-11-12 21:36:37 +01:00
]
},
{
"data": {
"text/plain": [
2024-12-10 15:43:30 +01:00
"345"
2024-11-12 21:36:37 +01:00
]
},
2024-12-10 15:43:30 +01:00
"execution_count": 9,
2024-11-12 21:36:37 +01:00
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
2024-12-10 15:43:30 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA/AAAAT7CAYAAAA5P1tDAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz9eYztaX7Xeb6f5becPfaIu9+bN/el9ipTVbbBqDXdDKYZDYMGBjQIJNAwjCz4AwkjmX8s22MkGKRRjxG0mrZ6sDVmGg80Izc0GPBaVc6qrFwr17vGXWKPs/6WZ5s/4maWcyrLNlTdvLl8X1IolXF+9+g58ZMizud8n+/3USmlhBBCCCGEEEIIIT7Q9INegBBCCCGEEEIIIX5/EuCFEEIIIYQQQogPAQnwQgghhBBCCCHEh4AEeCGEEEIIIYQQ4kNAArwQQgghhBBCCPEhIAFeCCGEEEIIIYT4EJAAL4QQQgghhBBCfAjYB72AD4oYI7dv32YwGKCUetDLEUIIIYQQQgjxIZRSYjqdcvr0abT+/tbMJcDfc/v2bc6dO/eglyGEEEIIIYQQ4iPg5s2bnD179vv6nBLg7xkMBsDJD3k4HD7g1QghhBBCCCGE+DCaTCacO3funYz5/SQB/p63t80Ph0MJ8EIIIYQQQgghvif3ozX7IzPELoTAT/zET3Dp0iU6nQ6XL1/mJ3/yJ0kpPeilCSGEEEIIIYQQ37OPTAX+Z3/2Z/m5n/s5fv7nf56nnnqKZ599lr/4F/8io9GIH/uxH3vQyxNCCCGEEEIIIb4nH5kA/1u/9Vv8yT/5J/njf/yPA3Dx4kV+8Rd/ka997WvveX3TNDRN887/TyaT92WdQgghhBBCCCHEf46PzBb6L33pS/y7f/fve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{},
"output_type": "display_data"
}
],
"source": [
"fig = plt.figure(figsize=(20,16))\n",
"ax1, ax2 = fig.subplots(2)\n",
"\n",
"import cv2\n",
"# ax = fig1.gca()\n",
"# ax2 = fig2.gca()\n",
"im = cv2.imread(\"../DATASETS/hof2/output.png\")\n",
"ax2.imshow(im)\n",
"ax1.set_aspect(1)\n",
"ax2.set_aspect(1)\n",
"nodes = []\n",
"for data_class in ['train', 'val', 'test']:\n",
" target_dir = os.path.join(desired_source, data_class)\n",
" for subdir, dirs, files in os.walk(target_dir):\n",
" for file in files:\n",
" if file.endswith('.txt'):\n",
2024-12-10 15:43:30 +01:00
" print(data_class, file)\n",
2024-11-12 21:36:37 +01:00
" input_data_dict = dict()\n",
" full_data_path = os.path.join(subdir, file)\n",
" nodes.extend(parse_txt(full_data_path, dt, ax1, ax2))\n",
2024-12-10 15:43:30 +01:00
" # break\n",
" # break\n",
" # break\n",
2024-11-12 21:36:37 +01:00
"fig.show()\n",
"len(nodes)"
]
2024-11-12 21:37:20 +01:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
2024-11-12 21:36:37 +01:00
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}