Friday 15 February 2013

osx - OpenCV write frame to file python -



osx - OpenCV write frame to file python -

hey im starting play around opencv , cant webcam output saved file. here have. runs fine, launches webcam , creates "output.avi" issue output.avi tiny(414 bytes) , same exact bytes each time run program. im guessing issue fourcc encoding havent been able find works in case. running on mac os x. allow me know if need anymore information.

import numpy np import cv2 path = ('/full/path/directory/output.avi') cap = cv2.videocapture(0) cap.set(1, 20.0) #match fps cap.set(3,640) #match width cap.set(4,480) #match height fourcc = cv2.cv.cv_fourcc(*'xvid') video_writer = cv2.videowriter(path,fourcc, 20.0, (640,480)) while(cap.isopened()): #read frame ret, frame = cap.read() if ret==true: #show frame cv2.imshow('frame',frame) #write frame video_writer.write(frame) if cv2.waitkey(1) & 0xff == ord('q'): break else: break # release if job finished cap.release() video_writer.release() cv2.destroyallwindows()

the main problem not coding safely:

path = ('/full/path/directory/output.avi') cap = cv2.videocapture(0) if not cap: print "!!! failed videocapture: invalid parameter!" sys.exit(1) cap.set(1, 20.0) #match fps cap.set(3,640) #match width cap.set(4,480) #match height # define codec , create videowriter object fourcc = cv2.videowriter_fourcc(*'xvid') video_writer = cv2.videowriter(path, fourcc, 20.0, (640,480)) if not video_writer : print "!!! failed videowriter: invalid parameters" sys.exit(1) # ...

so when videocapture() or videowriter() fails, programme knows can't go on.

also, notice how legacy cv2.cv.cv_fourcc() phone call replaced cv2.videowriter_fourcc(). did because this page shows up-to-date samples on how stuff python. seek all fourcc codes until find 1 works in system.

another of import thing realize setting frame size of capture interface may not work because photographic camera might not back upwards resolution. same can said fps. why problem? since need define these settings in videowriter constructor, frames sent object must have exact dimension, else author won't able write frames file.

this how should go this:

path = ('/full/path/directory/output.avi') cap = cv2.videocapture(0) if not cap: print "!!! failed videocapture: invalid parameter!" sys.exit(1) # next might fail if device doesn't back upwards these values cap.set(1, 20.0) #match fps cap.set(3,640) #match width cap.set(4,480) #match height # it's safer retrieve afterwards fps = cap.get(cv_cap_prop_fps) w = cap.get(cv_cap_prop_frame_width); h = cap.get(cv_cap_prop_frame_height); # define codec , create videowriter object fourcc = cv2.videowriter_fourcc(*'xvid') video_writer = cv2.videowriter(path, fourcc, fps, (w, h)) if not video_writer : print "!!! failed videowriter: invalid parameters" sys.exit(1) while (cap.isopened()): ret, frame = cap.read() if ret == false: break cv2.imshow('frame',frame) video_writer.write(frame) if cv2.waitkey(1) & 0xff == ord('q'): break cap.release() video_writer.release()

python osx opencv avi fourcc

No comments:

Post a Comment