opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
78.94k stars 55.8k forks source link

Compile opencv with static vcpkg ffmpeg (include x264,x265) on windows with python binding. #19973

Closed talregev closed 3 years ago

talregev commented 3 years ago

I am trying to compile opencv on windows 10, with vcpkg ffmpeg: vcpkg install ffmpeg[x264,x265]:x64-windows-static I using these flags: cmake .. -DOPENCV_FFMPEG_USE_FIND_PACKAGE=FFMPEG -DOPENCV_FFMPEG_SKIP_DOWNLOAD=on -DOPENCV_FFMPEG_SKIP_BUILD_CHECK=on -DCMAKE_PREFIX_PATH=D:/src/vcpkg/installed/x64-windows-static -DCMAKE_MODULE_PATH=D:/src/vcpkg/installed/x64-windows-static/share/ffmpeg -DBUILD_SHARED_LIBS=OFF -DWITH_OPENMP=on

it compile opencv with python binding, but I cannot use the x264 codec on writer. Can you help?

talregev commented 3 years ago

When I compile I get this warning, I think it connected to my problem:

LINK : warning LNK4217: symbol 'x264_levels' defined in 'libx264.lib(tables.o)' is imported by 'avcodec.lib(libx264.o)' in function 'X264_init' [D:\src\ope
ncv-4.5.2\build\apps\interactive-calibration\opencv_interactive-calibration.vcxproj]
LINK : warning LNK4217: symbol 'x264_levels' defined in 'libx264.lib(tables.o)' is imported by 'avcodec.lib(libx264.o)' in function 'X264_init' [D:\src\ope
ncv-4.5.2\build\modules\python3\opencv_python3.vcxproj]

https://docs.microsoft.com/en-us/cpp/error-messages/tool-errors/linker-tools-warning-lnk4217?view=msvc-160

alalek commented 3 years ago

Are you able to compile any other project with vcpkg's ffmpeg artifacts using find_package(FFMPEG)?

Currently this looks like a configuration problem of external dependencies and this can't be fixed in OpenCV (should not, at least).

OPENCV_FFMPEG_SKIP_BUILD_CHECK=on

Why are you disable this check? You wont to see / investigate issue on a simple check, but you trying to investigate problem on whole OpenCV.


BTW, OpenCV is able to build with properly configured external FFmpeg artifacts, at least in form of shared DLLs.


Build / usage questions should go to Users OpenCV Q/A forum: https://forum.opencv.org/ This tracker is for issues and bugs that needs fix in OpenCV.

talregev commented 3 years ago

Are you able to compile any other project with vcpkg's ffmpeg artifacts using find_package(FFMPEG)?

I didn't try. my only interest is compile ffmpeg with opencv.

Why are you disable this check?

I can remove this option OPENCV_FFMPEG_SKIP_BUILD_CHECK=on and see what happen.

BTW, OpenCV is able to build with properly configured external FFmpeg artifacts, at least in form of shared DLLs.

I try share dll, but it not working with binding python. it looking the dll, even I put them on the path. also you ship your python-opencv as static.

Build / usage questions should go to Users OpenCV Q/A forum: https://forum.opencv.org/

You have a direct link how to compile ffmpeg with x264 on windows with python binding? because I didn't find any guide.

talregev commented 3 years ago

When I running the python binding I get this error:

[ERROR:0] global D:\src\opencv-4.5.2\modules\videoio\src\cap.cpp (162) cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.5.2) D:\src\opencv-4.5.2\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): D:\Android\android-sdk-windows\extras\android\support\samples\Support4Demos\res\raw in function 'cv::icvExtractPattern'
talregev commented 3 years ago

without OPENCV_FFMPEG_SKIP_BUILD_CHECK=on it still compile as before. this option check if ffmped can be compile. ffmpeg can compile but I cannot able run and use it. I didn't try to use it from cpp. maybe from cpp is working. Can you try to do it with vcpkg? vcpkg is opensource, if you can see the problem on them, you or me can open an issue or PR to fix it. it will be really wonderful.

alalek commented 3 years ago

BTW, vcpkg has opencv package (AFAIK, use opencv4), including ffmpeg "feature".

talregev commented 3 years ago

BTW, vcpkg has opencv package (AFAIK, use opencv4), including ffmpeg "feature".

but it cannot compile with python binding. I ask for python binding, but it not easy to add it as a feature. You also require that python will come with numpy. Can it done only with download the include for numpy, then it will added later when installing the package?

talregev commented 3 years ago

I also ask you for the binaries itself. if you can release a binaries as a gpl that I can run it: https://github.com/opencv/opencv-python/issues/469

alalek commented 3 years ago

There is configuration setup for mentioned builder above (not vcpkg-based and not verified with static builds): https://gist.github.com/alalek/3eab870371e96483c078e32b3de47daf


Can it done only with download the include for numpy, then it will added later when installing the package?

You can use virtualenv for build. There is no problem to install numpy in general, so other configurations are not tested.

talregev commented 3 years ago

There is configuration setup for mentioned builder above (not vcpkg-based and not verified with static builds): https://gist.github.com/alalek/3eab870371e96483c078e32b3de47daf

That nice. How I tell it where to find the ffmpeg? -DCMAKE_PREFIX_PATH=D:/src/vcpkg/installed/x64-windows-static?

You can use virtualenv for build. There is no problem to install numpy in general, so other configurations are not tested.

It not the problem to add the numpy later. but as way vcpkg work, you cannot add while compile. If I want to add a python feature to opencv vcpkg, it first compile python3, then it compile opencv. (also need to adjust how it will find the python inside vcpkg). and you cannot download numpy, everything in vcpkg compile from source code.

Can you at least support ffmpeg from vcpkg? with python binding from opencv compilation process. (not compile opencv vcpkg). Can you try it yourself? maybe the problem is inside vcpkg, and we can fix it.

talregev commented 3 years ago

Also I am using FindFFMPEG.cmake from vcpkg:

FindFFMPEG.zip

talregev commented 3 years ago

From the link you send me:

# Python 3.8+ ignores system PATH and requires to use os.add_dll_directory() instead

Maybe that was missing for me when I try to compile ffmepg vcpkg as a share? Can you add it to the python loader? check if it python 3.8+ and windows and add this line automatic?

talregev commented 3 years ago

I was able compile opencv with ffmpeg according to the instruction from your link. I also added the bin of ffmpeg with os dll manually: os.add_dll_directory(r"D:\src\ffmpeg-4.4-full_build-shared\bin") When I run this program:

import cv2

# Press the green button in the gutter to run the script.
if __name__ == '__main__':

    path = r'D:\Android\android-sdk-windows\extras\android\support\samples\Support4Demos\res\raw'
    cap = cv2.VideoCapture(path)
    # fourcc = cv2.VideoWriter_fourcc(*'XVID')
    # out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

    while(cap.isOpened()):
        ret, frame = cap.read()
        cv2.imshow('frame', frame)
        # out.write(frame)

        if cv2.waitKey(10) == ord('q'):
            break

i get the same error:

C:\Users\talre\Desktop\slitled\imageprocessing\opencv>python main.py
OpenCV loader: os.name="nt"  platform.system()="Windows"
OpenCV loader: loading config: C:\Users\talre\AppData\Local\Programs\Python\Python39\lib\site-packages\cv2\config.py
OpenCV loader: loading config: C:\Users\talre\AppData\Local\Programs\Python\Python39\lib\site-packages\cv2\config-3.9.py
OpenCV loader: PYTHON_EXTENSIONS_PATHS=['C:/Users/talre/AppData/Local/Programs/Python/Python39/Lib/site-packages/cv2\\python-3.9']
OpenCV loader: BINARIES_PATHS=['D:/src/opencv-4.5.2/tal/install\\x64/vc16/bin']
OpenCV loader: PATH=D:/src/opencv-4.5.2/tal/install\x64/vc16/bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;D:\Users\Tal\Downloads\phones\Heimdall Suite;D:\Android\android-sdk-windows\platform-tools;D:\mingw-w64\mingw64\bin;C:\Program Files (x86)\Windows Kits\10\bin\10.0.17134.0\x64;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\ProgramData\chocolatey\bin;C:\OpenSSL-Win64\bin\;C:\Program Files\CMake\bin;C:\ProgramData\chocolatey\lib\cunit\lib;C:\opencv\x64\vc16\bin;C:\Program Files\Java\jdk-11.0.6\bin;C:\dev\ros2-windows\bin;D:\msys64\usr\bin;C:\Program Files\nodejs\;C:\Program Files\Java\jdk-14.0.1\bin;C:\Program Files\Git\cmd;C:\Users\talre\AppData\Local\Programs\Python\Python39;C:\Users\talre\AppData\Local\Programs\Python\Python39\Scripts;D:\src\ffmpeg-4.4-full_build-shared\bin;C:\Ruby27-x64\bin;C:\Users\talre\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Mercurial\;D:\Android\flutter\bin;d:\Microsoft VS Code\bin;C:\Users\talre\AppData\Roaming\npm;C:\Users\talre\AppData\Local\Microsoft\WindowsApps
OpenCV loader: replacing cv2 module
OpenCV loader: DONE
[ERROR:0] global D:\src\opencv-4.5.2\modules\videoio\src\cap.cpp (162) cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.5.2) D:\src\opencv-4.5.2\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): D:\Android\android-sdk-windows\extras\android\support\samples\Support4Demos\res\raw in function 'cv::icvExtractPattern'

Can you verify this?

talregev commented 3 years ago

I understand my problem. I forget to add a file. I just added a path. So maybe everything is working. I will check and get back to you.

talregev commented 3 years ago

So everything is working. static compile like I show in the first msg. also your way of ffmpeg. @alalek Will you consider ship a python with ffmpeg as gpl binaries?

talregev commented 3 years ago

@alalek I notice, when I compile ffmpeg with share library, the python binding don't add the bin folder of ffmpeg to binary var. Can we add it automatic? https://github.com/opencv/opencv/blob/master/modules/python/package/template/config.py.in#L4 CMAKE_PYTHON_BINARIES_PATH

alalek commented 3 years ago

Can we add it automatic?

Probably, No (as OpenCV doesn't control installation of FFmpeg).