I am using OpenCV with python 2.7.3 on Linux 64-bit machine. I wanted to fetch frames from my Logitech C270 and store it as an AVI video. The code is working fine, it also shows me the video getting captured and the output file is also created. But when i try to play the file it is not playing at all as well as i am getting 'cv2.VideoWriter object has no attribute release' error on terminal. So, if someone can tell me how to release the cv2.VideoWriter after completion.
import numpy as np
import cv2
cap = cv2.VideoCapture(1)
fourcc = cv2.cv.CV_FOURCC('X','V','I','D')
out = cv2.VideoWriter('output.avi', fourcc, 20.0,(640,480))
while(True):
ret, frame = cap.read()
if cap.isOpened() == 0:
cap.open(1)
if ret==True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(gray)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
Instead of using the lines below:
fourcc = cv2.cv.CV_FOURCC('X','V','I','D')
out = cv2.VideoWriter('output.avi', fourcc, 20.0,(640,480))
Use those one:
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (int(cap.get(3)),int(cap.get(4))))
I think, this will help you.
Instead of using this line
fourcc = cv2.cv.CV_FOURCC('X','V','I','D')
Use this one
fourcc = cv2.VideoWriter_fourcc(*'XVID')
Related
I'm a beginner for python.
as below code. I need to record vedio,but after run code. it not show the image.
please help me
python 3.9
opencv 4.6
import cv2
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
result = cv2.VideoWriter('Output.avi', fourcc, 60.0,(640,480))
while (cap.isOpened()): # check Video
check , frame = cap.read()
if check == True :
cv2.imshow("Output", frame)
result.write(frame)
if cv2.waitKey(1) & 0xFF == ord("e"):
break
result.release()
cap.release()
cv2.destroyAllWindows()
I try to check camera device. so i open them by application. it can work normally
I'm trying to run motion saliency on a video and see the results. However, I keep getting this particular error:(ret, img_sal) = saliency.computeSaliency(gray)
cv2.error: Unknown C++ exception from OpenCV code
Here is the code below:
import cv2
vid = cv2.VideoCapture('vids/test.mp4')
saliency = cv2.saliency.MotionSaliencyBinWangApr2014_create()
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fps = vid.get(cv2.CAP_PROP_FPS)
print(fps)
while True:
success, frame = vid.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
(ret, img_sal) = saliency.computeSaliency(gray)
saliencyMap = (img_sal * 255).astype('uint8')
cv2.imshow('Map', saliencyMap)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cv2.destroyAllWindows()
vid.release()
I would also appreciate if any other errors could be pointed out. I'm trying to save the video after applying motion saliency, so any help to that would be highly appreciated. Thank you.
I have two different MP4 videos, I can read one of them through cv2.read but can't read the other video.
I have tried storing both the videos at same location.
import numpy as np
import cv2
# Capture video from file
cap = cv2.VideoCapture('GP190763.MP4')
print(cap.get(3))
print(cap.isOpened())
while True:
ret, frame = cap.read()
print(frame)
print(ret)
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#cv2.imshow('frame',gray)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
else:
break
cap.release()
"ret" variable is false for one of the videos and True for the other video.
I have double checked that both videos are MP4 videos.
Any other properties of video I should be comparing ?
I am trying to save a video in OpenCV but i keep getting the error "could not demultiplex stream". I then checked size and found out that it was in kB. I primarily want to save grayscale videos how do i make it possible?
Is there any specific codec i need to use?
mplayer gives the following output
MPlayer 1.1-4.8 (C) 2000-2012 MPlayer Team
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
Playing output.avi.
libavformat version 54.20.4 (external)
Mismatching header version 54.20.3
AVI file format detected.
[aviheader] Video stream found, -vid 0
AVI: Missing video stream!? Contact the author, it may be a bug :(
libavformat file format detected.
[lavf] stream 0: video (mpeg4), -vid 0
VIDEO: [MP4V] 1280x720 24bpp -nan fps 0.0 kbps ( 0.0 kbyte/s)
Clip info:
encoder: Lavf54.20.4
Load subtitles in ./
Failed to open VDPAU backend libvdpau_nouveau.so: cannot open shared object file: No such file or directory
[vdpau] Error when calling vdp_device_create_x11: 1
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
libavcodec version 54.35.1 (external)
Mismatching header version 54.35.0
Unsupported AVPixelFormat 53
Selected video codec: [ffodivx] vfm: ffmpeg (FFmpeg MPEG-4)
==========================================================================
Audio: no sound
Starting playback...
V: 0.0 0/ 0 ??% ??% ??,?% 0 0
Exiting... (End of file)
Right now i tried with multiple codec formats
import imutils
import cv2
import numpy as np
interval = 30
outfilename = 'output.avi'
threshold=100.
fps = 10
cap = cv2.VideoCapture("video.mp4")
ret, frame = cap.read()
height, width, nchannels = frame.shape
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
out = cv2.VideoWriter( outfilename,fourcc, fps, (width,height))
ret, frame = cap.read()
frame = imutils.resize(frame, width=500)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
while(True):
frame0 = frame
ret, frame = cap.read()
frame = imutils.resize(frame, width=500)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
if not ret:
deletedcount +=1
break
if np.sum( np.absolute(frame-frame0) )/np.size(frame) > threshold:
out.write(frame)
else:
print "Deleted"
cv2.imshow('Feed - Press "q" to exit',frame)
key = cv2.waitKey(interval) & 0xFF
if key == ord('q'):
print('received key q' )
break
cap.release()
out.release()
print('Successfully completed')
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
out = cv2.VideoWriter('output.avi',-1, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret:
gray = cv2.cvtColor(src=frame, code=cv2.COLOR_BGR2GRAY)
out.write(gray)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
Try this one
Select Intel iyuv codec.
The out.avi is non working file.
The output.avi is new working file.
If video is not getting saved, possibly the reason may be its capture size which is hardcoded as (640,480).
You can try the below code:
cap = cv2.VideoCapture(0)
fourcc_codec = cv2.VideoWriter_fourcc(*'XVID')
fps = 20.0
capture_size = (int(cap.get(3)), int(cap.get(4)))
out = cv2.VideoWriter("output.avi", fourcc_codec, fps, capture_size)
You can also check if you are passing the correct shape, do it like this:
h, w, _ = frame.shape
size = (w, h)
out = cv2.VideoWriter('video.avi', cv2.VideoWriter_fourcc(*'XVID'), 30, size)
I tried playing a video from a file, as given in the tutorials. My program was as follows:
import numpy as np
import cv2
cap = cv2.VideoCapture('output.avi')
while(cap.isOpened()):
ret, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('outVideo',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
But I got the following error:
Traceback (most recent call last):
File "playVideo.py", line 8, in <module>
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /home/hp/opencv/modules/imgproc/src/color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function ipp_cvtColor
I checked ret and it turned out to be False.
So the actual problem is with saving video. I used the following code to save 'output.avi' using VideoWriter function:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
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)
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
I am not able to open 'output.avi', even using VLC
First :
check ret value with: ret==True
Second as tutorials said:
Make sure proper versions of ffmpeg or gstreamer is installed. Sometimes, it is a headache to work with Video Capture mostly due to wrong installation of ffmpeg/gstreamer.
from:
http://docs.opencv.org/3.1.0/dd/d43/tutorial_py_video_display.html#gsc.tab=0
Finally check the video codec:
Can't open video with opencv2
Change the "while"- loop parameter to "ret" - and the order of the cap.read()
- ret is True if there is a valid next frame in the video/file stream.
import numpy as np
import cv2
cap = cv2.VideoCapture('output.avi')
ret, frame = cap.read()
while(ret):
ret, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('outVideo',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
ret, frame = cap.read()
cap.release()
cv2.destroyAllWindows()
I had faced the same error. But the issue was due to a missing package. It wasn't detected while using a jupyter notebook, but it showed up when I run the .py through terminal.
sudo apt-get install python-tk
This solved the error for me, hope it helps somebody else too :)