I want to save my webcam video using opencv.
I wrote this code.
import numpy as np
import cv2
cap=cv2.VideoCapture(0)
#Define the codec
#FourCC code is passed as cv2.VideoWriter_fourcc('M','J','P','G')
#or cv2.VideoWriter_fourcc(*'MJPG') for MJPG.
fourcc = cv2.VideoWriter_fourcc(*'XVID')
#Define VideWrite object
#cv2.VideoWrite('arg1',arg2,arg3,(width,heigh))
#arg1:output file name
#arg2:Specify Fourcc code
#arg3: frames per seconds
#FourCC is a 4-byte code used to specify video codec
out=cv2.VideoWriter('SaveAVideo.avi',fourcc,20.0, (640,480))
while(cap.isOpened()):
ret,frame = cap.read()
print('frame =',frame)
print('ret = ',ret)
if ret==True:
frame =cv2.flip(frame,0)
#Write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(0) & 0xFF== ord('q'):
break
else:
break
print('after while loop')
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
The problem I am facing is that it is recording only black screen, although I have checked my webcam.
As I said in my comment, the solution is to change:
cv2.waitKey(0)
to:
cv2.waitKey(another_value)
by example 1.
According to the docs the parameter that receives cv2.waitKey() indicates the delay that is given:
In your case it is necessary since saving the image in the video, also is always convenient since an image is given at 60Hz so Overcoming that frequency is unnecessary.
Related
I'm simply trying to read IP Camera live stream through OpenCV's simple code, i.e as follows:
import numpy as np
import cv2
src = 'rtsp://id:pass#xx.xx.xx.xx'
cap = cv2.VideoCapture(src)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
The problem here is, sometime it works like a charm by showing the running live video, but sometime else it creates a lot of blank windows which keeps popping up until the job is killed. Like the below image:
Why does it happen, also how can we avoid it?
Maybe you should cover the case that the video capture fails to establish a healthy stream.
Note that it is possible to not to receive a frame in some cases even though video capture opens. This can happen due to various reasons such as congested network traffic, insufficient computational resources, power saving mode of some IP cameras.
Therefore, I would suggest you to check in the frame size and make sure that your VideoCapture object is receiving the frame at right shape. (You can debug and see the size of a visible frame to learn the expected resolution of the camera.)
A change in your loop like following might help
min_expected_frame_size = [some integer]
while(cap.isOpened()):
ret, frame = cap.read()
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
if ret==True and ((width*height) >= min_expected_frame_size):
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# 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()
It records properly using python but when I convert it using pyinstaller the video produces an output.avi without content (0 byte). I also tried cx_freeze but the result is the same.
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
You can probably try changing this to
out = cv2.VideoWriter('output.mp4',fourcc, 20.0, (640,480))
I know basically nothing except to change to mp4 or wav in the programming. But once you get the file out of the program you could save the file and change it's encoding. You can do this on windows by renaming it. But I don't know about Macs. Or if you wanted to you could make it .exe if you wanted.
I want to record video with Raspberry Pi + wedcam (logitech). Although I found many examples which are actually almost the same code as following:
import numpy as np
import cv2
path = ('/.../output.avi')
fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter(path,fourcc, 20.0, (640,480))
while(cap.isOpened()):
#read the frame
ret, frame = cap.read()
if ret==True:
#Write the frame
video_writer.write(frame)
#show the frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
video_writer.release()
cv2.destroyAllWindows()
First question, I have tried all solutions from OpenCV write frame to file python
but it seems those solutions didn't suitable for me...
so I want to know if anyone has other solutions for this problem, I will appreciate!
Second question, I found that someone use
cv2.VideoWriter_fourcc('XVID')
instead of
cv2.cv.CV_FOURCC(*'XVID')
Would that be the problem? Also, I have tried to use cv2.VideoWriter_fourcc('XVID'), but get an error: 'module' object has no attribute 'VideoWriter_fourcc'... How can I solve this?
Thanks!
You used 'out' to create your video writer object
out = cv2.VideoWriter(path,fourcc, 20.0, (640,480))
So perhaps you should replace video_writer.write(frame) with out.write(frame)
Also replace video_writer.release() with out.release()
I was trying the following piece of code for storing the captured video into a file. The Live Stream is getting displayed correctly , but for whatever length I record the video, the target file is a 5.7kB file that doesn't contain any video.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
fourcc= cv2.cv.FOURCC(*'DIVX')
ret=cap.set(3,500)
ret=cap.set(4,500)
out= cv2.VideoWriter('out.avi',fourcc,20,(500,500))
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
out.write(frame)
cv2.imshow('out.avi',frame)
if cv2.waitKey(1) &0xFF ==ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
Does anyone have any idea what I might be doing wrong here?
Please help to figure it out .
It might be because the encoding of fourcc is not supported/installed on your machine, thus makes it impossible to export the frames to the video. It happens to me all the time. What works for me is the mp4 codec (in opencv: cv2.cv.CV_FOURCC('m', 'p', '4', 'v')). So, try this:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
fourcc= cv2.cv.CV_FOURCC('m', 'p', '4', 'v')
ret=cap.set(3,500)
ret=cap.set(4,500)
out= cv2.VideoWriter('out.avi',fourcc,20,(500,500))
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
out.write(frame)
cv2.imshow('out.avi',frame)
if cv2.waitKey(1) &0xFF ==ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
Otherwise, you can try to install all the codec compatible with ffmpeg (It seems that opencv uses ffmpeg to export the frames to video file) and retry your original code.
I'm new in Python (2.7) and I try to work on video processing (with module openCv "cv2"). Starting with tutorials, I try to use the script of this tutorial : paragraph "Saving a video".
Everything works fine excepting that the video I'm saving is empty. I can find output.avi in my directory but its memory size is 0kb an, of course when I run it, no video is displayed.
After a few changes here is my code :
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
# 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()
Does anyone know why it is not working properly ?
Thanks a lot.
Edwin
I never worked with openCV, but I bet the problem is in
cap = cv2.VideoCapture(0)
This is a C version of the VideoCapture method http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture
Maybe you can try to do the same. Something like
cap = cv2.VideoCapture(0)
if (not cap.isOpened()):
print "Error"
EDIT: just downloaded Python and OpenCV and discovered the problem was the codec. Try to change
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
for
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))
and select the codec by hand.
Could be the output resolution is different from the input. Check width and height of cap.
size = (int(cap.get(3)), int(cap.get(4)))
Change either your camera or the output resolution.
I have Windows 10, Python3.7.6, OpenCV 4.2.0. In my case, the problem is video encoder. Both "XVID" and "X264" result in an empty output video. I changed the encoder to "DIVX" and the video is generated successfully.