I wrote this code
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(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
But my the code is not capturing any videos. The file output.avi is 6kb is size no matter how long the recording goes.
The same code was implemented in raspberry pi with picamera. The video in the window is extremely slow and also the captured file shows only 1 frame for the whole time of video.
I'm using OpenCV 3.1.
Related
The following steps are to be carried out:
pick video file
and play it
tried the following code, but didn't pick video file
import numpy as np
import cv2
cap = cv2.VideoCapture('E:\Moives\Avengers.Infinity.War.mkv')
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
You could use vlc module for python maybe that can solve your question
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()
I tried as given in the to tutorial, but I am not able to open the video. I guess there is some problem with the fourcc thing in OpenCV-Python. My code was as follows:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
#fourCc = cv.VideoWriter_fourcc(*'XVID')
fourCc = cv2.VideoWriter_fourcc('X','V','I','D')
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()
It does create a file named output.avi (of non-zero size), but I am not able to play it, even using VLC.
NOTE: The same thing in C++ is working fine
I'm programing in python and opencv on Ubuntu but the cv2.destoyAllWindows() doesn't seem to work,as in this example:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
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()
when i press 'q' the program end but the window remain on the screen how ever on windows i didn't face such thing
any help I will be thankful