简体   繁体   English

无法用 videoCapture 打开视频 opencv

[英]Cant open video with videoCapture opencv

I have following code which reads frames from video file and stores as jpg.我有以下代码,它从视频文件中读取帧并存储为 jpg。 The code works fine if I read frames from camera directly but for video file it doesn't read the frames.如果我直接从相机读取帧,则代码可以正常工作,但对于视频文件,它不会读取帧。

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

Thanks谢谢

You can try this instead:你可以试试这个:

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

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

相关问题 使用OpenCV VideoCapture发行打开视频文件 - Issue opening video file with OpenCV VideoCapture Python OpenCV videocapture 不从源捕获视频 - Python OpenCV videocapture not capturing video from source 访问/打开 OpenCV VideoCapture 或 Vidgear 中的嵌入式视频 - Access/Open embedded videos in OpenCV VideoCapture or Vidgear OpenCV VideoCapture称视频有0帧(C ++和Python) - OpenCV VideoCapture says video has 0 frames (C++ and Python) OpenCV(3.3.0)在带有视频的VideoCapture上返回失败,但可用于网络摄像头[OS X] - OpenCV (3.3.0) returns fail on VideoCapture with Video but works with Webcam [OS X] 使用 openCV cv2VideoCapture 打开视频文件时出错 - Error opening video file with openCV cv2VideoCapture 使用OpenCV的VideoCapture和Python从视频中获取慢速图像(帧) - Slow image(frame) grabbing from video with OpenCV's VideoCapture and Python 通过单击两个键以 OpenCV VideoCapture 结束时关闭视频的问题 - Issue closing a video when it ends with OpenCV VideoCapture by clicking two keys 从 python Opencv 接收 Gazebo gstreamer UDP 视频 - Receving Gazebo gstreamer UDP video from python Opencv Videocapture OpenCV:VideoCapture的阅读框将视频推向奇怪的错误位置 - OpenCV: reading frames from VideoCapture advances the video to bizarrely wrong location
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM