API

io

class cvbase.io.AsyncDumper[source]
run()[source]

Method to be run in sub-process; can be overridden in sub-class

cvbase.io.dump(obj, file=None, format=None, **kwargs)[source]

Dump contents to json/yaml/pickle strings or files.

This method provides a unified api for dumping to files, and also supports custom arguments for each file format.

Parameters:
  • file (None or str or file-like object) – if None, then dump to a str, otherwise to a file specified by the filename or file-like object
  • obj (any) – the python object to be dumped
  • format (None or str) – same as load()
Returns:

True for success, False otherwise

Return type:

bool

cvbase.io.load(file, format=None, **kwargs)[source]

Load contents from json/yaml/pickle files, and also supports custom arguments for each file format.

This method provides a unified api for loading from serialized files.

Parameters:
  • file (str or file-like object) – filename or the file-like object
  • format (None or str) – if it is None, file format is inferred from the file extension, otherwise use the specified one. Currently supported formats are “json”, “yaml”, “yml”, “pickle” and “pkl”
Returns:

The content from the file

conversion

cvbase.conversion.is_list_of(in_list, expected_type)[source]

Check whether it is a list of objects of a certain type

cvbase.conversion.list_cast(in_list, dst_type)[source]

Convert a list of items to some type

cvbase.conversion.merge_list(in_list)[source]

Merge a list of list into a single list

Parameters:in_list (list) – the list of list to be merged
Returns:the flat list
Return type:list
cvbase.conversion.slice_list(in_list, lens)[source]

Slice a list into several sub lists by a list of given length

Parameters:
  • in_list (list) – the list to be sliced
  • lens (int or list) – the expected length of each out list
Returns:

list of sliced list

Return type:

list

cvbase.conversion.to_bool(var)[source]

Convert a variable to bool type

image

cvbase.image.bgr2gray(img, keepdim=False)[source]

Convert a BGR image to grayscale image

Parameters:
  • img (ndarray or str) – either an image or path of an image
  • keepdim (bool) – if set to False(by default), return the gray image with 2 dims, otherwise 3 dims.
Returns:

the grayscale image

Return type:

ndarray

cvbase.image.bgr2hsv(img)[source]

Convert a BGR image to HSV image

Parameters:img (ndarray or str) – either an image or path of an image
Returns:the HSV image
Return type:ndarray
cvbase.image.bgr2rgb(img)[source]

Convert a BGR image to RGB image

Parameters:img (ndarray or str) – either an image or path of an image
Returns:the RGB image
Return type:ndarray
cvbase.image.crop_img(img, bboxes, scale_ratio=1.0, pad_fill=None)[source]

Crop image patches

3 steps: scale the bboxes -> clip bboxes -> crop and pad

Parameters:
  • img (ndarray) – image to be cropped
  • bboxes (ndarray) – shape (k, 4) or (4, ), location of cropped bboxes
  • scale_ratio (float) – scale ratio of bboxes, default by 1.0 (no scaling)
  • pad_fill (number or list) – value to be filled for padding, None for no padding
Returns:

cropped image patches

Return type:

list or ndarray

cvbase.image.gray2bgr(img)[source]

Convert a grayscale image to BGR image

Parameters:img (ndarray or str) – either an image or path of an image
Returns:the BGR image
Return type:ndarray
cvbase.image.hsv2bgr(img)[source]

Convert a HSV image to BGR image

Parameters:img (ndarray or str) – either an image or path of an image
Returns:the BGR image
Return type:ndarray
cvbase.image.img_from_bytes(content, flag=<sphinx.ext.autodoc.importer._MockObject object>)[source]

Read an image from bytes

Parameters:
  • content (bytes) – images bytes got from files or other streams
  • flag (int) – same as read_img()
Returns:

image array

Return type:

ndarray

cvbase.image.limit_size(img, max_edge, return_scale=False, interpolation=<sphinx.ext.autodoc.importer._MockObject object>)[source]

Limit the size of an image

If the long edge of the image is greater than max_edge, resize the image

Parameters:
  • img (ndarray) – input image
  • max_edge (int) – max value of long edge
  • return_scale (bool) – whether to return scale besides the resized image
  • interpolation (enum) – interpolation method
Returns:

(resized image, scale factor)

Return type:

tuple

cvbase.image.pad_img(img, shape, pad_val)[source]

Pad an image to a certain shape

Parameters:
  • img (ndarray) – image to be padded
  • shape (tuple) – expected padding shape
  • pad_val (float or int or list) – values to be filled in padding areas
Returns:

padded image

Return type:

ndarray

cvbase.image.read_img(img_or_path, flag=<sphinx.ext.autodoc.importer._MockObject object>)[source]

Read an image

Parameters:
  • img_or_path (ndarray or str) – either an image or path of an image
  • flag (int) – flags specifying the color type of a loaded image
Returns:

image array

Return type:

ndarray

cvbase.image.resize(img, size, return_scale=False, interpolation=<sphinx.ext.autodoc.importer._MockObject object>)[source]

Resize image by expected size

Parameters:
  • img (ndarray) – image or image path
  • size (tuple) – (w, h)
  • return_scale (bool) – whether to return w_scale and h_scale
  • interpolation (enum) – interpolation method
Returns:

resized image

Return type:

ndarray

cvbase.image.resize_by_ratio(img, ratio, interpolation=<sphinx.ext.autodoc.importer._MockObject object>)[source]

Resize image by a ratio

Parameters:
  • img (ndarray) – image or image path
  • ratio (float) – scale factor
  • interpolation (enum) – interpolation method
Returns:

resized image

Return type:

ndarray

cvbase.image.resize_keep_ar(img, max_long_edge, max_short_edge, return_scale=False, interpolation=<sphinx.ext.autodoc.importer._MockObject object>)[source]

Resize image with aspect ratio unchanged

The long edge of resized image is no greater than max_long_edge, the short edge of resized image is no greater than max_short_edge.

Parameters:
  • img (ndarray) – image or image path
  • max_long_edge (int) – max value of the long edge of resized image
  • max_short_edge (int) – max value of the short edge of resized image
  • return_scale (bool) – whether to return scale besides the resized image
  • interpolation (enum) – interpolation method
Returns:

(resized image, scale factor)

Return type:

tuple

cvbase.image.resize_like(img, dst_img, return_scale=False, interpolation=<sphinx.ext.autodoc.importer._MockObject object>)[source]

Resize image to the same size of a given image

Parameters:
  • img (ndarray) – image or image path
  • dst_img (ndarray) – the given image with expected size
  • return_scale (bool) – whether to return w_scale and h_scale
  • interpolation (enum) – interpolation method
Returns:

resized image

Return type:

ndarray

cvbase.image.rgb2bgr(img)[source]

Convert a RGB image to BGR image

Parameters:img (ndarray or str) – either an image or path of an image
Returns:the BGR image
Return type:ndarray
cvbase.image.rotate_img(img, angle, center=None, scale=1.0, border_value=0, auto_bound=False)[source]

Rotate an image

Parameters:
  • img (ndarray or str) – image to be rotated
  • angle (float) – rotation angle in degrees, positive values mean clockwise rotation
  • center (tuple) – center of the rotation in the source image, by default it is the center of the image.
  • scale (float) – isotropic scale factor
  • border_value (int) – border value
  • auto_bound (bool) – whether to adjust the image size to cover the whole rotated image
Returns:

rotated image

Return type:

ndarray

cvbase.image.scale_size(size, scale)[source]

Scale a size

Parameters:
  • size (tuple) – w, h
  • scale (float) – scaling factor
Returns:

scaled size

Return type:

tuple

cvbase.image.write_img(img, file_path, params=None, auto_mkdir=True)[source]

Write image to file

Parameters:
  • img (ndarray) – image to be written to file
  • file_path (str) – file path
  • params (None or list) – same as opencv imwrite interface
  • auto_mkdir (bool) – if the parrent folder of file_path does not exist, whether to create it automatically
Returns:

successful or not

Return type:

bool

video

class cvbase.video.VideoReader(filename, cache_capacity=10)[source]

Video class with similar usage to a list object.

This video warpper class provides convenient apis to access frames. There exists an issue of OpenCV’s VideoCapture class that jumping to a certain frame may be inaccurate. It is fixed in this class by checking the position after jumping each time.

Cache is used when decoding videos. So if the same frame is visited for the second time, there is no need to decode again if it is stored in the cache.

Example:
>>> import cvbase as cvb
>>> v = cvb.VideoReader('sample.mp4')
>>> len(v)  # get the total frame number with `len()`
120
>>> for img in v:  # v is iterable
>>>     cvb.show_img(img)
>>> v[5]  # get the 6th frame
current_frame()[source]

Get the current frame (frame that is just visited)

Returns:if the video is fresh, return None, otherwise return the frame.
Return type:ndarray or None
cvt2frames(frame_dir, file_start=0, filename_tmpl='{:06d}.jpg', start=0, max_num=0, show_progress=True)[source]

Convert a video to frame images

Parameters:
  • frame_dir (str) – output directory to store all the frame images
  • file_start (int) – from which filename starts
  • filename_tmpl (str) – filename template, with the index as the variable
  • start (int) – starting frame index
  • max_num (int) – maximum number of frames to be written
  • show_progress (bool) – whether to show a progress bar
fourcc

str – “four character code” of the video

fps

int – fps of the video

frame_cnt

int – total frames of the video

get_frame(frame_id)[source]

Get frame by frame id

Parameters:frame_id (int) – id of the expected frame, 1-based index
Returns:return the frame if successful, otherwise None.
Return type:ndarray or None
height

int – height of video frames

opened

bool – indicate whether the video is opened

position

int – current cursor position, indicating which frame

read()[source]

Read the next frame

If the next frame have been decoded before and in the cache, then return it directly, otherwise decode and return it, put it in the cache.

Returns:return the frame if successful, otherwise None.
Return type:ndarray or None
resolution

tuple – video resolution (width, height)

vcap

cv2.VideoCapture – raw VideoCapture object

width

int – width of video frames

cvbase.video.check_ffmpeg(func)[source]

A decorator to check if ffmpeg is installed

cvbase.video.concat_video(*args, **kwargs)[source]

Concatenate multiple videos into a single one

Parameters:
  • video_list (list) – a list of video filenames
  • out_file (str) – output video filename
  • vcodec (None or str) – output video codec, None for unchanged
  • acodec (None or str) – output audio codec, None for unchanged
  • log_level (str) – log level of ffmpeg
cvbase.video.convert_video(*args, **kwargs)[source]

Convert a video with ffmpeg

This provides a general api to ffmpeg, the executed command is:

ffmpeg -y <pre_options> -i <in_file> <options> <out_file>

Options(kwargs) are mapped to ffmpeg commands by the following rules:

  • key=val: “-key val”
  • key=True: “-key”
  • key=False: “”
Parameters:
  • in_file (str) – input video filename
  • out_file (str) – output video filename
  • pre_options (str) – options appears before “-i <in_file>”
cvbase.video.cut_video(*args, **kwargs)[source]

Cut a clip from a video

Parameters:
  • in_file (str) – input video filename
  • out_file (str) – output video filename
  • start (None or float) – start time (in seconds)
  • end (None or float) – end time (in seconds)
  • vcodec (None or str) – output video codec, None for unchanged
  • acodec (None or str) – output audio codec, None for unchanged
  • log_level (str) – log level of ffmpeg
cvbase.video.frames2video(frame_dir, video_file, fps=30, fourcc='XVID', filename_tmpl='{:06d}.jpg', start=0, end=0, show_progress=True)[source]

Read the frame images from a directory and join them as a video

Parameters:
  • frame_dir (str) – frame directory
  • video_file (str) – output video filename
  • fps (int) – fps of the output video
  • fourcc (str) – foutcc of the output video, this should be compatible with the output file type
  • filename_tmpl (str) – filename template, with the index as the variable
  • start (int) – starting frame index
  • end (int) – ending frame index
  • show_progress (bool) – whether to show a progress bar
cvbase.video.resize_video(*args, **kwargs)[source]

Resize a video

Parameters:
  • in_file (str) – input video filename
  • out_file (str) – output video filename
  • size (tuple) – expected (w, h), eg, (320, 240) or (320, -1)
  • ratio (tuple or float) – expected resize ratio, (2, 0.5) means (w*2, h*0.5)
  • keep_ar (bool) – whether to keep original aspect ratio
  • log_level (str) – log level of ffmpeg

timer

class cvbase.timer.Timer(start=True, print_tmpl=None)[source]

A flexible Timer class.

Example:
>>> import time
>>> import cvbase as cvb
>>> with cvb.Timer():
>>>     # simulate a code block that will run for 1s
>>>     time.sleep(1)
1.000
>>> with cvb.Timer(print_tmpl='hey it taks {:.1f} seconds'):
>>>     # simulate a code block that will run for 1s
>>>     time.sleep(1)
hey it taks 1.0 seconds
>>> timer = cvb.Timer()
>>> time.sleep(0.5)
>>> print(timer.since_start())
0.500
>>> time.sleep(0.5)
>>> print(timer.since_last_check())
0.500
>>> print(timer.since_start())
1.000
is_running

bool – indicate whether the timer is running

since_last_check()[source]

Time since the last checking.

Either since_start() or since_last_check() is a checking operation.

Returns(float): the time in seconds

since_start()[source]

Total time since the timer is started.

Returns(float): the time in seconds

start()[source]

Start the timer.

exception cvbase.timer.TimerError(message)[source]
cvbase.timer.check_time(timer_id)[source]

Add check points in a single line

This method is suitable for running a task on a list of items. A timer will be registered when the method is called for the first time.

Example:
>>> import time
>>> import cvbase as cvb
>>> for i in range(1, 6):
>>>     # simulate a code block
>>>     time.sleep(i)
>>>     cvb.check_time('task1')
2.000
3.000
4.000
5.000
Parameters:timer_id (str) – timer identifier

progress

class cvbase.progress.ProgressBar(task_num=0, bar_width=50, start=True)[source]

A progress bar which can print the progress

cvbase.progress.track_parallel_progress(func, tasks, process_num, initializer=None, initargs=None, bar_width=50, chunksize=1, skip_first=False, keep_order=True)[source]

Track the progress of parallel task execution with a progress bar

The built-in multiprocessing module is used for process pools and tasks are done with Pool.map() or Pool.imap_unordered().

Parameters:
  • func (callable) – the function to be applied to each task
  • tasks (tuple of 2 or list) – a list of tasks
  • process_num (int) – the process(worker) number
  • initializer (None or callable) – see multiprocessing.Pool for details
  • initargs (None or tuple) – see multiprocessing.Pool for details
  • chunksize (int) – see multiprocessing.Pool for details
  • bar_width (int) – width of progress bar
  • skip_first (bool) – whether to skip the first sample when calculating fps
  • keep_order (bool) – if True, Pool.imap() is used, otherwise Pool.imap_unordered() is used
Returns:

the results

Return type:

list

cvbase.progress.track_progress(func, tasks, bar_width=50, **kwargs)[source]

Track the progress of tasks execution with a progress bar

Tasks are done with a simple for-loop.

Parameters:
  • func (callable) – the function to be applied to each task
  • tasks (tuple of 2 or list) – a list of tasks
  • bar_width (int) – width of progress bar
Returns:

the results

Return type:

list

visualize

class cvbase.visualize.Color(*args, **kwargs)[source]

Color associated with RGB values

8 colors in total: red, green, blue, cyan, yellow, magenta, white and black.

cvbase.visualize.draw_bboxes(img, bboxes, colors=(0, 255, 0), top_k=0, thickness=1, show=True, win_name='', wait_time=0, out_file=None)[source]

Draw bboxes on an image

Parameters:
  • img (str or ndarray) – the image to be shown
  • bboxes (list or ndarray) – a list of ndarray of shape (k, 4)
  • colors (list or Color or tuple) – a list of colors, corresponding to bboxes
  • top_k (int) – draw top_k bboxes only if positive
  • thickness (int) – thickness of lines
  • show (bool) – whether to show the image
  • win_name (str) – the window name
  • wait_time (int) – value of waitKey param
  • out_file (str or None) – the filename to write the image
cvbase.visualize.draw_bboxes_with_label(img, bboxes, labels, top_k=0, bbox_color=(0, 255, 0), text_color=(0, 255, 0), thickness=1, font_scale=0.5, show=True, win_name='', wait_time=0, out_file=None)[source]

Draw bboxes with label text in image

Parameters:
  • img (str or ndarray) – the image to be shown
  • bboxes (list or ndarray) – a list of ndarray of shape (k, 4)
  • labels (str or list) – label name file or list of label names
  • top_k (int) – draw top_k bboxes only if positive
  • bbox_color (Color or tuple) – color to draw bboxes
  • text_color (Color or tuple) – color to draw label texts
  • thickness (int) – thickness of bbox lines
  • font_scale (float) – font scales
  • show (bool) – whether to show the image
  • win_name (str) – the window name
  • wait_time (int) – value of waitKey param
  • out_file (str or None) – the filename to write the image
cvbase.visualize.show_img(img, win_name='', wait_time=0)[source]

Show an image

Parameters:
  • img (str or ndarray) – the image to be shown
  • win_name (str) – the window name
  • wait_time (int) – value of waitKey param

det

cvbase.det.bbox_ops.bbox2roi(bbox_list, stack=True)[source]

Convert bboxes to rois by adding index at the first col.

Parameters:
  • bbox_list (list) – a list of ndarray (k_i, 4)
  • stack (bool) – whether to stack all the rois
Returns:

rois of shape (sum_k, 4)

Return type:

ndarray or list

cvbase.det.bbox_ops.bbox_clip(bboxes, img_shape)[source]

Limit bboxes to fit the image size

Parameters:
  • bboxes (ndarray) – shape (…, 4*k)
  • img_shape (tuple) – (height, width)
cvbase.det.bbox_ops.bbox_denormalize(deltas, means=[0, 0, 0, 0], stds=[1, 1, 1, 1])[source]

Denormalize bbox deltas

Parameters:
  • deltas (ndarray) – shape(…, 4*k)
  • means (ndarray or list) – shape(4, ) or (4*k, )
  • stds (ndarray or list) – shape(4, ) or (4*k, )
Returns:

denormalized deltas, same shape as input deltas

Return type:

ndarray

cvbase.det.bbox_ops.bbox_flip(bboxes, img_shape)[source]

Flip bboxes horizontally

Parameters:
  • bboxes (ndarray) – shape (…, 4*k)
  • img_shape (tuple) – (height, width)
cvbase.det.bbox_ops.bbox_normalize(deltas, means=[0, 0, 0, 0], stds=[1, 1, 1, 1])[source]

Normalize bbox deltas

Parameters:
  • deltas (ndarray) – shape(…, 4*k)
  • means (ndarray or list) – shape(4, ) or (4*k, )
  • stds (ndarray or list) – shape(4, ) or (4*k, )
Returns:

normalized deltas, same shape as input deltas

Return type:

ndarray

cvbase.det.bbox_ops.bbox_overlaps(bboxes1, bboxes2, mode='iou')[source]

Calculate the ious between each bbox of bboxes1 and bboxes2

Parameters:
  • bboxes1 (ndarray) – shape (n, 4)
  • bboxes2 (ndarray) – shape (k, 4)
  • mode (str) – iou (intersection over union) or iof (intersection over foreground)
Returns:

shape (n, k)

Return type:

ious(ndarray)

cvbase.det.bbox_ops.bbox_perturb(bbox, offset_ratio, num, clip_shape=None, min_iou=None, max_iou=None, max_try=20)[source]

Perturb a bbox around it to generate more bboxes

Parameters:
  • bbox (ndarray) – shape(4,)
  • offset_ratio (float) – max offset ratio (w.r.t the bbox w and h)
  • num (int) – number of bboxes to be generated
  • clip_shape (None or tuple) – (h, w)
  • min_iou (float) – minimum iou of perturbed bboxes with original bbox
  • max_iou (float) – maximum iou of perturbed bboxes with original bbox
Returns:

perturbed bboxes of shape (num, 4)

Return type:

ndarray

cvbase.det.bbox_ops.bbox_scaling(bboxes, scale, clip_shape=None)[source]

Scaling bboxes and clip the boundary(optional)

Parameters:
  • bboxes (ndarray) – shape(…, 4)
  • scale (float) – scaling factor
  • clip (None or tuple) – (h, w)
Returns:

scaled bboxes

Return type:

ndarray

cvbase.det.bbox_ops.bbox_transform(proposals, gt, means=[0, 0, 0, 0], stds=[1, 1, 1, 1])[source]

Calculate regression deltas from proposals and ground truths

dx = (gx - px) / pw, dw = log(gw / pw)

Parameters:
  • proposals (ndarray) – shape (…, 4)
  • gt (ndarray) – shape (…, 4) or (1.., 4)
Returns:

same shape as proposals

Return type:

ndarray

cvbase.det.bbox_ops.bbox_transform_inv(bboxes, deltas, means=[0, 0, 0, 0], stds=[1, 1, 1, 1])[source]

Get ground truth bboxes from input bboxes and deltas

gw = pw * exp(dw), gx = px + dx * pw

Parameters:
  • bboxes (ndarray) – shape (…, 4) [x1, y1, x2, y2]
  • deltas (ndarray) – shape (…, 4*k) [dx, dy, dw, dh]
Returns:

same shape as input deltas

Return type:

ndarray

cvbase.det.bbox_ops.flat2list(bbox_flat, num_classes=30)[source]

Convert a flat array to a list of array

Parameters:
  • bbox_flat (list) – shape (sum_n, k+1)
  • num_classes (int) – num of classes, length of list
Returns:

a list of (n, k) arrays

Return type:

list

cvbase.det.bbox_ops.list2flat(bbox_list)[source]

Convert a list of bboxes to a numpy array with one col added

Parameters:bbox_list (list) – a list of (n, k) arrays
Returns:shape (sum_n, k+1)
Return type:ndarray
cvbase.det.eval.average_precision(recalls, precisions, mode='area')[source]

Calculate average precision (for single or multiple scales)

Parameters:
  • recalls (ndarray) – shape (num_scales, num_dets) or (num_dets, )
  • precisions (ndarray) – shape (num_scales, num_dets) or (num_dets, )
  • mode (str) – ‘area’ or ‘11points’, ‘area’ means calculating the area under precision-recall curve, ‘11points’ means calculating the average precision of recalls at [0, 0.1, …, 1]
Returns:

calculated average precision

Return type:

float or ndarray

cvbase.det.eval.bbox_recalls(gts, proposals, proposal_nums=None, iou_thrs=None, print_summary=True)[source]

Calculate recalls

Parameters:
  • gts (list or ndarray) – a list of arrays of shape (n, 4)
  • proposals (list or ndarray) – a list of arrays of shape (k, 4) or (k, 5)
  • proposal_nums (int or list of int or ndarray) – top N proposals
  • thrs (float or list or ndarray) – iou thresholds
Returns:

recalls of different ious and proposal nums

Return type:

ndarray

cvbase.det.eval.eval_map(det_results, gt_bboxes, gt_labels, gt_ignore=None, scale_ranges=None, iou_thr=0.5, dataset=None, print_summary=True)[source]

Evaluate mAP of a dataset

Parameters:
  • det_results (list) – a list of list, [[cls1_det, cls2_det, …], …]
  • gt_bboxes (list) – ground truth bboxes of each image, a list of K*4 array
  • gt_labels (list) – ground truth labels of each image, a list of K array
  • gt_ignore (list) – gt ignore indicators of each image, a list of K array
  • scale_ranges (list or None) – a list of tuples, [(min1, max1), (min2, max2), …]
  • iou_thr (float) – IoU threshold
  • dataset (None or str) – dataset name, there are minor differences in metrics for different datsets, e.g. “voc07”, “voc12”, “det”, “vid”
  • print_summary (bool) – whether to print the mAP summary
Returns:

(mAP, [dict, dict, …])

Return type:

tuple

cvbase.det.eval.get_cls_results(det_results, gt_bboxes, gt_labels, gt_ignore, class_id)[source]

Get det results and gt information of a certain class.

cvbase.det.eval.plot_iou_recall(*args, **kwargs)[source]

Plot IoU-Recalls curve

Parameters:
  • recalls (ndarray or list) – shape (k,)
  • iou_thrs (ndarray or list) – same shape as recalls
cvbase.det.eval.plot_num_recall(*args, **kwargs)[source]

Plot Proposal_num-Recalls curve

Parameters:
  • recalls (ndarray or list) – shape (k,)
  • proposal_nums (ndarray or list) – same shape as recalls
cvbase.det.eval.print_map_summary(mean_ap, results, dataset=None)[source]

Print mAP and results of each class

Parameters:
  • mean_ap (float) – calculated from eval_map
  • results (list) – calculated from eval_map
  • dataset (None or str or list) – get label names by dataset, see cvbase.read_labels()
cvbase.det.eval.print_recall_summary(recalls, proposal_nums, iou_thrs, row_idxs=None, col_idxs=None)[source]

Print recalls in a table

Parameters:
  • recalls (ndarray) – calculated from bbox_recalls
  • proposal_nums (ndarray or list) – top N proposals
  • iou_thrs (ndarray or list) – iou thresholds
  • row_idxs (ndarray) – which rows(proposal nums) to print
  • col_idxs (ndarray) – which cols(iou thresholds) to print
cvbase.det.eval.set_recall_param(proposal_nums, iou_thrs)[source]

Check proposal_nums and iou_thrs and set correct format

cvbase.det.eval.tpfp_default(det_bboxes, gt_bboxes, gt_ignore, iou_thr, area_ranges=None)[source]

Check if detected bboxes are true positive or false positive.

Parameters:
  • det_bbox (ndarray) – the detected bbox
  • gt_bboxes (ndarray) – ground truth bboxes of this image
  • gt_ignore (ndarray) – indicate if gts are ignored for evaluation or not
  • iou_thr (float) – the iou thresholds
Returns:

(tp, fp), two arrays whose elements are 0 and 1

Return type:

tuple

cvbase.det.eval.tpfp_imagenet(det_bboxes, gt_bboxes, gt_ignore, default_iou_thr, area_ranges=None)[source]

Check if detected bboxes are true positive or false positive.

Parameters:
  • det_bbox (ndarray) – the detected bbox
  • gt_bboxes (ndarray) – ground truth bboxes of this image
  • gt_ignore (ndarray) – indicate if gts are ignored for evaluation or not
  • default_iou_thr (float) – the iou thresholds for medium and large bboxes
  • area_ranges (list or None) – gt bbox area ranges
Returns:

two arrays (tp, fp) whose elements are 0 and 1

Return type:

tuple

cvbase.det.labels.read_labels(dataset_or_file)[source]

Read labels from file or list

optflow

cvbase.optflow.io.dequantize_flow(dx, dy, max_val=0.02, denorm=True)[source]

Recover flow from quantized flow

Parameters:
  • dx (ndarray) – quantized dx
  • dy (ndarray) – quantized dy
  • max_val (float) – maximum value used when quantizing.
  • denorm (bool) – whether to multiply flow values with width/height
Returns:

dequantized dx and dy

Return type:

tuple

cvbase.optflow.io.quantize_flow(flow, max_val=0.02, norm=True)[source]

Quantize flow to [0, 255] (much smaller size when dumping as images)

Parameters:
  • flow (ndarray) – optical flow
  • max_val (float) – maximum value of flow, values beyond [-max_val, max_val] will be truncated.
  • norm (bool) – whether to divide flow values by width/height
Returns:

quantized dx and dy

Return type:

tuple

cvbase.optflow.io.read_flow(flow_or_path, quantize=False, *args, **kwargs)[source]

Read an optical flow map

Parameters:
  • flow_or_path (ndarray or str) – either a flow map or path of a flow
  • quantize (bool) – whether to read quantized pair, if set to True, remaining args will be passed to dequantize_flow()
Returns:

optical flow

Return type:

ndarray

cvbase.optflow.io.write_flow(flow, filename, quantize=False, *args, **kwargs)[source]

Write optical flow to file

Parameters:
  • flow (ndarray) – optical flow
  • filename (str) – file path
  • quantize (bool) – whether to quantize the flow and save as 2 images, if set to True, remaining args will be passed to quantize_flow()
cvbase.optflow.visualize.flow2rgb(flow, color_wheel=None, unknown_thr=1000000.0)[source]

Convert flow map to RGB image

Parameters:
  • flow (ndarray) – optical flow
  • color_wheel (ndarray or None) – color wheel used to map flow field to RGB colorspace. Default color wheel will be used if not specified
  • unknown_thr (str) – values above this threshold will be marked as unknown and thus ignored
Returns:

an RGB image that can be visualized

Return type:

ndarray

cvbase.optflow.visualize.make_color_wheel(bins=None)[source]

Build a color wheel

Parameters:bins (list or tuple, optional) – specify number of bins for each color range, corresponding to six ranges: red -> yellow, yellow -> green, green -> cyan, cyan -> blue, blue -> magenta, magenta -> red. [15, 6, 4, 11, 13, 6] is used for default (see Middlebury).
Returns:color wheel of shape (total_bins, 3)
Return type:ndarray
cvbase.optflow.visualize.show_flow(*args, **kwargs)[source]

Show optical flow

Parameters:
  • flow (ndarray or str) – optical flow to be shown
  • win_name (str) – the window name
  • wait_time (int) – value of waitKey param