Skip to content
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

Camera improvements #2772

Merged
merged 7 commits into from Oct 23, 2021
Merged

Camera improvements #2772

merged 7 commits into from Oct 23, 2021

Conversation

Starbuck5
Copy link
Contributor

@Starbuck5 Starbuck5 commented Oct 13, 2021

This gets the pygame.camera module up to cross platform feature completeness.

It includes:

  • A new OpenCV backend. (Using the new common python port of opencv, opencv-python / import cv2
  • A custom OpenCV-mac backend that supports camera enumeration by using the system_profiler command
  • Programmatic backend management for the pygame.camera module (pygame.camera.get_backends(), pygame.camera.init(backend: str))
  • Maintain backwards compatibility with people using the PYGAME_CAMERA environment variable.
  • pygame.camera.colorspace function actually working when not on an _camera backend. (Except on windows python 2, where the _camera backend isn't built :/)
  • Removal of mac custom camera code that stopped working in 2009

The pygame.camera module is one of our more "troubled" modules, and this doesn't fix everything I've seen that isn't quite right, but I think it's a large positive step.

@lgtm-com
Copy link

lgtm-com bot commented Oct 13, 2021

This pull request introduces 4 alerts when merging 86ffff3 into fb51ba4 - view on LGTM.com

new alerts:

  • 4 for Except block handles 'BaseException'

@illume illume added the camera pygame.camera label Oct 16, 2021
@illume illume added this to the 2.0.3 milestone Oct 16, 2021
Copy link
Member

@illume illume left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 🎉 Nice improvements.

Left some questions and suggestions.

src_py/_camera_opencv.py Show resolved Hide resolved
src_py/_camera_opencv.py Show resolved Hide resolved
if flip_code is not None:
image = cv2.flip(image, flip_code)

image = numpy.fliplr(image)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove the dependency on numpy with some cv2 functions like in my cv2 camera module: https://github.com/pygame/parrotjoy/blob/master/parrotjoy/camera_cv.py#L46

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cv2 itself is dependent on numpy, so I figured it wouldn't hurt to do it like this.

src_py/camera.py Outdated
def _setup_opencv_legacy():
global list_cameras, Camera, colorspace

from pygame import _camera_opencv_highgui
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should bother with this backend. It's very hard to actually use compared to cv2. I'd suggest removing it in this PR. I'm pretty sure I'm the only user of this, and the module is experimental... so we don't need to give any warning before removing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to remove this one, I just wanted to keep this PR easy on the backwards compat. I think it'll be good to remove though.

except ImportError:
_camera = None

warnings.warn("The VideoCapture backend is not recommended and may be removed."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's kind of nice to use on windows still. As a user, I'm planning on still using VideoCapture still. Not sure there's any other person on earth that applies to though.

So, let's remove VideoCapture support now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My impression is that more people are using this than the legacy opencv. Like, I saw an issue where MyreMylar showed off windows webcam support with this. And my Camera backend doesn't support python2. I guess the transition path would be to an OpenCV backend.

I should at least add in that the VideoCapture module doesn't support enumeration either, in the doc.


list_cameras = _camera.list_cameras
Camera = _camera.Camera
def init(backend=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain the advantage of a backend arg? For other modules we use environment variables to do this. I guess it's a bit easier to test with?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that the backend arg and the get_backends() function will make the internal workings more transparent to users. Which is especially important for this module, since the backend choice impacts lots of things, like what modules need to be installed.

Why not an environment variable? With init(backend=), you can initialize multiple backends (at separate times) during program execution, or easily test if one crashes or not (which would be a bit clunkier with environment variables). It seems intuitive to me to have it in with the rest of the code being executed rather than an environment variable set at the beginning, especially since this module is so squirrely.

src_py/_camera_opencv.py Outdated Show resolved Hide resolved

return self.get_controls()

def get_controls(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, get_controls is sort of limited.

One problem with the cv2 control system is that controls are very different depending on camera/backend. Somehow keeping a set of well supported controls would be a nice feature to have.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely. Plus the mappings to whatever they're called in the native backends. But that's out of scope on this PR.

When I was experimenting with MSMF brightness control, I actually skipped around some of the OpenCV source to figure out how to do it.

The weird thing was that it was "sticky" between program runs, so it probably needs to get "unset" as a cleanup.

docs/reST/ref/camera.rst Outdated Show resolved Hide resolved
Copy link
Member

@illume illume left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

I'd like to merge this in. But as with all API changes, it would be good to have more discussion and input from users. But this can still happen even post-merge.

Copy link
Contributor

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Nice PR 🎉

@illume illume merged commit 69043a8 into pygame:main Oct 23, 2021
@Starbuck5 Starbuck5 deleted the camera-improvements branch May 23, 2022 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants