Skip to content

Commit a27885e

Browse files
committed
Fix cudacodec and cudastereo python bindings.
1 parent 95fd837 commit a27885e

File tree

5 files changed

+98
-20
lines changed

5 files changed

+98
-20
lines changed

modules/cudacodec/include/opencv2/cudacodec.hpp

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,11 @@ class CV_EXPORTS_W VideoReader
353353

354354
/** @brief Grabs, decodes and returns the next video frame.
355355
356-
If no frames has been grabbed (there are no more frames in video file), the methods return false .
357-
The method throws Exception if error occurs.
356+
@param [out] frame The video frame.
357+
@return `false` if no frames have been grabbed.
358+
359+
If no frames have been grabbed (there are no more frames in video file), the methods return false.
360+
The method throws an Exception if error occurs.
358361
*/
359362
CV_WRAP virtual bool nextFrame(CV_OUT GpuMat& frame, Stream &stream = Stream::Null()) = 0;
360363

@@ -376,17 +379,44 @@ class CV_EXPORTS_W VideoReader
376379

377380
/** @brief Returns previously grabbed video data.
378381
379-
@param [out] frame The returned data which depends on the provided idx. If there is no new data since the last call to grab() the image will be empty.
380-
@param idx Determins the returned data inside image. The returned data can be the:
381-
Decoded frame, idx = get(PROP_DECODED_FRAME_IDX).
382-
Extra data if available, idx = get(PROP_EXTRA_DATA_INDEX).
383-
Raw encoded data package. To retrieve package i, idx = get(PROP_RAW_PACKAGES_BASE_INDEX) + i with i < get(PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB)
384-
@return `false` if no frames has been grabbed
382+
@param [out] frame The returned data which depends on the provided idx.
383+
@param idx Determines the returned data inside image. The returned data can be the:
384+
- Decoded frame, idx = get(PROP_DECODED_FRAME_IDX).
385+
- Extra data if available, idx = get(PROP_EXTRA_DATA_INDEX).
386+
- Raw encoded data package. To retrieve package i, idx = get(PROP_RAW_PACKAGES_BASE_INDEX) + i with i < get(PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB)
387+
@return `false` if no frames have been grabbed
388+
389+
The method returns data associated with the current video source since the last call to grab() or the creation of the VideoReader. If no data is present
390+
the method returns false and the function returns an empty image.
391+
*/
392+
virtual bool retrieve(OutputArray frame, const size_t idx = static_cast<size_t>(VideoReaderProps::PROP_DECODED_FRAME_IDX)) const = 0;
393+
394+
/** @brief Returns previously grabbed encoded video data.
395+
396+
@param [out] frame The encoded video data.
397+
@param idx Determines the returned data inside image. The returned data can be the:
398+
- Extra data if available, idx = get(PROP_EXTRA_DATA_INDEX).
399+
- Raw encoded data package. To retrieve package i, idx = get(PROP_RAW_PACKAGES_BASE_INDEX) + i with i < get(PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB)
400+
@return `false` if no frames have been grabbed
385401
386402
The method returns data associated with the current video source since the last call to grab() or the creation of the VideoReader. If no data is present
387403
the method returns false and the function returns an empty image.
388404
*/
389-
CV_WRAP virtual bool retrieve(CV_OUT OutputArray frame, const size_t idx = static_cast<size_t>(VideoReaderProps::PROP_DECODED_FRAME_IDX)) const = 0;
405+
CV_WRAP inline bool retrieve(CV_OUT Mat& frame, const size_t idx) const {
406+
return retrieve(OutputArray(frame), idx);
407+
}
408+
409+
/** @brief Returns the next video frame.
410+
411+
@param [out] frame The video frame. If grab() has not been called then this will be empty().
412+
@return `false` if no frames have been grabbed
413+
414+
The method returns data associated with the current video source since the last call to grab(). If no data is present
415+
the method returns false and the function returns an empty image.
416+
*/
417+
CV_WRAP inline bool retrieve(CV_OUT GpuMat& frame) const {
418+
return retrieve(OutputArray(frame));
419+
}
390420

391421
/** @brief Sets a property in the VideoReader.
392422
@@ -395,7 +425,10 @@ class CV_EXPORTS_W VideoReader
395425
@param propertyVal Value of the property.
396426
@return `true` if the property has been set.
397427
*/
398-
CV_WRAP virtual bool set(const VideoReaderProps propertyId, const double propertyVal) = 0;
428+
virtual bool set(const VideoReaderProps propertyId, const double propertyVal) = 0;
429+
CV_WRAP inline bool setVideoReaderProps(const VideoReaderProps propertyId, double propertyVal) {
430+
return set(propertyId, propertyVal);
431+
}
399432

400433
/** @brief Set the desired ColorFormat for the frame returned by nextFrame()/retrieve().
401434
@@ -408,11 +441,12 @@ class CV_EXPORTS_W VideoReader
408441
@param propertyId Property identifier from cv::cudacodec::VideoReaderProps (eg. cv::cudacodec::VideoReaderProps::PROP_DECODED_FRAME_IDX,
409442
cv::cudacodec::VideoReaderProps::PROP_EXTRA_DATA_INDEX, ...).
410443
@param propertyVal
411-
In - Optional value required for querying specific propertyId's, e.g. the index of the raw package to be checked for a key frame (cv::cudacodec::VideoReaderProps::PROP_LRF_HAS_KEY_FRAME).
412-
Out - Value of the property.
444+
- In: Optional value required for querying specific propertyId's, e.g. the index of the raw package to be checked for a key frame (cv::cudacodec::VideoReaderProps::PROP_LRF_HAS_KEY_FRAME).
445+
- Out: Value of the property.
413446
@return `true` unless the property is not supported.
414447
*/
415-
CV_WRAP virtual bool get(const VideoReaderProps propertyId, CV_IN_OUT double& propertyVal) const = 0;
448+
virtual bool get(const VideoReaderProps propertyId, double& propertyVal) const = 0;
449+
CV_WRAP virtual bool getVideoReaderProps(const VideoReaderProps propertyId, CV_OUT double& propertyValOut, double propertyValIn = 0) const = 0;
416450

417451
/** @brief Retrieves the specified property used by the VideoSource.
418452

modules/cudacodec/misc/python/test/test_cudacodec.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,35 @@ def test_reader(self):
3434
ret, _gpu_mat2 = reader.nextFrame(gpu_mat)
3535
#TODO: self.assertTrue(gpu_mat == gpu_mat2)
3636
self.assertTrue(ret)
37+
38+
params = cv.cudacodec.VideoReaderInitParams()
39+
params.rawMode = True
40+
ms_gs = 1234
41+
reader = cv.cudacodec.createVideoReader(vid_path,[cv.CAP_PROP_OPEN_TIMEOUT_MSEC, ms_gs], params)
42+
ret, ms = reader.get(cv.CAP_PROP_OPEN_TIMEOUT_MSEC)
43+
self.assertTrue(ret and ms == ms_gs)
44+
ret, raw_mode = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_RAW_MODE)
45+
self.assertTrue(ret and raw_mode)
46+
47+
ret, colour_code = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_COLOR_FORMAT)
48+
self.assertTrue(ret and colour_code == cv.cudacodec.ColorFormat_BGRA)
49+
colour_code_gs = cv.cudacodec.ColorFormat_GRAY
50+
reader.set(colour_code_gs)
51+
ret, colour_code = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_COLOR_FORMAT)
52+
self.assertTrue(ret and colour_code == colour_code_gs)
53+
54+
ret, i_base = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_RAW_PACKAGES_BASE_INDEX)
55+
self.assertTrue(ret and i_base == 2.0)
56+
self.assertTrue(reader.grab())
57+
ret, gpu_mat3 = reader.retrieve()
58+
self.assertTrue(ret and isinstance(gpu_mat3,cv.cuda.GpuMat) and not gpu_mat3.empty())
59+
ret = reader.retrieve(gpu_mat3)
60+
self.assertTrue(ret and isinstance(gpu_mat3,cv.cuda.GpuMat) and not gpu_mat3.empty())
61+
ret, n_raw_packages_since_last_grab = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB)
62+
self.assertTrue(ret and n_raw_packages_since_last_grab > 0)
63+
ret, raw_data = reader.retrieve(int(i_base))
64+
self.assertTrue(ret and isinstance(raw_data,np.ndarray) and np.any(raw_data))
65+
3766
except cv.error as e:
3867
notSupported = (e.code == cv.Error.StsNotImplemented or e.code == cv.Error.StsUnsupportedFormat or e.code == cv.Error.GPU_API_CALL_ERROR)
3968
self.assertTrue(notSupported)

modules/cudacodec/src/video_reader.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ namespace
102102
void set(const ColorFormat _colorFormat) CV_OVERRIDE;
103103

104104
bool get(const VideoReaderProps propertyId, double& propertyVal) const CV_OVERRIDE;
105+
bool getVideoReaderProps(const VideoReaderProps propertyId, double& propertyValOut, double propertyValIn) const CV_OVERRIDE;
105106

106107
bool get(const int propertyId, double& propertyVal) const CV_OVERRIDE;
107108

@@ -252,7 +253,7 @@ namespace
252253
else{
253254
if (idx >= rawPacketsBaseIdx && idx < rawPacketsBaseIdx + rawPackets.size()) {
254255
if (!frame.isMat())
255-
CV_Error(Error::StsUnsupportedFormat, "Raw data is stored on the host and must retrievd using a cv::Mat");
256+
CV_Error(Error::StsUnsupportedFormat, "Raw data is stored on the host and must retrieved using a cv::Mat");
256257
Mat tmp(1, rawPackets.at(idx - rawPacketsBaseIdx).size, CV_8UC1, rawPackets.at(idx - rawPacketsBaseIdx).Data(), rawPackets.at(idx - rawPacketsBaseIdx).size);
257258
frame.getMatRef() = tmp;
258259
}
@@ -264,8 +265,9 @@ namespace
264265
switch (propertyId) {
265266
case VideoReaderProps::PROP_RAW_MODE :
266267
videoSource_->SetRawMode(static_cast<bool>(propertyVal));
268+
return true;
267269
}
268-
return true;
270+
return false;
269271
}
270272

271273
void VideoReaderImpl::set(const ColorFormat _colorFormat) {
@@ -303,20 +305,28 @@ namespace
303305
else
304306
break;
305307
}
306-
case VideoReaderProps::PROP_ALLOW_FRAME_DROP: {
308+
case VideoReaderProps::PROP_ALLOW_FRAME_DROP:
307309
propertyVal = videoParser_->allowFrameDrops();
308310
return true;
309-
}
310-
case VideoReaderProps::PROP_UDP_SOURCE: {
311+
case VideoReaderProps::PROP_UDP_SOURCE:
311312
propertyVal = videoParser_->udpSource();
312313
return true;
313-
}
314+
case VideoReaderProps::PROP_COLOR_FORMAT:
315+
propertyVal = static_cast<double>(colorFormat);
316+
return true;
314317
default:
315318
break;
316319
}
317320
return false;
318321
}
319322

323+
bool VideoReaderImpl::getVideoReaderProps(const VideoReaderProps propertyId, double& propertyValOut, double propertyValIn) const {
324+
double propertyValInOut = propertyValIn;
325+
const bool ret = get(propertyId, propertyValInOut);
326+
propertyValOut = propertyValInOut;
327+
return ret;
328+
}
329+
320330
bool VideoReaderImpl::get(const int propertyId, double& propertyVal) const {
321331
return videoSource_->get(propertyId, propertyVal);
322332
}

modules/cudacodec/test/test_video.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ CUDA_TEST_P(Video, Reader)
201201
// request a different colour format for each frame
202202
const std::pair< cudacodec::ColorFormat, int>& formatToChannels = formatsToChannels[i % formatsToChannels.size()];
203203
reader->set(formatToChannels.first);
204+
double colorFormat;
205+
ASSERT_TRUE(reader->get(cudacodec::VideoReaderProps::PROP_COLOR_FORMAT, colorFormat) && static_cast<cudacodec::ColorFormat>(colorFormat) == formatToChannels.first);
204206
ASSERT_TRUE(reader->nextFrame(frame));
205207
if(!fmt.valid)
206208
fmt = reader->format();

modules/cudastereo/include/opencv2/cudastereo.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,10 @@ disparity map.
355355
356356
@sa reprojectImageTo3D
357357
*/
358-
CV_EXPORTS_W void reprojectImageTo3D(InputArray disp, OutputArray xyzw, InputArray Q, int dst_cn = 4, Stream& stream = Stream::Null());
358+
CV_EXPORTS void reprojectImageTo3D(InputArray disp, OutputArray xyzw, InputArray Q, int dst_cn = 4, Stream& stream = Stream::Null());
359+
CV_EXPORTS_W inline void reprojectImageTo3D(GpuMat disp, CV_OUT GpuMat& xyzw, Mat Q, int dst_cn = 4, Stream& stream = Stream::Null()) {
360+
reprojectImageTo3D((InputArray)disp, (OutputArray)xyzw, (InputArray)Q, dst_cn, stream);
361+
}
359362

360363
/** @brief Colors a disparity image.
361364

0 commit comments

Comments
 (0)