Get start

install

pip

1pip install svkcore

from source

1git clone https://github.com/ZhaoJianghua/simple-vision-kit.git
2cd simple-vision-kit
3python setup.py install

usage

common

  • io: load and save data more convenient

 1from svkcore import common
 2
 3# save & load json
 4data = {'data': 1, "message": "hello"}
 5common.save_json(data, 'user_data.json')
 6data = common.load_json('user_data.json')
 7
 8# save & load pickle
 9data = {'data': np.array([1]), "message": "hello"}
10common.save_pickle(data, 'user_data.pkl')
11data = common.load_pickle(data, 'user_data.pkl')
12
13# read & write image platform compatile with win system
14img = common.cv2imread("path")
15common.cv2imwrite("path", img)
  • collect data: collect from a directory

 1from svkcore import common
 2
 3# collect images/annotations
 4img_ps = common.find_file_recursive(directory="root", suffixes=[".jpg"], ignore_case=True)
 5print(img_ps[:1])
 6# get basename head
 7hd = common.bsn_head("root/xxx/xxxx.jpg")
 8print(hd)
 9# collect examples
10examples = common.collect_examples(directory="dir", suffixes_list=[[".jpg"], [".xml"]])
11print(examples[:1])
12# or use collect_pascal_data to collect pascal format data
13examples = common.collect_pascal_data(directory="dir")
14print(examples[:1])
  • numpy operations: some useful numpy operations

1indexes = common.ndarray_index(shape=[7, 8])
2distances = common.points_distance(points0=[[0, 0]], points1=[[1, 1]])
3grids = common.generate_grid(panel_size=[1024, 1024], grid_size=(256, 256),
4                             overlap_size=(24, 24))
5# common.seg2point(seg, max_diameter: int, min_distance, fb_threshold: float = 0.5,
6#                  min_fore_count: int = 1, max_fore_count: int = -1,
7#                  avg_fore_score: float = 0.55, distance_weights=(1., 1.))
8# common.seg2line(seg, fb_threshold=0.5, smooth_width=3, partition_width=20,
9#                 partition_height=30)

annotation

Pascal and coco annotation load, save and convert.

 1from svkcore import annotation
 2
 3# load pascal annotation
 4ann = annotation.DTAnnotation.load("pascal_annotation_file")
 5for obj in ann:
 6    print(obj.name)
 7    print(obj.bndbox)
 8    print(obj.difficult)
 9    obj.name = obj.name + "-new"
10ann.dump("pascal_annotation_file_new")
11
12# convert pascal data to coco format
13dataset = annotation.DTDataset.load_pascal(annotation_paths=[],
14                                           image_paths=[])
15dataset.dump_coco("coco_format_dataset_file")

shapes

Base shapes which usually be used in image tasks.

 1from svkcore import shapes
 2
 3# shapes and operations
 4point0 = shapes.Point([0, 0])
 5point1 = shapes.Point([1, 1])
 6points = shapes.Points([point0, point1])
 7bndbox0 = points.bounding_box()
 8bndbox1 = shapes.Box([0, 0, 1, 1])
 9bsize = bndbox1.bsize()
10center = bndbox1.center()
11polygon = bndbox1.to_polygon()
12mask = bndbox1.to_mask()
13bndbox2 = polygon.bounding_box()
14area = polygon.area()

visualize

Visualize part for visualize common shapes.

 1from PIL import Image
 2from svkcore import visualize
 3from svkcore import annotation
 4
 5pil_image = Image.new("RGB", [600, 600])
 6boxes = [[100, 200, 400, 300]]
 7visualize.draw_boxes(pil_image, boxes=boxes)
 8visualize.draw_texts(pil_image, xys=[(100, 200)], texts=["box"])
 9visualize.draw_points(pil_image, points=[[50, 50]])
10visualize.draw_lines(pil_image, lines=[[0, 0], [300, 300]])
11
12ann = annotation.DTAnnotation.load("path")
13visualize.draw_annotation(pil_image, ann, name2cls={},
14                          add_unknown_name=True)