📝 API Reference
Aegear: a computer vision toolkit for tracking and analyzing fish behavior in controlled aquaculture environments.
calibration
Scene calibration module.
This module is used to calibrate the camera and the scene size to get the pixel to cm ratio.
It includes a class SceneCalibration
that handles the calibration process, including loading camera parameters,
assigning scene reference points, calibrating the scene, and rectifying images.
The calibration is performed using a set of screen points and a set of real-world reference points.
The class also provides a method to rectify images based on the calibration parameters. It uses OpenCV for image processing and assumes that the camera calibration parameters are stored in a file. The calibration points are expected to be in a specific order: top left, top right, bottom right, bottom left.
Note that this reference matching system is put in place due allow inconsistent camera placement with respect to the original take of the calibration pattern. This calibration system uses this information to rectify the image for easier tracking of the fish, and to estimate the pixel to cm ratio, hence allowing the correct metric tracking of the fish within the experiment.
SceneCalibration
Calibration of the camera and the scene size to get the pixel to cm ratio.
Source code in src/aegear/calibration.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
assign_scene_calibration(points)
Assign the scene calibration points.
Parameters
list
The scene reference points to use for calibration. The 4x2 array of floats, designating the borders of the reference area used for final image rectification and pixel to cm ratio calculation. By convention, the points are in the order: top left, top right, bottom right, bottom left.
Source code in src/aegear/calibration.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
calibrate(screen_pts)
Run the scene characterization.
Parameters
screen_pts : list The screen points to use for calibration, which within the scene match the points assigned for the scene reference. As for the reference points, the points are in the order: top left, top right, bottom right, bottom left.
Returns
float The pixel to cm ratio.
Source code in src/aegear/calibration.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
rectify_image(image)
Rectify the image.
Parameters
image : np.ndarray The image to rectify.
Returns
np.ndarray The rectified image.
Source code in src/aegear/calibration.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
|
rectify_point(point)
Rectify a single point using the current calibration.
Parameters
point : tuple of float The (x, y) coordinates of the point to rectify.
Returns
tuple of float The rectified (x, y) coordinates.
Source code in src/aegear/calibration.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
unrectify_point(point)
Map a point from the rectified image back to its original (distorted) image coordinates.
Source code in src/aegear/calibration.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
datasets
TrackingDataset
Bases: Dataset
Source code in src/aegear/datasets.py
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 |
|
transform_offset_for_heatmap(offset, transform)
Apply rotation and scale to an offset vector, then map to heatmap coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
offset
|
np.ndarray shape (2,), the vector (search - template) |
required | |
transform
|
Tuple[float, float]
|
Tuple[float, float] = (rotation_deg, scale) |
required |
Returns:
Type | Description |
---|---|
np.ndarray of shape (2,), transformed and rescaled offset in heatmap coordinates |
Source code in src/aegear/datasets.py
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
|
CachedTrackingDataset
Bases: Dataset
Cached version of TrackingDataset. Loads crops and metadata from disk, avoiding video decoding at runtime. Each sample contains (template, search, heatmap).
Source code in src/aegear/datasets.py
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 |
|
BackgroundWindowDataset
Bases: Dataset
Dataset for sampling background (no-fish) windows from a video, using a sliding window approach. The user provides a list of frame indices known to contain only background (no fish present). Each sample is a cropped window from a background frame, with optional augmentation, rotation, and scaling. The output is (image, heatmap), where heatmap is always a zero tensor.
Source code in src/aegear/datasets.py
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 |
|
split_coco_annotations(coco_json_path, train_ratio=0.8, seed=42)
Loads a COCO JSON and splits it into train/val dictionaries based on image-level split.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coco_json_path
|
Path
|
Path to the COCO annotations.json. |
required |
train_ratio
|
float
|
Ratio of images to assign to the training set. |
0.8
|
seed
|
int
|
Random seed for reproducibility. |
42
|
Returns:
Type | Description |
---|---|
Tuple[dict, dict]
|
Tuple[dict, dict]: (train_dict, val_dict) |
Source code in src/aegear/datasets.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
model
CBAM
Bases: Module
Lightweight convolutional block attention module (CBAM) for channel and spatial attention.
Source code in src/aegear/model.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
EfficientUNet
Bases: Module
EfficientUNet backbone based on EfficientNet-B0, enhanced with CBAM (Convolutional Block Attention Module) attention blocks after each encoder and decoder stage.
The architecture removes the deepest (last) encoder and decoder stages compared to a standard UNet, resulting in a lighter model with fewer parameters and reduced memory usage, while retaining strong feature extraction and localization capabilities.
CBAM modules are used to improve feature representation by applying both channel and spatial attention at multiple levels of the network, allowing the model to focus on the object of interest while ignoring irrelevant information. This is particularly useful in scenarios where the object of interest (e.g., fish) may be small and difficult to distinguish from the background, or when there are multiple objects present in the image.
Source code in src/aegear/model.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
SiameseTracker
Bases: Module
Siamese UNet model for tracking, based on EfficientUNet.
This model is designed to take two inputs: a template image and a search image. The template image is the reference image of the object to be tracked, while the search image is the current frame in which the object is being searched for. The model processes both images through a shared UNet architecture, extracting features from both images and then concatenating them at each stage of the decoder. This allows the model to leverage the spatial information from both images, improving the tracking performance.
Source code in src/aegear/model.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
ConvClassifier
Bases: Module
A simple convolutional network for binary classification. This model is designed to classify whether a fish is present in a given region of interest (ROI) of the image.
Source code in src/aegear/model.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
|
motiondetection
Motion detection module.
This module provides the MotionDetector class that identifies motion by comparing three consecutive frames. The algorithm converts frames to grayscale, computes the absolute difference between frames, applies binary thresholding, combines the results, and uses morphological operations to filter the motion regions before extracting contours.
MotionDetector
Motion detector class that identifies motion by comparing three consecutive frames.
Source code in src/aegear/motiondetection.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
detect(prev_frame, this_frame, next_frame)
Detect motion by comparing three consecutive frames.
The function converts the frames to grayscale, computes the absolute differences, thresholds them to produce binary images, combines the thresholded images, applies morphological operations to remove noise, and finally extracts contours. Detected contours are classified into "good" (within the area range) and "bad" (outside the area range but above a minimum threshold).
Parameters
prev_frame : numpy.ndarray Previous frame in BGR color space. this_frame : numpy.ndarray Current frame in BGR color space. next_frame : numpy.ndarray Next frame in BGR color space.
Returns
Tuple[List[numpy.ndarray], List[numpy.ndarray]] A tuple containing two lists of contours: - The first list contains contours with areas between min_area and max_area. - The second list contains contours with areas outside that range but above MIN_AREA.
Source code in src/aegear/motiondetection.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
tracker
Prediction
A class to represent a prediction made by the model.
Source code in src/aegear/tracker.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
FishTracker
Source code in src/aegear/tracker.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
|
run_tracking(video, start_frame, end_frame, model_track_register, progress_reporter=None, ui_update=None)
Run the tracking on a video.
Source code in src/aegear/tracker.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
training
Module containing various training-related utilities and functions.
WeightedBCEWithLogitsLoss
Custom loss function that applies weighted binary cross-entropy with logits. It emphasizes the center of the Gaussian heatmap.
Source code in src/aegear/training.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
EfficientUNetLoss
Bases: WeightedBCEWithLogitsLoss
Source code in src/aegear/training.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
SiameseLoss
Bases: EfficientUNetLoss
Siamese loss function that combines the EfficientUNetLoss with an RGB consistency loss.
Source code in src/aegear/training.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
rgb_consistency_loss(template_img, search_img, pred_heatmap)
Compute the RGB consistency loss between template and search images based on the predicted heatmap.
Source code in src/aegear/training.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
get_confidence(heatmap)
Get confidence score from a heatmap by finding the maximum value.
Source code in src/aegear/training.py
16 17 18 19 20 21 22 23 24 |
|
overlay_heatmap_on_rgb(rgb_tensor, heatmap, alpha=0.5, centroid_color=(0, 1, 0))
Overlay heatmap onto RGB image and draw a circle at the predicted centroid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rgb_tensor
|
[3, H, W] tensor |
required | |
heatmap
|
[H, W] numpy array |
required | |
alpha
|
blending weight |
0.5
|
|
centroid_color
|
(R, G, B) tuple in range 0–1 |
(0, 1, 0)
|
Returns: overlay: [H, W, 3] numpy image
Source code in src/aegear/training.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
get_centroids_per_sample(heatmap)
Get centroids from a batch of heatmaps.
Source code in src/aegear/training.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
trajectory
Utility functions for working with 2D trajectories in image frames, including drawing, smoothing, and computing properties of motion paths.
Assumes trajectory is a list of (x, y) pixel coordinates sampled at video frame rate.
smooth_trajectory(trajectory, filterSize=15)
Apply Savitzky-Golay filter to smooth a trajectory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trajectory
|
list of (t, x, y
|
Frame id with raw trajectory points. |
required |
filterSize
|
int
|
Window size for filtering (must be odd and >= 5). |
15
|
Returns:
Type | Description |
---|---|
list[tuple[int, int, int]]
|
list of (t, x, y): Smoothed trajectory points. |
Source code in src/aegear/trajectory.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
detect_trajectory_outliers(trajectory, threshold=20.0)
Detects large jumps in pixel space, indicating likely tracking failures.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trajectory
|
list[tuple[int, int, int]]
|
List of (frame_idx, x, y) tuples. |
required |
threshold
|
float
|
Maximum allowed pixel movement per frame. |
20.0
|
Returns:
Type | Description |
---|---|
list[int]
|
List of frame indices where jump exceeds threshold. |
Source code in src/aegear/trajectory.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
utils
Kalman2D
A simple 2D Kalman filter for tracking.
Source code in src/aegear/utils.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
resource_path(relative_path)
Get the absolute path to the resource, works for dev and PyInstaller.
Source code in src/aegear/utils.py
12 13 14 15 16 17 18 19 |
|
get_latest_model_path(directory, model_name)
Find the latest model file in the given directory matching the base model name. Model files are expected to be named as: modelname_YYYY-MM-DD.pth
Source code in src/aegear/utils.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
video
VideoClip
Minimalistic video clip class for reading video files.
Source code in src/aegear/video.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
get_frame(t)
Return the frame at time t
(in seconds).
Source code in src/aegear/video.py
16 17 18 19 20 21 |
|
get_frame_by_index(frame_id)
Return the frame at the given frame index.
Source code in src/aegear/video.py
23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
get_frame_width()
Return the width of the video frames.
Source code in src/aegear/video.py
37 38 39 40 41 |
|
get_frame_height()
Return the height of the video frames.
Source code in src/aegear/video.py
43 44 45 46 47 |
|
get_frame_shape()
Return the shape of the video frames.
Source code in src/aegear/video.py
49 50 51 52 53 |
|