API

label_convert

class cvtools.label_convert.VOC2COCO(root, mode='train', cls=['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'], cls_replace=None, use_xml_name=True, read_test=False)[源代码]

convert voc-like dataset to coco-like dataset

参数:
  • root (str) -- path include images, xml, file list
  • mode (str) -- 'train', 'val', 'trainval', 'test'. used to find file list.
  • cls (str or list) -- class name in a file or a list.
  • cls_replace (dict) -- a dictionary for replacing class name. if not needed, you can just ignore it.
  • use_xml_name (bool) -- image filename source, if true, using the same name as xml for the image, otherwise using 'filename' in xml context for the image.
  • read_test (bool) -- Test if the picture can be read normally.
class cvtools.label_convert.DOTA2COCO(label_root, image_root, classes=['large-vehicle', 'swimming-pool', 'helicopter', 'bridge', 'plane', 'ship', 'soccer-ball-field', 'basketball-court', 'ground-track-field', 'small-vehicle', 'harbor', 'baseball-diamond', 'tennis-court', 'roundabout', 'storage-tank'], path_replace=None, box_form='x1y1wh')[源代码]

convert DOTA labels to coco-like format labels.

参数:
  • label_root (str) -- label file path, for example, '/home/data/DOTA/train/labelTxt'
  • image_root (str) -- image path, for example, '/home/data/DOTA/train/images'
  • classes (str or list) -- class name in a file or a list.
  • path_replace (dict) -- replace same things in images path, if not needed, you can just ignore it.
  • box_form (str) -- coco bbox format, default 'x1y1wh'.
class cvtools.label_convert.COCO2Dets(anns_file, num_coors=4)[源代码]

将DOTA-COCO兼容格式GT转成检测结果表达形式results,保存成pkl results: {

image_id: dets, # image_id必须是anns中有效的id image_id: dets, ...

} dets: {

cls_id:[[位置坐标,得分], [...], ...], cls_id: [[位置坐标,得分], [...], ...], ...

},

handle_ann(ann)[源代码]

如果想自定义ann处理方式,继承此类,然后重新实现此方法

data_augs

class cvtools.data_augs.Compose(transforms)[源代码]

Composes several augmentations together. :param transforms: list of transforms to compose. :type transforms: List[Transform]

Example

>>> augmentations.Compose([
>>>     transforms.CenterCrop(10),
>>>     transforms.ToTensor(),
>>> ])
class cvtools.data_augs.RandomSampleCrop[源代码]

Crop :param img: the image being input during training :type img: Image :param boxes: the original bounding boxes in pt form :type boxes: Tensor :param labels: the class labels for each bbox :type labels: Tensor :param mode: the min and max jaccard overlaps :type mode: float tuple

返回:
(img, boxes, classes)
img (Image): the cropped image boxes (Tensor): the adjusted bounding boxes in pt form labels (Tensor): the class labels for each bbox
class cvtools.data_augs.RandomRotate[源代码]

随机旋转0度、90度、180度、270度

class cvtools.data_augs.RandomVerMirror[源代码]

竖直方向(flipping around the x-axis)镜像

class cvtools.data_augs.RandomHorMirror[源代码]

水平方向(flipping around the y-axis)镜像

label_analysis

class cvtools.label_analysis.COCOAnalysis(img_prefix, ann_file=None)[源代码]

coco-like datasets analysis

vis_instances(save_root, vis='bbox', vis_cats=None, output_by_cat=False, box_format='x1y1wh')[源代码]

Visualise bbox and polygon in annotation.

包含某一类的图片上所有类别均会被绘制。

参数:
  • save_root (str) -- path for saving image.
  • vis (str) -- 'bbox' or 'segmentation'
  • vis_cats (list) -- categories to be visualized
  • output_by_cat (bool) -- output visual images by category.
  • box_format (str) -- 'x1y1wh' or 'polygon'

evaluation

cvtools.evaluation.get_classes(dataset)[源代码]

Get class names of a dataset.

cvtools.evaluation.average_precision(recalls, precisions, mode='area')[源代码]

Calculate average precision (for single or multiple scales).

参数:
  • 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]
返回:

calculated average precision

返回类型:

float or ndarray

cvtools.evaluation.eval_map(det_results, gt_bboxes, gt_labels, gt_ignore=None, scale_ranges=None, iou_thr=0.5, dataset=None, print_summary=True, calc_ious=<function bbox_overlaps>)[源代码]

Evaluate mAP of a dataset.

参数:
  • det_results (list) -- a list of list, [[cls1_det, cls2_det, ...], ...] cls1_det为np.array,包含K*5,包含得分, x1y1x2y2形式
  • gt_bboxes (list) -- ground truth bboxes of each image, a list of K*4 array. x1y1x2y2形式
  • 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, optional) -- [(min1, max1), (min2, max2), ...]
  • iou_thr (float) -- IoU threshold,目前还不支持polyiou
  • dataset (None or str or list) -- dataset name or dataset classes, there are minor differences in metrics for different datsets, e.g. "voc07", "imagenet_det", etc.
  • print_summary (bool) -- whether to print the mAP summary
返回:

(mAP, [dict, dict, ...])

返回类型:

tuple

cvtools.evaluation.print_map_summary(mean_ap, results, dataset=None)[源代码]

Print mAP and results of each class.

参数:
  • mean_ap (float) -- calculated from eval_map
  • results (list) -- calculated from eval_map
  • dataset (None or str or list) -- dataset name or dataset classes.
cvtools.evaluation.eval_recalls(gts, proposals, proposal_nums=None, iou_thrs=None, print_summary=True)[源代码]

Calculate recalls.

参数:
  • 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
返回:

recalls of different ious and proposal nums

返回类型:

ndarray

cvtools.evaluation.print_recall_summary(recalls, proposal_nums, iou_thrs, row_idxs=None, col_idxs=None)[源代码]

Print recalls in a table.

参数:
  • 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
cvtools.evaluation.plot_num_recall(recalls, proposal_nums)[源代码]

Plot Proposal_num-Recalls curve.

参数:
  • recalls (ndarray or list) -- shape (k,)
  • proposal_nums (ndarray or list) -- same shape as recalls
cvtools.evaluation.plot_iou_recall(recalls, iou_thrs)[源代码]

Plot IoU-Recalls curve.

参数:
  • recalls (ndarray or list) -- shape (k,)
  • iou_thrs (ndarray or list) -- same shape as recalls
class cvtools.evaluation.EvalCropQuality(ann_file, crop_ann_file, results=None, num_coors=4)[源代码]

此类设计目前不够完善,convert_crop_gt应隐藏在内部

file_io

cvtools.file_io.load_json(file)[源代码]

加载json文件

参数:file -- 包含路径的文件名

Returns:

cvtools.file_io.load_pkl(file)[源代码]

加载pickle序列化对象

参数:file -- 包含路径的文件名
返回:unpickle object
Raises:UnpicklingError
cvtools.file_io.readlines(file)[源代码]

按行读取str到list

参数:file -- 包含路径的文件名

Returns:

cvtools.file_io.read_file_to_list(file)[源代码]

读入单个文件输出list,支持中文

参数:file -- 包含路径的文件名
返回:所有文件内容放在list中返回
cvtools.file_io.read_files_to_list(files, root='')[源代码]

读入单个或多个文件合成一个list输出,支持中文

此函数设计是一个教训,只有必要的参数才能设计成位置参数,其它参数为关键字参数

参数:
  • files (str) -- 文件名
  • root (root) -- 可选,文件名路径。如果指定files不可加路径
cvtools.file_io.read_key_value(file)[源代码]

支持注释,支持中文

参数:file (str) -- 包含路径的文件名
cvtools.file_io.dump_json(data, to_file='data.json')[源代码]

写json文件

参数:
  • data -- 待保存成json格式的对象
  • to_file -- 保存的文件名
cvtools.file_io.dump_pkl(data, to_file='data.pkl')[源代码]

使用pickle序列化对象

参数:
  • data -- 待序列化对象
  • to_file -- 保存的文件名
cvtools.file_io.write_list_to_file(data, dst, line_break=True)[源代码]

保存list到文件

参数:
  • data (list) -- list中元素只能是基本类型
  • dst (str) -- 保存的文件名
  • line_break -- 是否加换行

Returns:

cvtools.file_io.write_key_value(data, to_file)[源代码]

写字典到文件中(非序列化)

每行以字符':'分割key和value

参数:
  • data (dict) -- dict中元素只能是基本类型
  • to_file -- 保存的文件名

Returns:

cvtools.file_io.write_str(data, to_file)[源代码]

写字符串到文件

参数:
  • data (str) -- str对象
  • to_file (str) -- 保存的文件名

utils

cvtools.utils.get_files_list(root, file_type=None, basename=False)[源代码]

file_type is a str or list.

cvtools.utils.makedirs(path)[源代码]

对os.makedirs进行扩展

从路径中创建文件夹,可创建多层。如果仅是文件名,则无须创建,返回False; 如果是已存在文件或路径,则无须创建,返回False

参数:path -- 路径,可包含文件名。纯路径最后一个字符需要是os.sep
cvtools.utils.find_in_path(name, path)[源代码]

Find a file in a search path

cvtools.utils.imread(img_or_path, flag='color')[源代码]

Read an image.

参数:
  • img_or_path (ndarray or str) -- Either a numpy array or image path. If it is a numpy array (loaded image), then it will be returned as is.
  • flag (str) -- Flags specifying the color type of a loaded image, candidates are color, grayscale and unchanged.
返回:

Loaded image array.

返回类型:

ndarray

cvtools.utils.imwrite(img, file_path, params=None, auto_mkdir=True)[源代码]

Write image to file

参数:
  • img (ndarray) -- Image array to be written.
  • file_path (str) -- Image file path.
  • params (None or list) -- Same as opencv's imwrite() interface.
  • auto_mkdir (bool) -- If the parent folder of file_path does not exist, whether to create it automatically.
返回:

Successful or not.

返回类型:

bool

cvtools.utils.draw_boxes_texts(img, boxes, texts=None, colors=None, line_width=1, draw_start=False, box_format='x1y1x2y2')[源代码]

Draw bboxes on an image.

参数:
  • img (str or ndarray) -- The image to be displayed.
  • boxes (list or ndarray) -- A list of ndarray of shape (k, 4).
  • texts (list) -- A list of shape (k).
  • colors (list[tuple or Color]) -- A list of colors.
  • line_width (int) -- Thickness of lines.
  • draw_start (bool) -- Draw a dot at the first vertex of the box.
  • box_format (str) -- x1y1x2y2(default), x1y1wh, xywh, xywha, polygon
cvtools.utils.draw_class_distribution(y, save_name='class_distribution.png')[源代码]

绘制饼图,其中y是标签列表

cvtools.utils.draw_hist(data, bins=10, x_label='区间', y_label='频数/频率', title='频数/频率分布直方图', show=True, save_name='hist.png', density=True)[源代码]

绘制直方图 data: 必选参数,绘图数据 bins: 直方图的长条形数目,可选项,默认为10

cvtools.utils.x1y1wh_to_x1y1x2y2(xywh)[源代码]

Convert [x1 y1 w h] box format to [x1 y1 x2 y2] format. supported type: list, type and np.ndarray

cvtools.utils.x1y1x2y2_to_x1y1wh(xyxy)[源代码]

Convert [x1 y1 x2 y2] box format to [x1 y1 w h] format.

cvtools.utils.xywh_to_x1y1x2y2(xywh)[源代码]

Convert [x y w h] box format to [x1 y1 x2 y2] format.

cvtools.utils.x1y1x2y2_to_xywh(x1y1x2y2)[源代码]

Convert [x1 y1 x2 y2] box format to [x y w h] format.

cvtools.utils.x1y1wh_to_xywh(x1y1wh)[源代码]

Convert [x1 y1 w h] box format to [x y w h] format. supported type: list, type and np.ndarray

cvtools.utils.rotate_rect(rect, center, angle)[源代码]

一个数学问题:2x2矩阵(坐标)与旋转矩阵相乘. 在笛卡尔坐标系中,angle>0, 逆时针旋转; angle<0, 顺时针旋转

参数:
  • rect -- x1y1x2y2形式矩形
  • center -- 旋转中心点
  • angle -- 旋转角度,范围在(-180, 180)
返回:

x1y1x2y2x3y3x4y4 format box

cvtools.utils.xywha_to_x1y1x2y2x3y3x4y4(xywha)[源代码]

用旋转的思路做变换是最通用和最简单的

警告:目前多维一起操作还有些问题!

参数:xywha -- (5,)一维list或(K, 5)多维array
class cvtools.utils.Timer[源代码]

A simple timer.

cvtools.utils.get_time_str(form='%Y%m%d_%H%M%S')[源代码]

for example form='%Y%m%d_%H%M%S_%f'

cvtools.utils.bbox_overlaps(bboxes1, bboxes2, mode='iou')[源代码]

Calculate the ious between each bbox of bboxes1 and bboxes2.

参数:
  • bboxes1 (ndarray) -- shape (n, 4)
  • bboxes2 (ndarray) -- shape (k, 4)
  • mode (str) -- iou (intersection over union) or iof (intersection over foreground)
返回:

shape (n, k)

返回类型:

ious(ndarray)

cvtools.utils.is_str(x)[源代码]

Whether the input is an string instance.

cvtools.utils.iter_cast(inputs, dst_type, return_type=None)[源代码]

Cast elements of an iterable object into some type.

参数:
  • inputs (Iterable) -- The input object.
  • dst_type (type) -- Destination type.
  • return_type (type, optional) -- If specified, the output object will be converted to this type, otherwise an iterator.
返回:

The converted object.

返回类型:

iterator or specified type

cvtools.utils.list_cast(inputs, dst_type)[源代码]

Cast elements of an iterable object into a list of some type.

A partial method of iter_cast().

cvtools.utils.tuple_cast(inputs, dst_type)[源代码]

Cast elements of an iterable object into a tuple of some type.

A partial method of iter_cast().

cvtools.utils.is_seq_of(seq, expected_type, seq_type=None)[源代码]

Check whether it is a sequence of some type.

参数:
  • seq (Sequence) -- The sequence to be checked.
  • expected_type (type) -- Expected type of sequence items.
  • seq_type (type, optional) -- Expected sequence type.
返回:

Whether the sequence is valid.

返回类型:

bool

cvtools.utils.is_list_of(seq, expected_type)[源代码]

Check whether it is a list of some type.

A partial method of is_seq_of().

cvtools.utils.is_tuple_of(seq, expected_type)[源代码]

Check whether it is a tuple of some type.

A partial method of is_seq_of().

cvtools.utils.slice_list(in_list, lens)[源代码]

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

参数:
  • in_list (list) -- The list to be sliced.
  • lens (int or list) -- The expected length of each out list.
返回:

A list of sliced list.

返回类型:

list

cvtools.utils.concat_list(in_list)[源代码]

Concatenate a list of list into a single list.

参数:in_list (list) -- The list of list to be merged.
返回:The concatenated flat list.
返回类型:list