def crop_random_sample_from_image(crop_width, crop_height, image, scales):

    height, width, _ = image.shape

    def check_scale(scale):
        scaled_width, scaled_height = int(width / scale), int(height / scale)
        return (scaled_width > crop_width) and (scaled_height > crop_height)

    valid_scales = [s for s in scales if check_scale(s)]

    if len(valid_scales) == 0:
        raise RuntimeError("Receive an image where "
                           "no scale would fit a sample")

    scale = random.choice(valid_scales)

    scaled_width, scaled_height = int(width / scale), int(height / scale)

    min_x = random.randint(0, scaled_width - crop_width)
    min_y = random.randint(0, scaled_height - crop_height)
    assert (min_x >= 0) and (min_y >= 0)

    # resize image, crop sample
    resized_image = cmtd.resized_image(image, scaled_width, scaled_height)
    assert resized_image.shape[:2] == (scaled_height, scaled_width)

    sample = resized_image[min_y:min_y + crop_height,
                           min_x:min_x + crop_width, :]
    assert sample.shape[:2] == (crop_height, crop_width)

    return sample
def crop_random_sample_from_image(crop_width, crop_height, image, scales):

    height, width, _ = image.shape

    def check_scale(scale):
        scaled_width, scaled_height = int(width / scale), int(height / scale)
        return (scaled_width > crop_width) and (scaled_height > crop_height)

    valid_scales = [s for s in scales if check_scale(s)]

    if len(valid_scales) == 0:
        raise RuntimeError("Receive an image where "
                           "no scale would fit a sample")

    scale = random.choice(valid_scales)

    scaled_width, scaled_height = int(width / scale), int(height / scale)

    min_x = random.randint(0, scaled_width - crop_width)
    min_y = random.randint(0, scaled_height - crop_height)
    assert (min_x >= 0) and (min_y >= 0)

    # resize image, crop sample
    resized_image = cmtd.resized_image(image,
                                       scaled_width, scaled_height)
    assert resized_image.shape[:2] == (scaled_height, scaled_width)

    sample = resized_image[min_y: min_y + crop_height,
                           min_x: min_x + crop_width,
                           :]
    assert sample.shape[:2] == (crop_height, crop_width)

    return sample
def create_negatives_process_file(negatives_counter, file_path,
                                  model_width, model_height, negatives_path):

    negative_image = cv2.imread(file_path)

    # negative images are 1.5 bigger than the model
    model_to_negative_factor = 1.5
    minimum_image_shape = (model_height * model_to_negative_factor,
                           model_width * model_to_negative_factor)

    # <= because the C++ code has some weird +1's
    if (negative_image.shape[1] <= minimum_image_shape[1]) or  \
       (negative_image.shape[0] <= minimum_image_shape[0]):
        # must resize the image
        negative_filename = "negative_sample_%i.resized.png" \
                            % negatives_counter

        # width / height
        image_ratio = float(negative_image.shape[1]) / negative_image.shape[0]
        if (negative_image.shape[1] <= minimum_image_shape[1]):
            resized_size = (int(minimum_image_shape[1]),
                            int(minimum_image_shape[1] / image_ratio))

        if (negative_image.shape[0] <= minimum_image_shape[0]):
            resized_size = (int(minimum_image_shape[0] * image_ratio),
                            int(minimum_image_shape[0]))

        resized_shape = (resized_size[1],
                         resized_size[0],
                         negative_image.shape[2])
        assert resized_shape[0] >= minimum_image_shape[0]
        assert resized_shape[1] >= minimum_image_shape[1]

        negative_image_resized = resized_image(negative_image,
                                               resized_size[0],
                                               resized_size[1])
        negative_image = negative_image_resized
    else:
        # we can just copy
        negative_filename = "negative_sample_%i.png" % negatives_counter

    image_output_path = os.path.join(negatives_path, negative_filename)
    cv2.imwrite(image_output_path, negative_image)
    print(".", end="")
    global print_counter
    print_counter += 1  # no need to be thread safe
    if print_counter % 10:
        sys.stdout.flush()
    return
def create_negatives_process_file(negatives_counter, file_path, model_width,
                                  model_height, negatives_path):

    negative_image = cv2.imread(file_path)

    # negative images are 1.5 bigger than the model
    model_to_negative_factor = 1.5
    minimum_image_shape = (model_height * model_to_negative_factor,
                           model_width * model_to_negative_factor)

    # <= because the C++ code has some weird +1's
    if (negative_image.shape[1] <= minimum_image_shape[1]) or  \
       (negative_image.shape[0] <= minimum_image_shape[0]):
        # must resize the image
        negative_filename = "negative_sample_%i.resized.png" \
                            % negatives_counter

        # width / height
        image_ratio = float(negative_image.shape[1]) / negative_image.shape[0]
        if (negative_image.shape[1] <= minimum_image_shape[1]):
            resized_size = (int(minimum_image_shape[1]),
                            int(minimum_image_shape[1] / image_ratio))

        if (negative_image.shape[0] <= minimum_image_shape[0]):
            resized_size = (int(minimum_image_shape[0] * image_ratio),
                            int(minimum_image_shape[0]))

        resized_shape = (resized_size[1], resized_size[0],
                         negative_image.shape[2])
        assert resized_shape[0] >= minimum_image_shape[0]
        assert resized_shape[1] >= minimum_image_shape[1]

        negative_image_resized = resized_image(negative_image, resized_size[0],
                                               resized_size[1])
        negative_image = negative_image_resized
    else:
        # we can just copy
        negative_filename = "negative_sample_%i.png" % negatives_counter

    image_output_path = os.path.join(negatives_path, negative_filename)
    cv2.imwrite(image_output_path, negative_image)
    print(".", end="")
    global print_counter
    print_counter += 1  # no need to be thread safe
    if print_counter % 10:
        sys.stdout.flush()
    return
Example #5
0
def compute_resized_positive_example(input_image, input_image_path, box,
                                     box_scale, model_octave, cropping_border,
                                     model_width, model_height):
    """
    The input box is expected to be a tight box around the pedestrian
    """

    assert input_image != None
    assert type(model_width) is int
    assert type(model_height) is int
    assert type(cropping_border) is int

    image_height, image_width, image_depth = input_image.shape

    # adjust the ratio, add the border --

    # adjusting the ratio is a bad idea, pedestrians look "fat"
    # instead, we adjust the left/right border
    #box = adjust_box_ratio(box, model_width, model_height)

    example_width = model_width + (2 * cropping_border)
    example_height = model_height + (2 * cropping_border)

    model_scale = 2**model_octave
    box_relative_scale = box_scale / model_scale
    desired_input_box_width = example_width * box_relative_scale
    desired_input_box_height = example_height * box_relative_scale

    input_box_width, input_box_height = box.width(), box.height()

    top_bottom_border = (desired_input_box_height - input_box_height) / 2.0
    left_right_border = (desired_input_box_width - input_box_width) / 2.0

    desired_input_box_shape = (desired_input_box_height,
                               desired_input_box_width)

    if (left_right_border <= 0) or False:
        print("model width, height ==", (model_width, model_height))
        print("example_width ==", example_width)
        print("box_scale ==", box_scale)
        print("model_scale ==", model_scale)
        print("box_relative_scale ==", box_relative_scale)
        print("desired_input_box_width ==", desired_input_box_width)
        print("input_box_width ==", input_box_width)
        print("input_box_height ==", input_box_height)
        print("input_box ratio ==", input_box_height / float(input_box_width))
        p1 = (int(box.min_corner.x), int(box.min_corner.y))
        p2 = (int(box.max_corner.x), int(box.max_corner.y))
        print("input_box min_corner, max_corner == ", p1, ",", p2)
        color = (255, 10, 50)
        failure_image = input_image.copy()
        cv2.rectangle(failure_image, p1, p2, color)
        global failures_count
        failure_filename = "failure_case_%i_%s" % (
            failures_count, os.path.basename(input_image_path))
        cv2.imwrite(failure_filename, failure_image)
        print("Created %s skipping one bounding_box in picture %s" %
              (failure_filename, input_image_path))
        failures_count += 1
        return None  # indicate that something went wrong

    if (top_bottom_border < 0):
        print("model width, height ==", (model_width, model_height))
        print("example_width ==", example_width)
        print("box_scale ==", box_scale)
        print("model_scale ==", model_scale)
        print("box_relative_scale ==", box_relative_scale)
        print("desired_input_box_width ==", desired_input_box_width)
        print("input_box_width ==", input_box_width)
        print("input_box_height ==", input_box_height)
        print("input_box ratio ==", input_box_height / float(input_box_width))
        print("desired width ==", desired_input_box_width)
        print("desired height==", desired_input_box_height)
        p1 = (int(box.min_corner.x), int(box.min_corner.y))
        p2 = (int(box.max_corner.x), int(box.max_corner.y))
        print("input_box min_corner, max_corner == ", p1, ",", p2)
        color = (255, 10, 50)
        failure_image = input_image.copy()
        cv2.rectangle(failure_image, p1, p2, color)
        global failures_count
        failure_filename = "failure_case_%i_%s" % (
            failures_count, os.path.basename(input_image_path))
        cv2.imwrite(failure_filename, failure_image)
        print("Created %s skipping one bounding_box in picture %s" %
              (failure_filename, input_image_path))
        failures_count += 1
        raise "adflkj"
        return None  # indicate that something went wrong

    box = adjust_box_border(box, top_bottom_border, left_right_border)
    # the box now has the desired_input_box_width and _height

    box_shape = (box.height(), box.width())
    #print(box_shape, "=?=", desired_input_box_shape)
    assert abs(box_shape[0] - desired_input_box_shape[0]) <= 1.0
    assert abs(box_shape[1] - desired_input_box_shape[1]) <= 1.0

    # crop the box --
    # which part of the box is outside the image ?
    left_border = int(max(0, 0 - box.min_corner.x))
    top_border = int(max(0, 0 - box.min_corner.y))
    right_border = int(max(0, box.max_corner.x - image_width))
    bottom_border = int(max(0, box.max_corner.y - image_height))

    # define the part of the box that is inside the image
    inner_box = box.copy()
    inner_box.min_corner.x = max(0, box.min_corner.x)
    inner_box.min_corner.y = max(0, box.min_corner.y)
    inner_box.max_corner.x = min(box.max_corner.x, image_width)
    inner_box.max_corner.y = min(box.max_corner.y, image_height)

    # crop
    cropped_image = input_image[
        inner_box.min_corner.y:inner_box.max_corner.y,
        inner_box.min_corner.x:inner_box.max_corner.x, :]
    # extrapolate --

    #border_type = cv2.BORDER_CONSTANT
    border_type = cv2.BORDER_REPLICATE
    #border_type = cv2.BORDER_WRAP
    #border_type = cv2.BORDER_REFLECT
    #border_type = cv2.BORDER_REFLECT_101
    cropped_image = cv2.copyMakeBorder(cropped_image, top_border,
                                       bottom_border, left_border,
                                       right_border, border_type)

    #print(cropped_image.shape[:2], "=?=", box_shape)
    #assert cropped_image.shape[:2] == box_shape

    # rescale the box so it fits the desired dimensions --
    resized_positive_example = cmtd.resized_image(cropped_image, example_width,
                                                  example_height)
    assert resized_positive_example.shape == (example_height, example_width,
                                              image_depth)

    return resized_positive_example
def compute_resized_positive_example(
    input_image, input_image_path, box, box_scale, model_octave, cropping_border, model_width, model_height
):
    """
    The input box is expected to be a tight box around the pedestrian
    """

    assert input_image != None
    assert type(model_width) is int
    assert type(model_height) is int
    assert type(cropping_border) is int

    image_height, image_width, image_depth = input_image.shape

    # adjust the ratio, add the border --

    # adjusting the ratio is a bad idea, pedestrians look "fat"
    # instead, we adjust the left/right border
    # box = adjust_box_ratio(box, model_width, model_height)

    example_width = model_width + (2 * cropping_border)
    example_height = model_height + (2 * cropping_border)

    model_scale = 2 ** model_octave
    box_relative_scale = box_scale / model_scale
    desired_input_box_width = example_width * box_relative_scale
    desired_input_box_height = example_height * box_relative_scale

    input_box_width, input_box_height = box.width(), box.height()

    top_bottom_border = (desired_input_box_height - input_box_height) / 2.0
    left_right_border = (desired_input_box_width - input_box_width) / 2.0

    desired_input_box_shape = (desired_input_box_height, desired_input_box_width)

    if (left_right_border <= 0) or False:
        print("model width, height ==", (model_width, model_height))
        print("example_width ==", example_width)
        print("box_scale ==", box_scale)
        print("model_scale ==", model_scale)
        print("box_relative_scale ==", box_relative_scale)
        print("desired_input_box_width ==", desired_input_box_width)
        print("input_box_width ==", input_box_width)
        print("input_box_height ==", input_box_height)
        print("input_box ratio ==", input_box_height / float(input_box_width))
        p1 = (int(box.min_corner.x), int(box.min_corner.y))
        p2 = (int(box.max_corner.x), int(box.max_corner.y))
        print("input_box min_corner, max_corner == ", p1, ",", p2)
        color = (255, 10, 50)
        failure_image = input_image.copy()
        cv2.rectangle(failure_image, p1, p2, color)
        global failures_count
        failure_filename = "failure_case_%i_%s" % (failures_count, os.path.basename(input_image_path))
        cv2.imwrite(failure_filename, failure_image)
        print("Created %s skipping one bounding_box in picture %s" % (failure_filename, input_image_path))
        failures_count += 1
        return None  # indicate that something went wrong

    if top_bottom_border < 0:
        print("model width, height ==", (model_width, model_height))
        print("example_width ==", example_width)
        print("box_scale ==", box_scale)
        print("model_scale ==", model_scale)
        print("box_relative_scale ==", box_relative_scale)
        print("desired_input_box_width ==", desired_input_box_width)
        print("input_box_width ==", input_box_width)
        print("input_box_height ==", input_box_height)
        print("input_box ratio ==", input_box_height / float(input_box_width))
        print("desired width ==", desired_input_box_width)
        print("desired height==", desired_input_box_height)
        p1 = (int(box.min_corner.x), int(box.min_corner.y))
        p2 = (int(box.max_corner.x), int(box.max_corner.y))
        print("input_box min_corner, max_corner == ", p1, ",", p2)
        color = (255, 10, 50)
        failure_image = input_image.copy()
        cv2.rectangle(failure_image, p1, p2, color)
        global failures_count
        failure_filename = "failure_case_%i_%s" % (failures_count, os.path.basename(input_image_path))
        cv2.imwrite(failure_filename, failure_image)
        print("Created %s skipping one bounding_box in picture %s" % (failure_filename, input_image_path))
        failures_count += 1
        raise "adflkj"
        return None  # indicate that something went wrong

    box = adjust_box_border(box, top_bottom_border, left_right_border)
    # the box now has the desired_input_box_width and _height

    box_shape = (box.height(), box.width())
    # print(box_shape, "=?=", desired_input_box_shape)
    assert abs(box_shape[0] - desired_input_box_shape[0]) <= 1.0
    assert abs(box_shape[1] - desired_input_box_shape[1]) <= 1.0

    # crop the box --
    # which part of the box is outside the image ?
    left_border = int(max(0, 0 - box.min_corner.x))
    top_border = int(max(0, 0 - box.min_corner.y))
    right_border = int(max(0, box.max_corner.x - image_width))
    bottom_border = int(max(0, box.max_corner.y - image_height))

    # define the part of the box that is inside the image
    inner_box = box.copy()
    inner_box.min_corner.x = max(0, box.min_corner.x)
    inner_box.min_corner.y = max(0, box.min_corner.y)
    inner_box.max_corner.x = min(box.max_corner.x, image_width)
    inner_box.max_corner.y = min(box.max_corner.y, image_height)

    # crop
    cropped_image = input_image[
        inner_box.min_corner.y : inner_box.max_corner.y, inner_box.min_corner.x : inner_box.max_corner.x, :
    ]
    # extrapolate --

    # border_type = cv2.BORDER_CONSTANT
    border_type = cv2.BORDER_REPLICATE
    # border_type = cv2.BORDER_WRAP
    # border_type = cv2.BORDER_REFLECT
    # border_type = cv2.BORDER_REFLECT_101
    cropped_image = cv2.copyMakeBorder(cropped_image, top_border, bottom_border, left_border, right_border, border_type)

    # print(cropped_image.shape[:2], "=?=", box_shape)
    # assert cropped_image.shape[:2] == box_shape

    # rescale the box so it fits the desired dimensions --
    resized_positive_example = cmtd.resized_image(cropped_image, example_width, example_height)
    assert resized_positive_example.shape == (example_height, example_width, image_depth)

    return resized_positive_example