コード例 #1
0
ファイル: test_itertoolz.py プロジェクト: Xuefeng-Zhu/toolz
def test_topk():
    assert topk(2, [4, 1, 5, 2]) == (5, 4)
    assert topk(2, [4, 1, 5, 2], key=lambda x: -x) == (1, 2)
    assert topk(2, iter([5, 1, 4, 2]), key=lambda x: -x) == (1, 2)

    assert topk(2, [{'a': 1, 'b': 10}, {'a': 2, 'b': 9},
                    {'a': 10, 'b': 1}, {'a': 9, 'b': 2}], key='a') == \
        ({'a': 10, 'b': 1}, {'a': 9, 'b': 2})

    assert topk(2, [{'a': 1, 'b': 10}, {'a': 2, 'b': 9},
                    {'a': 10, 'b': 1}, {'a': 9, 'b': 2}], key='b') == \
        ({'a': 1, 'b': 10}, {'a': 2, 'b': 9})
コード例 #2
0
ファイル: test_itertoolz.py プロジェクト: 183amir/toolz
def test_topk():
    assert topk(2, [4, 1, 5, 2]) == (5, 4)
    assert topk(2, [4, 1, 5, 2], key=lambda x: -x) == (1, 2)
    assert topk(2, iter([5, 1, 4, 2]), key=lambda x: -x) == (1, 2)

    assert topk(2, [{'a': 1, 'b': 10}, {'a': 2, 'b': 9},
                    {'a': 10, 'b': 1}, {'a': 9, 'b': 2}], key='a') == \
        ({'a': 10, 'b': 1}, {'a': 9, 'b': 2})

    assert topk(2, [{'a': 1, 'b': 10}, {'a': 2, 'b': 9},
                    {'a': 10, 'b': 1}, {'a': 9, 'b': 2}], key='b') == \
        ({'a': 1, 'b': 10}, {'a': 2, 'b': 9})
コード例 #3
0
def segment_batch_of_images(model, list_of_images, list_of_filenames):

    results = model.detect(list(list_of_images), verbose=1)

    def area(box):
        top, left, bottom, right = box
        return abs(bottom - top) * abs(right - left)

    def max_bound(item):
        if set(max_bounded_box) == set(item):
            return 1
        else:
            return 0

    for counter, output in enumerate(results):

        x = topk(1, output['rois'], key=area)

        if not x:
            print("No bounding Box detected")
        else:
            max_bounded_box = list(x[0])
            max_mask_id = 0

            for index, item in enumerate(map(max_bound, output['rois'])):
                if item == 1:
                    max_mask_id = index
                    break

            mask_img = output['masks'][:, :, max_mask_id]
            mask_rgb = cv2.cvtColor(mask_img, cv2.COLOR_GRAY2RGB)
            img = list_of_images[counter]
            new_Image = np.asarray(img) * (mask_rgb != 0)

            # Try and run with even black background by commenting the below one line

            new_Image = np.where(new_Image == 0, 255, new_Image)
            new_Image = Image.fromarray(new_Image.astype('uint8'), 'RGB')

            if not list_of_filenames:

                new_Image.save(
                    os.path.join(args.output_image_path,
                                 "filename_zero" + str(counter) + '.jpg'))

            else:

                new_Image.save(
                    os.path.join(args.output_image_path,
                                 list_of_filenames[counter]))
コード例 #4
0
def segment_images(model, image):
    new_image_path = os.path.join(args.output_image_path,
                                  image['image_id']) + '.jpg'
    if os.path.isfile(new_image_path):
        print(f'Skipping previously segmened image {image["image_id"]}.')
        return image['image_id']

    results = model.detect([image['image']], verbose=1)
    output = results[0]

    def area(box):
        top, left, bottom, right = box
        return abs(bottom - top) * abs(right - left)

    def max_bound(item):
        if set(max_bounded_box) == set(item):
            return 1
        else:
            return 0

    x = topk(1, output['rois'], key=area)
    if not x:
        print("No bounding Box detected")
    else:
        max_bounded_box = list(x[0])
        max_mask_id = 0

        for index, item in enumerate(map(max_bound, output['rois'])):
            if item == 1:
                max_mask_id = index
                break
        mask_img = output['masks'][:, :, max_mask_id]
        mask_rgb = cv2.cvtColor(mask_img, cv2.COLOR_GRAY2RGB)
        new_image = image['image'] * (mask_rgb != 0)
        new_image = np.where(mask_rgb == 0, 255, new_image)
        new_image = Image.fromarray(new_image, 'RGB')
        new_image.save(new_image_path)

    return image['image_id']
コード例 #5
0
ファイル: test_itertoolz.py プロジェクト: zhouyujoe/toolz
def test_topk_is_stable():
    assert topk(4, [5, 9, 2, 1, 5, 3], key=lambda x: 1) == (5, 9, 2, 1)
コード例 #6
0
ファイル: test_itertoolz.py プロジェクト: caioaao/toolz
def test_topk_is_stable():
    assert topk(4, [5, 9, 2, 1, 5, 3], key=lambda x: 1) == (5, 9, 2, 1)
コード例 #7
0
def predict_discrete(limit, distance_fn, fm):
    nearest_neighbors = topk(limit,
                             trainset_fms,
                             key=lambda x: -distance_fn(x[1], fm))

    return map(op.itemgetter(0), nearest_neighbors)