gpt4 book ai didi

python-3.x - 在OpenCV中播放电影

转载 作者:行者123 更新时间:2023-12-02 16:14:08 25 4
gpt4 key购买 nike

尝试播放电影时出现以下错误:

cv2.imshow("Video Output", frames)
TypeError: Expected Ptr<cv::UMat> for argument 'mat'
注释掉的行是我解决此问题的尝试,但是仍然出现错误。
我究竟做错了什么?
import cv2
import numpy as np

vid = cv2.VideoCapture("resources/Plaza.mp4")
while True:
frames = vid.read()
# frames = cv2.cvtColor(frames, cv2.COLOR_RGB2BGR)
# frames_arr = np.array(frames)
cv2.imshow("Video Output", frames)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

最佳答案

更好的实现是:


  • 检查视频是否使用vid.isOpened()打开

  • vid = cv2.VideoCapture("resources/Plaza.mp4")
    while vid.isOpened():


  • 如果成功返回框架,则显示它。

  • ret, frames = vid.read()

    if ret:
    # frames = cv2.cvtColor(frames, cv2.COLOR_RGB2BGR)
    # frames_arr = np.array(frames)
    cv2.imshow("Video Output", frames)
    if cv2.waitKey(1) & 0xFF == ord('q'):
    break


  • 确保始终关闭所有窗口并释放VideoCapture对象
  • cv2.destoyAllWindows()
    vid.release()


  • 码:
    import cv2
    import numpy as np

    vid = cv2.VideoCapture("resources/Plaza.mp4")
    while vid.isOpened():
    ret, frames = vid.read()

    if ret:
    # frames = cv2.cvtColor(frames, cv2.COLOR_RGB2BGR)
    # frames_arr = np.array(frames)
    cv2.imshow("Video Output", frames)
    if cv2.waitKey(1) & 0xFF == ord('q'):
    break
    else
    # continue with the next frame
    continue

    cv2.destoyAllWindows()
    vid.release()

    关于python-3.x - 在OpenCV中播放电影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64498035/

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