Skip to content

[cv2] Completed type stubs for the following functions and their shared parameters: #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2022

Conversation

Avasam
Copy link
Contributor

@Avasam Avasam commented Nov 23, 2021

  • cv2.norm
  • cv2.resize
  • cv2.cvtColor
  • cv2.imread
  • cv2.inRange
  • cv2.imwrite
  • cv2.calcHist
  • cv2.normalize
  • cv2.compareHist
  • cv2.matchTemplate
  • cv2.minMaxLoc
  • cv2.bitwise_and
  • ndarray

I filled in the type stubs based on our usage in https://github.com/Toufool/Auto-Split (Toufool/AutoSplit/pull/92) and what documentation I could find online.
Fixes microsoft/pylance-release/issues/2089

@ghost
Copy link

ghost commented Nov 23, 2021

CLA assistant check
All CLA requirements met.

@Avasam Avasam force-pushed the complete-type-stub-for-AutoSplit branch from 675265b to 650dba1 Compare November 24, 2021 02:04
@Avasam Avasam force-pushed the complete-type-stub-for-AutoSplit branch 4 times, most recently from 29e2783 to 9063684 Compare April 8, 2022 17:32
…ameters:

norm
resize
cvtColor
imread
inRange
imwrite
calcHist
normalize
compareHist
matchTemplate
minMaxLoc
bitwise_and
ndarray
@Avasam Avasam force-pushed the complete-type-stub-for-AutoSplit branch from 9063684 to 66168fc Compare April 8, 2022 17:38
@Avasam
Copy link
Contributor Author

Avasam commented Apr 8, 2022

Any update? This has been stalling for over 4 months without any acknowledgment.

@bschnurr
Copy link
Member

bschnurr commented Apr 8, 2022

Sorry forgot about this PR. I was hoping this OpenCV PR was going to solve everything.
https://github.com/opencv/opencv/pull/20370/files

Only suggestion would be to use the OpenCV alias names for parameters like Mat instead ndarray directly.
In that PR I linked they have a bit table of alias names.

I'm no openCV expert, not sure is Mat is exactly an ndarry though.

@Avasam
Copy link
Contributor Author

Avasam commented Apr 8, 2022

Sorry forgot about this PR. I was hoping this OpenCV PR was going to solve everything. https://github.com/opencv/opencv/pull/20370/files

Oh looks like they might be adding some autogenerated type stubs soon. That would be nice.

Only suggestion would be to use the OpenCV alias names for parameters like Mat instead ndarray directly. In that PR I linked they have a bit table of alias names.

The type alias don't exist yet, but I can easily do Mat = np.ndarray if you'd like.
This PR also specified the generic types of ndarray (numpy.ndarray[int, numpy.dtype[typing.Any]]) so it doesn't result in ndarray[Unknown] (partially Unknown) errors with strict typing. If you're hesitant about that, I can split that in a different PR.

I'm no openCV expert, not sure is Mat is exactly an ndarry though.

Not an expert either, but I know that cv2 (opencv-python) uses numpy arrays directly, there's no "Mat" (Mat class definition in C++ : https://docs.opencv.org/4.5.5/d3/d63/classcv_1_1Mat.html)

@bschnurr
Copy link
Member

bschnurr commented Apr 8, 2022

does pandas dtype example help?
https://github.com/pandas-dev/pandas/blob/9b4f03e30a5b4d2c073d4f0c357fe1d02e7cb05d/pandas/_typing.py#L129

maybe numpy.ndarray[int, numpy.dtype[object]] is better following typeshed notes below.

https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#conventions
"Note that Any is not the correct type to use if you want to indicate that some function can accept literally anything: in those cases use object instead."

@bschnurr
Copy link
Member

bschnurr commented Apr 8, 2022

More discussion here. python/typing#516

@bschnurr
Copy link
Member

bschnurr commented Apr 8, 2022

does numpy.DTypeLike help? I see pandas using that a lot

@Avasam
Copy link
Contributor Author

Avasam commented Apr 8, 2022

Thanks for the references !

Numpy themselves use dtype[Any]
image
object is not compatible, but I guess it should be np.generic
image
image

does numpy.DTypeLike help? I see pandas using that a lot

image

So then: Mat = np.ndarray[int, np.dtype[np.generic]]
image

@Avasam
Copy link
Contributor Author

Avasam commented Apr 8, 2022

Or even Mat = npt.NDArray[np.generic] which is equivalent to Mat = np.ndarray[Any, np.dtype[Any]]. This way we don't add Any directly to this code base, and don't restrict the Mat to int/float (in case that causes issues with co/contravariance). While still avoiding strict type checker issues due to Unknown or missing generic params.

@bschnurr
Copy link
Member

Thanks for the contributions. Going forward I'd like to fix a bunch of the return types too.

@bschnurr bschnurr merged commit bff9cff into microsoft:main Apr 12, 2022
@bschnurr
Copy link
Member

hmm. np.generic might only work for python version 3.9.

@Avasam
Copy link
Contributor Author

Avasam commented Apr 12, 2022

hmm. np.generic might only work for python version 3.9.

Is something not working? numpy.generic has been added 17 years ago. However the type stub has only been created around 3 years ago.

@bschnurr
Copy link
Member

still need to test on python 3.8 and 3.7... just saw similar issue with panda's test failing

@neur1n
Copy link

neur1n commented Dec 16, 2022

Why the dts and normType arguments in some functions (such as normalize) changed from dst and norm_type? I think these new naming is different from the API's.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[cv2-stubs] cv2.norm function is missing a parameter
3 participants