gpt4 book ai didi

python - 如何使用 FFMPEG 驱动程序使 opencv 工作

转载 作者:行者123 更新时间:2023-12-04 22:45:50 25 4
gpt4 key购买 nike

我的 linuxbox 上有一个摄像头,它运行良好:

# $ ls -al /dev/video*
# crw-rw----+ 1 root video 81, 0 janv. 8 16:13 /dev/video0
# crw-rw----+ 1 root video 81, 1 janv. 8 16:13 /dev/video1
# $ groups
# adm cdrom sudo dip video plugdev lpadmin lxd sambashare docker libvirt

来自带有 cv2 的 python,它与默认驱动程序 CAP_V4L2

配合良好
>>> from pathlib import Path
>>> import cv2
>>> print(cv2.VideoCapture(0, apiPreference=cv2.cv2.CAP_V4L2).isOpened())
True
>>>

我想用 FFMPEG 驱动访问它(没有成功):

>>> print(cv2.VideoCapture(0, apiPreference=cv2.CAP_FFMPEG).isOpened())
False
>>>

从 Python 的角度来看,opencv 看起来像是有 FFMPEG 驱动程序:

  >>> cv2.__version__
'4.4.0'
>>> info = cv2.getBuildInformation()
>>> video, parallel = info.index('Video'), info.index('Parallel')
>>> print(info[video:parallel])
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)
>>>

从 Linux 方面看也不错:

$ dpkg -l |grep -i opencv
ii libopencv-core4.2:amd64 4.2.0+dfsg-5 amd64 computer vision core library
ii libopencv-imgcodecs4.2:amd64 4.2.0+dfsg-5 amd64 computer vision Image Codecs library
ii libopencv-imgproc4.2:amd64 4.2.0+dfsg-5 amd64 computer vision Image Processing library
ii libopencv-videoio4.2:amd64 4.2.0+dfsg-5 amd64 computer vision Video I/O library

$ dpkg -l |grep -i ffm
ii ffmpeg 7:4.2.4-1ubuntu0.1 amd64 Tools for transcoding, streaming and playing of multimedia files
ii gstreamer1.0-libav:amd64 1.16.2-2 amd64 ffmpeg plugin for GStreamer
ii libavcodec-extra:amd64 7:4.2.4-1ubuntu0.1 amd64 FFmpeg library with extra codecs (metapackage)
ii libavcodec-extra58:amd64 7:4.2.4-1ubuntu0.1 amd64 FFmpeg library with additional de/encoders for audio/video codecs
ii libavdevice58:amd64 7:4.2.4-1ubuntu0.1 amd64 FFmpeg library for handling input and output devices - runtime files
ii libavfilter7:amd64 7:4.2.4-1ubuntu0.1 amd64 FFmpeg library containing media filters - runtime files
ii libavformat58:amd64 7:4.2.4-1ubuntu0.1 amd64 FFmpeg library with (de)muxers for multimedia containers - runtime files
ii libavresample4:amd64 7:4.2.4-1ubuntu0.1 amd64 FFmpeg compatibility library for resampling - runtime files
ii libavutil56:amd64 7:4.2.4-1ubuntu0.1 amd64 FFmpeg library with functions for simplifying programming - runtime files
ii libffmpegthumbnailer4v5 2.1.1-0.2build2 amd64 shared library for ffmpegthumbnailer
ii libpostproc55:amd64 7:4.2.4-1ubuntu0.1 amd64 FFmpeg library for post processing - runtime files
ii libswresample3:amd64 7:4.2.4-1ubuntu0.1 amd64 FFmpeg library for audio resampling, rematrixing etc. - runtime files
ii libswscale5:amd64 7:4.2.4-1ubuntu0.1 amd64 FFmpeg library for image scaling and various conversions - runtime files
$

最佳答案

OpenCV 是否使用 FFMPEG 从相机捕获?我认为它适用于文件(或流),因为 VideoCapture 既用于相机又用于文件/流。

在我的 Windows 设置中,FFMPEG 用作从相机捕获的接口(interface)也不起作用(但例如 CAP_DSHOW 起作用),而作为文件的编解码器,它读取一个帧:

cap = cv2.VideoCapture("..\\ooo.mp4", apiPreference=cv2.CAP_FFMPEG) # OK


>>> print(cv2.VideoCapture(0, apiPreference=cv2.CAP_FFMPEG).isOpened())
False
>>> print(cv2.VideoCapture(0, apiPreference=cv2.CAP_DSHOW).isOpened())
True


import cv2
# cap = cv2.VideoCapture(0, apiPreference=cv2.CAP_FFMPEG) # prints "ERROR"
cap = cv2.VideoCapture(0, apiPreference=cv2.CAP_DSHOW) # OK, shows an image
ret, fr = cap.read()
if ret: cv2.imshow("FR", fr)
else: print("ERROR")
cv2.waitKey(0)

另请参阅:https://trac.ffmpeg.org/wiki/Capture/Webcam

"Linux

Uses the video4linux2 (or simply v4l2) input device to capture liveinput such as from a webcam. See the v4l2 input device documentationfor more information. "

对于 Windows,FFMPEG 在我的测试中提到使用 DirectShow:

>Windows
>dshow
>
>Uses the dshow (DirectShow) input device which is the preferred option for
>Windows users.

关于python - 如何使用 FFMPEG 驱动程序使 opencv 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65641483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com