Stream Parser

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.

Parse different types of camera streams.

This module is used to parse different types of camera streams. The module provides the StreamParser base class which provides a uniform way of parsing all camera streams. The module provides different subclasses, each for a different type of camera streams (e.g. image streams, and MJPEG streams).

Examples

Example 1: To parse a camera image stream: 1. Initialize an object of ImageStreamParser using 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.

::
parser = ImageStreamParser(‘http://128.10.29.33/axis-cgi/jpg/image.cgi’) frame, frame_size = parser.get_frame() cv2.imshow(‘frame’, frame) print frame_size cv2.waitKey()

Example 2: To parse a camera MJPEG stream: 1. Initialize an object of MJPEGStreamParser using the URL of the camera MJPEG stream. 2. Open the stream by calling the open_stream method. 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 stream by calling the close_stream method.

::

parser = MJPEGStreamParser(‘http://128.10.29.33/axis-cgi/mjpg/video.cgi’) parser.open_stream() t = time.time() while time.time() - t < 5:

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

parser.close_stream()

class CAM2ImageArchiver.StreamParser.StreamParser(url)[source]

Bases: object

Represent the base class for camera stream parsers.

Parameters:url (str) – The URL of the stream.
url

str – The URL of the stream.

open_stream()[source]

Open the stream.

Raises:error.UnreachableCameraError – If the camera is unreachable.
close_stream()[source]

Close the MJPEG stream.

restart_stream()[source]

Restart the 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 camera stream.

This method is an abstract method that must be overridden by subclasses.

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.
  • NotImplementedError – If the method is not overridden in the subclass.
class CAM2ImageArchiver.StreamParser.ImageStreamParser(url)[source]

Bases: CAM2ImageArchiver.StreamParser.StreamParser

Represent a parser for a camera image stream.

This class subclasses the StreamParser class and inherits its attributes and constructor.

Notes

A camera that provides an image stream is a camera that provides a URL to get the most recent frame (regardless of how recent it is). Hence, Parsing an image stream is as simple as downloading the most recent frame from the given URL whenever requested. There is no need to call open_stream or close_stream since they do nothing.

get_frame()[source]

Get the most recent frame from the camera image stream.

Returns:

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

Raises:
  • error.CorruptedFrameError – If the frame is corrupted.
  • error.UnreachableCameraError – If the camera is unreachable.
class CAM2ImageArchiver.StreamParser.MJPEGStreamParser(url)[source]

Bases: CAM2ImageArchiver.StreamParser.StreamParser

Represent a parser for a camera MJPEG stream.

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

Parameters:url (str) – The URL of the MJPEG stream.
mjpeg_stream

file-like object – The handle to the camera MJPEG stream.

open_stream()[source]

Open the MJPEG stream.

Raises:error.UnreachableCameraError – If the camera is unreachable.
close_stream()[source]

Close the MJPEG stream.

get_frame()[source]

Get the most recent frame from the camera MJPEG stream.

Returns:

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

Raises:
  • error.CorruptedFrameError – If the frame is corrupted.
  • error.ClosedStreamError – If the MJPEG stream needs to be opened first.

Notes

MJPEG Stream Format: –myboundary Content-Type: image/jpeg Content-Length: [size of image in bytes] [empty line] ….. binary data ….. [empty line] –myboundary Content-Type: image/jpeg Content-Length: [size of image in bytes] [empty line] ….. binary data ….. [empty line]

class CAM2ImageArchiver.StreamParser.MJPGm3u8StreamParser(url)[source]

Bases: CAM2ImageArchiver.StreamParser.StreamParser

Represent a parser for a camera MJPEG stream. *Does not have to be MJPEG, .m3u8 media file works as well.

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

Parameters:url (str) – The URL of the MJPEG stream.
mjpeg_stream

file-like object – The handle to the camera MJPEG stream.

get_frame()[source]

Get the most recent frame from the camera MJPEG stream.

Returns:

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

Raises:
  • error.CorruptedFrameError – If the frame is corrupted.
  • error.ClosedStreamError – If the MJPEG stream needs to be opened first.