opencv VideoCapture() 无法通过 python 打开在 C++ 上打开
opencv VideoCapture() couldn't open on c++ by opened by python
美好的一天,
c++
我从柯南安装了 opencv/4.5.2
(通过 cmake macro) and try a simple piece of code:
cv::VideoCapture cap(0, cv::CAP_MSMF);
if (!cap.isOpened()) {
cerr << "couldn't open\n";
return -1; // <<-- stop here
}
// ...
# ...
conan_cmake_run(
REQUIRES
${CONAN_EXTRA_REQUIRES}
opencv/4.5.2
OPTIONS
${CONAN_EXTRA_OPTIONS}
BASIC_SETUP
CMAKE_TARGETS # individual targets to link to
BUILD
missing)
调试和 release 生成可执行文件的结果相同。
硬件
$ls -ltrh /dev/video*
crw-rw----+ 1 root video 81, 1 июн 27 22:26 /dev/video1
crw-rw----+ 1 root video 81, 0 июн 27 22:26 /dev/video0
python
python 上的相同代码工作正常:
import cv2
c = cv2.VideoCapture(0, 0)
while(True):
r, frame = c.read()
cv2.imshow('p', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
c.release()
cv2.destroyAllWindows()
修复静态 libjpeg-turbo
依赖性
为了满足conan
而自动生成cmake文件
你应该构建静态 libjpeg-turbo
库。
git clone https://github.com/libjpeg-turbo/libjpeg-turbo.git
cd libjpeg-turbo
cmake -B build -DCMAKE_BUILD_TYPE=Release .
sudo cmake --install build --prefix /usr/
OS
$uname -a
Linux omen 5.10.41-1-MANJARO #1 SMP PREEMPT Fri May 28 19:10:32 UTC 2021 x86_64 GNU/Linux
PS
将用户添加到 video
组或 运行 ./main
作为 root 没有任何好处。
UPD1
cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_VERBOSE);
std::cout << cv::getBuildInformation();
return 0;
发布|调试构建 Video I/O
部分的两个日志都是空的。但是 python 有以下部分:
Video I/O:
DC1394: NO
FFMPEG: YES
avcodec: YES (58.109.100)
avformat: YES (58.61.100)
avutil: YES (56.60.100)
swscale: YES (5.8.100)
avresample: NO
GStreamer: NO
v4l/v4l2: YES (linux/videodev2.h)
UPD2
这是在 VideoCapture cap(0, cv::CAP_ANY)
之后记录
[DEBUG:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/videoio_registry.cpp (191) VideoBackendRegistry VIDEOIO: Builtin backends(7): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); CV_IMAGES(960); CV_MJPEG(950); UEYE(940)
[DEBUG:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/videoio_registry.cpp (215) VideoBackendRegistry VIDEOIO: Available backends(7): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); CV_IMAGES(960); CV_MJPEG(950); UEYE(940)
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/videoio_registry.cpp (217) VideoBackendRegistry VIDEOIO: Enabled backends(7, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); CV_IMAGES(960); CV_MJPEG(950); UEYE(940)
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (324) getPluginCandidates VideoIO plugin (GSTREAMER): glob is 'libopencv_videoio_gstreamer*.so', 1 location(s)
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (331) getPluginCandidates - ./cmake-build-debug/bin: 0
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (335) getPluginCandidates Found 0 plugin(s) for GSTREAMER
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (324) getPluginCandidates VideoIO plugin (MSMF): glob is 'libopencv_videoio_msmf*.so', 1 location(s)
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (331) getPluginCandidates - ./cmake-build-debug/bin: 0
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (335) getPluginCandidates Found 0 plugin(s) for MSMF
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (324) getPluginCandidates VideoIO plugin (UEYE): glob is 'libopencv_videoio_ueye*.so', 1 location(s)
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (331) getPluginCandidates - ./cmake-build-debug/bin: 0
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (335) getPluginCandidates Found 0 plugin(s) for UEYE
正如我在使用 conan
脚本构建 opencv
之前所写:
macro(run_conan)
# Download automatically, you can also just copy the conan.cmake file
if (NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://github.com/conan-io/cmake-conan/raw/v0.15/conan.cmake" "${CMAKE_BINARY_DIR}/conan.cmake")
endif ()
include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_add_remote(
NAME
bincrafters
URL
https://api.bintray.com/conan/bincrafters/public-conan)
conan_cmake_run(
REQUIRES
${CONAN_EXTRA_REQUIRES}
opencv/4.5.2
fmt/8.0.0
OPTIONS
${CONAN_EXTRA_OPTIONS}
BASIC_SETUP
CMAKE_TARGETS # individual targets to link to
BUILD
missing)
endmacro()
在 Linux 中(在我的例子中是 manjaro)你应该安装 pacman -S v4l-utils
并在 CMakeLists.txt
指令的某处添加:set(CONAN_EXTRA_OPTIONS ${CONAN_EXTRA_OPTIONS} opencv:with_v4l=True)
.
在 cv::getBuildInformation()
部分之后,我有以下内容:
Video I/O:
v4l/v4l2: YES (linux/videodev2.h)
瞧!
美好的一天,
c++
我从柯南安装了 opencv/4.5.2
(通过 cmake macro) and try a simple piece of code:
cv::VideoCapture cap(0, cv::CAP_MSMF);
if (!cap.isOpened()) {
cerr << "couldn't open\n";
return -1; // <<-- stop here
}
// ...
# ...
conan_cmake_run(
REQUIRES
${CONAN_EXTRA_REQUIRES}
opencv/4.5.2
OPTIONS
${CONAN_EXTRA_OPTIONS}
BASIC_SETUP
CMAKE_TARGETS # individual targets to link to
BUILD
missing)
调试和 release 生成可执行文件的结果相同。
硬件
$ls -ltrh /dev/video*
crw-rw----+ 1 root video 81, 1 июн 27 22:26 /dev/video1
crw-rw----+ 1 root video 81, 0 июн 27 22:26 /dev/video0
python
python 上的相同代码工作正常:
import cv2
c = cv2.VideoCapture(0, 0)
while(True):
r, frame = c.read()
cv2.imshow('p', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
c.release()
cv2.destroyAllWindows()
修复静态 libjpeg-turbo
依赖性
为了满足conan
而自动生成cmake文件
你应该构建静态 libjpeg-turbo
库。
git clone https://github.com/libjpeg-turbo/libjpeg-turbo.git
cd libjpeg-turbo
cmake -B build -DCMAKE_BUILD_TYPE=Release .
sudo cmake --install build --prefix /usr/
OS
$uname -a
Linux omen 5.10.41-1-MANJARO #1 SMP PREEMPT Fri May 28 19:10:32 UTC 2021 x86_64 GNU/Linux
PS
将用户添加到 video
组或 运行 ./main
作为 root 没有任何好处。
UPD1
cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_VERBOSE);
std::cout << cv::getBuildInformation();
return 0;
发布|调试构建 Video I/O
部分的两个日志都是空的。但是 python 有以下部分:
Video I/O:
DC1394: NO
FFMPEG: YES
avcodec: YES (58.109.100)
avformat: YES (58.61.100)
avutil: YES (56.60.100)
swscale: YES (5.8.100)
avresample: NO
GStreamer: NO
v4l/v4l2: YES (linux/videodev2.h)
UPD2
这是在 VideoCapture cap(0, cv::CAP_ANY)
[DEBUG:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/videoio_registry.cpp (191) VideoBackendRegistry VIDEOIO: Builtin backends(7): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); CV_IMAGES(960); CV_MJPEG(950); UEYE(940)
[DEBUG:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/videoio_registry.cpp (215) VideoBackendRegistry VIDEOIO: Available backends(7): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); CV_IMAGES(960); CV_MJPEG(950); UEYE(940)
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/videoio_registry.cpp (217) VideoBackendRegistry VIDEOIO: Enabled backends(7, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); CV_IMAGES(960); CV_MJPEG(950); UEYE(940)
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (324) getPluginCandidates VideoIO plugin (GSTREAMER): glob is 'libopencv_videoio_gstreamer*.so', 1 location(s)
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (331) getPluginCandidates - ./cmake-build-debug/bin: 0
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (335) getPluginCandidates Found 0 plugin(s) for GSTREAMER
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (324) getPluginCandidates VideoIO plugin (MSMF): glob is 'libopencv_videoio_msmf*.so', 1 location(s)
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (331) getPluginCandidates - ./cmake-build-debug/bin: 0
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (335) getPluginCandidates Found 0 plugin(s) for MSMF
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (324) getPluginCandidates VideoIO plugin (UEYE): glob is 'libopencv_videoio_ueye*.so', 1 location(s)
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (331) getPluginCandidates - ./cmake-build-debug/bin: 0
[ INFO:0] global /home/user/.conan/data/opencv/4.5.2/_/_/build/82e231b59ebe3f3bd9c050e2b1b60042669442a6/source_subfolder/modules/videoio/src/backend_plugin.cpp (335) getPluginCandidates Found 0 plugin(s) for UEYE
正如我在使用 conan
脚本构建 opencv
之前所写:
macro(run_conan)
# Download automatically, you can also just copy the conan.cmake file
if (NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://github.com/conan-io/cmake-conan/raw/v0.15/conan.cmake" "${CMAKE_BINARY_DIR}/conan.cmake")
endif ()
include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_add_remote(
NAME
bincrafters
URL
https://api.bintray.com/conan/bincrafters/public-conan)
conan_cmake_run(
REQUIRES
${CONAN_EXTRA_REQUIRES}
opencv/4.5.2
fmt/8.0.0
OPTIONS
${CONAN_EXTRA_OPTIONS}
BASIC_SETUP
CMAKE_TARGETS # individual targets to link to
BUILD
missing)
endmacro()
在 Linux 中(在我的例子中是 manjaro)你应该安装 pacman -S v4l-utils
并在 CMakeLists.txt
指令的某处添加:set(CONAN_EXTRA_OPTIONS ${CONAN_EXTRA_OPTIONS} opencv:with_v4l=True)
.
在 cv::getBuildInformation()
部分之后,我有以下内容:
Video I/O:
v4l/v4l2: YES (linux/videodev2.h)
瞧!