6.2. server package

6.2.2. Submodules

6.2.3. server.abstract_camera module

abstract_camera.py: Holds the Python definition of a Camera as defined in pepi.thrift.

class server.abstract_camera.AbstractCamera[source]

Bases: object

AbstractCamera is an abstract base class that defines the interface required from an Camera. Camera objects are used to capture imagery a physical camera. The means in which this image is obtained does not matter, as long as presented interface is consistent.

Camera objects are intended to be used by BaseCameraServer subclasses.

A concrete Camera should subclass AbstractCamera and must implement all methods marked with @abstractmethod.

get_current_resolution()[source]

Gets the current resolution of this camera.

Returns:a list of length 2 representing the resolution i.e. (x, y)
get_max_resolution()[source]

Gets the maximum resolution supported by this camera.

Returns:a list of length 2 representing the resolution i.e. (x, y)
low_res_still()[source]

Captures a still from the camera and returns it as 3-dimensional RGB array representing the image.

Returns:[[[R, G, B]] NumPy array of Numpy.uint8 0-255 values.
set_resolution(x, y)[source]

If supported, sets the resolution of the camera.

Parameters:
  • x – the x component of the desired resolution
  • y – the x component of the desired resolution
still()[source]

Captures a still from the camera and returns it as 3-dimensional RGB array representing the image.

Returns:[[[R, G, B]] NumPy array of Numpy.uint8 0-255 values.
class server.abstract_camera.RGBImage(array)[source]

Bases: object

A utility object to convert images of different formats to RGB arrays

array

The array RGB array that represents this RGBImage :return:

classmethod frombytes(mode, size, bytes_)[source]

Construct a RGBImage from the pixel data in a buffer.

Parameters:
Raises:

ValueError: when the bytes are malformed or not an image

Returns:

RGBImage

classmethod fromfile(file)[source]

Construct a RGB image from the given file

Parameters:file – a filename (str) or file object.
Raises:ValueError: when the file object is malformed or not an image
Returns:RGBImage
classmethod fromstring(string)[source]
list

RGBImage as a native Python list.

Returns:Returns this RGB as a native Python list.
low_res

Returns this RGB image as a numpy array.

Returns:this RGB image as a numpy array

6.2.4. server.base_camera_server module

base_camera_server.py: Holds the Python implementation of a CameraServer as defined in pepi.thrift.

class server.base_camera_server.BaseCameraServer(cameras, stream=True)[source]

Bases: object

BaseCameraServer is the minimal Python implementation of a CameraServer as defined in pepi.thrift. CameraServers are used in with the Apache Thrift protocol to provide RPC mechanisms, allowing control of this server over RPC, if it is launched with Thrift.

A CameraServer subclassing BaseCameraServer may override any of these methods to better reflect their use. However, care must be taken to ensure that the side effects of the subclass’s methods do not affect other methods. For example, if you were to change the capture method to store images in a list for whatever reason, you would need to change the image retrieval methods.

A CameraServer’s use-case is to provide a server that controls a number of cameras to be controlled in a consistent manner. This allows for a client to seamlessly control all implementations of CameraServer’s, over Thrift without needing to concern themselves with what cameras are attached, the procedure call names, etc.

This BaseCameraServer implementation supports multiple connected cameras, that are transparent to the connecting client. When retrieving images, a list of encoded images are returned. The order of this list remains consistent across procedure calls.

STREAM_PORT = 6001
class StreamInfo(port, folder, streamer)

Bases: tuple

folder

Alias for field number 1

port

Alias for field number 0

streamer

Alias for field number 2

BaseCameraServer.enumerate_methods()[source]

Retrieves a map of the methods available on this server. This is useful for clients to verify the methods it can expect to be able to call if being called remotely.

Returns:dict of <method_name: [arguments]>
BaseCameraServer.identify()[source]

Get the unique identifier of this server.

Returns:the server’s unique identifier string
BaseCameraServer.ping()[source]

Ping the server to check if it is active and responding.

Returns:True (always)
BaseCameraServer.retrieve_stills_jpg(with_data_code)[source]

Retrieves the images stored under with_data_code, if they exist, and encodes them into a .jpg str (i.e. bytes).

The order of the returned images is consistent, e.g. Camera #1, #2 .., #x returned in that order.

Parameters:with_data_code – the data_code from which the image will be retrieved
Raises:ImageUnavailable: when image requested with an invalid/unknown data_code
Returns:a list of strings with each string containing an encoded as a .jpg
BaseCameraServer.retrieve_stills_png(with_data_code)[source]

Retrieves the images stored under with_data_code, if they exist, and encodes them into a .png str (i.e. bytes).

The order of the returned images is consistent, e.g. Camera #1, #2 .., #x returned in that order.

Parameters:with_data_code – the data_code from which the image will be retrieved
Raises:ImageUnavailable: when image requested with an invalid/unknown data_code
Returns:a list of strings with each string containing an encoded as a .png
BaseCameraServer.shutdown()[source]

Shutdown the server (i.e. power-off).

Subclasses may choose to ignore calls to this function, in which case they should override this function to do nothing.

Returns:None
BaseCameraServer.start_capture(data_code)[source]

Immediately starts the process of capturing from this server’s Camera(s), and stores the captured data under the given unique data_code.

Note: the received data_code is assumed to be unique. Subclasses may choose to implement better isolation methods, but this is not guaranteed nor required.

Parameters:data_code – the requested data_code to store the capture under
Returns:None
BaseCameraServer.stream_urls()[source]

Get the a list of URLs where the MJPG image stream of each camera connected to this server may be accessed.

The order of the returned images is consistent, e.g. Camera #1, #2 .., #x returned in that order.

Returns:a list of the stream URLs as a string
class server.base_camera_server.CameraTimelapser(camera, folder, interval)[source]

Bases: threading.Thread

CameraTimelapser is a utility class designed to call the low_res_still() method on a concrete AbstractCamera at a defined rate and save it to a given folder for the purposes of timelapsing or streaming from the camera.

run()[source]

6.2.5. server.dummy_camera module

dummy_camera.py: A concrete AbstractCamera that generates random images of RGB noise for debugging purposes.

class server.dummy_camera.DummyCamera(resolution=[1920, 1080])[source]

Bases: server.abstract_camera.AbstractCamera

DummyCamera is a concrete AbstractCamera that generates random images of RGB noise.

MAX_RESOLUTION = [1920, 1080]
get_current_resolution()[source]

Gets the current resolution of this camera.

Returns:a list of length 2 representing the resolution i.e. (x, y)
get_max_resolution()[source]

Gets the maximum resolution supported by this camera.

Returns:a list of length 2 representing the resolution i.e. (x, y)
low_res_still()[source]

Captures a low resolution still from the camera and returns it as 3-dimensional RGB array representing the image.

Returns:multidimensional list of row, column, RGB values between 0-255.
resolution
set_resolution(x, y)[source]

If supported, sets the resolution of the camera.

Parameters:
  • x – the x component of the desired resolution
  • y – the x component of the desired resolution
still()[source]

Captures a still from the camera and returns it as 3-dimensional RGB array representing the image.

Returns:multidimensional list of row, column, RGB values between 0-255.

6.2.6. server.iptools module

Tools related to IP operations, mainly getting IP’s and checking if servers exist at a certain IP:port combination.

class server.iptools.IPTools[source]

Bases: object

Static methods that work on IPv4-based connections, such as getting current IP, gateway IP, etc.

static current_ips()[source]

Gets the best-candidate IPs of this computer on the network.

static gateway_ip()[source]

Gets the IP of this computer’s gateway, if it exists.

static get_first_digits_from(ip, num_digits, with_dot=True)[source]

Gets the given sets of digits from an IP, with or without the trailing period.

Note:

if num_digits exceeds 4, the ip is returned unchanged.

Parameters:
  • ip – the IP address string to trim
  • num_digits – the number of digit sets to keep
  • with_dot – True to keep the trailing period, False to not
static get_subnet_from(ip, with_dot=True)[source]

Gets the first 3 sets of digits from an IP, with or without the trailing period.

Parameters:
  • ip – the IP address string to trim
  • with_dot – True to keep the trailing period, False to not

6.2.7. server.pepi_thrift_loader module

Loads PEPI’s Thrift interface definitions as Python objects accessible under pepi_thrift after importing this module.

6.2.8. server.stream module

class server.stream.MJPGStreamer(img_path, ip='0.0.0.0', port=6001)[source]

Bases: object

Starts a HTTP stream based on JPEG images obtained from the specified folder.

static jpeg_image_generator(path, quality=85, resolution=(640, 480))[source]
Generates JPEG bytes from any image file in the given path based on the second newest file modified in the given
path.

JPEG files (and other image formats) are compressed to a JPEG as they must be modified to be resized.

Args:
path: path to the folder to check for new files quality: JPEG quality to compress to (0 lowest quality, 100 highest) resolution: resolution to yield the JPEGs as
static newest_file_in_folder(path, delete_old=True)[source]

Generator that yields the second newest file by modified time in the given path. The second newest file is yielded so that files in the process of being written are not used before they are complete; this is generally not an issue that the second newest file faces.

Args:
path: path to the folder to check for new files delete_old: True to delete all but the second_newest and newest files, False to not delete any
stream_handler_factory(img_path)[source]

Create a MJPGStreamHandler with the img_path set inside of it.

This is necessary due to how BaseHTTPServer creates the BaseHTTPRequestHandler.

Args:
img_path: the path to give to MJPGStreamHandler
class server.stream.ThreadedHTTPServer(server_address, RequestHandlerClass, bind_and_activate=True)[source]

Bases: socketserver.ThreadingMixIn, http.server.HTTPServer

A multi-threaded HTTP server, i.e. creates a new thread to respond to each connection, so multiple connections can coexist.

allow_reuse_address = True
daemon_threads = True

6.2.9. Module contents