【问题标题】:Cant open video with videoCapture opencv无法使用videoCapture opencv打开视频
【发布时间】:2021-03-22 00:04:26
【问题描述】:

我有以下代码从视频文件中读取帧并存储为 jpg。如果我直接从相机读取帧,则代码可以正常工作,但对于视频文件,它不会读取帧。

cap = cv2.VideoCapture('C:/Users/lostpanda.mp4')
#cap = cv2.VideoCapture(0)
count = 0
while cap.isOpened():
  ret,frame = cap.read()
  print(ret,frame)
  cv2.imshow('window-name', frame)
  name = 'C:/Users/video_testing/video-frames/' + str(count) + '.jpg'
  #cv2.imwrite("frame%d.jpg" % count, frame)
  cv2.imwrite(name,frame)
  count = count + 1
  if cv2.waitKey(10) & 0xFF == ord('q'):
    break

谢谢

【问题讨论】:

  • 嘿!错误信息是什么?
  • 谢谢@VidyaGanesh,我在运行上述代码时没有收到任何错误,但我无法读取帧并将它们保存在文件夹中。如果我将输入更改为从视频中读取帧,代码工作正常
  • 考虑单独使用 ffmpeg。它可以将视频解压缩成一系列图片。您的问题描述应该是“无法使用 VideoCapture 打开视频”,其余的不是问题的一部分。
  • windows... UAC 问题>?试试把它调低?

标签: python opencv video video-streaming


【解决方案1】:

你可以试试这个:

import cv2 
import os 

# Read the video from specified path 
cam =cv2.VideoCapture(r"C:/Users/lostpanda.mp4") 

try:
    # creating a folder named data 
    if not os.path.exists('video-frames'): 
        os.makedirs('video-frames') 

# if not created then raise error 
except OSError: 
    print ('Error: Creating directory of data') 

# frame 
currentframe = 0

while(True): 
  
# reading from frame 
    ret,frame = cam.read() 

    if ret: 
        # if video remains continue creating images 
        name = './video-frames/frame' + str(currentframe)+ '.jpg'
        print ('Creating...' + name) 

        # write extracted images 
        cv2.imwrite(name, frame) 

        #Counter to show number of frames that are being created 
        currentframe += 1
    else: 
        break

# Release all space and windows once done 
cam.release() 
cv2.destroyAllWindows()

【讨论】:

  • 嗨,@VidyaGanesh,我试过这个,但我注意到在我之前的代码和这段代码中,当我打印帧时我无法读取帧,我没有检索到。我的文件路径是正确的
猜你喜欢
  • 1970-01-01
  • 2012-04-30
  • 2023-01-11
  • 1970-01-01
  • 2017-09-28
  • 2014-01-20
  • 2018-07-26
  • 2015-09-11
  • 2013-01-22
相关资源
最近更新 更多