Skip to content

Instantly share code, notes, and snippets.

@ma-he-sh
Forked from thearn/opencv-python-ipcam.py
Created July 27, 2018 22:54
Show Gist options
  • Save ma-he-sh/852d27bfd54cc708ebb3d07e3b252983 to your computer and use it in GitHub Desktop.
Save ma-he-sh/852d27bfd54cc708ebb3d07e3b252983 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment