Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
python-opencv ip camera example
import base64
import time
import urllib2
import cv2
import numpy as np
"""
Examples of objects for image frame aquisition from both IP and
physically connected cameras
Requires:
- opencv (cv2 bindings)
- numpy
"""
class ipCamera(object):
def __init__(self, url, user=None, password=None):
self.url = url
auth_encoded = base64.encodestring('%s:%s' % (user, password))[:-1]
self.req = urllib2.Request(self.url)
self.req.add_header('Authorization', 'Basic %s' % auth_encoded)
def get_frame(self):
response = urllib2.urlopen(self.req)
img_array = np.asarray(bytearray(response.read()), dtype=np.uint8)
frame = cv2.imdecode(img_array, 1)
return frame
class Camera(object):
def __init__(self, camera=0):
self.cam = cv2.VideoCapture(camera)
if not self.cam:
raise Exception("Camera not accessible")
self.shape = self.get_frame().shape
def get_frame(self):
_, frame = self.cam.read()
return frame
@tmkasun

This comment has been minimized.

Show comment Hide comment
@tmkasun

tmkasun Oct 9, 2013

thanks

tmkasun commented Oct 9, 2013

thanks

@raurey

This comment has been minimized.

Show comment Hide comment
@raurey

raurey Oct 1, 2015

any brand?? i'm going to work with a foscam IP cam....

raurey commented Oct 1, 2015

any brand?? i'm going to work with a foscam IP cam....

@haaku

This comment has been minimized.

Show comment Hide comment
@haaku

haaku Nov 26, 2015

Got error
error: [Errno 10054] An existing connection was forcibly closed by the remote host

haaku commented Nov 26, 2015

Got error
error: [Errno 10054] An existing connection was forcibly closed by the remote host

@baijuep

This comment has been minimized.

Show comment Hide comment
@baijuep

baijuep Mar 13, 2016

I tried the above code and gets error as
File "z-live_feed.py", line 14
def init(self, 192.168.1.103, user=admin, password=1234QWER):
^
IndentationError: expected an indented block
I had edited these lines
class ipCamera(object):
def init(self, 192.168.1.103, user=admin, password=1234QWER):
self.url = url

baijuep commented Mar 13, 2016

I tried the above code and gets error as
File "z-live_feed.py", line 14
def init(self, 192.168.1.103, user=admin, password=1234QWER):
^
IndentationError: expected an indented block
I had edited these lines
class ipCamera(object):
def init(self, 192.168.1.103, user=admin, password=1234QWER):
self.url = url

@danielAlbuquerque

This comment has been minimized.

Show comment Hide comment
@danielAlbuquerque

danielAlbuquerque May 11, 2016

@baijuep, you need to write URI inside quotes, like that: 'http://192.168.1.103'

danielAlbuquerque commented May 11, 2016

@baijuep, you need to write URI inside quotes, like that: 'http://192.168.1.103'

@villamarinella

This comment has been minimized.

Show comment Hide comment
@villamarinella

villamarinella Jul 1, 2016

How can it run?
This is only def, no main.
I am not same smart like you

How can it run?
This is only def, no main.
I am not same smart like you

@Bhulok

This comment has been minimized.

Show comment Hide comment
@Bhulok

Bhulok Aug 21, 2016

what is this?
Process finished with exit code 0

Bhulok commented Aug 21, 2016

what is this?
Process finished with exit code 0

@ekangBoyong

This comment has been minimized.

Show comment Hide comment
@ekangBoyong

ekangBoyong Mar 14, 2017

goodday new to this one. how can use this example. thanks

goodday new to this one. how can use this example. thanks

@neeraj55206

This comment has been minimized.

Show comment Hide comment
@neeraj55206

neeraj55206 Oct 17, 2017

how to use this code ?

how to use this code ?

@iareizagau

This comment has been minimized.

Show comment Hide comment
@iareizagau

iareizagau Mar 2, 2018

How can I use this code?
I need to connect to IP cameras using user and password. Thanks

How can I use this code?
I need to connect to IP cameras using user and password. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment