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
Python 2.7 openCV imwrite写入0 kb图像帧_Python 2.7_Opencv_Opencv3.3 - Fatal编程技术网

Python 2.7 openCV imwrite写入0 kb图像帧

Python 2.7 openCV imwrite写入0 kb图像帧,python-2.7,opencv,opencv3.3,Python 2.7,Opencv,Opencv3.3,我正在运行下面的代码将视频转换为帧。问题是它正在创建大小为0 KB的图像文件,当我打开它时,它没有显示任何内容。。我不明白是什么造成了这个问题。我需要安装任何图像编解码器吗 ''' Using OpenCV takes a mp4 video and produces a number of images. I am using OpenCV 3.3.0 version and Python 2.7 Which will produce a folder called

我正在运行下面的代码将视频转换为帧。问题是它正在创建大小为0 KB的图像文件,当我打开它时,它没有显示任何内容。。我不明白是什么造成了这个问题。我需要安装任何图像编解码器吗

    '''
    Using OpenCV takes a mp4 video and produces a number of images. I am using OpenCV 3.3.0 version and Python 2.7


    Which will produce a folder called data with the images, There will be 2000+ images for example.mp4.
    '''
    import cv2
import numpy as np
import os

# Playing video from file:

try:
        cap = cv2.VideoCapture('aa.mkv')
except:
        print "Could not open video file"
        raise
print cap.grab()

try:
    if not os.path.exists('data'):
        os.makedirs('data')
except OSError:
    print ('Error: Creating directory of data')

currentFrame = 0
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    if not frame is None:
        # Saves image of the current frame in jpg file
        name = './data/frame' + str(currentFrame) + '.jpg'
        print ('Creating...' + name)
        cv2.imwrite(name, frame)
        #cv2.imshow(name, frame)
    else:
        break

    # To stop duplicate images
    currentFrame += 1

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

您没有使用imwrite函数来写入帧。此外,imshow函数名拼写错误。我已对您的代码进行了更改。试试这个:

import cv2
import numpy as np
import os

# Playing video from file:
cap = cv2.VideoCapture('aa.mkv')

try:
    if not os.path.exists('data'):
        os.makedirs('data')
except OSError:
    print ('Error: Creating directory of data')

currentFrame = 0
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    if not frame is None:
        # Saves image of the current frame in jpg file
        name = './data/frame' + str(currentFrame) + '.jpg'
        print ('Creating...' + name)
        cv2.imwrite(name, frame)
        cv2.imshow(name, frame)
    else:
        break

    # To stop duplicate images
    currentFrame += 1

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

你的imwrite命令在哪里?如果你检查你的
,你会看到它是空的。或者,您可以检查您的
ret
,查看它是否为
False
,即帧未被读取。ret也不会返回任何内容。我的程序是正确的,我没有在问题中包含imwrite()。实际问题在于OpenCV版本。pip是否安装了opencv python,现在一切正常。我猜OP在某个时候的代码中有
imwrite
,这就是
w
的来源。我得到了这个错误。cv2.error:C:\build\master\u winpack-bindings-win32-vc14-static\opencv\modules\highgui\src\window.cpp:325:error:(-215)size.width>0&&size.height>0在函数cv::imshowI中对代码进行了更改。现在检查一下。同时检查视频路径是否正确。这次程序执行时没有任何错误,但没有输出。它没有写任何帧。编辑之前的代码仅写入一个0字节的图像帧0。视频的位置正确,它与我的程序运行的位置相同。请尝试在任何其他视频上运行代码,并检查它是否正常工作。我已经测试过了,在我的视频上工作得很好。