trap/test_training_data.ipynb
2024-12-06 08:28:48 +01:00

2.4 MiB

In [1]:
from pathlib import Path
from typing import List
from trap.frame_emitter import Track
from trap.frame_emitter import Camera
from trap.tracker import TrackReader


path = Path("EXPERIMENTS/raw/hof3/")
calibration_path = Path("../DATASETS/hof3/calibration.json")
homography_path = Path("../DATASETS/hof3/homography.json")
camera = Camera.from_paths(calibration_path, homography_path, 12)
/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
  from .autonotebook import tqdm as notebook_tqdm
In [2]:
reader = TrackReader(path, camera.fps, exclude_whitelisted = False, include_blacklisted=False)
In [ ]:
from trap.tracker import FinalDisplacementFilter


tracks: List[Track] = [t for t in reader]
filter = FinalDisplacementFilter(2)
tracks = filter.apply(tracks, camera)
In [4]:
tracks[0]
Out[4]:
Track(track_id='7', history=[Detection(track_id='7', l=1320.8199462890625, t=251.9683380126953, w=111.053955078125, h=234.67745971679688, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3264, det_class=1), Detection(track_id='7', l=1339.9742431640625, t=238.920166015625, w=105.037109375, h=222.37574768066406, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3265, det_class=1), Detection(track_id='7', l=1352.3323974609375, t=229.05804443359375, w=104.625732421875, h=221.97633361816406, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3266, det_class=1), Detection(track_id='7', l=1359.267822265625, t=220.7544708251953, w=106.5965576171875, h=226.98056030273438, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3267, det_class=1), Detection(track_id='7', l=1362.710693359375, t=213.61810302734375, w=109.2371826171875, h=233.524169921875, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3268, det_class=1), Detection(track_id='7', l=1364.126708984375, t=203.14120483398438, w=111.1407470703125, h=238.55650329589844, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3269, det_class=1), Detection(track_id='7', l=1367.715576171875, t=190.77456665039062, w=112.6883544921875, h=245.13031005859375, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3270, det_class=1), Detection(track_id='7', l=1375.32275390625, t=181.7355194091797, w=108.6031494140625, h=238.25833129882812, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3271, det_class=1), Detection(track_id='7', l=1380.20654296875, t=170.72500610351562, w=103.9525146484375, h=230.0364227294922, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3272, det_class=1), Detection(track_id='7', l=1385.44189453125, t=161.61102294921875, w=97.076416015625, h=216.43988037109375, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3273, det_class=1), Detection(track_id='7', l=1387.068603515625, t=149.4908447265625, w=97.640869140625, h=220.21246337890625, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3274, det_class=1), Detection(track_id='7', l=1389.26220703125, t=143.24551391601562, w=97.9940185546875, h=224.08978271484375, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3275, det_class=1), Detection(track_id='7', l=1388.7193603515625, t=135.30145263671875, w=98.697021484375, h=228.9639129638672, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3276, det_class=1), Detection(track_id='7', l=1391.044921875, t=126.18832397460938, w=95.349853515625, h=223.96878051757812, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3277, det_class=1), Detection(track_id='7', l=1392.5245361328125, t=115.1940689086914, w=91.294921875, h=215.87339782714844, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3278, det_class=1), Detection(track_id='7', l=1394.88720703125, t=104.04446411132812, w=87.3721923828125, h=207.13995361328125, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3279, det_class=1), Detection(track_id='7', l=1395.7288818359375, t=94.92156982421875, w=87.2978515625, h=207.88204956054688, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3280, det_class=1), Detection(track_id='7', l=1395.14404296875, t=89.08332061767578, w=87.2071533203125, h=209.1320037841797, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3281, det_class=1), Detection(track_id='7', l=1393.6158447265625, t=83.175048828125, w=88.0908203125, h=213.34136962890625, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3282, det_class=1), Detection(track_id='7', l=1393.5189208984375, t=79.1900634765625, w=87.788330078125, h=212.45541381835938, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3283, det_class=1), Detection(track_id='7', l=1392.7437744140625, t=71.73565673828125, w=84.64892578125, h=204.18603515625, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3284, det_class=1), Detection(track_id='7', l=1391.8515625, t=61.526771545410156, w=79.6556396484375, h=191.9879913330078, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3285, det_class=1), Detection(track_id='7', l=1390.78076171875, t=58.899993896484375, w=77.8787841796875, h=187.45034790039062, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3286, det_class=1), Detection(track_id='7', l=1389.577392578125, t=54.75666809082031, w=77.93115234375, h=188.03045654296875, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3287, det_class=1), Detection(track_id='7', l=1387.60986328125, t=49.06880187988281, w=79.3365478515625, h=193.26239013671875, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3288, det_class=1), Detection(track_id='7', l=1385.806396484375, t=39.1656494140625, w=80.3294677734375, h=197.7169189453125, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3289, det_class=1), Detection(track_id='7', l=1385.39453125, t=38.04023742675781, w=78.2938232421875, h=193.96432495117188, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3290, det_class=1), Detection(track_id='7', l=1387.2510986328125, t=26.112152099609375, w=71.546875, h=176.45062255859375, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3291, det_class=1), Detection(track_id='7', l=1384.037353515625, t=11.176986694335938, w=72.8916015625, h=180.7523193359375, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3292, det_class=1), Detection(track_id='7', l=1383.413818359375, t=2.1067962646484375, w=74.3514404296875, h=185.46435546875, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3293, det_class=1), Detection(track_id='7', l=1383.9927978515625, t=0.0, w=74.515625, h=185.3157501220703, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3294, det_class=1), Detection(track_id='7', l=1381.23828125, t=0.0, w=74.3914794921875, h=185.56240844726562, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3295, det_class=1), Detection(track_id='7', l=1375.34130859375, t=0.0, w=68.0294189453125, h=170.82057189941406, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3296, det_class=1), Detection(track_id='7', l=1377.630126953125, t=0.0, w=62.1622314453125, h=153.26541137695312, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3297, det_class=1), Detection(track_id='7', l=1377.28564453125, t=0.0, w=60.074951171875, h=145.32818603515625, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3298, det_class=1), Detection(track_id='7', l=1374.962890625, t=0.0, w=60.5577392578125, h=145.00042724609375, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3299, det_class=1), Detection(track_id='7', l=1371.738525390625, t=0.0, w=60.5992431640625, h=142.3328094482422, conf=1, state=<DetectionState.Confirmed: 2>, frame_nr=3300, det_class=1)], predictor_history=None, predictions=None, fps=12, source=1)
In [5]:
from trap.tracker import Smoother


smoother = Smoother(12, convolution=True)
In [6]:
import cv2
from matplotlib import pyplot as plt
from matplotlib.axes import Axes
import tqdm
In [7]:
fig = plt.figure(figsize=(20,16))
ax1, ax2 = fig.subplots(2)

im = cv2.imread("../DATASETS/hof3/output.png")
ax2.imshow(im)
ax1.set_aspect(1)
ax2.set_aspect(1)



for track in (bar:= tqdm.tqdm(tracks)):
    t = track.get_with_interpolated_history()
    t = smoother.smooth_track(t)
    points = t.get_projected_history(None, camera)
    x, y = points[:,0], points[:,1]
    ax1.plot(x, y, alpha=.2)
    ax1.scatter(x, y, marker='x', alpha=.2)
    l = [d.get_foot_coords()[0] for d in track.history]
    t = [d.get_foot_coords()[1] for d in track.history]
    ax2.plot(l, t, alpha=.2)
    ax2.scatter(l, t, marker='x', alpha=.2)
    # if bar.n > 100:
    #     break
  0%|          | 0/4722 [00:00<?, ?it/s]100%|██████████| 4722/4722 [01:11<00:00, 65.98it/s] 
In [10]:
import numpy as np
from trajectron.environment import Environment

from trap.tracker import Smoother
env = Environment(node_type_list=['PEDESTRIAN'], standardization=None)
n = 9
# n = 20
# n = 21
# print([d.frame_nr for d in tracks[n].history])
track = tracks[n].get_with_interpolated_history()
node_s = smoother.smooth_track(track).to_trajectron_node(camera, env)
node_o = track.to_trajectron_node(camera, env)
# print(node.data[:, {'velocity': ['x', 'y']}] * 12)

fig = plt.figure(figsize=(20,16))
(ax1, ax2), (ax3, ax4), (ax5, ax6) = fig.subplots(3,2)

for i in range(len(track.history)):
    ax1.plot(node_o.data[:, {'position': ['x']}])
    ax1.plot(node_s.data[:, {'position': ['x']}])
    ax2.plot(node_o.data[:, {'position': ['y']}])
    ax2.plot(node_s.data[:, {'position': ['y']}])
    ax3.plot(node_o.data[:, {'velocity': ['x']}])
    ax3.plot(node_s.data[:, {'velocity': ['x']}])
    ax4.plot(node_o.data[:, {'velocity': ['y']}])
    ax4.plot(node_s.data[:, {'velocity': ['y']}])
    ax5.plot(np.linalg.norm(node_o.data[:, {'velocity': ['x', 'y']}], axis=1) * 12)
    ax5.plot(np.linalg.norm(node_s.data[:, {'velocity': ['x', 'y']}], axis=1) * 12)
    
    ax6.plot(np.linalg.norm(node_o.data[:, {'acceleration': ['x', 'y']}], axis=1) * 12)
    ax6.plot(np.linalg.norm(node_s.data[:, {'acceleration': ['x', 'y']}], axis=1) * 12)
    # np.linalg.norm(node.data[:, {'acceleration': ['x', 'y']}], axis=1) * 12
# node.data[:, {'acceleration': ['x', 'y']}]
In [ ]:

In [ ]: