tshino / softcam

A library to make a virtual webcam on Windows
MIT License
110 stars 30 forks source link

Create Python binding for softcam #12

Closed kevinpl07 closed 1 year ago

kevinpl07 commented 1 year ago

Hello,

I tried creating a python binding for softcam but I can't seem to make it work.

Those are the things i looked into: Write C++ extensions for Python - Visual Studio Speeding up Python with C++ and Pybind11

Has anyone tried that or has any ideas what I could do?

Thanks in advance and best regards! Kev

tshino commented 1 year ago

Hi, thank you for your interest and effort! Actually, I have some experience with pybind11 in other projects, I'm sure that you are going in the right direction.

Since in python we are likely using a Numpy array to hold an image, you can use py::array_t<uint8_t> to pass the image to C++. Since the element order (like row/column major) of a Numpy array can be different from the softcam's expectation, py::array::c_style option would be useful to avoid costly memory copy and to ensure you can safely pass the pointer to softcam.

Maybe I can do the whole thing to create a python bind of softcam, by just taking half a day, and I'm happy to do that. So, I don't want to take your work, but if you are okay, I will do that.

Anyway, thanks for the great idea!

kevinpl07 commented 1 year ago

Hi @tshino,

if you are really willing to do that, that'd be amazing! I'm not that experienced with Visual Studio and C++, hence my frustration. Let me know if I could be of any assistance and thanks for your help!

Kind regards Kev

tshino commented 1 year ago

Hi @kevinpl07, I've just made it and it is already working! The code is in the branch for #13, so you can try it now.

Steps to build:

  1. run DownloadPybind11.bat.
  2. activate your target Python environment in Command Prompt.
  3. run LaunchVisualStudio.bat in the environment.
  4. on Visual Studio, build solution with Release|x64 configuration.
  5. copy softcam.pyd and softcam.dll to an appropriate directory.
  6. in python, you can import softcam!

I will write a detailed instruction like above as well as sample python code to use it.

kevinpl07 commented 1 year ago

Hi @tshino,

you're amazing. It works like a charm! Thank you so much for your help!

tshino commented 1 year ago

Thank you again for your post! That made this library more valuable.