rikai.types package¶
Submodules¶
rikai.types.geometry module¶
Geometry types
- class rikai.types.geometry.Box2d(xmin: float, ymin: float, xmax: float, ymax: float)¶
Bases:
ToNumpy
,Sequence
,ToDict
,Drawable
2-D Bounding Box, defined by
(xmin, ymin, xmax, ymax)
Example
>>> box = Box2d(1, 2, 3, 4) >>> box / 2 Box2d(xmin=0.5, ymin=1.0, xmax=1.5, ymax=2.0) >>> box * (3.5, 5) Box2d(xmin=3.5, ymin=10.0, xmax=10.5, ymax=20.0) >>> # Box2d can be used directly with PIL.ImageDraw >>> draw = PIL.ImageDraw.Draw(img) >>> draw.rectangle(box, fill="green", width=2)
- classmethod from_center(center_x: float, center_y: float, width: float, height: float) Box2d ¶
Factory method to construct a
Box2d
from the center point coordinates:{center_x, center_y, width, height}
.
- classmethod from_top_left(xmin: float, ymin: float, width: float, height: float) Box2d ¶
Construct a
Box2d
from the top-left based coordinates:{x0, y0, width, height}
.Top-left corner of an image / bbox is (0, 0).
Several public datasets, including Coco Dataset, use this coordinations.
- Parameters
References
- iou(other: Union[Box2d, List[Box2d], ndarray]) Union[float, ndarray] ¶
Compute intersection over union(IOU).
- static ious(boxes1: Union[List[Box2d], ndarray], boxes2: Union[List[Box2d], ndarray]) Optional[ndarray] ¶
Compute intersection over union(IOU).
- Parameters
boxes1 (
numpy.ndarray
) – a list of Box2d with length of Nboxes2 (
numpy.ndarray
) – a list of Box2d with length of M
- Returns
For two lists of box2ds, which have the length of N, and M respectively, this function should return a N*M matrix, each element is the iou value (float,[0, 1]). Returns None if one of the inputs is empty.
- Return type
numpy.ndarray
, optional
Example
>>> import random >>> >>> def a_random_box2d(): ... x_min = random.uniform(0, 1) ... y_min = random.uniform(0, 1) ... x_max = random.uniform(x_min, 1) ... y_max = random.uniform(y_min, 1) ... return Box2d(x_min, y_min, x_max, y_max) >>> >>> list1 = [a_random_box2d() for _ in range(0, 2)] >>> >>> list2 = [a_random_box2d() for _ in range(0, 3)] >>> >>> Box2d.ious(list1, list2)
- class rikai.types.geometry.Box3d(center: Point, length: float, width: float, height: float, heading: float)¶
-
A 3-D bounding box
- heading¶
The heading of the bounding box (in radians). The heading is the angle required to rotate +x to the surface normal of the box front face. It is normalized to
[-pi, pi)
.- Type
References
- class rikai.types.geometry.Mask(data: Union[list, ndarray], width: Optional[int] = None, height: Optional[int] = None, mask_type: Type = Type.POLYGON)¶
Bases:
ToNumpy
,ToDict
,Drawable
2-d Mask over an image
This 2D mask can be built from:
A binary-valued (0 or 1) 2D-numpy matrix.
A Run Length Encoded (RLE) data. It supports both row-based RLE (
Mask.Type.RLE
) or column-based RLE (Mask.Type.COCO_RLE
) which is used in the Coco dataset.A Polygon
[x0, y0, x1, y1, ..., xn, yn]
- Parameters
Examples
from pycocotools.coco import COCO from rikai.types import Mask coco = COCO("instance_train2017.json") ann = coco.loadAnns(ann_id) image = coco.loadImgs(ann["image_id"]) if ann["iscrowed"] == 0: mask = Mask.from_polygon( ann["segmentation"], width=image["width], height=image["height"], ) else: mask = Mask.from_coco_rle( ann["segmentation"]["counts"], width=image["width], height=image["height"], )
- static from_coco_rle(data: list[int], width: int, height: int) Mask ¶
Convert a COCO RLE mask (segmentation) into Mask
- static from_mask(mask: ndarray) Mask ¶
Build mask from a numpy array.
- Parameters
mask (np.ndarray) – A binary-valued (0/1) numpy array
- static from_polygon(data: list[list[float]], width: int, height: int) Mask ¶
Build mask from a Polygon
rikai.types.rle module¶
rikai.types.video module¶
Video related types and utils
- class rikai.types.video.Segment(start_fno: int, end_fno: int)¶
Bases:
object
A video segment bounded by frame numbers
- class rikai.types.video.SingleFrameSampler(stream: VideoStream, sample_rate: int = 1, start_frame: int = 0, max_samples: int = -1)¶
Bases:
VideoSampler
A simple sampler that just returns one out of every sample_rate frames
- class rikai.types.video.VideoSampler(stream: VideoStream)¶
Bases:
ABC
Subclasses will implement different ways to retrieve samples from a given VideoStream.
- class rikai.types.video.VideoStream(uri: str)¶
Bases:
Displayable
,ToDict
Represents a particular video stream at a given uri
- class rikai.types.video.YouTubeVideo(vid: str)¶
Bases:
Displayable
Represents a YouTubeVideo, the basis of many open-source video data sets. This classes uses the ipython display library to integrate with jupyter notebook display and uses youtube-dl to download and create a VideoStream instance which represents a particular video stream file obj
- display(width: int = 400, height: int = 300, **kwargs)¶
Visualization in jupyter notebook with custom options
- get_stream(ext: str = 'mp4', quality: str = 'worst') VideoStream ¶
Get a reference to a particular stream
- Parameters
ext (str, default 'mp4') – The preferred extension type to get. One of [‘ogg’, ‘m4a’, ‘mp4’, ‘flv’, ‘webm’, ‘3gp’] See: https://pythonhosted.org/Pafy/#Pafy.Stream.extension
quality (str, default 'worst') – Either ‘worst’ (lowest bitrate) or ‘best’ (highest bitrate) See: https://pythonhosted.org/Pafy/index.html#Pafy.Pafy.getbest
- Returns
v – VideoStream referencing an actual video resource
- Return type
rikai.types.vision module¶
Vision Related User-defined Types:
- class rikai.types.vision.Image(image: Union[bytes, bytearray, IOBase, str, Path])¶
Bases:
ToNumpy
,ToPIL
,Asset
,Displayable
,ToDict
An external Image Asset.
It contains a reference URI to an image stored on the remote system.
- Parameters
image (bytes, file-like object, str or
Path
) – It can be the content of image, or a URI / Path of an image.
- crop(box: Union[Box2d, List[Box2d]], format: Optional[str] = None) Union[Image, List[Image]] ¶
Crop image specified by the bounding boxes, and returns the cropped images.
Support crop images in batch, to save I/O overhead to download the original image.
- display(**kwargs)¶
Custom visualizer for this image in jupyter notebook
- Parameters
kwargs (dict) – Optional display arguments
- Returns
img
- Return type
IPython.display.Image
- classmethod from_array(array: ndarray, uri: Optional[Union[str, Path]] = None, mode: Optional[str] = None, format: Optional[str] = None, **kwargs) Image ¶
Create an image in memory from numpy array.
- Parameters
array (np.ndarray) – Array data
uri (str or Path) – The external URI to store the data.
mode (str, optional) – The mode which PIL used to create image. See supported modes on PIL document.
format (str, optional) – The image format to save as. See supported formats for details.
kwargs (dict, optional) – Optional arguments to pass to PIL.Image.save.
See also
PIL.Image.fromarray
,numpy_to_image()
- static from_pil(img: PILImage, uri: Optional[Union[str, Path]] = None, format: Optional[str] = None, **kwargs) Image ¶
Create an image in memory from a
PIL.Image
.- Parameters
img (
PIL.Image
) – An PIL Image instanceuri (str or Path) – The URI to store the image externally.
format (str, optional) –
The image format to save as. See supported formats for details.
kwargs (dict, optional) –
Optional arguments to pass to PIL.Image.save.
- static read(uri: Union[str, Path]) Image ¶
Create an embedded image from external URI
- Parameters
uri (str or Path) – The URI pointed to an image.
- to_numpy() ndarray ¶
Convert this image into an
numpy.ndarray
.
- to_pil() PILImage ¶
Return an PIL image.
Note
The caller should close the image. https://pillow.readthedocs.io/en/stable/reference/open_files.html#image-lifecycle
Module contents¶
Semantic types
- noindex