Video written through OpenCV on Raspberry Pi not running - python

I was working on saving live feed from USB webcam through opencv on Raspberry PI 4 B+ . Here is the code
import cv2
cap = cv2.VideoCapture(0)
fourcc=cv2.VideoWriter_fourcc(''D','I','V','X'')
out=cv2.VideoWriter('output.mp4',fourcc,25,(640,480))
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
out.write(frame)
if cv2.waitKey(1) & 0xFF== ord('q'):
break
cap.release()
cv2.destroyAllWindows()
The video file is created but I am not able to run that file. I also tried with different formats like 'XVID','MJPG','H264' but faced the same issue.
My opencv version is 4.3.038

There are two issues, I would like to address:
Issue #1: DIVX should be declared as:
fourcc = cv2.VideoWriter_fourcc('D', 'I', 'V', 'X')
Issue #2:
You have declared to create the video with the size (640, 480). Therefore each frame you returned should be also (640, 480)
frame = cv2.resize(frame, (640, 480))
But if you use it with DIVX you will have a warning:
OpenCV: FFMPEG: tag 0x58564944/'DIVX' is not supported with codec id 12 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x7634706d/'mp4v'
Instead of DIVX use mp4v for creating .mp4 videos.
Code:
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
out = cv2.VideoWriter('output.mp4', fourcc, 25, (640, 480), isColor=True)
while True:
ret, frame = cap.read()
frame = cv2.resize(frame, (640, 480))
cv2.imshow('frame', frame)
out.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()

You can try this code. Works for me
import cv2
cap = cv2.VideoCapture(0)
video_speed = 15 #This frame rate works well on my case
video_name = 'output.avi'
writer = cv2.VideoWriter(video_name, cv2.VideoWriter_fourcc('M','J','P','G'),video_speed, (640,480))
while True:
ret , frame = cap.read()
if ret == True:
writer.writer(frame)
cv2.imshow('Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
writer.release()
cv2.destroyAllWindows()

Related

Simple OpenCV Test only shows first frame

this simple python script should show continually the webcam video but it only shows the first frame.
import cv2
print('cv2 version is ' + str(cv2.getVersionString()))
def capture_config(camera_port=0):
frame_height = 480
frame_width = 640
cap = cv2.VideoCapture(camera_port)
cap.set(3, frame_width)
cap.set(4, frame_height)
if not cap.isOpened():
print('Unable to read camera feed')
return False
return cap
cap = capture_config()
while cap:
ret, frame = cap.read()
cv2.imshow('captured frame', frame)
if cv2.waitKey(0) & 0xff == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
the output is
cv2 version is 4.5.1
It' on ubuntu 20.03 and python 3.8
any valid reason why the script would only show the first frame ?

OpenCV - Video doesn't play after saving in Python

I tried to save a video after it changes resolution to 300x300 on Python, but my video can't play after saving with
0xc10100be error: "This file isn't playable. That might be because the file type is unsupported, the file extension is incorrect, or the file is corrupt."
Here is my program:
import numpy as np
import cv2
cap = cv2.VideoCapture("F:\\mi\\Camera\\2b7d9eccaddffffe3c9ba70b7fe6c12e(0).mp4")
cv2.namedWindow("vid1", 0)
cv2.resizeWindow("vid1", 300,300)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (300,300))
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('vid1',frame)
out.write(frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
What is wrong?
after asking my teacher about this problem, He fixed my program:
import numpy as np
import cv2
cap = cv2.VideoCapture("F:\\mi\\Camera\\2b7d9eccaddffffe3c9ba70b7fe6c12e(0).mp4")
cv2.namedWindow('frame',0)
cv2.resizeWindow('frame',300,300)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (300,300))
while(cap.isOpened()):
ret, frame = cap.read()
if ret:
vidout=cv2.resize(frame,(300,300)) #create vidout funct. with res=300x300
out.write(vidout) #write frames of vidout function
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
Thanks for your attention about my question!
I appears to me that you're not doing anything with "out".
You should add:
out.write(frame)
inside the loop.

Why isn't the video feed read by the web camera in python code?

When I run my python opencv code, the web camera does not read a video feed. There are no any errors, but the black color output with wifi sign and loading sign i there. How to fix that and read the video feed. Here is my code and the output.
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480))
print(cap.isOpened())
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
out.write(frame)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
If you are only using one camera you can try the following:
cap = cv2.VideoCapture(-1)
This will pick up the first webcam the system can find.
How are you overlaying the wifi/loading signs - I assume they are not part of the webcam feed?

Opencv Writing video as output.avi from video file not from webcam

I am trying to write a video from video file it's not working but when I am using webcam code it's working
there are 2 files one is file.py and the other is web.py
here is my file.py
import numpy as np
import cv2
cap = cv2.VideoCapture('People - 6387.mp4')
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()
and this is my webcam.py this is working properly and writing video
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()`

Python OpenCV Opening Vid File vs. Opening Webcam

I cant figure out why this is not working.
The following code works perfectly using my webcam:
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==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
Yet when I exchange the Webcam for a video file, the output does not generate a video. Only a 5.7kb file named output.avi:
import numpy as np
import cv2
cap = cv2.VideoCapture('Input.avi')
# 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==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
I can see in my windows that the video is being processed but is not being saved. I have also tried changing the resolution to match the initial video file.
I'm using OpenCV on Ubuntu, and this worked for me:
out = cv2.VideoWriter("output.avi", cv.CV_FOURCC(*'DIVX'), fps, (640, 480))
See if it works on Windows.

Categories