1

I am running the below code to convert video into frames. Problem is it is creating Image files with 0 KB size and when I open it is not showing anything.. I don't understand what is creating the problem. Do I need to install any Image codecs?

    '''
    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()
Ramineni Ravi Teja
  • 3,568
  • 26
  • 37

1 Answers1

3

You are not using imwrite function to write frames. Also your imshow function name is misspelled. I have made changes in your code. Try this:

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()
Jazz
  • 916
  • 1
  • 8
  • 22
  • I'm going to guess the OP had `imwrite` in their code at some point and that's where the `w` came from. – alkasm Sep 21 '17 at 04:24
  • I am getting this error. cv2.error: C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\highgui\src\window.cpp:325: error: (-215) size.width>0 && size.height>0 in function cv::imshow – Ramineni Ravi Teja Sep 21 '17 at 08:25
  • I have made changes in code. Now check it. Also check whether path to video is right. – Jazz Sep 21 '17 at 09:07
  • This time program executed without any error, but no output. It hasn't written any frames. Previous code before editing was writing only one image frame0 with 0 bytes. Location of the video is correct, it is in the same location where my program is running. – Ramineni Ravi Teja Sep 21 '17 at 12:43
  • Try to run code on any another video and check whether it is working or not. I have tested it, working fine on my video. – Jazz Sep 21 '17 at 15:09
  • I have running on different videos of different formats. I feel it is an issue with ffmpeg codec. What version of Python and OpenCV are you using? I feel video itself is not readable. If I select "0" webcam as my input in VideoCapture(0) it is working fine and converting into frames. – Ramineni Ravi Teja Sep 22 '17 at 05:28
  • I am using Python v3.6.2 and OpenCV v3.2. I guess problem is in your video. Try to convert it into any other extension format and read it. – Jazz Sep 22 '17 at 08:09
  • I can able to play video in player. I tried .mkv format and .mp4 format. OpenCv supports both the above formats since it uses ffmpeg codec in backend. ffmpeg supports both the formats. I have tried with ffmpeg codec separately. I can able to convert video to frames. I am using below command. ffmpeg -i aa.mkv -r 0.25 output_%04d.png – Ramineni Ravi Teja Sep 22 '17 at 08:52
  • Can you try some .mkv format video and convert into frames and let me know is it supporting or not. – Ramineni Ravi Teja Sep 22 '17 at 09:15
  • It was my bad. It was the problem with my OpenCV version. Now everything running fine. – Ramineni Ravi Teja Sep 22 '17 at 19:08