Detection¶
Detection module for object detection using YOLO.
Detector ¶
Detector(
model: DetectorModel | str = DetectorModel.YOLO26x,
weights: str | None = None,
conf: float = 0.25,
nms: float = 0.7,
max_det: int = 300,
device: str = "auto",
half: bool = False,
imgsz: int = 640,
classes: list[int] | None = None,
class_names: list[str] | None = None,
rect: bool = False,
agnostic_nms: bool = False,
embed: list[int] | None = None,
fast: bool = True,
batch: int = 8,
)
A wrapper around Ultralytics detection models for running object detection on videos and selected frames.
This class loads a YOLO (v8, v11, 26) or RT-DETR model from a local models/
directory (or from a user-supplied .pt file) and provides convenience
methods to:
- detect objects frame by frame in a video and return results as a pandas DataFrame,
- run detection only on specified frame indices,
- process a batch of videos and save per-video detection text files, and
- query basic video properties (FPS, frame count).
The detector automatically chooses an inference device (cuda, xpu,
mps, or cpu) when device="auto", and it can optionally enable half-precision
inference on GPU.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
DetectorModel or str
|
Built-in model weights to use. Accepts a |
YOLO26x
|
weights
|
str
|
Optional custom model weights to load. If relative, path is resolved
under |
None
|
conf
|
float
|
Confidence threshold for detections. Default is |
0.25
|
nms
|
float
|
IoU / non-maximum suppression threshold. Default is |
0.7
|
max_det
|
int
|
Maximum number of detections per frame. Default is |
300
|
device
|
(auto, cuda, xpu, cpu, mps)
|
Inference device to use. If |
"auto"
|
half
|
bool
|
Whether to enable half-precision inference. This is only effective on
GPU (CUDA). Default is |
False
|
imgsz
|
int
|
Inference image size (longest side, pixels), forwarded to Ultralytics
|
640
|
classes
|
list of int
|
Restrict detection to these class indices, forwarded to Ultralytics
|
None
|
class_names
|
list of str
|
Restrict detection to these class names (e.g. |
None
|
rect
|
bool
|
Whether to use rectangular (non-square) inference, forwarded to Ultralytics
|
False
|
agnostic_nms
|
bool
|
Whether to use class-agnostic NMS, forwarded to Ultralytics
|
False
|
embed
|
list of int
|
Layer indices to return feature embeddings from, forwarded to Ultralytics
|
None
|
fast
|
bool
|
Whether |
True
|
batch
|
int
|
Frames per forward pass, used when |
8
|
Notes
- The class expects model weight files to be located under
<module_dir>/models/when using the built-in weight names. - Returned detection tables typically contain the columns:
frame, res, x, y, w, h, conf, class.
Initialize a Detector for Ultralytics YOLO/RT-DETR models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
DetectorModel or str
|
Built-in model to use. Accepts a |
YOLO26x
|
weights
|
str
|
Customized model weights to load.
Default is None, which means using the built-in weights in |
None
|
conf
|
float
|
Confidence threshold. Default is 0.25. |
0.25
|
nms
|
float
|
IoU/NMS threshold. Default is 0.7. |
0.7
|
max_det
|
int
|
Maximum detections per frame. Default is 300. In crowded scenes, you may want to increase this. |
300
|
device
|
(auto, cuda, xpu, cpu, mps)
|
Inference device. Default is "auto". |
"auto"
|
half
|
bool
|
Whether to use half precision (GPU only). Default is False. |
False
|
imgsz
|
int
|
Inference image size, as passed to Ultralytics |
640
|
classes
|
list of int
|
Class indices to restrict detection to, as passed to Ultralytics
|
None
|
class_names
|
list of str
|
Class names to restrict detection to (e.g. |
None
|
rect
|
bool
|
Rectangular inference, as passed to Ultralytics |
False
|
agnostic_nms
|
bool
|
Class-agnostic NMS, as passed to Ultralytics |
False
|
embed
|
list of int
|
Layer indices to return feature embeddings from, as passed to
Ultralytics |
None
|
fast
|
bool
|
Whether |
True
|
batch
|
int
|
Default frames per forward pass when |
8
|
Source code in src/dnt/detect/yolo/detector.py
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 | |
detect ¶
detect(
input_video: str,
iou_file: str | None = None,
video_index: int | None = None,
video_tot: int | None = None,
start_frame: int | None = None,
end_frame: int | None = None,
verbose: bool = True,
show: bool = False,
message: str | None = None,
return_df: bool = True,
) -> pd.DataFrame | None
Run object detection on a video and return per-frame detections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_video
|
str
|
Path to the input video file. |
required |
iou_file
|
str
|
If provided, detection results are written to this file (CSV without header). |
None
|
video_index
|
int
|
Index of this video in a batch, used only for progress display. |
None
|
video_tot
|
int
|
Total number of videos in the batch, used only for progress display. |
None
|
start_frame
|
int
|
Frame index to start detection from. If None or out of range, starts at 0. |
None
|
end_frame
|
int
|
Frame index to stop detection at. If None or out of range, uses the last frame. |
None
|
verbose
|
bool
|
Whether to show a progress bar. Default is True. |
True
|
show
|
bool
|
Whether to display the video frames with detections. Default is False.
Not supported when |
False
|
message
|
str | None
|
Optional message shown in the progress bar description. Default is None. |
None
|
return_df
|
bool
|
Whether to return the detections. Default is True. With False, detections are
only written to |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame or None
|
DataFrame with columns:
|
Notes
With self.fast, iou_file is written incrementally as
<iou_file>.part and renamed when complete; an interrupted run resumes from that
file on the next call (see detect_fast()).
Source code in src/dnt/detect/yolo/detector.py
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 | |
detect_fast ¶
detect_fast(
input_video: str,
iou_file: str | None = None,
video_index: int | None = None,
video_tot: int | None = None,
start_frame: int | None = None,
end_frame: int | None = None,
verbose: bool = True,
message: str | None = None,
batch: int | None = None,
return_df: bool = True,
) -> pd.DataFrame | None
Like detect(), but batches frames through the Ultralytics AutoBackend directly.
detect() calls Ultralytics' high-level model.predict() once per frame. That call
rebuilds an inference dataset around the single frame and forces several CUDA
synchronizations on every call, regardless of batch size, which is roughly 2x slower
than running several frames through the backend in one forward pass. This method
does that instead: it reuses Ultralytics' own model-family-correct
preprocess()/inference()/postprocess() (so decoding, e.g. RT-DETR's vs YOLO's
NMS, is unchanged), just without model.predict()'s per-frame dataset/callback setup.
batch here means frames within this one video, unlike detect_batch()'s batch,
which means a list of videos.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_video
|
str
|
Path to the input video file. |
required |
iou_file
|
str
|
If provided, detection results are written to this file (CSV without header), batch by batch (see Notes). |
None
|
video_index
|
int
|
Index of this video in a batch, used only for progress display. |
None
|
video_tot
|
int
|
Total number of videos in the batch, used only for progress display. |
None
|
start_frame
|
int
|
Frame index to start detection from. If None or out of range, starts at 0. |
None
|
end_frame
|
int
|
Frame index to stop detection at. If None or out of range, uses the last frame. |
None
|
verbose
|
bool
|
Whether to show a progress bar. Default is True. |
True
|
message
|
str | None
|
Optional message shown in the progress bar description. Default is None. |
None
|
batch
|
int
|
Number of frames sent through the model in one forward pass. Default is None:
use |
None
|
return_df
|
bool
|
Whether to return the detections. Default is True. With False, rows are only
written to |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame or None
|
Same columns and semantics as |
Notes
- No
show=preview: batching trades per-frame latency for throughput, so a live preview does not fit this path. Usedetect()for that. batch=1reproducesdetect()exactly (verified frame-for-frame, CPU and CUDA, YOLO and RT-DETR): the speed gain atbatch=1comes purely from skippingmodel.predict()'s per-frame dataset/callback setup (see_ensure_predictor()), not from any change in what gets computed.-
batch > 1on CUDA withhalf=Truecan shift a small fraction of results: running the same image alone vs. as part of a larger batch takes a different path through cuDNN/cuBLAS's FP16 kernels, so reduction order (and, rarely, which of two near-duplicate boxes NMS keeps) can differ slightly. Measured on an RTX 5070 Ti, RT-DETR-x, 200 frames,batch=8:half=Trueshifted results on 6% of frames (up to ~0.17 confidence, ~67 px on the frames it did shift);half=Falseshifted only 2.5% of frames (up to 0.02 confidence, 1 px, i.e. rounding noise). This is inherent to batched half-precision GPU inference, not specific to this bypass;detect()called once per frame never hits it, since it never batches. If exact reproducibility withdetect()matters more than throughput, usehalf=Falseand/orbatch=1. -
With
iou_file, each batch's rows are appended to<iou_file>.partas soon as they are detected, and the file is renamed toiou_fileonly once the whole range is done, so an existingiou_fileis always complete (anddetect_batch()can keep skipping existing files). If<iou_file>.partalready exists, e.g. after a crash or Ctrl-C, detection resumes from it: the rows of its last frame are dropped and that frame is detected again. Delete the.partfile to start over instead. Withbatch > 1andhalf=True, frames after the resume point are grouped into batches differently than in an uninterrupted run, so they can differ slightly in the way described above.
Source code in src/dnt/detect/yolo/detector.py
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 | |
detect_frames ¶
detect_frames(
input_video: str,
frames: list[int],
verbose: bool = True,
) -> pd.DataFrame
Run object detection on specific frames of a video.
This method is useful when you don't need to process the entire video and only want detections for selected frame indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_video
|
str
|
Path to the input video file. |
required |
frames
|
list of int
|
List of frame indices to process. |
required |
verbose
|
bool
|
Whether to show a progress bar. Default is True. |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns
|
Source code in src/dnt/detect/yolo/detector.py
900 901 902 903 904 905 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 956 957 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 | |
detect_batch ¶
detect_batch(
input_videos: list[str],
output_path: str | None = None,
is_overwrite: bool = False,
is_report: bool = True,
verbose: bool = True,
message: str | None = None,
) -> list[str]
Run detection on multiple videos and optionally write per-video output files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_videos
|
list of str
|
Paths to the input video files to be processed. |
required |
output_path
|
str
|
Directory where per-video detection files will be written. If None, detections are not written to disk and the returned list will be empty. |
None
|
is_overwrite
|
bool
|
If False (default), existing detection files with the same name will be skipped. If True, they will be regenerated. |
False
|
is_report
|
bool
|
If True (default), existing detection files (that were skipped) are still included in the returned list. |
True
|
verbose
|
bool
|
If True, prints progress messages. Default is True. |
True
|
message
|
str | None
|
Optional message shown in each progress bar description. Default is None. |
None
|
Returns:
| Type | Description |
|---|---|
list of str
|
A list of paths to detection files that were created or already existed.
If |
Notes
- Each video's
detect()call uses thisDetectorinstance'sfast/batch(seeDetector), the same as callingdetect()directly; there is no separate argument for them here. - A video whose
_iou.txtdoesn't exist yet but whose_iou.txt.partdoes (an interruptedfastrun) resumes from the.partfile; withis_overwrite=Truethe.partfile is deleted and the video starts over.
Source code in src/dnt/detect/yolo/detector.py
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 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 | |
get_fps
staticmethod
¶
get_fps(video: str) -> float
Return the frames-per-second (FPS) value of a video file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
str
|
Path to the video file. |
required |
Returns:
| Type | Description |
|---|---|
float
|
FPS of the video. Returns 0.0 if the video cannot be opened. |
Source code in src/dnt/detect/yolo/detector.py
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 | |
get_frames
staticmethod
¶
get_frames(video: str) -> int
Return the total number of frames in a video file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
str
|
Path to the video file. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Total frame count. Returns 0 if the video cannot be opened. |
Source code in src/dnt/detect/yolo/detector.py
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 | |
DetectorModel ¶
Bases: str, Enum
Enum of available YOLO and RT-DETR model weights.
Each member represents a different model variant with its corresponding weight file name.