Not able to play .h264 video on OpenCV? - python

I captured a standard video from camera of Raspberry pi. The codec of the file is h264.
To play the video I do:
import numpy as np
import cv2
cap = cv2.VideoCapture('foo.h264')
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()
Video starts But stops after sometime throwing this error:
Traceback (most recent call last):
File "play_video.py", line 9, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /home/nikhil/Downloads/opencv-2.4/modules/imgproc/src/color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cvtColor
My machine has Ubuntu 12.04. I played an .avi file it plays smoothly. Is the problem with the .h264 or with the OpenCV?

Your loop should check that frame is not-empty, and not that the video was opened successfully - this check should be done just once at the beginning.
When the frame after last is read frame is empty and you are then trying to convert it to gray.

Related

Why am I getting "cv2.error: Unknown C++ exception from OpenCV code" when I'm using cv2.BackgroundSubtractorKNN() in Python?

A simple program to show the feed from my webcam is running fine. I'm getting the error only when I try to run cv2.BackgroundSubtractorKNN() within the loop.
I have applied the following fix:
Uninstalled the latest version of OpenCV (which I was using) and installed an older version 4.5.4. But the error still persists.
Here's my code and the corresponding messages in the terminal.
import cv2
cap = cv2.VideoCapture(0)
mog = cv2.BackgroundSubtractorKNN()
while(True):
ret, frame = cap.read()
fgmask = mog.apply(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Message in terminal
PS D:\Python ground up\Open_CV> python -u "d:\Python ground up\Open_CV\backgroundsub.py"
Traceback (most recent call last):
File "d:\Python ground up\Open_CV\backgroundsub.py", line 7, in <module>
fgmask = mog.apply(frame)
cv2.error: Unknown C++ exception from OpenCV code
I think it's createBackgroundSubtractorKNN not BackgroundSubtractorKNN

Problem using OpenCV and Raspberry PI camera V2

I had been using the following code for the past two months with no problem whatsoever, however, just yesterday I have been getting errors using the code which I will detail as follows. The code in question is used to take photos of objects using the raspberry pi camera.
I initialize the raspberry pi camera v2 using the following code:
cap = cv2.VideoCapture('/dev/video0', cv2.CAP_V4L)
#set dimensions
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 2560)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1440)
After which I take a photo from the camera and write the image to a filename using the following lines of code:
ret, frame = cap.read()
cv2.imwrite(fileName , frame)
I have been getting the following error quite recently:
[ WARN:0#26.652] global /io/opencv/modules/videoio/src/cap_v4l.cpp (1000) tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.
Traceback (most recent call last):
File "/home/pi/Documents/proj_trash_can/main.py", line 55, in <module>
clickPicture(cam_capture, filename)
File "/home/pi/Documents/proj_trash_can/cam.py", line 22, in clickPicture
cv2.imwrite(fileName , frame)
cv2.error: OpenCV(4.5.5) /io/opencv/modules/imgcodecs/src/loadsave.cpp:801: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'
Try this. Worked for me Using Raspberry pi 4, Bullseye. You haven't mentioned what filename is?
import cv2
import numpy as np
cap = cv2.VideoCapture('/dev/video0', cv2.CAP_V4L)
#set dimensions
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 2560)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1440)
while cap.isOpened():
ret, frame = cap.read()
cv2.imwrite('Atharva_Naik.jpg' , frame)
cv2.imshow('Video', frame)
cv2.waitKey(0)
cap.release()
cv2.destroyAllWindows()

Python OpenCV Camera Error

For some reason, I cannot read camera video data using OpenCV and Python 3.
Here is the code I am using:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
_, frame = cap.read()
cv2.imshow('frame',frame)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
This code returns the following error:
Traceback (most recent call last):
File "C:\Path\To\File\VideoTest.py", line 10, in <module>
cv2.imshow('frame',frame)
cv2.error: D:\Build\OpenCV\opencv-3.3.1\modules\highgui\src\window.cpp:339: error: (-215) size.width>0 && size.height>0 in function cv::imshow
The computer is running Windows Server 2012 R2 and has one USB webcam permanently in use in addition to the new one I am trying to read data from. I have tried changing the line cap = cv2.VideoCapture(0) to cap = cv2.VideoCapture(1) and had an identical error.
I tried replicating the error on my laptop using the same code and USB webcam, but it worked perfectly and I was able to stream the video.
**EDIT**
To debug, I ran the following program line-by-line in the Python shell.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
print(cap.read())
cap = cv2.VideoCapture(1)
print(cap.read())
The code outputted (False, None) twice.

OpenCV Python Assertion failed error when playing .avi

I am just beginning to learn OpenCV and am working through the tutorials here: https://opencv24-python-tutorials.readthedocs.io/en/stable/py_tutorials/py_gui/py_video_display/py_video_display.html
This is the code I've written to play the sample video.
import numpy as np
import cv2
cap = cv2.VideoCapture('samples/vtest.avi')
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()
This is the error message I get:
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /tmp/opencv-20161020-7399-1yrk4nm/opencv-2.4.13.1/modules/imgproc/src/color.cpp, line 3739
Traceback (most recent call last):
File "vidFile_tutorial.py", line 14, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /tmp/opencv-20161020-7399-1yrk4nm/opencv-2.4.13.1/modules/imgproc/src/color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cvtColor
I'm unsure that the problem is an assertion fail error since I've chosen the file as specified by the tutorial. Moreover, when I attempt to play vtest.avi manually I encounter a codec error with QuickTime. Therefore, I suspect it's video codec issues with OpenCV. But I'm unsure if the diagnosis is correct and how I would go about fixing it.
Thanks in advance.
Okay,
So I've managed to resolve the issue by using OpenCV 3.1.0 rather than 2.4.13.1.
With OpenCV 3.1.0 everything works fine.

Trouble with raspberry pi and OpenCV

I have a project in raspberry pi and I am using python. However I have a problem with the OpenCV when I am trying to run 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()`
I get this error:
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/opencv-3.1.0/modules/imgproc/src/color.cpp, line 8000
Traceback (most recent call last):
File "test.py", line 11, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /home/pi/opencv-3.1.0/modules/imgproc/src/color.cpp:8000: error: (-215) scn == 3 || scn == 4 in function cvtColor"
I have Python 3.4.2,OpenCV 3.1.0 and Numpy 1.8.2.
So I found an answer. All I had to do was run this code on my raspberry pi:
sudo modprobe bcm2835-v4l2
Thank you for all the help.

Categories

Resources