Repository navigation
Decorators around annotate functions, convert PIL images to np.ndarray - #875
Conversation
…y and back, if required
|
Seems that checker doesn't like variable docstrings at all - not them being below the declarations. |
* Bad decision, but makes the pre-commit hook happy
|
Ready for review. Second commit removes the docstring I think is important, but makes the pre-commit hook pass. |
|
Hi @LinasKo 👋🏻 Thanks for your interest in supervision and time working on this PR. Thank you also for the in-depth description that helped me understand your thinking. From the description of the PR, I conclude that this is a kind of invitation to discussion, and potential changes are still possible. Here is my hierarchy of priorities: documentation, mypy happiness, being DRY. This is a library. I have no problem with repetition (within reason, of course) as long as users have total comfort.
Could you elaborate a little on this thought? I'm not quite sure I know what you mean :) |
| opencv-python-headless = ">=4.5.5.64" | ||
| requests = { version = ">=2.26.0,<=2.31.0", optional = true } | ||
| tqdm = { version = ">=4.62.3,<=4.66.1", optional = true } | ||
| types-pillow = "^10.2.0.20240206" |
There was a problem hiding this comment.
@onuralpszr I'm curious about your opinion here.
There was a problem hiding this comment.
I added this and poetry.lock by mistake. This commit should only have touched annotators/base.py.
Shall I revert that?
There was a problem hiding this comment.
Hi @LinasKo, my main question is how will you run annotated = Image.fromarray(annotated) without Pillow?
There was a problem hiding this comment.
@LinasKo I'm also updating my previous comment about docs. Please give me few minutes.
There was a problem hiding this comment.
Oh, that's right, you don't have Pillow as a dependency at all? That's curious.
Seems like me having it installed when running the tests was preventing any dependency errors from popping up.
Yes, it needs to be added.
There was a problem hiding this comment.
@SkalskiP types-pillow or type-opencv etc. for mypy and type checks not a dep for actual package
Awesome! I am very happy that people like you want to join the community.
I try to make the quality of the code as high as possible, but I believe that specific appeals change when you build an open-source library that you want others to contribute to. I try to build the code so that outsiders can come in and make changes without being thoroughly familiar with the entire library. I can expand on this thought if you want.
@onuralpszr, can we add some ruff exceptions to the rule here? Also:
|
|
pre-commit/pre-commit-hooks#159 This is a known issue so I am probably going to remove that pre-commit rule from our list and going to add docs rules for python to enforce us to type properly. For short solution either let's remove or exclude file but I would prefer to remove rule and adding rule in separate PR. I suggest @LinasKo remove "- id: check-docstring-first" rule and I will come up something better but I don't wanna add more commit into this PR. |
* Add docstring for ImageType type * As discussed in roboflow#875 (comment)
|
@SkalskiP At the time of writing the only conflicts are the hash in pyproject.toml <<<<<<< feature/annotators-accept-PIL-images
tqdm = { version = ">=4.62.3,<=4.66.1", optional = true }
pillow = "^10.2.0"
=======
tqdm = { version = ">=4.62.3,<=4.66.2", optional = true }
>>>>>>> develop( The workflow I'm used to is running a rebase onto the latest There's also #889 waiting to be merged. The tidiest way is to merge that in, squish commits in this PR, rebase on top. What's your usual process? Do you prefer an extra commit to resolve conflicts before merging, or do you rebase? In the latter case, does it affect GitHub PR history? Do I just hand it off to one of you at this point? |
|
@LinasKo can you do resolve the conflict or I can do it too ? |
|
@LinasKo accept our develop side then add pillow via poetry and make sure you effect lock file as well. If you cant I can handle it too |
|
@onuralpszr, "Accept our develop side" - do you mean a merge request from On my machine, it's ready to be pushed, so let me know and I'll do it. |
|
I figured out what you meant. Resolve here, accept develop changes, then pull, and add Pillow. 1 sec. |
|
@LinasKo you need add pillow as well I thought you gonna add while merging but please re add it :) |
|
@onuralpszr, That was a merge-via-UI, which would not have helped generate the hash. Should be ready now. Dependency resolution took ages. Thanks! |
If you struggle I can clean up too no problem. :) |
It's a trivial fix - various steps just took ages :) |
Signed-off-by: Onuralp SEZER <thunderbirdtr@gmail.com>
|
@LinasKo thank you for your contribution, merging in !! |
|
Hi @LinasKo and @onuralpszr ! 👋🏻 Thanks for pushing this PR to the finish line without me! I really like how this change has been implemented. Very clean with minimal code changes. 🔥 |


Description
PR for #859: Annotators will now accept both
np.ndarrayandPIL.Image.Image.np.ndarrayandPIL.Image.Imagenp.ndarray, converts to it, the does annotation, and converts the image back.annotateimplementation I could find, including the deprecatedBoxAnnotator.ImgTypeImgType, but the static checker did not allow it below the declaration, so it does not appear in my VSCode when hovering over the type. More on this later.No new dependencies, but internally, a new image type is now created in
supervision/annotators/base.pyHow has this change been tested, please provide a testcase or example of how you tested the change?
For all annotator types, visualised the before and after results, both when
np.ndarrayimage, andPIL.Image.Imageare passed in. Ran the same on latestdevelopbranch to check for regressions (with PIL images commented out as that's not supported)Colab: https://colab.research.google.com/drive/14dtq8SrY1FanCPMT0GGZ2R-7jrbvfQw4?usp=sharing
Test assets:
Image: https://github.com/roboflow/supervision/assets/6500785/ec6ca6cb-0215-413b-96c6-f6802c34ece8
Video: https://github.com/roboflow/supervision/assets/6500785/4994d0bc-e79b-4f63-ac03-c83ddf472756
Any specific deployment considerations
None that I'm aware of.
Docs
Would we need to explain what
ImageTypeis in more detail somewhere?Tradeoffs
There was a surprising amount of tradeoffs to consider for this one. A simple change, but without knowing your philosophy around documentation, the "right" solution might be different:
UnionvsTypeVarThe issue with
Unionis that it does not guarantee the type passed in toannotatewill be the same as the one returned. If anyone's usingmypy, it will produce red error squiggles. Every. Single. Time.I know you're not aiming for
mypyconsistency yet, but users who do would need atyping.castcall orisinstancewhenever annotators are used, or learn to ignore error markers.On a positive side,
Union[img_t1, img_t2]is self-documenting and very clear as to what it is.TypeVaris an alternative that binds the values, very similar to how C++ templates would treat typeT. E.g. If you passnp.ndarray, the static checkers will know it'snp.ndarraywhen it's returned.The downside - it's not self-documenting. When you mouse over
ImgType, you get(variable) ImgType: type[ImgType]and need to go to the source to see what it is, so it's harder to know what arguments to pass in.But, mouse over the result of
annotate, and you'll get a nice cleannp.ndarrayorPIL.Image.Image, depending on what you passed in.In my opinion, it's annoying when you copy-paste the simplest examples of a library and it already produces error markers. The static type safety is worth it, and
TypeVaris the way to go, with docs that could be added to explain usage.In light of that, I added a docstring, but it needs to be below the declaration to appear in VSCode, and that's prevented by the pre-commit hook. I moved it above
TypeVarfor now.Simple functions vs Decorator vs Wrapper
annotateas is, taking annd.array, and make a decorator that takesImgTypeand converts. It's what we want to happen, but the issue is docs - the user will seenp.ndarrayas required argument, but actually it's the more broaderImgTypeannotateto acceptImgType(that we always know will benp.ndarray. Docs are now correct from user's perspective, but not if someone was somehow calling the function directly / forgets to add the decorator.annotateinto 2: publicannotatetakingImgTypeand private_annotatetakingnp.ndarray. The user sees the general one that does the conversion, and each annotator implments_annotate. The downside? Docs, again. We'd lose the ability to have different ones for each annotator.