lavaflow.utils package
lavaflow.utils.config module
Utility functions for config.
- lavaflow.utils.config.compare_config(a, b, aname='a', bname='b', keys=True, types=True, values=True)[source]
Function to compare the key structure and value types of two configs.
- Parameters
a (dict) – config
b (dict) – config
aname (str) – config name
bname (str) – config name
keys (bool) – check if keys are not the same at each level of the dictionary
types (bool) – check if types are not the same
values (bool) – check if values are not the same
- Returns
comparison summary
- Return type
d (dict)
Examples
>>> a = { 'a': 1, 'b': { 'c': 3, 'd': 4, 'e': 'x' }} >>> b = { 'a': 3, 'b': { 'c': 3, 'd': 'y', 'g': 7 }} >>> compare_config(a, b) ['a.a = 1 != b.a = 3', "a.b.keys() - b.b.keys() = ['e']", "b.b.keys() - a.b.keys() = ['g']", "type(a.b.d) = <class 'int'> != type(b.b.d) = <class 'str'>"]
- lavaflow.utils.config.remove_inline_comments(config_yaml, prefix='#')[source]
Remove inline comments from config yaml based on comment prefix.
- Parameters
config_yaml (str) – config yaml with inline comments
prefix (str) – comment prefix (default: #)
- Returns
config with inline comments removed
- Return type
config_yaml (str)
lavaflow.utils.core module
Utility functions.
- lavaflow.utils.core.chain(x)[source]
Function to flatten a list of lists.
- Parameters
x (list) – list of lists of items
- Returns
list of items
- Return type
items (list)
Examples
>>> x = [[1, 2], [3, 4]] >>> chain(x) [1, 2, 3, 4]
- lavaflow.utils.core.compose(functions)[source]
Compose an iterator of functions in sequence.
- Parameters
functions (iterable) – an iterator of functions
- Returns
composition of functions
- Return type
f (function)
Examples
>>> g = compose([lambda x: x + 1, lambda x: x * 2]) >>> g(1) 4
- lavaflow.utils.core.dictify(f, x, y)[source]
Wrap a function to take a dictionary as input and to map input key(s) to output key(s).
- Parameters
f (function) – function to be wrapped
x (str|list) – input key(s)
y (str|list) – output key(s)
- Returns
new function
- Return type
g (function)
Examples
>>> g = dictify(lambda a: a + 1, 'a', 'b') >>> g({'a': 1}) {'a': 1, 'b': 2}
>>> g = dictify(lambda a, b: a + b, ['a', 'b'], 'c') >>> g({'a': 1, 'b': 2}) {'a': 1, 'b': 2, 'c': 3}
>>> g = dictify(lambda a, b: (a + b, b - a), ['a', 'b'], ['c', 'd']) >>> g({'a': 1, 'b': 2}) {'a': 1, 'b': 2, 'c': 3, 'd': 1}
- lavaflow.utils.core.windowify(f, i, x, j, y, chain_args)[source]
Wrap a function to take a list of dictionaries as input and to map input key(s) to output key(s).
- Parameters
f (function) – function to be wrapped
i (int|list) – input item(s)
x (str|list) – input key(s)
j (int|list) – output item(s)
y (str|list) – output key(s)
chain_args (bool) – flatten inputs
- Returns
new function
- Return type
g (function)
Examples
>>> g = windowify(lambda a0, a1, b0, b1: (a0 + a1, b0 + b1), [0, 2], ['a', 'b'], [1], ['c', 'd'], True) >>> g([{'a': 1, 'b': 1}, {'a': 2, 'b': 2} , {'a': 3, 'b': 3}]) [{'a': 1, 'b': 1}, {'a': 2, 'b': 2, 'c': 4, 'd': 4}, {'a': 3, 'b': 3}]
>>> g = windowify(lambda a, b: (a[0] + a[1], b[0] + b[1]), [0, 2], ['a', 'b'], [1], ['c', 'd'], False) >>> g([{'a': 1, 'b': 1}, {'a': 2, 'b': 2} , {'a': 3, 'b': 3}]) [{'a': 1, 'b': 1}, {'a': 2, 'b': 2, 'c': 4, 'd': 4}, {'a': 3, 'b': 3}]
lavaflow.utils.exif module
Utility functions.
- lavaflow.utils.exif.get_lens_profile(tags)[source]
Function to extract lens profile from exif.
- Parameters
tags (dict) – lens profile extracted using https://exiftool.org/
- Returns
camera maker string model (str): camera model string lens (str): lens model string focal_length (double): focal length (mm) aperture (double): aperture distance (double): approximate focal distance (m)
- Return type
maker (str)
lavaflow.utils.geometry module
Utility functions for geometry.
- lavaflow.utils.geometry.distance(a, b)[source]
Compute the distance between two points.
- Parameters
a (tuple) – point 1
b (tuple) – point 2
- Returns
distance between two points
- Return type
dist (float)
- lavaflow.utils.geometry.line_proximity(p, a, b)[source]
Compute the shortest distance between the point p and the line passing through two points (a - b)
- Parameters
p (tuple) – point to compute proximity
a (tuple) – line point 1
b (tuple) – line point 2
- Returns
the shortest distance
- Return type
dist (float)
lavaflow.utils.io module
Utility functions.
- class lavaflow.utils.io.NumpyJsonEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]
Bases:
JSONEncoderExtension of json.JSONEncoder that supports number arrays as lists only.
Constructor for JSONEncoder, with sensible defaults.
If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.
If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.
If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an OverflowError). Otherwise, no such check takes place.
If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.
If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.
If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.
If specified, separators should be an (item_separator, key_separator) tuple. The default is (’, ‘, ‘: ‘) if indent is
Noneand (‘,’, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.If specified, default is a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a
TypeError.
- class lavaflow.utils.io.NumpyLineEncoder(separators=(',', ';', ':'), **kwargs)[source]
Bases:
objectClass for serializing numpy arrays into a single line format for efficiency.
Note that this is intended for numeric numpy arrays with up to three axes.
- Parameters
separators (list|tuple) – separators to use for each axis in order
kwargs (dict) – keyword arguments to pass to np.array2string
- encode(x)[source]
Encode numpy array.
- Parameters
x (np.ndarray) – numpy array
- Returns
encoded numpy array
- Return type
s (str)
Examples
>>> encoder = NumpyLineEncoder() >>> x = np.array([[[1,2,3],[4,5,6],[7,8,9]],[[1,2,3],[4,5,6],[7,8,9]]]) >>> print(encoder.encode(x)) 1,2,3;4,5,6;7,8,9:1,2,3;4,5,6;7,8,9
lavaflow.utils.opencv module
Utility functions for OpenCV.
- lavaflow.utils.opencv.applyMask(img, mask, offset=0, radius=0, spread=0)[source]
Apply mask to image.
The mask can be dilated or eroded using
cv2.morphologyExand smoothed usingcv2.GaussianBlue. A positive offset uses dilation and a negative offset uses erosion to expand or contract the mask respectively.- Parameters
img (np.ndarray) – image
mask (np.ndarray) – monocrhome mask
offset (int) – number of pixels to offset mask
radius (int) – blur radius (must be odd)
spread (float) – blur spread
- Returns
image with mask applied
- Return type
img (np.ndarray)
- lavaflow.utils.opencv.drawPoints(vis, v, color=(255, 255, 255), points=1, line=0, loop=1, size=3, thickness=1)[source]
Draw points on an image.
- Parameters
vis (np.ndarray) – visualization image
v (np.ndarray) – array of points, shaped (N, 2)
color (tuple) – color
points (int) – flag to draw points
line (int) – flag to connect points with lines
loop (int) – flag to connect end point with start point (ignored if line = 0)
size (int) – point size
thickness (int) – line thickness
- Returns
visualization image
- Return type
vis (np.ndarray)
- lavaflow.utils.opencv.drawSegments(vis, s, color=(255, 255, 255), points=0, line=1, size=3, thickness=1)[source]
Draw line segments on an image.
- Parameters
vis (np.ndarray) – visualization image
s (np.ndarray) – array of line segments, shaped (N, 2, 2)
color (tuple) – color
points (int) – flag to draw points
line (int) – flag to connect points with lines
size (int) – point size
thickness (int) – line thickness
- Returns
visualization image
- Return type
vis (np.ndarray)
- lavaflow.utils.opencv.drawTriangles(vis, tri, color=(255, 255, 255), segment_color=(0, 0, 255), points=1, edges=1, segments=0, size=3, thickness=1)[source]
Draw triangulation on an image.
- Parameters
vis (np.ndarray) – visualization image
tri (dict) – output from triangle.triangulate
color (tuple) – color
segment_color (tuple) – color for segment constraints
points (int) – flag to draw triangle points
edges (int) – flag to draw triangle edges
segments (int) – flag to draw segment constraints
size (int) – point size
thickness (int) – line thickness
- Returns
visualization image
- Return type
vis (np.ndarray)
- lavaflow.utils.opencv.drawVelocityGrid(vis, x, y, u, v, color=(255, 255, 255), spacing=1, magnification=1, points=0, arrows=1, size=3, thickness=1, tipLength=0.05)[source]
Draw (N, M) velocity grid on an image.
- Parameters
vis (np.ndarray) – visualization image
x (np.ndarray) – grid x coordinates, shaped (N, M)
y (np.ndarray) – grid y coordinates, shaped (N, M)
u (np.ndarray) – velocity horizontal components, shaped (N, M)
v (np.ndarray) – velocity vertical components, shaped (N, M)
spacing (double) – minimum spacing
magnification (float) – magnification to apply to velocity
color (tuple) – color
points (int) – flag to draw points
arrows (int) – flag to draw arrows
size (int) – point size
thickness (int) – line thickness
tipLength (int) – arrow tip length
- Returns
visualization image
- Return type
vis (np.ndarray)
- lavaflow.utils.opencv.drawVelocityLine(vis, x, y, u, v, color=(255, 255, 255), spacing=1, magnification=1, points=0, arrows=1, size=3, thickness=1, tipLength=0.05)[source]
Draw (N, M) velocity grid on an image.
- Parameters
vis (np.ndarray) – visualization image
x (np.ndarray) – line x coordinates, shaped (N)
y (np.ndarray) – line y coordinates, shaped (N)
u (np.ndarray) – velocity horizontal components, shaped (N)
v (np.ndarray) – velocity vertical components, shaped (N)
spacing (double) – minimum spacing
magnification (float) – magnification to apply to velocity
color (tuple) – color
points (int) – flag to draw points
arrows (int) – flag to draw arrows
size (int) – point size
thickness (int) – line thickness
tipLength (int) – arrow tip length
- Returns
visualization image
- Return type
vis (np.ndarray)
- lavaflow.utils.opencv.ensureColor(img)[source]
Ensure image is BGR not GRAY.
- Parameters
img (np.ndarray) – image
- Returns
image converted from GRAY to BGR
- Return type
img (np.ndarray)
- lavaflow.utils.opencv.imgrid(array, shape=None, max_size=(2160, 3840))[source]
Function for composing multiple images into a grid.
- Parameters
array (list) – list of images to compose
shape (tuple) – grid shape (rows, columns)
max_size (tuple) – resolution used to scale image grid
- Returns
image grid
- Return type
grid (np.ndarray)
- lavaflow.utils.opencv.immask(img, mask, color=(255, 255, 0), alpha=0.5)[source]
Function to apply a transparent mask overlay to an image
- Parameters
img (np.ndarray) – image
mask (np.ndarray) – mask to overlay
color (tuple) – mask color
alpha (float) – mask transparency
- Returns
img with mask applied
- Return type
background (np.ndarray)