Example #1
0
def segment_lines(image, directory_name, write_to_file):
    (h, w) = image.shape[:2]
    original_image = image.copy()
   
    image = cv2.bitwise_not(image)
    # display_image("here", image)
    image = cv2.dilate(image, np.ones((3, 3), np.uint8), iterations=1)
    
    horizontal_projection = get_horizontal_projection(image)

    y, count = 0, 0
    is_space = False
    ycoords = []
    for i in range(h):
        if not is_space:
            if horizontal_projection[i] == 0:
                is_space = True
                count = 1
                y = i

        else:
            if horizontal_projection[i] > 0:
                is_space = False
                ycoords.append(y / count)

            else:
                y += i
                count += 1

    previous_height = 0

    # if os.path.exists(directory_name):
    #     shutil.rmtree(directory_name)

    # os.makedirs(directory_name)
    line_images = []

    for i in range(len(ycoords)):
        if i == 0:
            continue

        # cv2.line(image, (0, int(ycoords[i])), (w, int(ycoords[i])), (255, 255, 255), 2) 
        image_cropped = original_image[previous_height:int(ycoords[i]), :]

        line_images.append(image_cropped)

        previous_height = int(ycoords[i])
        if write_to_file == 1:
            cv2.imwrite(directory_name + "/" + "segment_" + str(i) + ".png", image_cropped)

    image_cropped = original_image[int(ycoords[-1]):h, :]
    line_images.append(image_cropped)
    if write_to_file == 1:
        cv2.imwrite(directory_name + "/" + "segment_" + str(i + 1) + ".png", image_cropped)
    
    # cv2.imwrite("segmented_lines.png", image)
    return line_images
Example #2
0
    input_path = args["input_path"]
    line_segmets_path = args["line_segments_path"]

    image = cv2.imread("./segmented_lines/csep1622/segment_1/segment_41.png")
    display_image("source", image)

    processed_image = convert_to_binary_and_invert(image)

    # cv2.imwrite("binary.png", processed_image)
    image = processed_image.copy()  # original copy of the image
    img_line = processed_image.copy()

    edged = processed_image
    # cv2.imwrite("img_cnt.png", edged)

    hp = get_horizontal_projection(edged)
    baseline = get_baseline_y_coord(get_horizontal_projection(processed_image))

    print("now baseline is: ", baseline)
    h, w = processed_image.shape
    cv2.line(img_line, (0, baseline), (w, baseline), (255, 255, 255), 1)
    # cv2.imwrite("baseline.png", img_line)

    pen_size = get_pen_size(edged)
    print("pen_size", pen_size)

    y = edged[:, baseline]
    print("y", y)
    exit()
    count = 0
    flag = False
def segment_words(path):
    """
    this function keeps the list of word separatation points in word_separation list
    but segments into sub words and saves the sub words segements in their designated directory
    """
    files = [
        f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))
    ]
    image = cv2.imread(os.path.join(path, files[0]))
    directory_name = path + "/" + files[0][:-4]

    if os.path.exists(directory_name):
        shutil.rmtree(directory_name)
    os.makedirs(directory_name)

    original_image = image.copy()
    image = convert_to_binary_and_invert(image)
    # image_with_line = image.copy()

    (h, w) = image.shape

    # image_with_line = cv2.dilate(image, np.ones((2, 2), np.uint8), iterations=1)  # needs some tuning
    horizontal_projection = get_horizontal_projection(image)
    baseline_y_coord = get_baseline_y_coord(horizontal_projection)
    # cv2.line(image_with_line, (0, baseline_y_coord), (w, baseline_y_coord), (255, 255, 255), 1)

    # display_image("image without dotting", image_without_dotting)
    vertical_projection = get_vertical_projection(image)

    print("shape of vertical projections is: ", len(vertical_projection))

    x, count = 0, 0
    is_space = False
    xcoords = []
    distances = []

    for i in range(w):
        if not is_space:
            if vertical_projection[i] == 0:
                is_space = True
                count = 1
                x = i

        else:
            if vertical_projection[i] > 0:
                is_space = False
                xcoords.append(x / count)
                distances.append(count)

            else:
                x += i
                count += 1

    distance = get_distance_between_words(distances)

    previous_width = 0
    word_separation = xcoords.copy()
    print(len(word_separation))
    for i in range(len(xcoords)):
        if i == 0:
            previous_width = int(xcoords[i])
            continue

        if distances[i - 1] >= distance:
            pass
            # cv2.line(image, (previous_width, 0), (previous_width, h), (255, 255, 255), 1)
        else:
            # print(i)
            word_separation[i - 1] = -1
        sub_word = original_image[:, previous_width:int(xcoords[i])]
        cv2.imwrite(directory_name + "/" + "segment_" + str(i) + ".png",
                    sub_word)
        # display_image("sub word", sub_word)
        previous_width = int(xcoords[i])

    if distances[-2] < distance:
        word_separation[-2] = -1

    sub_word = original_image[:, int(xcoords[-1]):w]
    cv2.imwrite(directory_name + "/" + "segment_" + str(len(xcoords)) + ".png",
                sub_word)
    display_image("sub word", sub_word)

    print("word sep: ", word_separation)

    previous_width = 0
    sub_seg_points = []
    # word_separation = list(filter(lambda a: a != -1, word_separation))
    flag = False
    for i in range(len(word_separation)):

        if word_separation[i] == -1 and flag == False:

            flag = True
            sub_seg_points = []
            sub_seg_points.append(xcoords[i - 1])
            # sub_seg_points.append(xcoords[i])

        if word_separation[i] == -1 and flag:
            sub_seg_points.append(xcoords[i])

        if word_separation[i] != -1 and flag:
            sub_seg_points.append(xcoords[i])
            flag = False
            print("sub seg: ", sub_seg_points)
            sub_image = image[:,
                              int(sub_seg_points[0]):int(sub_seg_points[-1])]
            display_image("duh", sub_image)
            for i in range(1, len(sub_seg_points) - 1):
                cv2.line(image, (int(sub_seg_points[i]), 0),
                         (int(sub_seg_points[i]), h), (255, 255, 255), 1)
            display_image("display", image)

        previous_width = int(word_separation[i])

    cv2.line(image, (int(xcoords[-1]), 0), (int(xcoords[-1]), h),
             (255, 255, 255), 1)

    print("word: ", word_separation)
    print("xcoord: ", xcoords)
    display_image("final output", image)
    cv2.imwrite("dis.png", image)
def segment_words(line_images, path, img_name, input_path, train,
                  acc_char_map):
    """
    this function keeps the list of word separatation points in word_separation list
    but segments into sub words and saves the sub words segements in their designated directory
    """
    # files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
    # image = cv2.imread(os.path.join(path, files[1]))
    # print(os.path.join(path, files[1]))
    gt_words = get_words_from_text(img_name, input_path)
    if (train):
        char_map = acc_char_map
    else:
        if (not acc_char_map or acc_char_map == {}):
            char_map = load_features_map()
        else:
            char_map = acc_char_map

    recognized_chars = ''
    """"
    directory_name = "./segmented_words"
    if os.path.exists(directory_name):
        shutil.rmtree(directory_name)
    os.makedirs(directory_name)char_map
    """
    curr_word_idx = 0
    wrong_seg_words = 0
    for image in line_images:

        original_image = image.copy()
        # image_with_line = image.copy()
        (h, w) = image.shape

        horizontal_projection = get_horizontal_projection(image)
        baseline_y_coord = get_baseline_y_coord(horizontal_projection)
        # cv2.line(image_with_line, (0, baseline_y_coord), (w, baseline_y_coord), (255, 255, 255), 1)

        vertical_projection = get_vertical_projection(image)

        # print("shape of vertical projections is: ", len(vertical_projection))

        x, count = 0, 0
        is_space = False
        xcoords = []
        distances = []

        for i in range(w):
            if not is_space:
                if vertical_projection[i] == 0:
                    is_space = True
                    count = 1
                    x = i

            else:
                if vertical_projection[i] > 0:
                    is_space = False
                    xcoords.append(x / count)
                    distances.append(count)

                else:
                    x += i
                    count += 1

        previous_width = 0
        word_separation = xcoords.copy()
        # word_separation = list(filter(lambda a: a != -1, word_separation))
        distance = get_distance_between_words(distances)
        # print("distance is ",distance)
        for i in range(len(word_separation)):

            if distances[i] > distance:
                pass
            else:
                word_separation[i] = -1

        word_separation = list(filter(lambda a: a != -1, word_separation))
        # print(word_separation)

        previous_width = image.shape[1]
        seg_points = []
        for i in range(len(word_separation)):
            i = len(word_separation) - i - 1

            word = original_image[:, int(word_separation[i]):previous_width]
            display_image("word", word)
            # cv2.line(image, (int(word_separation[i]), 0), (int(word_separation[i]), image.shape[0]),(255, 255, 255), 1) # noqa
            previous_width = int(word_separation[i])
            seg_points = contour_seg(word, baseline_y_coord)

            if (len(gt_words) > curr_word_idx and train):
                feat_vectors = batch_get_feat_vectors(word, seg_points,
                                                      gt_words[curr_word_idx])
            else:
                feat_vectors = batch_get_feat_vectors(word, seg_points, None)
            if (train):
                if (len(gt_words) > curr_word_idx):
                    aux_map = compare_and_assign(feat_vectors,
                                                 gt_words[curr_word_idx],
                                                 char_map)
                    if (aux_map != -1):
                        char_map = aux_map
                    else:
                        # print(f'Rejected Word #{curr_word_idx}')
                        wrong_seg_words += 1
                else:
                    wrong_seg_words += 1
            else:
                recognized_chars += ' ' + match_feat_to_char(
                    char_map, feat_vectors)
            curr_word_idx += 1
        display_image("word sep", image)

    if (train):
        try:
            with open('./config_map.json', 'w') as f:
                f.write(
                    json.dumps(char_map, ensure_ascii=False, default=convert))
                f.close()
                # print(char_map)
                return wrong_seg_words, curr_word_idx - 1, char_map, 0
        except Exception:
            # print(char_map)
            return wrong_seg_words, curr_word_idx - 1, char_map, 0
    else:
        end_time = time.time()
        try:
            with open(f'./output/text/{img_name.replace("png", "txt")}',
                      'w') as f:
                f.write(recognized_chars)
        except Exception:
            return 0, 0, {}, end_time

        # print(f'recognized_text: {recognized_chars}')
        return 0, 0, {}, end_time