|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 7, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "2\r" |
| 13 | + ] |
| 14 | + } |
| 15 | + ], |
| 16 | + "source": [ |
| 17 | + "import cv2\n", |
| 18 | + "import numpy as np\n", |
| 19 | + "from pathlib import Path\n", |
| 20 | + "\n", |
| 21 | + "from boxmot import OcSort\n", |
| 22 | + "\n", |
| 23 | + "\n", |
| 24 | + "# Initialize the tracker\n", |
| 25 | + "tracker = OcSort(\n", |
| 26 | + " asso_func=\"centroid\",\n", |
| 27 | + " min_hits=10,\n", |
| 28 | + " asso_threshold=0.98,\n", |
| 29 | + " det_thresh = 0.7,\n", |
| 30 | + " max_age=20,\n", |
| 31 | + " use_byte=True,\n", |
| 32 | + " Q_xy_scaling = 0.01,\n", |
| 33 | + " Q_s_scaling = 0.0001,\n", |
| 34 | + ")\n", |
| 35 | + "\n", |
| 36 | + "def get_parabolic_array(i):\n", |
| 37 | + " \"\"\"\n", |
| 38 | + " Generates coordinates where x increases linearly,\n", |
| 39 | + " y follows a parabolic curve (y = base + coeff * i^2),\n", |
| 40 | + " and the angle matches the tangent of the curve.\n", |
| 41 | + " \"\"\"\n", |
| 42 | + " # -- FIRST ROW --\n", |
| 43 | + " # Define x1, y1\n", |
| 44 | + " x1 = 144 + i\n", |
| 45 | + " y1 = 212 + 0.01 * (i ** 2)\n", |
| 46 | + " \n", |
| 47 | + " # Slope = 2 * coeff * i => for 0.01 * i^2, slope = 2*0.01*i = 0.02*i\n", |
| 48 | + " slope1 = 0.02 * i\n", |
| 49 | + " # Angle in radians\n", |
| 50 | + " angle1 = np.arctan(slope1)\n", |
| 51 | + " \n", |
| 52 | + " # -- SECOND ROW --\n", |
| 53 | + " # Define x2, y2 with a different coefficient\n", |
| 54 | + " x2 = 425 + i\n", |
| 55 | + " y2 = 281 + 0.02 * (i ** 2)\n", |
| 56 | + " \n", |
| 57 | + " # Slope for 0.02 * i^2 is 2*0.02*i = 0.04*i\n", |
| 58 | + " slope2 = 0.04 * i\n", |
| 59 | + " # Angle in radians\n", |
| 60 | + " angle2 = np.arctan(slope2)\n", |
| 61 | + " \n", |
| 62 | + " # Build the array\n", |
| 63 | + " det = np.array([\n", |
| 64 | + " [x1, y1, 45, 30, angle1, 0.82, 0], # row 1\n", |
| 65 | + " [x2, y2, 45, 30, angle2, 0.72, 65] # row 2\n", |
| 66 | + " ])\n", |
| 67 | + " \n", |
| 68 | + " return det\n", |
| 69 | + "\n", |
| 70 | + "\n", |
| 71 | + "for i in range(0, 100):\n", |
| 72 | + "\n", |
| 73 | + " #frame = cv2.imread(str(img_dir / (file.stem + '.png')))\n", |
| 74 | + " frame = np.zeros((1080,1080,3))\n", |
| 75 | + " \n", |
| 76 | + " det = get_parabolic_array(i)\n", |
| 77 | + "\n", |
| 78 | + " # Update the tracker\n", |
| 79 | + " res = tracker.update(det, frame) # --> M X (x, y, x, y, id, conf, cls, ind)\n", |
| 80 | + " \n", |
| 81 | + " # Plot tracking results on the image\n", |
| 82 | + " tracker.plot_results(frame, show_trajectories=True, fontscale=2, thickness=4)\n", |
| 83 | + "\n", |
| 84 | + " # Display the frame\n", |
| 85 | + " cv2.imshow('BoXMOT', frame)\n", |
| 86 | + "\n", |
| 87 | + " print(len(tracker.active_tracks),end='\\r')\n", |
| 88 | + "\n", |
| 89 | + " key = cv2.waitKey(1) & 0xFF\n", |
| 90 | + " if key == ord('q') or key ==27:\n", |
| 91 | + " break\n", |
| 92 | + "\n", |
| 93 | + "cv2.destroyAllWindows()" |
| 94 | + ] |
| 95 | + } |
| 96 | + ], |
| 97 | + "metadata": { |
| 98 | + "kernelspec": { |
| 99 | + "display_name": "boxmot-YDNZdsaB-py3.11", |
| 100 | + "language": "python", |
| 101 | + "name": "python3" |
| 102 | + }, |
| 103 | + "language_info": { |
| 104 | + "codemirror_mode": { |
| 105 | + "name": "ipython", |
| 106 | + "version": 3 |
| 107 | + }, |
| 108 | + "file_extension": ".py", |
| 109 | + "mimetype": "text/x-python", |
| 110 | + "name": "python", |
| 111 | + "nbconvert_exporter": "python", |
| 112 | + "pygments_lexer": "ipython3", |
| 113 | + "version": "3.11.5" |
| 114 | + } |
| 115 | + }, |
| 116 | + "nbformat": 4, |
| 117 | + "nbformat_minor": 2 |
| 118 | +} |
0 commit comments