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.
Related
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
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.
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.
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.
I'm using ubuntu 13.10 along with opencv 2.4.9 with python 2.7.
I have written the following code but it seems to fails on runtime.
import cv2
c1=cv2.VideoCapture(2) #camera id
c2=cv2.VideoCapture(1) #camera id
while(True):
ret,frame = c1.read()
ret,frame2 = c2.read()
frame = cv2.cvtColor(frame,0)
frame2 = cv2.cvtColor(frame2,0)
cv2.imshow('frame',frame)
cv2.imshow('frame2',frame2)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
c1.release()
c2.release()
cv2.destroyAllWindows()
But on running this in Ubuntu i get the following error :
VIDIOC_QUERYMENU: Invalid argument
libv4l2: error turning on stream: Invalid argument
VIDIOC_STREAMON: Invalid argument
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file
/build/buildd/opencv-2.4.5+dfsg/modules/imgproc/src/color.cpp, line 3358
Traceback (most recent call last):
File "/home/bini/KV/IP_Proj/webcam basics opencv.py", line 8, in <module>
frame2 = cv2.cvtColor(frame2,0)
cv2.error: /build/buildd/opencv-2.4.5+dfsg/modules/imgproc/src/color.cpp:3358: error:
(-215) scn == 3 || scn == 4 in function cvtColor
The same code used to work fine on windows.
Can someone help me please as to why is this occuring..??? i have on idea.
Thanks in advance
For linux,if you're using 1 camera , at first you must change your camera id to 0 , but it seems that you want use 2 camera , this is because the resolution, framerate and protocol used by your cameras overloads the USB connection so read this link! also this is a base code for connect and use linux webcam:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
#set the width and height, and UNSUCCESSFULLY set the exposure time
cap.set(3,1080)
cap.set(4,1024)
cap.set(15, 0.1)
while True:
ret, img = cap.read()
cv2.imshow("input",img)
key = cv2.waitKey(10)
if key == 27:
break
cv2.destroyAllWindows()
cv2.VideoCapture(0).release()
One (or both) of your cameras isn't initializing correctly. I just ran your code on my machine (Ubuntu 14.04) and both frames come up showing live views from my two connected cameras. Can you view both cameras in cheese or guvcview?
I do get a bunch of these messages:
VIDIOC_QUERYMENU: Invalid argument
errors but neither of these:
libv4l2: error turning on stream: Invalid argument
VIDIOC_STREAMON: Invalid argument