简体   繁体   English

使用 python opencv 播放视频文件

[英]playing video file using python opencv

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 : link but not working again我尝试了以下答案: 链接但无法再次工作

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.希望它有效。

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()

This code worked for me.这段代码对我有用。 It shows both the original and the grayscale video output.它显示了原始和灰度视频输出。 Press 'q' to exit.按“q”退出。 I also didnt see the need for cap = capture... in your code.我也没有在您的代码中看到需要 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()

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

The problem in my code was in the part (While).我的代码中的问题出在 (While) 部分。 It should be while (True) instead the one in your code它应该是 while (True) 而不是代码中的那个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM