opencv python- not able to write videos - python

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

Related

How could i play a local video file in python?

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

Python VideoCapture(0) my camera bad work

This my codes :
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()
I write the code like this and try to open my camera, but the camera image looks like the screenshot.
Stop working screen shot
Any idea ? What is the reason?
It's indentation problem.
Try this code:
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()
Provide proper indentation so the program works fine.
Press "q" to stop the program.

OpenCV in macbook and raspberry pi

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.

Opencv-python cannot write video file

I am trying to save a video file with opencv3 in python. The video that I want to save comes from another video file which I modify for tracking purposes. What I get is an empty .avi file, and I don't understand why.
If it helps, I'm on OSX.
Thanks for the help.
Here is the relevant part of the code:
import cv2
import numpy as np
cap = cv2.VideoCapture('Vid.avi')
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (width-110, heigh-210))
while (cap.isOpened()):
success, frame = cap.read()
if success:
hsv_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_frame,lower,upper) #lower and upper are defined in another part of the code
hsv_res = cv2.bitwise_and(hsv_frame,hsv_frame, mask= mask)
out.write(hsv_res)
cv2.imshow('Video', hsv_res)
if cv2.waitKey(50) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()

saving a video that has background subtraction applied to it in python 3.5 using open cv

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

Categories

Resources