삽질 기록

Linux 환경에서 OpenCV의 VideoCapture 사용 시 Webcam이 죽어도 읽히지 않을 때

꾸준희 2021. 6. 25. 11:23
728x90
반응형

 

아래와 같은 식으로 Linux 환경에서 OpenCV(C++)의 VideoCapture 클래스를 사용하여 Webcam을 사용하고자 할 때 웹캠이 read 되지 않는 문제가 있다.

 

VideoCapture cap;
// open the default camera using default API
cap.open(0);

 

 

 

그럴 땐 먼저 웹캠 USB가 제대로 꽂혀있는지 확인한 뒤, 디바이스가 제대로 연결 되었는지 아래와 같이 확인해보고, ffplay를 사용하여 웹캠이 정상 동작하는지 확인해본다. 

 

그 전에 연결되어있는 카메라의 device 명을 아래와 같이 확인해야한다. 

$ v4l2-ctl --list-devices

 

ffplay를 사용하여 연결되어있는 Webcam 을 테스트 하는 명령어는 아래와 같다. 

$ ffplay /dev/video0

 

 

 

이렇게 했는데 제대로 동작하되, VideoCapture 가 먹히지 않는다면 OpenCV를 Cmake 빌드 시 잘못 빌드했을 확률이 높다. 잘못 빌드했다는 것의 의미는 리눅스 환경에서 웹캠(video)을 지원하는 Video4Linux 를 포함하지 않고 빌드하여 사용한 것이다. 자세한 사항은 아래 링크(opencv cmake tutorial)를 참고하자. 

 

 

https://docs.opencv.org/4.5.2/db/d05/tutorial_config_reference.html

 

OpenCV: OpenCV configuration options reference

Prev Tutorial: OpenCV installation overview Next Tutorial: Installation in Linux Introduction NoteWe assume you have read OpenCV installation overview tutorial or have experience with CMake. Configuration options can be set in several different ways: Comma

docs.opencv.org

 

 

 

문제의 플래그는 아래와 같다. 

 

WITH_V4L (Linux; default: ON )

Capture images from camera using Video4Linux API. Linux kernel headers must be installed.

 

아래 플래그 두개를 포함하여 cmake build 해준다. 

 

-D WITH_V4L=ON 
-D WITH_LIBV4L=ON 

 

명령어는 아래와 같다. 

 

cmake 
-DCMAKE_BUILD_TYPE=Release 
-D WITH_GSTREAMER=OFF 
-D WITH_FFMPEG=ON 
-D WITH_TBB=ON 
-D WITH_GTK=ON 
-D WITH_QT=OFF 
-D WITH_CUDA=OFF 
-D WITH_OPENGL=OFF 
-D WITH_OPENCL=OFF 
-D BUILD_opencv_world=ON 
-D BUILD_TESTS=OFF 
-D BUILD_EXAMPLES=OFF 
-D BUILD_PERF_TESTS=OFF 
-D WITH_V4L=ON 
-D WITH_LIBV4L=ON 
-DCMAKE_INSTALL_PREFIX=/path ..

 

그러면 아래와 같이 빌드가 된다. 

-- General configuration for OpenCV 3.4.0 =====================================
--   Version control:               unknown
-- 
--   Platform:
--     Timestamp:                   2021-06-25T01:11:28Z
--     Host:                        Linux 5.4.0-71-generic x86_64
--     CMake:                       3.10.2
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               Release
-- 
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3
--       requested:                 SSE3
--     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2
--       SSE4_1 (3 files):          + SSSE3 SSE4_1
--       SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (9 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++11:                       YES
--     C++ Compiler:                /usr/bin/c++  (ver 7.5.0)
--     C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):
--     Linker flags (Debug):
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          dl m pthread rt /usr/lib/x86_64-linux-gnu/libtbb.so
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 calib3d core dnn features2d flann highgui imgcodecs imgproc ml objdetect photo python_bindings_generator shape stitching superres video videoio videostab world
--     Disabled:                    js
--     Disabled by dependency:      -
--     Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 python3 ts viz
--     Applications:                apps
--     Documentation:               NO
--     Non-free algorithms:         NO
-- 
--   GUI: 
--     GTK+:                        YES (ver 2.24.32)
--       GThread :                  YES (ver 2.56.4)
--       GtkGlExt:                  NO
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
--     WEBP:                        build (ver encoder: 0x020e)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
--     JPEG 2000:                   build (ver 1.900.1)
--     OpenEXR:                     build (ver 1.7.1)
-- 
--   Video I/O:
--     DC1394:                      NO
--     FFMPEG:                      YES
--       avcodec:                   YES (ver 57.107.100)
--       avformat:                  YES (ver 57.83.100)
--       avutil:                    YES (ver 55.78.100)
--       swscale:                   YES (ver 4.8.100)
--       avresample:                YES (ver 3.7.0)
--     libv4l/libv4l2:              1.14.2 / 1.14.2
--     v4l/v4l2:                    linux/videodev2.h
--     gPhoto2:                     NO
-- 
--   Parallel framework:            TBB (ver 2017.0 interface 9107)
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Intel IPP:                   2017.0.3 [2017.0.3]
--            at:                   /path ~~
--     Intel IPP IW:                sources (2017.0.3)
--               at:                /path ~~
--     Lapack:                      NO
--     Eigen:                       NO
--     Custom HAL:                  NO
-- 
--   Python (for build):            /usr/bin/python2.7
-- 
--   Java:
--     ant:                         NO
--     JNI:                         /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Matlab:                        NO
-- 
--   Install to:                    /path ~~ /opencv-3.4.0-ffmpeg-gtk
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /path ~~ /opencv-3.4.0/build

 

 

그러면 아래와 같이 Video I/O 에 대한 정보를 볼 수 있다. 

 

 

 

 

그 다음 make install 해주면 Video (Webcam)가 정상적으로 동작하는 OpenCV 라이브러리를 사용 할 수 있다. 

 

728x90
반응형
1 2 3 4 5 6 7 8 9 ··· 46