【问题标题】:How to save video as frames in list and process the list of frames simultaneously?如何将视频保存为列表中的帧并同时处理帧列表?
【发布时间】:2021-10-01 19:43:07
【问题描述】:

我想从相机读取输入并将帧附加到列表中,显示列表中的帧。代码在附加到列表后需要花费大量时间来读取帧并显示它。

def test(source_file):
    ImagesSequence=[]
    i=0
    capture = VideoCapture(source_file)
    while(1):
        ret, frame = capture.read()
        while(True):
            imshow('Input', frame)

            ImagesSequence.append(frame)
            imshow('Output',ImagesSequence[i].astype(np.uint8))
            i=i+1
        if cv2.waitKey(60) & 0xFF == ord('q'):
            break
    return ImagesSequence

test(0)

【问题讨论】:

  • 那里有一个(实际的)无限循环。这是一个初学者的问题。你应该学习基本的编程教程。

标签: python python-3.x opencv cv2


【解决方案1】:
from cv2 import *
import numpy as np
ImagesSequence=[]
i=0
cap = cv2.VideoCapture(0)
while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # Display the resulting frame
    cv2.imshow('frame', frame)
    ImagesSequence.append(cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY))
    cv2.imshow('output', ImagesSequence[i].astype(np.uint8))
    i=i+1
    if cv2.waitKey(60) == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

【讨论】:

    【解决方案2】:

    正如 Christoph 指出的那样,您在程序中运行了一个实际的无限循环,删除它会修复您的程序。

    def test(source_file):
        ImagesSequence=[]
        i=0
        capture = cv2.VideoCapture(source_file)
    
        while True:
            ret, frame = capture.read()
            cv2.imshow('Input', frame)
            ImagesSequence.append(frame)
            cv2.imshow('Output',ImagesSequence[i].astype(np.uint8))
    
            i=i+1
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        return ImagesSequence
    
    test(0)
    

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      相关资源
      最近更新 更多