I am an open cv beginner. I followed the steps in this tutorial to practice using it. http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html#display-video
I changed some lines for performance on my mac:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
size = (int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'DIVX') # upper case - yl3
out = cv2.VideoWriter('output.avi',fourcc, 20, size, 1) #20.0: number of frames per sec
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,1)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
I am using Python 2.7.10, opencv 2.4.12 and mac OS X 10.10.5
Now I can successfully get the camera to work and generate an output.avi
file in the same directory, but the file was only 414k overtime and I can't open it. It seemed that it contained nothing but black.
Can anyone help me with that?
Read, Write and Display a video using OpenCV 1 Reading a Video. In OpenCV, a video can be read either by using the feed from a camera connected to a computer or by reading a video file. 2 Displaying a video. After reading a video file, we can display the video frame by frame. ... 3 Writing a video. ...
python - OpenCV videowrite doesn't write video - Stack Overflow I used the following page from OpenCV 3.0.0 tutorial: Tutorial in docs When I tried to use the example that saves videos, it doesn't work. It displays the content from the webcam, and also creates ... Stack Overflow About Products For Teams
Most common are frame size error and api preference error. If the frame size is not similar to the video, then even though we get a video file at the output directory, it will be blank. If you are using the NumPy shape method to retrieve frame size, remember to reverse the output as OpenCV will return height x width x channels.
VideoWriter_fourcc ('M', 'J', 'P', 'G') in Python. VideoWriter::fourcc ('M', 'J', 'P', 'G') in C++. The video codec specifies how the video stream is compressed. It converts uncompressed video to a compressed format or vice versa.
So I had the same problem as you. I narrowed it down to the value of cv2.cv.CV_FOURCC
. Those are apparently case sensitive. I wasn't able to get the .avi
codecs working, but if you don't mind a MPEG
, this worked for me.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
size = (int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
fps = 20
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') # note the lower case
vout = cv2.VideoWriter()
success = vout.open('output.mp4',fourcc,fps,size,True)
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, -1)
vout.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
vout.release()
cv2.destroyAllWindows()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With