Camera

Copyright 2017 Purdue University

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Provide classes to deal with different types of cameras.

This module is used to deal with different types of cameras. The module provides the Camera base class which provides a uniform way of dealing with all types of cameras. The module provides different subclasses, each for a different type of cameras (e.g. IP cameras, and non-IP cameras). The module also provides the StreamFormat Enum for the different camera stream formats.

Examples

Example 1: To get frames from a non-IP camera image stream: 1. Initialize a NonIPCamera object using the ID and the URL of the camera image stream. 2. Use the get_frame method to get the most recent frame at any point of time, as well as the frame size. There is no need to call open_stream or close_stream.

::
camera = NonIPCamera(1, ‘http://images.webcams.travel/preview/1169307993.jpg’) frame, frame_size = camera.get_frame() cv2.imshow(‘frame’, frame) print frame_size cv2.waitKey()

Example 2: To get frames from an IP camera image stream: 1. Initialize an IPCamera object using the ID, IP, image stream path, and other optional parameters. 2. Use the get_frame method to get the most recent frame at any point of time, as well as the frame size. While dealing with image streams of IP cameras, there is no need to call open_stream or close_stream.

::
camera = IPCamera(1, ‘128.10.29.33’, ‘/axis-cgi/jpg/image.cgi’) frame, frame_size = camera.get_frame() cv2.imshow(‘frame’, frame) print frame_size cv2.waitKey()

Example 3: To get frames from an IP camera MJPEG stream: 1. Initialize an IPCamera object using the ID, IP, image stream path, MJPEG stream path, and other optional parameters. 2. Open the camera MJPEG stream by calling the open_stream method with the StreamFormat.MJPEG parameter. 3. Use the get_frame method to get the most recent frame at any point of time, as well as the frame size. 4. At the end when no more frames are needed, Close the camera MJPEG stream by calling the close_stream method.

::
camera = IPCamera(1, ‘128.10.29.33’, ‘/axis-cgi/jpg/image.cgi’,
‘/axis-cgi/mjpg/video.cgi’)

camera.open_stream(StreamFormat.MJPEG) t = time.time() while time.time() - t < 5:

frame, frame_size = camera.get_frame() cv2.imshow(‘frame’, frame) print frame_size cv2.waitKey(30)

camera.close_stream()

class CAM2ImageArchiver.camera.StreamFormat[source]

Bases: object

Represent an Enum for the different camera stream formats.

IMAGE

int – The constant class variable representing image streams.

MJPEG

int – The constant class variable representing MJPEG streams.

IMAGE = 1
MJPEG = 2
class CAM2ImageArchiver.camera.Camera(_id)[source]

Bases: object

Represent the base class for all types of cameras.

Parameters:id (int) – The unique camera ID.
id

int – The unique camera ID.

parser

StreamParser – The parser of the camera stream.

Notes

A camera handles a single stream at a time. Use the open_stream method to open the stream of the desired format (e.g. MJPEG). Then, use the method get_frame to get frames from the currently open stream. Then, use the method close_stream to close the currently open stream. This class does not allow dealing with multiple streams from the same camera object at the same time.

open_stream(stream_format)[source]

Open the camera stream of the given format.

Parameters:stream_format (int) – The stream format of the camera. This can be any of the StreamFormat class variables (e.g. StreamFormat.IMAGE or StreamFormat.MJPEG)
Raises:error.UnreachableCameraError – If the camera is unreachable.
close_stream()[source]

Close the currently open camera stream.

restart_stream()[source]

Restart the currently open camera stream.

This method restarts the stream by closing then opening it. This is useful because some cameras closes a stream if it is open for a long period of time.

get_frame()[source]

Get the most recent frame from the currently open camera stream.

Returns:

  • numpy.ndarray – The downloaded frame.
  • int – The size of the downloaded frame in bytes.

Raises:
  • error.CorruptedFrameError – If the frame is corrupted.
  • error.UnreachableCameraError – If the camera is unreachable.
  • error.ClosedStreamError – If the stream needs to be opened first.
class CAM2ImageArchiver.camera.IPCamera(_id, ip, image_path, mjpeg_path=None, port=None)[source]

Bases: CAM2ImageArchiver.camera.Camera

Represent an IP camera.

This class subclasses the Camera class and inherits its attributes and extends its constructor.

Parameters:
  • id (int) – The unique camera id.
  • ip (str) – The IP address of the camera.
  • image_path (str) – The path to the camera image stream.
  • mjpeg_path (str, optional) – The path to the camera MJPEG stream.
  • port (int, optional) – The port of the camera.
ip

str – The IP address of the camera.

image_path

str – The path to the camera image stream.

mjpeg_path

str – The path to the camera MJPEG stream.

port

int – The port of the camera.

Notes

By default, the constructor of this class initializes an ImageStreamParser so that frames can be retrieved from the image stream without the need to call the open_stream method.

open_stream(stream_format)[source]

Open the camera stream of the given format.

Parameters:

stream_format (int) – The stream format of the camera. This can be any of the StreamFormat class variables (e.g. StreamFormat.IMAGE or StreamFormat.MJPEG)

Raises:
  • ValueError – If the value of stream_format is invalid.
  • error.UnreachableCameraError – If the camera is unreachable.
close_stream()[source]

Close the currently open camera stream.

Notes

After closing the currently open camera stream, this method initializes an ImageStreamParser so that frames can be retrieved from the image stream without the need to call the open_stream method.

get_url(stream_format=1)[source]

Get the URL to the camera stream of the given format.

Parameters:stream_format (int, optional) – The stream format of the camera. This can be any of the StreamFormat class variables (e.g. StreamFormat.IMAGE or StreamFormat.MJPEG)
Returns:url – The URL to the camera stream of the given format.
Return type:str
Raises:ValueError – If the value of stream_format is invalid.
class CAM2ImageArchiver.camera.NonIPCamera(_id, url)[source]

Bases: CAM2ImageArchiver.camera.Camera

Represent a non-IP camera.

This class represents a camera whose IP is not known. A web server hides the information about the camera, and provides only a URL to get the most recent frame from the camera. This class subclasses the Camera class and inherits its attributes and extends its constructor.

Parameters:
  • id (int) – The unique camera ID.
  • url (str) – The URL that is used to get the most recent frame from the camera.
url

str – The URL that is used to get the most recent frame from the camera.

Notes

By default, the constructor of this class initializes an ImageStreamParser so that frames can be retrieved from the image stream without the need to call the open_stream method.

get_url()[source]
class CAM2ImageArchiver.camera.StreamCamera(_id, url)[source]

Bases: CAM2ImageArchiver.camera.Camera

Represent a Stream camera similar to a non-ip camera, but faster frame rates.

This class represents a camera whose IP is not known. A web server hides the information about the camera, and provides only a URL to get the most recent frame from the camera. The This class subclasses the Camera class and inherits its attributes and extends its constructor.

Parameters:
  • id (int) – The unique camera ID.
  • url (str) – The URL that is used to get the most recent frame from the camera.
url

str – The URL that is used to get the most recent frame from the camera.