VideoWriter outputs only a 5.7kB File in OpenCV Python - python

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.

Related

Unable to read the webcam stream : VideoCapture

I'm working on Windows10 and coding in Python.
I'm trying to read live stream from webcam.
webcam = cv2.VideoCapture(0)
(rval, im) = webcam.read()
After I checked value of 'im'; it is 'None'.
Am I missing anything? Please help.
import cv2
import numpy as np
webcam = cv2.VideoCpature(0) # If 0 doesn't work try swapping it with 1
while True:
ret, frame = webcam.read()
cv2.imshow("frame", frame)
cv2.waitKey(1)
webcam.release()
cv2.destroyAllWindows()
try running that (source)

Video recorder doesn’t write video in exe format Code:

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.

Trouble saving video in opencv

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.

Using Raspberry Pi + Webcam to record video in python always gets an empty video or only one frame video

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()

Webcam + Open CV Python | Black screen

I am using the code below, but I get a black image. Could you please help me rectify the error?
import cv2
import numpy as np
c = cv2.VideoCapture(0)
while(1):
_,f = c.read()
cv2.imshow('e2',f)
if cv2.waitKey(5)==27:
break
cv2.destroyAllWindows()
Update: See github.com/opencv/opencv/pull/11880 and linked conversations, only few backends support -1 as index.
Although this is an old post, this answer can help people who are still facing the same problem. If you have a single webcam but it renders all black, use cv2.VideoCapture(-1). This will get you the working camera.
Just change cv2.waitKey(0) to cv2.waitKey(30) and this issue will be resolved.
I've faced with same problem. Updating neither opencv nor webcam driver works. I am using kaspersky as antivirus. When I disable the kaspersky, then black output problem solved.
BTW, I can see the running .py file in kaspersky console > reports > host intrusion prevention. It reports application privilege control rule triggered - application: myfile.py, result: blocked: access to video capturing devices
Try this:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
This worked for me:
I did a pip install imutils. Imutils is a library with series of convenience functions to make basic image processing functions such as translation, rotation, resizing, skeletonization, displaying Matplotlib images, sorting contours, detecting edges, and much more easier with OpenCV and both Python 2.7 and Python 3.
import cv2
import imutils
cap = cv2.VideoCapture(0) # video capture source camera (Here webcam of laptop)
ret, frame = cap.read() # return a single frame in variable `frame`
while (True):
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
(grabbed, frame) = cap.read()
frame = imutils.resize(frame, width=400)
cv2.imshow('img1', frame) # display the captured image
if cv2.waitKey(1) & 0xFF == ord('q'): # save on pressing 'y'
cv2.imwrite('capture.png', frame)
cv2.destroyAllWindows()
break
cap.release()
Try put -0 on the index and pause any antivirus running
import cv2
import numpy as np
cap = cv2.VideoCapture(-0)
cap.set(3,640)
cap.set(3,480)
while(True):
success, img = cap.read()
cv2.imshow('frame',img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I faced the same issue after many calls with:
cap = cv2.VideoCapture(0)
and it solved when I changed the index to 1 :
cap = cv2.VideoCapture(1)
In my case just disabling Kaspersy has solved the problem.

Categories