Skip to content

Instantly share code, notes, and snippets.

@thearn
Last active January 12, 2023 17:01
Show Gist options
  • Select an option

  • Save thearn/5562029 to your computer and use it in GitHub Desktop.

Select an option

Save thearn/5562029 to your computer and use it in GitHub Desktop.

Revisions

  1. thearn revised this gist May 12, 2013. 1 changed file with 15 additions and 10 deletions.
    25 changes: 15 additions & 10 deletions opencv-python-ipcam.py
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,13 @@
    import cv2, time
    import urllib2, base64
    import base64
    import time
    import urllib2

    import cv2
    import numpy as np


    """
    Examples of objects for image frame aquisition from both IP and
    Examples of objects for image frame aquisition from both IP and
    physically connected cameras
    Requires:
    @@ -14,28 +18,29 @@

    class ipCamera(object):

    def __init__(self,url, user = None, password = None):
    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):
    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
    _, frame = self.cam.read()
    return frame
  2. thearn revised this gist May 12, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions opencv-python-ipcam.py
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,10 @@
    """
    Examples of objects for image frame aquisition from both IP and
    physically connected cameras
    Requires:
    - opencv (cv2 bindings)
    - numpy
    """


  3. thearn created this gist May 12, 2013.
    37 changes: 37 additions & 0 deletions opencv-python-ipcam.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    import cv2, time
    import urllib2, base64
    import numpy as np

    """
    Examples of objects for image frame aquisition from both IP and
    physically connected cameras
    """


    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