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()
Related
I want to write a video using OpenCv and process every frames in different seconds.
I am using cv2.VideoWriter, but the output file shows only the first frame of my video and it is not playing. I wanted to add text on the first frame and it did it, but it doesn't continue with the rest of the video.
As you can see from the code below, it creates a new output file for the new processed video.
It does create the mp4 file, but only showing first frame with the added text and it is not playing.
Any suggestion why this is happening?
Here is my code, I am using Spyder and windows.
fps = int(round(cap.get(5)))
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_video_file,fourcc,fps,(frame_width,frame_height))
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
if ret:
if cv2.waitKey(28) & 0xFF == ord('q'):
break
if between(cap, 0, 10000):
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
cv2.putText(frame,'hello',org=(10,600),fontFace=font,fontScale=10,color=(255,0,0),thickness=4)
pass
# Our operations on the frame come here
out.write(frame)
# Display the resulting frame
cv2.imshow('frame',frame)
Exactly I could not find the problem in your code because you did not give the between function. However you may try following snippet:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
if (cap.isOpened() == False):
print("Unable to read camera feed")
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
fps = int(round(cap.get(5)))
out = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
while (True):
ret, frame = cap.read()
if ret == True:
cv2.putText(frame, 'hello',
org=(10, 300),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=5,
color=(255, 0, 0),
thickness=4)
out.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
I'm trying to resize frame image-array in original (480,640,3) to (224,224,3), but I use
CV_CAP_PROP_FRAME_WIDTH, CV_CAP_PROP_FRAME_HEIGHT, only change size of frame that displayed on my screen. Thank you very much!
My code here!
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
# Display the resulting frame
cv2.imshow('frame',frame)
print(frame.shape)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
you can add following code in your while loop,
frame = cv2.resize(frame, (224, 224))
print(frame.shape)
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!
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'm trying to do frame difference this is my code below
import numpy as np
import cv2
current_frame =cv2.VideoCapture(0)
previous_frame=current_frame
while(current_frame.isOpened()):
current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
previous_frame_gray= cv2.cvtColor(previous_frame, cv2.COLOR_BGR2GRAY)
frame_diff=cv2.absdiff(current_frame_gray,previous_frame_gray)
cv2.imshow('frame diff ',frame_diff)
cv2.waitKey(1)
current_frame.copyto(previous_frame)
ret, current_frame = current_frame.read()
current_frame.release()
cv2.destroyAllWindows()
my problem is that I tried to create empty frame to save the first frame from current_frame
previous_frame=np.zeros(current_frame.shape,dtype=current_frame.dtype)
But I think it is not correct , then I tried to pass current_frame like this:
previous_frame=current_frame
Now I'm getting this error :
current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
TypeError: src is not a numpy array, neither a scalar
so what should I do for this ?
Thanks for help
You have mixed the VideoCapture object and the frame.
I've also made small changes in the frame copy and waitkey.
import cv2
cap = cv2.VideoCapture(0)
ret, current_frame = cap.read()
previous_frame = current_frame
while(cap.isOpened()):
current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
previous_frame_gray = cv2.cvtColor(previous_frame, cv2.COLOR_BGR2GRAY)
frame_diff = cv2.absdiff(current_frame_gray,previous_frame_gray)
cv2.imshow('frame diff ',frame_diff)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
previous_frame = current_frame.copy()
ret, current_frame = cap.read()
cap.release()
cv2.destroyAllWindows()