1 2 3 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 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
//! Media I/O, see [OpenCV //! videoio](http://docs.opencv.org/3.1.0/dd/de7/group__videoio.html) use core::*; use errors::*; use failure::Error; use mat::*; use std::os::raw::{c_char, c_double, c_int}; // ============================================================================= // VideoCapture // ============================================================================= enum CVideoCapture {} extern "C" { fn cv_videocapture_new(index: c_int) -> *mut CVideoCapture; fn cv_videocapture_from_file(path: *const c_char) -> *mut CVideoCapture; fn cv_videocapture_from_gst_pipeline(pipeline: *const c_char) -> *mut CVideoCapture; fn cv_videocapture_is_opened(ccap: *const CVideoCapture) -> bool; fn cv_videocapture_read(v: *mut CVideoCapture, m: *mut CMat) -> bool; fn cv_videocapture_drop(cap: *mut CVideoCapture); fn cv_videocapture_set(cap: *mut CVideoCapture, property: CapProp, value: c_double) -> bool; fn cv_videocapture_get(cap: *mut CVideoCapture, property: CapProp) -> c_double; } /// Video capturing from video files, image sequences or cameras. #[derive(Debug)] pub struct VideoCapture { inner: *mut CVideoCapture, } unsafe impl Send for CVideoCapture {} unsafe impl Send for VideoCapture {} /// Video capture's property identifier. #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[allow(missing_docs)] pub enum CapProp { /// Current position of the video file in milliseconds or video capture /// timestamp. PosMsec = 0, /// 0-based index of the frame to be decoded/captured next. PosFrames = 1, /// Relative position of the video file: 0 - start of the film, 1 - end of /// the film. PosAviRatio = 2, /// Width of the frames in the video stream. FrameWidth = 3, /// Height of the frames in the video stream. FrameHeight = 4, /// Frame rate. Fps = 5, /// 4-character code of codec. Fourcc = 6, /// Number of frames in the video file. FrameCount = 7, /// Format of the Mat objects returned by retrieve() . Format = 8, /// Backend-specific value indicating the current capture mode. Mode = 9, /// Brightness of the image (only for cameras). Brightness = 10, /// Contrast of the image (only for cameras). Contrast = 11, /// Saturation of the image (only for cameras). Saturation = 12, /// Hue of the image (only for cameras). Hue = 13, /// Gain of the image (only for cameras). Gain = 14, /// Exposure (only for cameras). Exposure = 15, /// Boolean flags indicating whether images should be converted to RGB. ConvertRgb = 16, /// Currently not supported WhiteBalanceBlueU = 17, /// Rectification flag for stereo cameras (note: only supported by DC1394 v /// 2.x backend currently) Rectification = 18, Monochrome = 19, Sharpness = 20, AutoExposure = 21, Gamma = 22, Temperature = 23, Trigger = 24, TriggerDelay = 25, WhiteBalanceRedV = 26, Zoom = 27, Focus = 28, Guid = 29, IsoSpeed = 30, Backlight = 32, Pan = 33, Tilt = 34, Roll = 35, Iris = 36, Settings = 37, Buffersize = 38, Autofocus = 39, } impl VideoCapture { /// Creates a capture device with specified camera id. If there is a single /// camera connected, just pass 0. pub fn new(index: c_int) -> Self { let cap = unsafe { cv_videocapture_new(index) }; VideoCapture { inner: cap } } /// Creates a capture device with the path of a video file (eg. video.avi). /// This also supports image sequence, eg. img_%02d.jpg, which will read /// samples like img_00.jpg, img_01.jpg, img_02.jpg, ...). pub fn from_path(path: &str) -> Self { let s = ::std::ffi::CString::new(path).unwrap(); let cap = unsafe { cv_videocapture_from_file((&s).as_ptr()) }; VideoCapture { inner: cap } } /// Create a capture device from a gstreamer pipeline (eg. /// gst-launch-1.0 v4l2src ! videoconvert ! appsink). pub fn from_pipeline(pipeline: &str) -> Self { let s = ::std::ffi::CString::new(pipeline).unwrap(); let cap = unsafe { cv_videocapture_from_gst_pipeline((&s).as_ptr()) }; VideoCapture { inner: cap } } /// Returns true if video capturing has been initialized already. pub fn is_open(&self) -> bool { unsafe { cv_videocapture_is_opened(self.inner) } } /// Grabs, decodes and returns the next video frame. `read` combines /// `VideoCapture::grab` and `VideoCapture::retrieve` in one call. This is /// the most convenient method for reading video files or capturing data /// from decode and return the just grabbed frame. /// /// If no frames has been grabbed (camera has been disconnected, or there /// are no more frames in video file), the methods return `None`. pub fn read(&self) -> Option<Mat> { let inner = CMat::new(); let status = unsafe { cv_videocapture_read(self.inner, inner) }; if status { Some(Mat::from_raw(inner)) } else { None } } /// Sets a property in the `VideoCapture`. pub fn set(&self, property: CapProp, value: f64) -> bool { unsafe { cv_videocapture_set(self.inner, property, value) } } /// Gets a property in the `VideoCapture`. pub fn get(&self, property: CapProp) -> Option<f64> { let ret = unsafe { cv_videocapture_get(self.inner, property) }; if ret != 0.0 { Some(ret) } else { None } } } impl Drop for VideoCapture { fn drop(&mut self) { unsafe { cv_videocapture_drop(self.inner); } } } // ============================================================================= // VideoWriter // ============================================================================= /// Opaque VideoWriter type. enum CvVideoWriter {} /// `VideoWriter` provides easy access to write videos to files. /// -On Linux FFMPEG is used to write videos; /// -On Windows FFMPEG or VFW is used; /// -On MacOSX QTKit is used. #[derive(Debug)] pub struct VideoWriter { inner: *mut CvVideoWriter, } extern "C" { fn cv_videowriter_default() -> *mut CvVideoWriter; fn cv_videowriter_new( path: *const c_char, fourcc: c_int, fps: c_double, frame_size: Size2i, is_color: bool, ) -> *mut CvVideoWriter; fn cv_videowriter_drop(w: *mut CvVideoWriter); fn cv_videowriter_open( w: *mut CvVideoWriter, path: *const c_char, fourcc: c_int, fps: c_double, frame_size: Size2i, is_color: bool, ) -> bool; fn cv_videowriter_is_opened(w: *mut CvVideoWriter) -> bool; fn cv_videowriter_write(w: *mut CvVideoWriter, m: *mut CMat); fn cv_videowriter_set(w: *mut CvVideoWriter, property: VideoWriterProperty, value: c_double) -> bool; fn cv_videowriter_get(w: *mut CvVideoWriter, property: VideoWriterProperty) -> c_double; } impl VideoWriter { /// `VideoWriter` constructor. /// -path – Name of the output video file. /// -fourcc – 4-character code of codec used to compress the frames. For /// example, VideoWriter::fourcc('P','I','M','1') is a MPEG-1 codec, /// VideoWriter::fourcc('M','J','P','G') is a motion-jpeg codec etc. List /// of codes can be obtained at Video Codecs by FOURCC page. /// -fps – Framerate of the created video stream. /// -frame_size – Size of the video frames. /// -is_color – If it is not zero, the encoder will expect and encode color /// frames, otherwise it will work with grayscale frames (the flag is /// currently supported on Windows only). pub fn new(path: &str, fourcc: c_int, fps: f64, frame_size: Size2i, is_color: bool) -> VideoWriter { let s = ::std::ffi::CString::new(path).unwrap(); let writer = unsafe { cv_videowriter_new((&s).as_ptr(), fourcc, fps, frame_size, is_color) }; VideoWriter { inner: writer } } /// `VideoWriter` constructor. /// -path – Name of the output video file. /// -fourcc – 4-character code of codec used to compress the frames. For /// example, VideoWriter::fourcc('P','I','M','1') is a MPEG-1 codec, /// VideoWriter::fourcc('M','J','P','G') is a motion-jpeg codec etc. List /// of codes can be obtained at Video Codecs by FOURCC page. /// -fps – Framerate of the created video stream. /// -frame_size – Size of the video frames. /// -is_color – If it is not zero, the encoder will expect and encode color /// frames, otherwise it will work with grayscale frames (the flag is /// currently supported on Windows only). pub fn open(&self, path: &str, fourcc: c_int, fps: f64, frame_size: Size2i, is_color: bool) -> bool { let s = ::std::ffi::CString::new(path).unwrap(); unsafe { cv_videowriter_open(self.inner, (&s).as_ptr(), fourcc, fps, frame_size, is_color) } } /// Writes the specified image to video file. It must have the same size as /// has been specified when opening the video writer. pub fn write(&self, mat: &Mat) { unsafe { cv_videowriter_write(self.inner, mat.inner) } } /// Returns true if video writer has been initialized already. pub fn is_open(&self) -> bool { unsafe { cv_videowriter_is_opened(self.inner) } } /// Sets a property in the `VideoWriter`. /// Note: `VideoWriterProperty::FrameBytes` is read-only. pub fn set(&self, property: VideoWriterProperty, value: f64) -> bool { unsafe { cv_videowriter_set(self.inner, property, value) } } /// Gets a property in the `VideoWriter`. pub fn get(&self, property: VideoWriterProperty) -> Option<f64> { let ret = unsafe { cv_videowriter_get(self.inner, property) }; if ret != 0.0 { Some(ret) } else { None } } } impl Default for VideoWriter { fn default() -> VideoWriter { VideoWriter { inner: unsafe { cv_videowriter_default() }, } } } impl Drop for VideoWriter { fn drop(&mut self) { unsafe { cv_videowriter_drop(self.inner); } } } /// `VideoWriter`'s property identifier. #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum VideoWriterProperty { /// Current quality of the encoded videostream. Quality = 1, /// (Read-only) Size of just encoded video frame; note that the encoding /// order may be different from representation order. FrameBytes = 2, /// Number of stripes for parallel encoding NStripes = 3, } // ============================================================================= // Utility functions // ============================================================================= /// Converts from [four character code](https://www.fourcc.org/) to `u32` pub fn codec_name_from_4cc(value: &str) -> Result<u32, Error> { if value.len() != 4 || value.chars().any(|c| !c.is_ascii()) { Err(CvError::UnicodeChars(value.into()).into()) } else { let bytes = value.as_bytes(); let result = ((bytes[0] as u32) & 0xFFu32) + (((bytes[1] as u32) & 0xFFu32) << 8) + (((bytes[2] as u32) & 0xFFu32) << 16) + (((bytes[3] as u32) & 0xFFu32) << 24); Ok(result) } } /// Converts to [four character code](https://www.fourcc.org/) from `u32`. pub fn codec_name_to_4cc(value: u32) -> String { let vec = vec![ (value & 0xFFu32) as u8, ((value & 0xFF00u32) >> 8) as u8, ((value & 0xFF0000u32) >> 16) as u8, ((value & 0xFF000000u32) >> 24) as u8, ]; String::from_utf8(vec).unwrap() }