feat: update conversion logic for std::vector<T> in Python bindings#20618
Merged
opencv-pushbot merged 1 commit intoopencv:3.4from Sep 1, 2021
Merged
Conversation
alalek
reviewed
Aug 27, 2021
70c7c64 to
d4f18b8
Compare
6 tasks
Contributor
Author
|
Failed test in aruco is fixed in opencv/opencv_contrib#3031 : In original test - 2d array passed to |
ebdc183 to
03c25a2
Compare
alalek
reviewed
Aug 31, 2021
| if (len > 0) | ||
| { | ||
| RNG rng(12345); | ||
| cv::Mat tmp(static_cast<int>(len), 1, CV_32SC4); |
Member
There was a problem hiding this comment.
avoid using of unnecessary cv:: in OpenCV source code
modules/core/src/bindings_utils.cpp
Outdated
| } | ||
|
|
||
| template <class T, class Formatter> | ||
| String dumpVector(const std::vector<T>& vec, Formatter format) |
4 tasks
`PyObject*` to `std::vector<T>` conversion logic:
- If user passed Numpy Array
- If array is planar and T is a primitive type (doesn't require
constructor call) that matches with the element type of array, then
copy element one by one with the respect of the step between array
elements. If compiler is lucky (or brave enough) copy loop can be
vectorized.
For classes that require constructor calls this path is not
possible, because we can't begin an object lifetime without hacks.
- Otherwise fall-back to general case
- Otherwise - execute the general case:
If PyObject* corresponds to Sequence protocol - iterate over the
sequence elements and invoke the appropriate `pyopencv_to` function.
`std::vector<T>` to `PyObject*` conversion logic:
- If `std::vector<T>` is empty - return empty tuple.
- If `T` has a corresponding `Mat` `DataType` than return
Numpy array instance of the matching `dtype` e.g.
`std::vector<cv::Rect>` is returned as `np.ndarray` of shape `Nx4` and
`dtype=int`.
This branch helps to optimize further evaluations in user code.
- Otherwise - execute the general case:
Construct a tuple of length N = `std::vector::size` and insert
elements one by one.
Unnecessary functions were removed and code was rearranged to allow
compiler select the appropriate conversion function specialization.
03c25a2 to
16b9514
Compare
4 tasks
alalek
reviewed
Sep 1, 2021
Merged
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #18412, resolves #14636
PyObject*tostd::vector<T>conversion logic:copy element one by one with the respect of the step between array elements. If compiler is lucky (or brave enough) copy loop can be vectorized.
For classes that require constructor calls this path is not possible, because we can't begin an object lifetime without hacks.
If PyObject* corresponds to Sequence protocol - iterate over the sequence elements and invoke the appropriate
pyopencv_tofunction.std::vector<T>toPyObject*conversion logic:std::vector<T>is empty - return empty tuple.Thas a correspondingMatDataTypethan returnNumpy array instance of the matching
dtypee.g.std::vector<cv::Rect>is returned asnp.ndarrayof shapeNx4anddtype=int.This branch helps to optimize further evaluations in user code.
Construct a tuple of length N =
std::vector::sizeand insert elements one by one.Unnecessary functions were removed and code was rearranged to allow compiler select the appropriate conversion function specialization.
Possible may resolve #10158 if
DataTypeforDMatchdeprecation will be revoked.Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.