Creating high frame rate video using opencv in python

asked 2018-03-07 08:18:06 -0500

Kakeh gravatar image

For one of my application i want to write code to process high frame rate videos in which a small white dot in a grayscale image will be moving

in order to start building above application, i wanted to create a high frame rate video in first place for which i have tried to write the following code

import cv2
import numpy as np
# as normal usage thrown me error i am using this
fourcc = cv2.cv.CV_FOURCC(*'XVID')
#last parameter 1, i want to create a grayscale video
writer = cv2.VideoWriter("output.avi", fourcc, 100,(700,500),1)
i = 0
j = 0 
for frame in range(100):
    ref_image_array = np.zeros((700,500),np.uint8)
    #trying to change the circle position in every image
    cv2.circle(ref_image_array,(490-i,690-j),5,255,-1)
    writer.write(ref_image_array)
    i= i + 1
    j= j + 1
writer.release()
print 'Writing Done'
cap = cv2.VideoCapture('output.avi')
while(cap.isOpened()):
    print 'valid file'
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

i find something wrong with the way i am writing , because i am not able to play the video, kindly guide me

edit retag flag offensive close merge delete

Comments

and the problem is ?

oh, python. how did you install cv2 ? (it might not have any video file capabilities)

try a print(cv2.getBuildInformation()) , and look at the "Video IO" section

berak gravatar imageberak ( 2018-03-07 08:19:19 -0500 )edit

i have installed anaconda, after which i copied the cv2.py in to the installation folder of anaconda

Kakeh gravatar imageKakeh ( 2018-03-07 21:27:35 -0500 )edit

sorry for not being explicit enough, os ?

cv2.py is a typo ? did you mean cv2.so ? or cv2.pyd ?

did you built it locally ?

berak gravatar imageberak ( 2018-03-08 03:35:30 -0500 )edit

i am using anaconda on windows, i have simply copied cv2.pyd from opencv library and pasted it here, C:\ProgramData\Anaconda2\Lib\site-packages\cv2.pyd so import will directly picks from it

Kakeh gravatar imageKakeh ( 2018-03-08 08:39:03 -0500 )edit

make sure, opencv_ffmpeg.dll is on your PATH (all video stuff is in there)

berak gravatar imageberak ( 2018-03-08 08:47:51 -0500 )edit