OpenCV streamming ip camera always return False - python

So far I have used OpenCV for streamming ip camera from Raspberry pi + pi camera. I want to save the video from ip camera with codec H.264 and it didn't work. I find out from this post https://github.com/skvark/opencv-python/issues/100 which told me that only manually built opencv library would support H264 codec.
So i followed this link to manually build opencv https://www.learnopencv.com/install-opencv3-on-ubuntu/ and succeeded. But when I use manually built opencv, I can no longer access my ip camera, the cap.open() always return None. Here is my code:
import cv2
cap = cv2.VideoCapture("http://10.10.1.240:8081/")
while True:
ret, frame = cap.read()
frame2 = cv2.flip(frame, 1)
cv2.imshow("frame2", frame)
key = cv2.waitKey(25)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
In the above code, ret is always False.
I have been stucked in this for 2 days without a real solution and explaination. Any help would be appriciated, thanks!

But when I use manually built opencv, I can no longer access my ip camera, the cap.open() always returns none.
Have you tried, checking if your ip camera is working properly and present on your network after building opencv from source? Did you try to stream from the camera using any media player after installation off opencv from source?.
As far as your code is concerned, try the following format for cv2.VideoCapture
cap = cv2.VideoCapture()
cap.open("rtsp://yourusername:yourpassword#172.16.30.248:555/Streaming/channels/2/")
yourusername-username given to your ip camera.
yourpassword-password for the given user name.
You can try the following as well.
cv2.VideoCapture("rstp://admin:PASSWORD#192.168.1.64/doc/page/previw.asp")
Thanks.

Make sure the ip address of your computer and your camera are in the same subnet mask.

Related

Python OpenCV with Imaging device USB3 Vision compliant camera - VideoCapture() index of camera not found

I have a problem with using external camera in Python OpenCV.
I am using Windows.
In device manager, camera is not under "Cameras", but under "Imaging devices" and is called USB3 Vision compliant camera.
The camera is working fine on app given by company (UcamViewer).
I want to connect to this camera using cv2.VideoCapture().
I have of course tried:
cv2.VideoCapture(k)
for k = [0..100]
Only k = 0 is occupied and works - that is my webcam.
I have also tried adding multiple possible values to the second parameter of the VideoCapture(), which did not work.
how to get video from a "USB3 Vision" device?
This question suggested to use cv.CAP_XIMEA as the second parameter of VideoCapture(), but CAP_XIMEA does not seem to be a possible value.
If I can not fix this issue and use VideoCapture(), what would you suggest to use to capture the video and convert it to format that can be used by openCV methods?
Any ideas how could I solve this issue will be appreciated.
Thank you for your help.

How to select a different webcam using cv2 in python

I am trying to stream live video from an external camera using cv2. I was able to write the simple code to stitch the frames and stream it. But am struggling to find how to change the camera.
I tried to run it after disabling the main webcam from the task manager, but it still did not work.
So, if anyone can help me with some clue regarding the same, that would be a great help.
Cameras are numbered for Windows. You can try a few indeces and check which camera index belongs to the camera you want.
capture = cv2.VideoCapture(index)

How to access webcam stream by ID on Windows

I am working with two webcams, currently using Python and OpenCV. The system is set up in a way that one camera is on the right and one camera is on the left. For the system to work correctly, knowing which camera is in which position is required.
The following is a way to stream video from the two webcams at once:
camera1 = cv2.VideoCapture(0)
camera2 = cv2.VideoCapture(1)
However, this is not ideal for me since there is a possibility that the left and right camera would switch index if they are unplugged and re-plugged. Is there a way, through OpenCV or otherwise, to open a video stream from a camera based on some sort of camera ID?
For a follow up question, although it would not be ideal, I would be ok with having to always plug each camera into specific USB ports. Is there a way to stream a camera from a designated port? For the VideoCapture function I mentioned earlier, is the index based on USB port or is it arbitrary? If the index assignment is consistent (i.e. the lowest active USB port always gets assigned 0, etc.), then this could be a solution to my problem. Through my testing this seems to be the case, but I would like to be sure.

setting webcam camera parameter in opencv python

I am using Windows 7 64 bit operation system together with Python 3 and OpenCV. My computer is interfaced to two Logitech webcam of the following model:
1) Logitech HD Webcam C615
2) Logitech QuickCam Pro 9000
In Python I am running the below script
import cv2
cap1 = cv2.VideoCapture()
cap2 = cv2.VideoCapture()
cap1.set(cv2.CAP_PROP_AUTOFOCUS,0) # I want to change the focus manually myself
cap1.set(cv2.CAP_PROP_FOCUS,10)
cap2.set(cv2.CAP_PROP_FOCUS,10)
cap1.set(cv2.CAP_PROP_EXPOSURE,25)
cap2.set(cv2.CAP_PROP_EXPOSURE,25)
#get frame
ret, frame1 = cap1.read()
ret, frame2 = cap2.read()
#display result
cv2.imshow('cam 1',frame1)
cv2.imshow('cam 2',frame2)
The problem is, regardless on how I change the parameters in my cv2.set, I don't see the change being reflected in the captured image.
I then went to download Logitech's driver for the camera called Logitech Webcam Software. Using their software I can make the appropriate settings in both of the cameras. When closing the software, and restart python and running the script, the captured images appear exactly according to the settings that I have set in the Logitech Webcam Software.
My questions are:
1) Why can I not set the camera settings directly using Python and OpenCV?
2) How should I change my script in order to interface the camera settings direct from Python?
Thanks!

opencv+python+linux+webcam = cannot capture frames

I am trying to code up simple face detection in python using opencv. But unfortunately my opencv is refusing to detect my webcam. I am not sure how it works internally, as documentation is very limited, but CaptureFromCAM(-1) returns some object, but QueryFrame returns nones. When I try to use one of my two cameras for example in cheese, I get video without a problem.
capture = cv.CaptureFromCAM(-1)
faceCascade = cv.Load("haarcascade_frontalface_alt.xml")
while (cv.WaitKey(15)==-1):
img = cv.QueryFrame(capture)
if img != None:
image = DetectFace(img, faceCascade)
cv.ShowImage("face detection test", image)
cv.ReleaseCapture(capture)
Any ideas?
Ok, I have figured it out. Basically my openvc was compiled with v4l (video for linux) support.
When solving this problem you first need to make sure your camera is working with some other application using v4l. If that is the case then you can try to recompile openvc with v4l support. For gentoo (which uses portage) it is very simple:
sudo su
USE="v4l v4l2" emerge -av opencv
for other package managers either figure something out or compile from source with USE_V4L=ON.

Categories

Resources