Exemple #1
0
def ha_SignCompare(image, text):
    img = Image.open(image).convert('RGB')

    threshold = 200
    limg = img.convert('L').point(lambda p: 0 if p > threshold else 1, '1')
    img_data = np.array(limg, dtype=np.int32)
    dire_sum_0 = img_data.sum(axis=0)
    dire_sum_1 = img_data.sum(axis=1)
    no0_dir0 = np.where(dire_sum_0>0)[0]
    no0_dir1 = np.where(dire_sum_1>0)[0]
    direction = 1
    if no0_dir0[-1] - no0_dir0[0] < no0_dir1[-1] - no0_dir1[0]:
        direction = 0

    code, message, char_count, char_list = 0, '', 0, []
    if direction == 0:
        borders = find_best_borders_projection(img, text, direction = direction, min_sum=1, min_gap=1)
        if borders:
            for border in borders:
                pred_char, conf, toplist = image_prediction(img.crop(border), topk = 30)
                char_list.append(toplist)
        else:
            borders = find_best_borders_connect(img, text, direction = direction)
            if borders:
                for border in borders:
                    pred_char, conf, toplist = image_prediction(img.crop(border), topk = 30)
                    char_list.append(toplist)
            else:
                borders = find_best_borders_gas(img, text, direction = direction)
                if borders:
                    for border in borders:
                        pred_char, conf, toplist = image_prediction(img.crop(border), topk = 30)
                        char_list.append(toplist)

    else:
        borders = find_best_borders_gas(img, text, direction = direction)
        if borders:
            for border in borders:
                pred_char, conf, toplist = image_prediction(img.crop(border), topk = 30)
                char_list.append(toplist)
    # show_borders(img, borders)
    if char_list:
        return 0, 'Successful', len(char_list), char_list
    else:
        return -1, 'Failed', 0, []
Exemple #2
0
def ha_SignCompare(image, text):
    img = Image.open(image).convert('RGB')
    direction = 0 if img.size[0] < img.size[1] else 1
    name_num = len(text)
    code, message, char_count, char_list = 0, '', 0, []
    if direction == 0:
        borders = find_best_borders_projection(img,
                                               text,
                                               direction=direction,
                                               min_sum=1,
                                               min_gap=1)
        if borders:
            for border in borders:
                pred_char, conf, toplist = image_prediction(img.crop(border),
                                                            topk=30)
                char_list.append(toplist)
        else:
            borders = find_best_borders_connect(img, text, direction=direction)
            if borders:
                for border in borders:
                    pred_char, conf, toplist = image_prediction(
                        img.crop(border), topk=30)
                    char_list.append(toplist)
            else:
                borders = find_best_borders_gas(img, text, direction=direction)
                if borders:
                    for border in borders:
                        pred_char, conf, toplist = image_prediction(
                            img.crop(border), topk=30)
                        char_list.append(toplist)

    else:
        borders = find_best_borders_gas(img, text, direction=direction)
        if borders:
            for border in borders:
                pred_char, conf, toplist = image_prediction(img.crop(border),
                                                            topk=30)
                char_list.append(toplist)

    if char_list:
        return 0, 'Successful', len(char_list), char_list
    else:
        return -1, 'Failed', 0, []
Exemple #3
0
def find_best_borders_connect(img, name, threshold = 200, direction = 0, overlap = 0.85, net_type = 'S'):
    limg = img.convert('L').point(lambda p: 0 if p > threshold else 1, '1')
    img_data = np.array(limg, dtype=np.int32)
    direction_sum = img_data.sum(axis=direction)
    up, down = np.where(direction_sum>0)[0][0], np.where(direction_sum>0)[0][-1]
    verdire_sum = img_data.sum(axis=1-direction)
    length = np.where(verdire_sum>0)[0][-1] - np.where(verdire_sum>0)[0][0]
    merge_area = find_connect(img, direction = direction, overlap = overlap)
    area_num = len(merge_area)
    char_num = len(name)
    name_list = list(name)
    char_min = length / char_num / 3
    char_max = min(length / char_num * 2, length / 3 * 2)

    # 连笔严重,无法切割出合适的连通区域
    if char_num > area_num:
        return []
    elif char_num == area_num:
        for area in merge_area:
            if (area[1] - area[0] <= char_min or area[1] - area[0] >= char_max ):
                return []
        max_seg = [-1] + [i for i in range(char_num)]
    else:
        for area in merge_area:
            if area[1] - area[0] >= char_max:
                return []
        confM = [[0 for i in range(area_num)] for j in range(area_num)]
        charM = [[' ' for i in range(area_num)] for j in range(area_num)]
        charConfM = [[[0] * char_num for i in range(area_num)] for j in range(area_num)]
        for i in range(area_num):
            for j in range(i, area_num):
                if (merge_area[j][1] - merge_area[i][0] >= char_min and
                    merge_area[j][1] - merge_area[i][0] <= char_max ):
                    if direction == 0:
                        border = [up, merge_area[i][0], down, merge_area[j][1]]
                    elif direction == 1:
                        border = [merge_area[i][0], up, merge_area[j][1], down]
                    pred_img = img.crop(border)
                    # display(pred_img)
                    charM[i][j], confM[i][j], _, charConfM[i][j] = image_prediction(pred_img, topk = 1, labels = name_list, net_type = net_type)

        _, max_conf, max_seg = find_seg_point(area_num, char_num, confM, charConfM, 2, 0, char_num, [-1], 0, [-1])

        if len(max_seg) != char_num + 1:
            return []
    borders = []
    for i in range(char_num):
        if direction == 0:
            borders.append([up, merge_area[max_seg[i]+1][0], down, merge_area[max_seg[i+1]][1]])
        elif direction == 1:
            borders.append([merge_area[max_seg[i]+1][0], up, merge_area[max_seg[i+1]][1], down])
        
    return borders
Exemple #4
0
def find_best_borders_gas(img, name, threshold = 200, direction = 0, gas_std = 8, net_type = 'S'):
    name_list = list(name)
    char_num = len(name)
    limg = img.convert('L').point(lambda p: 0 if p > threshold else 1, '1')
    img_data = np.array(limg, dtype=np.int32)
    direction_sum = img_data.sum(axis=direction)
    up, down = np.where(direction_sum>0)[0][0], np.where(direction_sum>0)[0][-1]
    verdire_sum = img_data.sum(axis=1-direction)
    gas8 = gaussian_filter1d(verdire_sum, gas_std)
    local_min = find_local_min(gas8)
    length = local_min[-1] - local_min[0]
    min_num = len(local_min)
    char_min = length / char_num / 2
    char_max = min(length / char_num * 2, length / 3 * 2)
    count = 0
    if min_num-1 < char_num:
        return []
    elif min_num-1 == char_num:
        for i in range(1, min_num):
            if local_min[i] - local_min[i-1] <= char_min or local_min[1] - local_min[0] >= char_max:
                return []
        max_seg = [i for i in range(min_num)]
    else:
        confM3 = [[0 for i in range(min_num)] for j in range(min_num)]
        charM3 = [[' ' for i in range(min_num)] for j in range(min_num)]
        charConfM3 = [[[0] * char_num for i in range(min_num)] for j in range(min_num)]
        for i in range(min_num):
            for j in range(i+1, min_num):
                if(local_min[j] - local_min[i] >= char_min and local_min[j] - local_min[i] <= char_max):
                    if direction == 0:
                        border = [up, local_min[i], down, local_min[j]]
                    elif direction == 1:
                        border = [local_min[i], up, local_min[j], down]
                    pred_img = img.crop(border)
                    # display(pred_img)
                    count += 1
                    charM3[i][j], confM3[i][j], toplist, charConfM3[i][j] = image_prediction(pred_img, topk = 1, labels=name_list, net_type = net_type)
        _, max_conf, max_seg = find_seg_point(min_num, char_num, confM3, charConfM3, 3, 0, char_num, [0], 0, [0])
        if len(max_seg) != char_num + 1:
            return []

    # print(name, ", minpoint count: ", min_num, ", predict count: ", count)
    borders = []
    for i in range(char_num):
        if direction == 0:
            borders.append([up, local_min[max_seg[i]], down, local_min[max_seg[i+1]]])
        elif direction == 1:
            borders.append([local_min[max_seg[i]], up, local_min[max_seg[i+1]], down])
    
    return borders