I am testing an opencv video capture code on my BBB (running Debian Wheezy: Linux beaglebone 3.8.13-bone79 #1 SMP Tue Oct 13 20:44:55 UTC 2015 armv7l GNU/Linux) but getting a select timeout error everytime i run the program. Now i am behind schedule of my final year Iris recognition system Project. (In case you want to know why i am going through this video capture code, it is for tracking the eyes then capture an image that will be processed to obtain an IrisCode.)
I installed opencv 3.0.0 following instructions on http://www.pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/#comment-393561 , but had to turn off ffmpeg (suggested on one of the forums: OpenCV 3.0.0 make error with FFMPEG ) after obtaining a compilation errors but firstly i had compiled ffmpeg from source.
Below is the Video_Capturing code i tested with;
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()
After running the code i am obtaining the following information (Note: The frame is only opening when i run the python script in LXTerminal accessed through tightvnc. It is failing to run through ssh);
root#beaglebone:~/Project/Testing# python Video_Streaming.py
(process:1488): Gtk-WARNING **: Locale not supported by C library.
Using the fallback 'C' locale.
Xlib: extension "RANDR" missing on display ":1".
select timeout
select timeout
select timeout
select timeout
^Cselect timeout
Traceback (most recent call last):
File "Video_Streaming.py", line 8, in <module>
ret, frame = cap.read()
KeyboardInterrupt
root#beaglebone:~/Project/Testing#
I'm stuck i need your help.
Related
I am an "advanced beginner" in Python, but a relative newbie with the Raspberry Pi...
What I'm trying to do:
I'm trying to capture a frame from the RTSP stream from a Wyze Cam V2 and save the image to a file. My code works - most of the time. But sometimes it fails for long periods of time. After much experimentation and trial and error I have determined that it is more likely to fail when the camera is in the dark! This seems very consistent.
My Code:
This is not the code from my actual project - it is the code I have been using to troubleshoot.
import cv2
import imageio
class Camera:
def __init__(self, ipaddress):
self.ipaddress = ipaddress
print("About to create VideoStream")
self.vs = cv2.VideoCapture(ipaddress, cv2.CAP_FFMPEG)
self.vs.set(cv2.CAP_PROP_BUFFERSIZE, 3)
if self.vs.isOpened():
print("Successfully created")
self.vs.release()
else:
print("Unable to create")
def capture(self):
self.vs.open(self.ipaddress)
success, frame = self.vs.read()
self.vs.release()
if success:
print("Capture Success")
return frame
else:
print("Failed to capture")
print("VideoCapture isOpen is " + str(self.vs.isOpened()))
return None
def is_opened(self):
return self.vs.isOpened()
# In actual code CAMNAME is the camera's name, PASSWORD is the password
# and XXX.XXX.X.XXX is the ip address
camera = Camera("rtsp://CAMNAME:PASSWORD#XXX.XXX.X.XXX/live")
leave = False
while not leave:
frame = camera.capture()
if frame is None:
print("Frame is none")
print("VideoCapture isOpen is " + str(camera.is_opened()))
else:
print("Successful capture - writing to file")
frame_color = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
imageio.imwrite("test.jpg", frame_color)
response = input("Capture again? ")
if len(response) == 0:
response = "y"
if response[0] == 'n':
leave = True
What I Have Tried:
The code works fine when run on Windows 10. So not directly a problem with the code. In fact, when the Pi is having trouble capturing, doing it at the same time on Windows works. So definitely the issue is the Pi interacting with OpenCV or the camera.
Since I had ++ trouble installing OpenCV:
I have tried re-installing it on a fresh install of the OS with PIP (sudo pip install opencv-contrib-python==4.1.0.25).
I tried to build OpenCV from scratch - took 2.5 days and failed miserably - likely I screwed up somewhere in the process, but don't feel like spending another 2 days doing this.
Finally I downloaded a Raspbian image with OpenCV pre-compiled (https://medium.com/#aadeshshah/pre-installed-and-pre-configured-raspbian-with-opencv-4-1-0-for-raspberry-pi-3-model-b-b-9c307b9a993a). All these install methods resulted in the same issues...
I have tried opening the VideoCapture without specifying cv2.CAP_FFMPEG. I feel like it was more reliable with this option.
I have tried leaving out the change in BUFFERSIZE. I'm not sure this line of code has any effect.
What I am Using:
Raspberry Pi Model B, Rev 2, 512 kb
Raspbian Stretch - though I have had the same issues with Buster.
Wyze Cam II "beta" firmware that provides RTSP support.
Python3
OpenCV 4.1.0 (cv2.version)
What happens:
I have been troubleshooting this intermittent problem for some time, and just today realized it always works with the garage (where the camera is located) is light, and fails when it is dark. (Which made the late night troubleshooting sessions so frustrating!)
I have had many problems in the past, but now the issue seems to be that if the garage (where the camera is located) is dark, the VideoCapture object will not be created (.isOpened() == False) or the read() method will return False, None.
I used to have a problem with read() returning an old image. I can tell it is old because the camera timestamps the captures. This is why I am always opening and closing the VideoCapture - I would rather it not return an image than return the wrong/old image.
In the past, with slightly different settings, I would get warnings on the screen either during the creation of the VideoCapture object, or during the read() command. These are usually along the lines of "[h264 # 0x1ea1780] error while decoding MB 78 67, bytestream -15". I have gotten different warnings but I don't have examples right now. If I get a warning, I often get a bad image.
I have also gotten images that are distorted - that bottom of the image (sometimes a few lines, sometimes more than half of the image) looks like it it is the same line of data over and over.
I am currently trying to do some video classification and am using anaconda along with jupyter notebook to do my training of data. However, I am encountering an error in jupyter notebook where I can't read in my video frames using cv2.VideoCapture but somehow it does work in my conda environment's terminal.
This is my file structure,
This is the error I'm currently encountering,
Terminal in the same anaconda environment works fine,
I did read somewhere that it might be due to an issue with conda and ffmepg but I have tried many solutions suggested by others to solve that issue including downloading opencv from opencv.org itself and setting the environment path variables instead of using conda install but it still doesn't work.
Does anyone have any idea on how to solve this issue?
Forgive me if I'm wrong, but I noticed that you are not using the same filename in your two tests. I was stuck at the same point, till I realized the path and filename was not the same between my "terminal test" and the jupyter notebook test.
I confirmed jupyter could access the file.
Windows attribute test:
!attrib data/TownCentreXVID.avi
Bash file test. See using bash commands in jupyter notebook for details:
!file data/TownCentreXVID.avi
Then tried again, and had no issues getting the same results from jupyter.
OpenCV in Python enables you to grab frames from the webcam/ or from a video file (like in your case) as Numpy array, modify it and then display it using OpenCV's cv2.imshow(). To do this OpenCV will create a window and push the frames there. However, this will not work in a IPython notebook.
To display in the jupyter notebook or any other IPython notebook, you will have to use the function
IPython.display.Image(data)
and not OpenCV's imshow().
Here is a chunk of code you can use:
cam = cv2.VideoCapture(0)
d = IPython.display.display("", display_id=1)
d2 = IPython.display.display("", display_id=2)
while True:
try:
t1 = time.time()
frame = get_frame(cam)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
im = array_to_image(frame)
d.update(im)
t2 = time.time()
s = f"""{int(1/(t2-t1))} FPS"""
d2.update( IPython.display.HTML(s) )
except KeyboardInterrupt:
print()
cam.release()
IPython.display.clear_output()
print ("Stream stopped")
break
def get_frame(cam):
# Capture frame-by-frame
ret, frame = cam.read()
#flip image for natural viewing
frame = cv2.flip(frame, 1)
return frame
#Use 'jpeg' instead of 'png' (~5 times faster)
def array_to_image(a, fmt='jpeg'):
#Create binary stream object
f = BytesIO()
#Convert array to binary stream object
PIL.Image.fromarray(a).save(f, fmt)
return IPython.display.Image(data=f.getvalue())
I am trying to use epiphan SDI2USB with ubuntu 18.04. I downloaded the correct driver from their support site and the driver is correctly installed. My kernel version is 4.15.0.
If I try to run vlc and i go to media menu --> Open Capture Device --> video device name --> Here I see both my /dev/video0 (integrated camera) and /dev/video1 (epiphan video) When I select /dev/video1 and click play, I can see it works fine.
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()
However when I write a simple python program from internet (i changed source from 0 to 1) and run it, I get an error
compass-admin#Alienware-13-R3:~$ sudo python3 cam.py
Insufficient buffer memory on /dev/video1 – decreasing buffers
Insufficient buffer memory on /dev/video1 – decreasing buffers
mmap: Invalid argument
munmap: Invalid argument
VIDEOIO ERROR: V4L: can’t open camera by index 1
munmap: Invalid argument
Traceback (most recent call last):
File “/home/compass-admin/.local/lib/python3.6/site-packages/numpy/lib/shape_base.py”, line 843, in split
len(indices_or_sections)
TypeError: object of type ‘int’ has no len()
I dont know if it is because of permissions or if you have to use a specific function in opencv. No matter what I do, I get the same error.
I changed permissions of /dev/video1 to 777, I also added the current user to video group but no luck. I also tried video source in opencv as 0, 1, -1 but no luck. Any help please.
I use WinPython to write my python programs. I need to solve the task of detecting faces in a video stream. I have installed opencv-python to WinPython using this command:
pip install opencv-python==3.4.0.12
When I run the following code, I get a False:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
ret, img = cap.read()
print(ret)
What I am doing wrong?
Seems like legit function result. As you can see, from documentation VideoCapture::read function returns retval and image, in case there was image to return. Apparently, "False" value of the ret variable in your code means that there was no image.
Edit:
I looked up documentation and here's what i've found:
"If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer."
Hi I'm new to opencv(version 2.4.7) and using it in python 2.7.4. I always get this error
HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP
whenever I use the command
cam = cv2.VideoCapture(cam_id)
The code works fine otherwise even with the error. I'm trying to use this wireless camera and it shows an image which has a magenta and green colored grid structure. My question is why am I getting the error and this weird image. The code gives nice image on other system also on my system itself. gstreamer-properties also have clear picture. The code:
from cv2 import cv
import cv2
import sys
def main():
cam_id = 0
# parameter
for i, arg in enumerate( sys.argv ):
if i == 0: continue
else: cam_id = arg
cam = cv2.VideoCapture(cam_id)
cv2.namedWindow("window", cv.CV_WINDOW_AUTOSIZE)
running = True
while running:
try:
flag, img = cam.read()
if flag:
cv2.imshow("window", img)
cv2.waitKey(30)
except KeyboardInterrupt:
running = False
cv2.destroyWindow("window")
main()
Sorry to update so late, I had figured out solution of the issue long ago but forgot to answer it here. It required loading a library before running the code. Use of following commands should do the trick.
For 32bit system:
$ LD_PRELOAD=/usr/lib/i386-linux-gnu/libv4l/v4l2convert.so python filename.py
For 64bit system:
$ LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libv4l/v4l2convert.so python filename.py
If this doesn't work then try locating the file v4l2convert.so by using command,
$ locate v4l2convert.so
As the output you'll see different paths, now try LD_PRELOAD with different paths.