NO video file was saved by using Python and OpenCV on my Raspberry PI

I have two pieces of codes. Here is the first one. It was mainly copied from save a video section on OpenCV-Python tutorial website, but I modified a little bit.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
cap.set(7,200)
out = cv2.VideoWriter('output.avi',cv2.cv.CV_FOURCC('X','V','I','D'), 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
cap.release()
cv2.destroyAllWindows()

Second one is here:

import cv
cv.NamedWindow('camera',1)
cap = cv.CaptureFromCAM(0)
fps = 20
fourcc = cv.CV_FOURCC('X','V','I','D')
cv.SetCaptureProperty(cap,cv.CV_CAP_PROP_FRAME_COUNT,200)
out = cv.CreateVideoWriter('output.avi',fourcc,fps,(640,480))
while True
    img = cv.QueryFrame(out,img)
    cv.WriteFrame(out,img)
    cv.ShowImage('camera',img)
    if cv.WaitKey(1) & 0xFF == ord('q'):
        break
cv.DestroyAllWindows()

Neither of them can make a video file saved or destroy the window in the end. No errors occurred in shell after running the code. I used Python 2.7.6 and OpenCV 2.3.1. Can somebody help me? Thanks a lot.

PS: I am not sure whether my method to set frame numbers correct or not.

Answers


It may have several reasons. Check the following:

  • Check that you can encode with XVID, maybe try with MJPEG first.
  • Set width and height of your input video by cap.set(3,640) and cap.set(4,480)

Need Your Help

Need help with syntax for test ISNUMERIC in case

sql case isnumeric

I am trying to stop input of negative values to process in any of the ranges, rather if they are negative they should drop down to the end of the 1st case statement as 'Invalid'. This is not workin...