使用 python opencv 播放视频文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33900546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:10:04  来源:igfitidea点击:

playing video file using python opencv

pythonopencv

提问by Musa

I'm trying to play a video file using python opencv this is my code , but it is not showing the vidfeo file when I run the code

我正在尝试使用 python opencv 播放视频文件,这是我的代码,但是当我运行代码时它没有显示 vidfeo 文件

import numpy as np
import cv2

cap = capture =cv2.VideoCapture('C2.mp4')
while(cap.isOpened()):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    cv2.waitKey(1)

cap.release()
cv2.destroyAllWindows()

I tried the answer in : linkbut not working again

我尝试了以下答案:链接但无法再次工作

回答by aamer aamer

I think u just have to increase the number inside cv2.waitKey() function to may be 25 or 30. You should get the desired result.

我认为你只需要将 cv2.waitKey() 函数中的数字增加到 25 或 30。你应该得到想要的结果。

Also, there is no need to write cap = capture = cv2.......

还有,不用写cap = capture = cv2.......

Simply, writing,

简单来说,写作,

cap = cv2.videoCapture('path of the video')

cap = cv2.videoCapture('视频路径')

should work as well . Hope it works.

也应该工作。希望它有效。

回答by Suhas Sreenivas

This code worked for me. It shows both the original and the grayscale video output. Press 'q' to exit. I also didnt see the need for cap = capture... in your code.

这段代码对我有用。它显示了原始和灰度视频输出。按“q”退出。我也没有在您的代码中看到需要 cap = capture... 。

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',frame)
    cv2.imshow('grayF',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
         break

cap.release()
cv2.destroyAllWindows()

回答by stacksonstacks

import numpy as np
import cv2

cap = cv2.VideoCapture('C2.mp4')
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == True:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        cv2.imshow('frame', gray)
        # & 0xFF is required for a 64-bit system
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
cap.release()
cv2.destroyAllWindows()

回答by user7845696

ren opencv_ffmpeg.dllto opencv_ffmpeg2413.dllon your project dir if opencv-2.4.13.exe

ren opencv_ffmpeg.dllopencv_ffmpeg2413.dll你的项目目录,如果opencv-2.4.13.exe

回答by minakshi das

First of all, there is no need of capture as you are not using capture in your code. The reason why your video file is not showing because you haven't saved it in the same directory, where you have saved the code.

首先,不需要捕获,因为您没有在代码中使用捕获。您的视频文件未显示的原因是因为您没有将其保存在保存代码的同一目录中。

You can either give the path where your file is saved as shown below

您可以提供保存文件的路径,如下所示

cap = cv2.VideoCapture('(path to the video file)/cv2.mp4')

Again you need to change the argument inside waitKey otherwise the program will not close the window that shows the video correctly.

同样,您需要更改 waitKey 中的参数,否则程序将不会关闭正确显示视频的窗口。

Try out the following, it will definitely work. Put an if statement with waitKey() function and increase the argument which indicates the number of milliseconds it will wait for a key function to 25 or whatever number you may like so that when you press ESC key, the window will be destroyed:

尝试以下方法,它肯定会奏效。将 if 语句与 waitKey() 函数一起放置,并将指示它将等待键函数的毫秒数增加到 25 或您可能喜欢的任何数字的参数,以便当您按下 ESC 键时,窗口将被销毁:

if cv2.waitKey(25) & 0xFF == 27:
  break

回答by soha samy

The problem in my code was in the part (While). It should be while (True) instead the one in your code

我的代码中的问题出在 (While) 部分。它应该是 while (True) 而不是代码中的那个