Saturday, June 22, 2024

Program D-Link DCS-5010L - Part 2

OpenCV is mainly focusing on video. It does not support audio directly. To playback audio, would rely on ffmpeg or GStreamer. ffmpeg is mostly a standalone offline multimedia converting tools. OpenCV has GStreamer built-in support, but it is optional. I used pip installed opencv-python on Windows for Python 3.12, and GStreamer isn't enabled. May refer to github opencv-python and https://discuss.bluerobotics.com/t/opencv-python-with-gstreamer-backend/8842. Even install the full package with pip install opencv-contrib-python won't get GStream enabled CV2. Might need to build with source. Either way, would need to install GStreamer first, which is available here: https://gstreamer.freedesktop.org/download. To build GStreamer enabled opencv-python:

git clone --recursive https://github.com/opencv/opencv-python.git
cd .\opencv-python
set CMAKE_ARGS="-DWITH_GSTREAMER=ON"
or $env:CMAKE_ARGS="-DWITH_GSTREAMER=ON" for PowerShell
pip wheel . --verbose

The build gets setuptools 59.2.0, I'm using miniConda and I'm seeing "conda 24.1.2 requires setuptools>=60.0.0, but you have setuptools 59.2.0 which is incompatible". This is not my environment setuptools out-of-date issue, have tried: pip install setuptools --upgrade, python -m pip install pip --upgrade, conda update -n base setuptools, nothing works. And after this, seeing:

  Running command Getting requirements to build wheel
  Traceback (most recent call last):
    File "C:\ProgramData\miniconda3\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353, in <module>
      main()
...
    File "C:\Users\squ\AppData\Local\Temp\pip-build-env-lekizdpu\overlay\Lib\site-packages\pkg_resources\__init__.py", line 2172, in <module>
      register_finder(pkgutil.ImpImporter, find_on_path)
                      ^^^^^^^^^^^^^^^^^^^
  AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
  error: subprocess-exited-with-error

Sounds same as opencv-python issues 988, one comment there mentioned:

I encountered the same problem while running pip wheel . --verbose |& tee install_opencv.log. It worked after removing the ==59.2.0" from the "setuptools==59.2.0" https://github.com/opencv/opencv-python/blob/4.x/pyproject.toml#L16.

The pyproject.toml file is under the root, with the setuptools line removed, the original problem is resolved. Build under Windows would need Visual Studio Build Tools (refer to Revisit 坦克大战编程)., need to do the build under the Native build environment. The build will try Ninja, Visual Studio and NMake generator, and will try from v144 to v141. I have v143, and have "Developer Command Prompt for VS 2022" environment launched, all Ninja and NMake will fail as no support of platform, VS v143 progress a bit far,  but still get error as:

 -- Trying 'Visual Studio 17 2022 x64 v143' generator

  -- The C compiler identification is unknown
  CMake Error at CMakeLists.txt:3 (ENABLE_LANGUAGE):
    No CMAKE_C_COMPILER could be found
.

After some research, turns out Windows SDK is needed (I deselected it when installing Visual Studio Build Tools to save some space). It takes a while to build, depends on your computer. But it works. The opencv_python-*.whl file will be generated in the root folder. Just run pip to install it.

At beginning of the build, it will show the configuration, make sure Gstreamer is ON. It will stay OFF if GStreamer develop isn't installed. And with Gstreamer is enabled, I'm getting runtime error as:

ImportError: DLL load failed while importing cv2: The specified module could not be found

which is similar as opencv-python issues 856. Installed Microsoft Visual C++ Redistributable for Visual Studio 2022, but that didn't help. My Windows is not the 'N' one, so not a problem of Windows Media Feature Pack. I have tried the build whl before Gstreamer option enabled and not seeing problem, so I believe the problem is with GStreamer dll, but adding "C:\gstreamer\1.0\msvc_x86_64\lib\gstreamer-1.0" to path didn't help. Have to hook up Sysinternals' Process Monitor, and it shows the run is looking for a bunch of gstreamer's runtime dll which were not in site-packages\cv2 folder. Added "C:\gstreamer\1.0\msvc_x86_64\bin" to environment variable 'PATH' didn't help. Copy dlls over works, but not a clean way. Per https://stackoverflow.com/questions/214852/python-module-dlls, since version python 3.8 they added a mechanism to do this more securely. Read documentation on os.add_dll_directory https://docs.python.org/3/library/os.html#os.add_dll_directory. So doing this in code would work:

import os
gst_root = os.getenv('GSTREAMER_1_0_ROOT_MSVC_X86_64', 'C:/gstreamer/1.0/msvc_x86_64/')
os.add_dll_directory(gst_root+'bin')
import cv2

With Gstreamer enabled, I'm still not able to open the stream with log as:

[ WARN:0@2.620] global cap_gstreamer.cpp:2840 cv::handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module souphttpsrc0 reported: Could not establish connection to server.
[ WARN:0@2.623] global cap_gstreamer.cpp:1698 cv::GStreamerCapture::open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0@2.623] global cap_gstreamer.cpp:1173 cv::GStreamerCapture::isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:0@2.623] global cap.cpp:206 cv::VideoCapture::open VIDEOIO(GSTREAMER): backend is generally available but can't be used to capture by name

A bit frustrate on this. The http url is all right as I can open it without using GStreamer. Most example I can find are using rtsp protocol but not http. Will leave this aside until I can figure out how to use GStreamer alone to open the stream. I'm going to try OpenCV+ffmpeg next

0 Comments:

Post a Comment