Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法使用videoCapture opencv打开视频_Python_Opencv_Video_Video Streaming - Fatal编程技术网

Python 无法使用videoCapture opencv打开视频

Python 无法使用videoCapture opencv打开视频,python,opencv,video,video-streaming,Python,Opencv,Video,Video Streaming,我有以下代码从视频文件中读取帧并存储为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

我有以下代码从视频文件中读取帧并存储为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

谢谢你,你可以试试这个:

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

您可以尝试以下方法:

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

嘿错误消息是什么?Thanks@VidyaGanesh,我在运行上述代码时没有收到任何错误,但我无法读取帧并将其保存在文件夹中。如果我将输入更改为从视频中读取帧,则代码工作正常。请考虑使用ffmpeg standalone。它可以将视频解压成一系列图片。您的问题描述应该是“无法使用VideoCapture打开视频”,其余部分不是问题的一部分。windows。。。UAC问题>?试着把它调低?嘿!错误消息是什么?Thanks@VidyaGanesh,我在运行上述代码时没有收到任何错误,但我无法读取帧并将其保存在文件夹中。如果我将输入更改为从视频中读取帧,则代码工作正常。请考虑使用ffmpeg standalone。它可以将视频解压成一系列图片。您的问题描述应该是“无法使用VideoCapture打开视频”,其余部分不是问题的一部分。windows。。。UAC问题>?试着把它调低?嗨,@VidyaGanesh,我试过这个,但我注意到在我以前的代码和这个代码中,当我打印帧时,我无法读取帧,我检索不到任何帧。我的文件路径是正确的。嗨,@VidyaGanesh,我试过这个,但我注意到在我以前的代码和这个代码中,当我打印帧时,我无法读取帧,我检索不到任何帧。我的文件路径是正确的