如何在opencv中显式访问视频捕获的mjpeg后端 [英] How to explicitly access mjpeg backend for videocapture in opencv

查看:138
本文介绍了如何在opencv中显式访问视频捕获的mjpeg后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行以下操作时:

availableBackends = [cv2.videoio_registry.getBackendName(b) for b in v2.videoio_registry.getBackends()]
print(availableBackends)

我得到了 ['FFMPEG','GSTREAMER','INTEL_MFX','V4L2','CV_IMAGES','CV_MJPEG'] .

如果我现在尝试:

print(cv2.CAP_FFMPEG)
print(cv2.CAP_GSTREAMER)
print(cv2.CAP_INTEL_MFX)
print(cv2.CAP_V4L2)
print(cv2.CAP_IMAGES)
print(cv2.CAP_MJPEG)

除最后一个 AttributeError之外的所有作品:模块'cv2.cv2'没有属性'CAP_MJPEG'.

如何显式设置cv2.CAP_MJPEG后端(cv2.CAP_CV_MJPEG也无法使用)?

How can I explicitely set cv2.CAP_MJPEG backend (cv2.CAP_CV_MJPEG does also not work)?

推荐答案

您可以看到所有标志此处.

您正在寻找 cv2.CAP_OPENCV_MJPEG .

以下测试创建MJPEG合成AVI视频文件,并使用 cv2.CAP_OPENCV_MJPEG 后端读取视频:

The following test creates MJPEG synthetic AVI video file, and reads the video using cv2.CAP_OPENCV_MJPEG backend:

import numpy as np
import cv2

#availableBackends = [cv2.videoio_registry.getBackendName(b) for b in cv2.videoio_registry.getBackends()]
#print(availableBackends)

print('cv2.CAP_OPENCV_MJPEG = ' + str(cv2.CAP_OPENCV_MJPEG))


intput_filename = 'vid.avi'

# Generate synthetic video files to be used as input:
###############################################################################
width, height, n_frames = 640, 480, 100  # 100 frames, resolution 640x480

# Use motion JPEG codec (for testing)
synthetic_out = cv2.VideoWriter(intput_filename, cv2.VideoWriter_fourcc(*'MJPG'), 25, (width, height))

for i in range(n_frames):
    img = np.full((height, width, 3), 60, np.uint8)
    cv2.putText(img, str(i+1), (width//2-100*len(str(i+1)), height//2+100), cv2.FONT_HERSHEY_DUPLEX, 10, (30, 255, 30), 20)  # Green number
    synthetic_out.write(img)

synthetic_out.release()
###############################################################################


# Read the video using CV_MJPEG backend
###############################################################################
cap = cv2.VideoCapture(intput_filename, cv2.CAP_OPENCV_MJPEG)

# Keep iterating break
while True:
    ret, frame = cap.read()  # Read frame from first video

    if ret:
        cv2.imshow('frame', frame)  # Display frame for testing
        cv2.waitKey(100) #Wait 100msec (for debugging)
    else:
        break

cap.release() #Release must be inside the outer loop
###############################################################################

这篇关于如何在opencv中显式访问视频捕获的mjpeg后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆