Connecting webcam through ip gives error "Cannot capture source" - python

I have been trying to use my smartphone camera with YOLO v3 to get better frames but an error keeps on coming "Cannot capture source". I am using the IP Webcam app on my android phone.
The IP Webcam app works by connecting devices using a local network. I tried uninstalling OpenCV and then reinstalled it as it was suggested somewhere on the internet but nothing happened.
{ installation: pip install opencv-python}
I am posting a small block of code that keeps on showing an error. The whole file or code can be accessed at https://github.com/ayooshkathuria/pytorch-yolo-v3/blob/master/cam_demo.py
'''
if CUDA:
model.cuda()
model.eval()
videofile = 'video.avi'
cap = cv2.VideoCapture('http://192.168.43.1:8080/browserfs.html')
assert cap.isOpened(), 'Cannot capture source'
frames = 0
start = time.time()
while cap.isOpened():
ret, frame = cap.read()
'''
I want to use my smartphone camera as a webcam because my system lacks it.
ERROR :
assert cap.isOpened(), 'Cannot capture source'
AssertionError: Cannot capture source

Related

How to connect video stream from python to Kurento Media Server

I am working on a Real Time Video Streaming project using RTMP protocol. I have to use DirectX to capture the screen and then Kurento Media Server to stream.
For capturing, I am using dxcam in python:
import dxcam
import cv2
# import time
# camera = dxcam.create() # returns a DXCamera instance on primary monitor
target_fps = 30
camera = dxcam.create(output_idx=0, output_color="BGR")
camera.start(target_fps=target_fps, video_mode=True)
writer = cv2.VideoWriter(
"video.mp4", cv2.VideoWriter_fourcc(*"mp4v"), target_fps, (1920, 1080)
)
for i in range(1000):
writer.write(camera.get_latest_frame())
# time.sleep(10)
camera.stop()
writer.release()
del camera
I need help starting with the Kurento Media server to stream the video captured in real time but I can't find any tutorial to do that. Can someone help me with that?

Get Live Stream from a Camera connected via usb using python

I want to grab live images from a camera which is connected via USB with my computer.
I am using an Industrial Camera with usb port.
At device manager the camera is shown with its name and id so I think it is connected to PC.
I ran a 'findcam' program but it is not showing any existance of camera
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('Live Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
The given code which i tried is running for webcam on my laptop but when the same code i run on my PC with an external camera.
it constantly showing an error.
The Error:
Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/industrialcamera/ICvideocapture.py", line 11, in <module>
cv2.imshow('Live Video', frame)
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:352: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
I tried changing Index -1 , 0 , 1 but the error is constant
please help to find, weather it is my PC problem or camera problem or is their is any other way to stream(in python)
Thank You
I started your code on my PC and it works fine. Try to set the camera resolution manually, if you have an error with size.width and size.height, something like this:
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,920)
You may have installed the wrong version of OpenCV which doesn't support video. Uninstall everything of opencv and then run:
pip install opencv-python
(Source: cv2.VideoCapture.open() always returns FALSE)
Try to install opencv-contrib-python via pip: pip install opencv-contrib-python or maybe try to upgrade the version of it : pip install opencv-python --upgrade
you can debugg by checking the ret value :
import cv2
cap = cv2.VideoCapture(0)
counter = 0
while True:.
ret, frame = cap.read()
if ret :
cv2.imshow('Live Video', frame)
print(f" frame {counter} : ok ")
if not ret :
print(f" frame {counter} : ...")
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

Problem streaming from IP camera with python openCV

Before asking this question I searched the site for a similar problem for the last 2 days but I couldn't find the one specific to me.
I have an IP camera whose IP address, username, etc. I have been given full access to. I can actually open the stream and watch it live by writing the IP into VLC Player >> Open Network Stream >> Network.
But what I want is to be able to watch the same live stream with python. Here's my code:
import urllib.request
import cv2
import numpy
url = 'rtsp://10.10.111.200/profile2/media.smp'
while True:
resp = urllib.request.urlopen(url)
b_array = bytearray(resp.read())
img_np = numpy.array(b_array, dtype=numpy.uint8)
img = cv2.imdecode(img_np, -1)
cv2.imshow('test', img)
if cv2.waitkey(10) == ord('q'):
exit(0)
When I run this code, it gives me the following error:
urllib.error.URLError: .
Then I figured that maybe I should change rtsp to http in url, but when I do, it gives me the following error,
cv2.error: OpenCV(3.4.3)
D:\Build\OpenCV\opencv-3.4.3\modules\imgcodecs\src\loadsave.cpp:737:
error: (-215:Assertion failed) !buf.empty() && buf.isContinuous() in
function 'cv::imdecode_' in the line img = cv2.imdecode(img_np,
-1)
which I think is because there's no data coming from the (very likely wrong since I changed to http) source.
I'm on Windows 10 64bit.
The library you are using to read the data stream does not support the rtsp protocol so it will never work, maybe you can use the following instead:
import cv2
capture_video = cv2.VideoCapture('rtsp://10.10.111.200/profile2/media.smp')
while(True):
ret, img = capture_video.read()
cv2.imshow('Test', img)
if cv2.waitKey(10) == ord('q'):
exit(0)

how to run webcam through python on rpi3

I am trying to use a MS lifecam with my raspberry-pi-3. It works on the command line, when I type the following command:
$ fswebcam img.jpg
Trying source module v4l2...
/dev/video0 opened.
...
Writing JPEG image to 'img.jpg' # this works fine
Now I want to run the camera through a python code:
import pygame
import pygame.camera
from pygame.locals import *
DEVICE = '/dev/video0'
SIZE = (640, 480) # I also tried with img size (384,288), same error
FILENAME = 'capture.jpg'
pygame.init()
pygame.camera.init()
camera = pygame.camera.Camera(DEVICE, SIZE)
camera.start() # error on executing this line
pygame.image.save(screen, FILENAME)
camera.stop()
The reported error is:
SystemError: ioctl(VIDIOC_S_FMT) failure: no supported formats
I am puzzled here. The camera is supported by rasp-pi, so it looks like my python code has to be updated somewhere. Can you help?
Try use this:
camera = pygame.camera.Camera(pygame.camera.list_cameras()[0])
camera.start()
img = camera.get_image()
pygame.image.save(img, FILENAME)
Had problem and once I stopped a process using the video stream the error was resolved.
details
I had the same problem. And while
/dev/video0
was listed, camera.start() resulted in the same error.
I had ran
sudo motion
earlier. so I verified the service was running, stopped it, then tried pygame. and it worked.
sudo service --status-all
sudo service motion stop
you can also use this :
import cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened():
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
cv2.destroyWindow("preview")

I need hikvision camera which has ip 20.0.0.14 and user name/password is admin/12345 to run by python code

I need hikvision camera which has ip 20.0.0.14 and user name/password is admin/12345 to run by python code
the original camera code is
import cv2.cv as cv
import time
cv.NamedWindow("camera", 1)
capture = cv.CaptureFromCAM(0)
while True:
img = cv.QueryFrame(capture)
cv.ShowImage("camera", img)
if cv.WaitKey(10) == 27:
break
cv.DestroyAllWindows()
i need help please
Here's the solution when using OpenCV3. In your sample code, you are not only not using the OpenCV2 interface, but you are accessing the very old cv (prior to OpenCV 2) interface. So my first suggestion is to get a current install of OpenCV working.
Possible source of rtsp urls for use with hikvision cameras:
https://www.ispyconnect.com/man.aspx?n=Hikvision
import cv2
# Note the following is the typical rtsp url for streaming from an ip cam
# source = "rtsp://user:password#ipaddress:port/<camera specific stuff>"
# Each manufacturer is different. For my alibi cameras, this would be
# a valid url to use with the info you provided.
source = "rtsp://admin:12345#20.0.0.14//Streaming/Channels/2"
cap = cv2.VideoCapture(source)
ok_flag = True
while ok_flag:
(ok_flag, img) = cap.read()
if not ok_flag: break
cv2.imshow("some window", img)
if cv2.waitKey(10) == 27:
break
cv2.destroyAllWindows()
Also note that this code works the same if the source is the path to a valid video file (like an .avi), or for a web camera (in which case you pass the integer number of the webcam, like 0).
Another error in your post is the cv.CaptureFromCAM(0), which would be capturing from the first webcam installed on the computer, not an ip stream.

Categories

Resources