letmaik / pyvirtualcam

🎥 Send frames to a virtual camera from Python
GNU General Public License v2.0
471 stars 50 forks source link

Numpy 2.0 is out, pyvirtualcam doesn't work with it #123

Closed wrzwicky closed 2 months ago

wrzwicky commented 3 months ago

Describe the bug Pyvirtualcam does not work with the new Numpy 2.0. You may want to update your dependencies to make that explicit, or update the code to support 2.0.

To Reproduce I installed the code for my Oak D Lite camera: pipenv shell pipenv install depthai pyvirtualcam This ended up installing Numpy 2.0.0.

I then ran their sample code to build a UVC camera. The result crashed from inside your camera.py, line 348 frame = np.array(frame.reshape(-1), copy=False, order='C') with the error:

ValueError: Unable to avoid copy while creating an array as requested.
If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).

When I made the change suggested, I got a video feed that showed a flat grey color that flickered in brightness.

I then downgraded Numpy to 1.26 (pipenv install numpy==1.26) and the camera feed started working fine. When I reverted my change to your file, the camera feed continued to work fine.

letmaik commented 3 months ago

Can you download and test the preview wheels from the artifacts at https://github.com/letmaik/pyvirtualcam/actions/runs/9624551391#artifacts? This was built against numpy 2.

letmaik commented 2 months ago

@wrzwicky Ping

wrzwicky commented 2 months ago

Sorry for being a slacker, turns out depthai released 2.27 around the time you were doing your fix. Their 2.27 actually fixes the true problem, as using their 2.26 with your 0.12.0 exhibits the same gray flickering screen I saw above.

More importantly, their 2.27 works fine with your 0.11.1.

You'll need to decide whether to push your change; asarray() allowing copy will crash less, but may also sometimes consume more CPU with no explanation. And it may even be wrong, since a crash really means the incoming frame data is not in the expected format. If that's true, you may want to catch and rethrow a new exception with a more applicable message. (I did this sort of thing all the time in Java.)

And also, since I'm here, I can't make sense of np.array(frame.reshape()) on line 348. Looking at the docs, it appears copy=False basically means np.array() does nothing, and since reshape(-1) returns a 1-D array, then order='C' also does nothing.

Ok I'll stop now. Thanks for your time and attention.

wrzwicky commented 2 months ago

Closing as solved. Bug was fixed in depthai; both pyvirtualcam 0.11.1 and the proposed 0.12.0 work fine.