gpt4 book ai didi

python-3.x - 结合 OpenCV 和 FFMPEG 以实现更快的视频处理

转载 作者:行者123 更新时间:2023-12-02 17:27:11 29 4
gpt4 key购买 nike

我正在尝试处理视频。为了让它更快,我想用 ffmpeg 阅读它,然后将帧发送到 cv2 进行处理。

这就是我所做的:

import cv2
import subprocess as sp
import numpy as np

input_file = 'testvideo.mp4'


cap = cv2.VideoCapture(input_file)
ret, frame = cap.read()
height, width, ch = frame.shape

ffmpeg = "C:\\Users\\totyped\\Downloads\\ffmpeg\\bin\\ffmpeg.exe"
dimension = '{}x{}'.format(width, height)
f_format = 'bgr24' # remember OpenCV uses bgr format
fps = str(cap.get(cv2.CAP_PROP_FPS))

command = [ffmpeg,
'-i', input_file,
'-r', fps, # FPS
'-pix_fmt', 'bgr24', # opencv requires bgr24 pixel format.
'-vcodec', 'mp4',
'-an','-sn', # disable audio processing
'-f', 'image2pipe', '-']

pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=10)

while True:
frame = pipe.stdout.read()
image = np.frombuffer(frame, dtype='uint8') # convert read bytes to np


cv2.imshow('Video', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
proc.stdin.close()
proc.stderr.close()
proc.wait()

我不断收到同样的错误:

error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:350: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'



这是因为我检索视频的方式有问题。

代码的部分灵感来自:
https://www.reddit.com/r/linuxquestions/comments/b4jxdb/how_could_i_interface_ffmpeg_with_opencv_in/

编辑:
import cv2
import subprocess as sp
import numpy as np

input_file = 'testvideo.mp4'


cap = cv2.VideoCapture(input_file)
ret, frame = cap.read()
height, width, ch = frame.shape

ffmpeg = "C:\\Users\\totyped\\Downloads\\ffmpeg\\bin\\ffmpeg.exe"
dimension = '{}x{}'.format(width, height)
f_format = 'bgr24' # remember OpenCV uses bgr format
fps = str(cap.get(cv2.CAP_PROP_FPS))

command = [ffmpeg,
'-i', input_file,
'-r', fps, # FPS
'-pix_fmt', 'bgr24', # opencv requires bgr24 pixel format.
'-vcodec', 'mp4',
'-an','-sn', # disable audio processing
'-f', 'image2pipe', '-']

pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=64000000)

while True:
frame = pipe.stdout.read(height*width*3)
print(frame)
image = np.frombuffer(frame, dtype='uint8') # convert read bytes to np


cv2.imshow('Video', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

最佳答案

尝试这样的事情 - 虽然我认为你可能会通过使用 ffmpeg 来找错树。因为这就是 OpenCV 在幕后使用的全部内容。如果您想要更高的性能,也许更多地关注多处理 - 但这取决于您正在执行的处理类型,您尚未讨论或显示:

#!/usr/bin/env python3
import numpy as np
import cv2
import subprocess as sp
import numpy as np

ffmpeg = "ffmpeg"
input_file='test.mp4'
height, width = 480, 640
command = [ffmpeg,
'-i', input_file,
'-pix_fmt', 'bgr24',
'-codec', 'rawvideo',
'-an',
'-sn',
'-f', 'image2pipe', '-']

pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=65536)

frameCount=0
while True:
nbytes = height*width*3
print(f"Frame: {frameCount} - Trying to read {nbytes} bytes")
frame = pipe.stdout.read(nbytes)
bytesRead = len(frame)
print(f"Read {bytesRead} bytes")
if bytesRead==0:
break
image = np.frombuffer(frame, dtype='uint8') # convert read bytes to np
image = image.reshape((480,640,3))

cv2.imshow('Video', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
frameCount += 1

关于python-3.x - 结合 OpenCV 和 FFMPEG 以实现更快的视频处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58474227/

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