trap/test_tracking_data.ipynb

286 lines
1.9 MiB
Text
Raw Normal View History

{
"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-12 20:37:20 +00:00
"execution_count": 20,
"metadata": {},
"outputs": [],
"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 20:37:20 +00:00
"from trajectron.environment import derivative_of\n",
"\n",
"from trap.tracker import Smoother"
]
},
{
"cell_type": "code",
2024-11-12 20:37:20 +00:00
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
2024-11-12 20:37:20 +00:00
"smoothing = True\n",
"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-12 20:37:20 +00:00
"min_track_length = 10\n",
"\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",
"desired_source = 'EXPERIMENTS/raw/hof2'\n"
]
},
{
"cell_type": "code",
2024-11-12 20:37:20 +00:00
"execution_count": 22,
"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-11-12 20:37:20 +00:00
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib.axes import Axes\n",
"from trap.frame_emitter import DetectionState\n",
"\n",
2024-11-12 20:37:20 +00:00
"if smoothing:\n",
" smoother = Smoother(window_len=12, convolution=False)\n",
"\n",
"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 20:37:20 +00:00
" # print(data[['track_id', 'node_id', 'state']])\n",
"\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 20:37:20 +00:00
" if node_values.shape[0] <= min_track_length:\n",
" continue\n",
"\n",
" new_first_idx = node_df['frame_id'].iloc[0]\n",
"\n",
2024-11-12 20:37:20 +00:00
" x = smoother.smooth(node_values[:, 0])\n",
" y = smoother.smooth(-node_values[:, 1])\n",
" 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 20:37:20 +00:00
" x = smoother.smooth(nv[:, 0] + .5*nv[:, 2])\n",
" y = smoother.smooth(nv[:, 1] + nv[:, 3])\n",
" 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-11-12 20:37:20 +00:00
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-11-12 20:37:20 +00:00
"Means: x: 1294.2634385663268 y: 820.3295331686878\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
2024-11-12 20:37:20 +00:00
"100%|██████████| 1823/1823 [00:17<00:00, 105.14it/s]\n"
]
},
{
"data": {
"text/plain": [
2024-11-12 20:37:20 +00:00
"191"
]
},
2024-11-12 20:37:20 +00:00
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
2024-11-12 20:37:20 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA/AAAAT7CAYAAAA5P1tDAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz9ebCl113f/77X8Ex7PEOfPuf03BpszXLLMrJs5+cklm2MTeLCN7n+lUJxbyicIkBikkCFuthFAcHBNwGuIcGBooAUJqmkfgUXfImDfvaPWZZtWXNLao09n/mcPT/TWuv+caRjN7aDbamH0/q+VKeq+3mevfezV5d27c/5rvVdKoQQEEIIIYQQQgghxBVNX+4bEEIIIYQQQgghxN9MArwQQgghhBBCCLELSIAXQgghhBBCCCF2AQnwQgghhBBCCCHELiABXgghhBBCCCGE2AUkwAshhBBCCCGEELuABHghhBBCCCGEEGIXsJf7BsTu473n3LlztNttlFKX+3aEEEIIIYQQF1kIgcFgwL59+9Ba6sCXiwR48S07d+4cBw8evNy3IYQQQgghhLjETp8+zYEDBy73bbxmSYAX37J2uw1s/8/b6XQu890IIYQQQgghLrZ+v8/Bgwd3soC4PCTAi2/Zy9PmO52OBHghhBBCCCFeQ2QJ7eUlixeEEEIIIYQQQohdQAK8EEIIIYQQQgixC0iAF0IIIYQQQgghdgEJ8EIIIYQQQgghxC4gAV4IIY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9APthqFoG87NbXD
"text/plain": [
"<Figure size 2000x1600 with 2 Axes>"
]
},
"metadata": {},
"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",
" 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",
" break\n",
" break\n",
" break\n",
"fig.show()\n",
"len(nodes)"
]
2024-11-12 20:37:20 +00:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}