Esempio n. 1
0
def ocr_core(image_src):
    """
    调用 ocr 识别图像
    :param image_path: skimage 处理过的图像
    :return: [[识别字符, bounding box], ...]
    """
    rectification = detect_skimage(image_src)
    result = []
    index = 0
    for ele in rectification:
        index += 1
        img = ele[0]

        minlength = img.shape[0] if img.shape[0] < img.shape[1] else img.shape[1]
        if minlength < 1280:
            factor = 1280 // minlength + 1
            img = cv2.resize(img, None, fx=factor, fy=factor, interpolation=cv2.INTER_CUBIC)

        img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        ret, th = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

        if th[0][0] == 0:
            th = cv2.bitwise_not(th)
        img = Image.fromarray(th).convert('L')
        res = crnnOcr(img)
        res = segmentation.correct(res).strip()
        result.append([res, ele[1]])
    return result
Esempio n. 2
0
def crnn_get(image):

    sim_pred = crnnOcr(image)

    print("NUMBER:")
    print(sim_pred)

    return sim_pred
Esempio n. 3
0
def crnnRec(im, text_recs, ocr_mode='keras', adjust=False):
    """
    crnn模型,ocr识别
    @@model,
    @@converter,
    @@im:Array
    @@text_recs:text box

    """
    index = 0
    results = {}
    xDim, yDim = im.shape[1], im.shape[0]

    for index, rec in enumerate(text_recs):
        results[index] = [
            rec,
        ]
        xlength = int((rec[6] - rec[0]) * 0.1)
        ylength = int((rec[7] - rec[1]) * 0.2)
        if adjust:
            pt1 = (max(1, rec[0] - xlength), max(1, rec[1] - ylength))
            pt2 = (rec[2], rec[3])
            pt3 = (min(rec[6] + xlength,
                       xDim - 2), min(yDim - 2, rec[7] + ylength))
            pt4 = (rec[4], rec[5])
        else:
            pt1 = (max(1, rec[0]), max(1, rec[1]))
            pt2 = (rec[2], rec[3])
            pt3 = (min(rec[6], xDim - 2), min(yDim - 2, rec[7]))
            pt4 = (rec[4], rec[5])

        # 图像倾斜角度
        degree = degrees(atan2(pt2[1] - pt1[1], pt2[0] - pt1[0]))

        part_img = dump_rotate_image(im, degree, pt1, pt2, pt3, pt4)

        image = Image.fromarray(part_img).convert('L')
        if ocr_mode == 'keras':
            sim_pred = ocr(image)
        else:
            sim_pred = crnnOcr(image)

        # 识别文字
        results[index].append(sim_pred)

    return results
Esempio n. 4
0
def crnnRec(im, text_recs, adjust=False):
    """
    crnn模型,ocr识别
    @@model,
    @@converter,
    @@im:Array
    @@text_recs:text box

    """
    index = 0
    results = {}
    xDim, yDim = im.shape[1], im.shape[0]

    for index, rec in enumerate(text_recs):
        results[index] = [
            rec,
        ]
        xlength = int((rec[6] - rec[0]) * 0.1)
        ylength = int((rec[7] - rec[1]) * 0.2)
        if adjust:
            pt1 = (max(1, rec[0] - xlength), max(1, rec[1] - ylength))
            pt2 = (rec[2], rec[3])
            pt3 = (min(rec[6] + xlength,
                       xDim - 2), min(yDim - 2, rec[7] + ylength))
            pt4 = (rec[4], rec[5])
        else:
            pt1 = (max(1, rec[0]), max(1, rec[1]))
            pt2 = (rec[2], rec[3])
            pt3 = (min(rec[6], xDim - 2), min(yDim - 2, rec[7]))
            pt4 = (rec[4], rec[5])

        degree = degrees(atan2(pt2[1] - pt1[1], pt2[0] - pt1[0]))  ##图像倾斜角度

        partImg = dumpRotateImage(im, degree, pt1, pt2, pt3, pt4)
        # 根据ctpn进行识别出的文字区域,进行不同文字区域的crnn识别
        # 图片格式转换并进行灰度处理
        image = Image.fromarray(partImg).convert('L')
        # 进行识别出的文字识别
        sim_pred = crnnOcr(image)

        results[index].append(sim_pred)  ##识别文字

    return results
Esempio n. 5
0
def crnnRec(im, ocrMode='keras'):
    """
    crnn模型,ocr识别
    @@model,
    @@converter,
    @@im:Array
    @@text_recs:text box

    """
    index = 0
    results = {}
    results[index] = list()
    image = Image.fromarray(im).convert('L')
    # 进行识别出的文字识别
    if ocrMode == 'keras':
        sim_pred = ocr(image)
    else:
        sim_pred = crnnOcr(image)

    results[index].append(sim_pred)  # 识别文字

    return results
Esempio n. 6
0
def crnnRec(im,boxes,ifIm=False,leftAdjust=False,rightAdjust=False,alph=0.2):
   """
   crnn模型,ocr识别
   @@model,
   @@converter,
   @@im:Array
   @@text_recs:text box
   @@ifIm:是否输出box对应的img
   
   """
   results = []
   im = Image.fromarray(im) 
   for index,box in enumerate(boxes):

       degree,w,h,cx,cy = solve(box)
       partImg,newW,newH = rotate_cut_img(im,degree,box,w,h,leftAdjust,rightAdjust,alph)
       newBox = xy_rotate_box(cx,cy,newW,newH,degree)
       partImg_ = partImg.convert('L')
       simPred = crnnOcr(partImg_)##识别的文本
       if simPred.strip()!=u'':
            results.append({'cx':cx,'cy':cy,'text':simPred,'w':newW,'h':newH,'degree':degree*180.0/np.pi})
 
   return results
Esempio n. 7
0
#coding:utf-8

import crnn.crnn as ocr

imageFileName = "./2.jpg"
preds = ocr.crnnOcr(imageFileName)
print(preds)
Esempio n. 8
0
def draw_boxes(img, image_name, boxes, opt, adjust):
    base_name = image_name.split('/')[-1]
    i = 0
    j = 0
    boxes = sorted(boxes, key=lambda x: x[1])
    boxesX0 = sorted(boxes, key=lambda x: x[0])
    xStart = boxesX0[0][0]
    iXStart = int(xStart)
    if (iXStart < 0):
        iXStart = 0
    boxesX2 = sorted(boxes, key=lambda x: x[2])
    xEnd = boxesX2[-1][2]
    previousY = 0
    resultMap = {
        '1_shopName': 'none',
        '2_city': 'none',
        '3_tel': 'none',
        '4_year': 'none',
        '5_total': 0,
        '6_receiptNO': 'none',
        '7_staffNO': 'none',
        '8_pointcard': 'none',
        '9_category': 0,
        'a_goods': 'none',
        'a_tax': 0,
        'b_subtotal': 0,
        'suffix_catPrice': [],
        'tel_before': '',
        'pos_tel_before': 0,
        'pos_tel_after': 0,
        'origin_result': [],
        'pos_shop': 0,
        'pos_time': 0,
        'pos_time_after': 0,
        'pos_tax_after': 0,
        'pos_staff': 0,
        'pos_ling': 0,
        'pos_ling_after': 0,
        'pos_category': 0,
        'pos_total': 0,
        'pos_subtotal': 0,
        'pos_tax': 0,
        'pos_card': 0,
        'pos_mny_after': 0,
        'pos_card_after': 0,
        'type_shop': 0
    }
    for box in boxes:
        if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm(box[3] -
                                                                 box[0]) < 5:
            j += 1
            continue
        if (i > 0 and int(box[1]) - previousY <= 11):
            previousY = int(box[1])
            continue
        previousY = int(box[1])
        i += 1
        crop_img = img[int(box[1]):int(box[5]), iXStart:int(xEnd)]
        # print('Y0={}, Y1={}, X0={}, X1={}'.format(int(box[1]),int(box[5]),iXStart,int(xEnd)))
        if opt.develop and i <= 5:
            cv2.imwrite(
                os.path.join("test/results",
                             base_name.split('.')[0] + "_" + str(i) + ".jpg"),
                crop_img)
        try:
            if opt.torch:
                from crnn.crnn import crnnOcr
                sim_pred = crnnOcr(
                    Image.fromarray(crop_img).convert('L')).strip()
            else:
                from ocr.model import predict as ocr
                sim_pred = ocr(Image.fromarray(crop_img).convert('L')).strip()
        except Exception as e:
            print('Exception for step {}--{}'.format(i, e))
            continue
        sim_pred = sim_pred.strip()
        sim_pred = numberUtils.removeQuote(sim_pred)
        sim_pred = jaconv.z2h(sim_pred, digit=True, ascii=True)
        resultMap['origin_result'].append(sim_pred)
        if opt.develop and i <= 5:
            with open(
                    os.path.join(
                        "test/results",
                        base_name.split('.')[0] + "_" + str(i) + ".txt"),
                    'a+') as f:
                f.write(sim_pred)
    commonUtils.getDrawboxResult(resultMap['origin_result'], resultMap)
    return resultMap
Esempio n. 9
0
from crnn.crnn import crnnOcr as crnnOcr
from PIL import Image
partImg = Image.open('/home/gavin/Desktop/test_ocr2.png')  ##单行文本图像
partImg = partImg.convert('L')
simPred = crnnOcr(partImg)  ##识别的文本
print(simPred)