deeplabcut.utils.video_processor
Author: Hao Wu hwu01@g.harvard.edu You can find the directory for your ffmpeg bindings by: "find / | grep ffmpeg" and then setting it.
This is the helper class for video reading and saving in DeepLabCut. Updated by AM
You can set various codecs below, fourcc = cv2.VideoWriter_fourcc(*'MJPG') i.e. 'XVID'
Classes:
| Name | Description |
|---|---|
VideoProcessor |
Abstract base class for video reading and writing. |
VideoProcessorCV |
OpenCV-backed video reader and writer. |
VideoProcessor
Bases: ABC
Abstract base class for video reading and writing.
Subclasses implement backend-specific video loading, metadata extraction, output video creation, frame reading, frame writing, and cleanup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Path to the input video. If empty, no input video is opened. |
''
|
|
str
|
Path to the output video. If empty, no output video is created. |
''
|
|
int
|
Number of frames to process. |
-1
|
|
float | None
|
Optional FPS override. |
None
|
|
str
|
FourCC codec string used for output videos. |
'X264'
|
|
int | Literal[''] | None
|
Output video height. |
''
|
|
int | Literal[''] | None
|
Output video width. |
''
|
Attributes:
| Name | Type | Description |
|---|---|---|
fname |
str
|
Input video path. |
sname |
str
|
Output video path. |
nframes |
int
|
Number of frames to process. |
video_fps |
float | None
|
Video frame rate. |
FPS |
float | None
|
Legacy alias for |
h |
int
|
Input video height. |
w |
int
|
Input video width. |
nc |
int
|
Number of channels. |
i |
int
|
Number of successfully loaded frames. |
vid |
Backend-specific input video object. |
|
svid |
Backend-specific output video object. |
|
sh |
int
|
Output video height. |
sw |
int
|
Output video width. |
Notes
height(), width(), fps(), counter(), and
frame_count() are retained as methods for backwards compatibility.
Methods:
| Name | Description |
|---|---|
close |
Implement your own. |
create_video |
Implement your own. |
get_info |
Implement your own. |
save_frame |
Implement your own. |
Source code in deeplabcut/utils/video_processor.py
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 | |
close
abstractmethod
create_video
abstractmethod
get_info
abstractmethod
VideoProcessorCV
Bases: VideoProcessor
OpenCV-backed video reader and writer.
This implementation uses cv2.VideoCapture for reading videos and
cv2.VideoWriter for writing videos. Frames returned by
load_frame are converted from OpenCV's native BGR channel order to
RGB channel order. Frames passed to save_frame are expected to be in
RGB order and are converted back to BGR before writing.
Attributes:
| Name | Type | Description |
|---|---|---|
fname |
str
|
Path to the input video. If empty, no input video is opened. |
sname |
str
|
Path to the output video. If empty, no output video is created. |
nframes |
int
|
Number of frames to process. If initialized as |
codec |
str
|
FourCC codec string used when creating the output video. |
h |
int
|
Input video height in pixels. |
w |
int
|
Input video width in pixels. |
nc |
int
|
Number of channels. This implementation uses |
i |
int
|
Number of frames successfully loaded through |
FPS |
float
|
Frames per second reported by OpenCV, or the user-provided override. |
sh |
int
|
Output video height in pixels. |
sw |
int
|
Output video width in pixels. |
vid |
VideoCapture | None
|
OpenCV video reader. |
svid |
VideoWriter | None
|
OpenCV video writer. |
Methods:
| Name | Description |
|---|---|
close |
Release OpenCV reader and writer resources. |
create_video |
Create an OpenCV video writer. |
get_info |
Populate metadata from the OpenCV video reader. |
get_video |
Open the input video with OpenCV. |
save_frame |
Write one RGB frame to the output video. |
Source code in deeplabcut/utils/video_processor.py
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 | |
close
Release OpenCV reader and writer resources.
This method is safe to call multiple times. After release, self.svid
and self.vid are set to None to avoid accidental reuse of closed
OpenCV handles.
Source code in deeplabcut/utils/video_processor.py
create_video
Create an OpenCV video writer.
Returns:
| Type | Description |
|---|---|
|
cv2.VideoWriter: OpenCV video writer for |
Notes
self.sw and self.sh are expected to be set by the base class
before this method is called. The codec is interpreted as a FourCC
string, preserving the historical OpenCV behavior.
Source code in deeplabcut/utils/video_processor.py
get_info
Populate metadata from the OpenCV video reader.
Sets
self.w: Frame width in pixels.
self.h: Frame height in pixels.
self.nframes: Number of frames to process.
self.FPS: Frames per second reported by OpenCV.
self.nc: Number of channels, always 3.
Notes
If self.nframes is -1 or greater than the total number of
frames reported by OpenCV, it is replaced by OpenCV's frame count.
Source code in deeplabcut/utils/video_processor.py
get_video
Open the input video with OpenCV.
Returns:
| Type | Description |
|---|---|
|
cv2.VideoCapture: OpenCV video capture object for |
save_frame
Write one RGB frame to the output video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray | None
|
RGB frame to write. |
required |
Notes
This method preserves the historical behavior of silently ignoring
None frames. Non-None frames are converted from RGB to BGR
before being passed to OpenCV.