program:
import numpy as np
import cv2
cap = cv2.VideoCapture(1)
fourcc = cv2.VideoWriter_fourcc(*'VID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(frame)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
error:
Traceback (most recent call last):
File "D:/ANIKET/python projects/img_process.py", line 5, in <module>
fourcc = cv2.VideoWriter_fourcc(*'VID')
TypeError: Required argument 'c4' (pos 4) not found
i m trying to save the video file.
but i m getting this error.
plz tell me what i am doing wrong
you can also use -1 as an default argument. It had worked in my case.
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',-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()
This code addresses both your original issue (replacing *'VID' with *'XVID') as well as your subsequent issue in the comment to your question:
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:
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()
Feel free to tell me if this works as you intended and point out any remaining issues!
Related
I am writing a code in python using the OpenCV Python library.
I wanted to access the webcam using the code written, but after executing the code in the terminal, the following error arises again and again.
Code written:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Error:
If this is a camera installed on your computer:
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
If this is a camera connected via USB:
import cv2
cap = cv2.VideoCapture(1)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
The below code is supposed to capture a video and save it.
import cv2
import numpy as np
from skimage.filters import gaussian
capture = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('My video.avi', fourcc, 10, (640,480))
while capture.isOpened:
ret, frame = capture.read()
if ret==True:
frame = gaussian(frame, sigma=5, multichannel=True)
out.write(frame)
cv2.imshow('My video', frame)
if cv2.waitKey(1) == 27:
break
capture.release()
out.release()
cv2.destroyAllWindows()
However, I get the following error:
error: OpenCV(3.4.3) D:\Build\OpenCV\opencv-3.4.3\modules\videoio\src\cap_ffmpeg.cpp:296: error: (-215:Assertion failed) image.depth() == CV_8U in function 'cv::`anonymous-namespace'::CvVideoWriter_FFMPEG_proxy::write'
If I remove the gaussian blur, the code works. What is wrong?
import cv2
import numpy as np
from skimage.filters import gaussian
capture = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
videoWriter = cv2.VideoWriter(
r'output_video_path', fourcc, 10.0, (640, 480))
while (True):
ret, frame = capture.read()
if ret:
frame = gaussian(frame, sigma=5, multichannel=True)
cv2.imshow('video', frame)
frame = np.uint8(255 * frame)
videoWriter.write(frame)
if cv2.waitKey(1) == 27:
break
capture.release()
videoWriter.release()
cv2.destroyAllWindows()
I have a video that I built using OpenCV VideoWriter.
I want to change a specific frame with a different one.
Is there a way to do it without rebuilding the entire video?
I am changing the third frame by making all pixels to zeros.
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))
frame_number = -1
while(True):
frame_number += 1
ret, frame = cap.read()
if frame_number == 3: # if frame is the third frame than replace it with blank drame
change_frame_with = np.zeros_like(frame)
frame = change_frame_with
out.write(frame)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
If you do not want to go through all the frames again:
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))
frame_number = -1
while(True):
frame_number += 1
ret, frame = cap.read()
if frame_number == 3: # if frame is the third frame than replace it with blank drame
change_frame_with = np.zeros_like(frame)
frame = change_frame_with
out.write(frame)
break # add a break here
else:
out.write(frame)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
But the above solution will not write all the other frames after 3 to the whole new video
This might help you :)
import cv2 as cv
vid = cv.VideoCapture('input.mp4')
total_frames = last_frame_number = vid.get(cv.CAP_PROP_FRAME_COUNT)
fourcc = cv.VideoWriter_fourcc(*'avc1')
writer = cv.VideoWriter("output.mp4", apiPreference=0,fourcc=fourcc,fps=video_fps[0], frameSize=(width, height))
frame_number = -1
while(True):
frame_number += 1
vid.set(1,frame_number)
ret, frame = vid.read()
if not ret or frame_number >= last_frame_number: break
# check these two lines first
if frame_number == changeable_frame_number :
frame = new_img_to_be_inserted
writer.write(frame)
# frame = np.asarray(frame)
gray = cv.cvtColor(frame, cv.IMREAD_COLOR)
# cv.imshow('frame',gray)
if cv.waitKey(1) & 0xFF == ord('q'):
break
vid.release()
writer.release()
cv.destroyAllWindows()
I have videostreams and I'd like to convert them to foreground detected videos on which everything is white that moving and all others are black.
When I run this below script nothing happens, python ide just waits. Should I wait, does the video render or do i make something wrong?
Thanks
import cv2
import numpy
cap = cv2.VideoCapture('2018_02_28_12_07_42.h264')
fgbg = cv2.createBackgroundSubtractorMOG2()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
#while(cap.isOpened()):
while True:
ret, frame = cap.read()
#if ret == True:
fgmask = fgbg.apply(frame)
out.write(frame)
cv2.imshow('original', frame)
cv2.imshow('fg', fgmask)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
out.release()
cv2.destroyWindows()
I have tried looking for the answer to my question but I have not really found an answer. I am wondering how to save a video file in python 3.5 that has background subtraction applied to it and in addition curious as to where the output file is supposed to be saved to?
Thanks in advance
tried out this code in anaconda 3.5 with opencv 3.1 works just fine to subtract background from a video file and then saving it to an output file
import cv2
import cv2 as cv
import numpy as np
capture = cv2.VideoCapture('people-walking.mp4')
size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*"DIB ")
video = cv2.VideoWriter('output.avi', fourcc, 30,size)
fgbg= cv2.createBackgroundSubtractorMOG2()
while True:
ret, img = capture.read()
if ret==True:
fgmask = fgbg.apply(img)
video.write(fgmask)
cv2.imshow('forehead',fgmask)
if(cv2.waitKey(27)!=-1):
break
capture.release()
video.release()
cv2.destroyAllWindows()
At first I was facing a problem. Then added isColor=False argument to the VideoWriter. It solved the issue.
Here's the code:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
out = cv2.createBackgroundSubtractorMOG2()
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
output = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480), isColor=False)
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,180)
outmask = out.apply(frame)
output.write(outmask)
cv2.imshow('original', frame)
cv2.imshow('Motion Tracker', outmask)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
output.release()
cap.release()
cv2.destroyAllWindows()