Ejemplo n.º 1
0
def ocr(image):
    # detect
    image = highlight(image)
    text_recs, img_framed, image = get_det_boxes(image)
    text_recs = sort_box(text_recs)
    result = charRec(image, text_recs)
    return result, img_framed
def proc_img(image):
    # detect
    text_recs, img_framed, image = get_det_boxes(
        image
    )  #gọi hàm get_det_boxes trả về ảnh đã vẽ (img_framed), tọa độ cắt ảnh (test_recs)
    text_recs = sort_box(text_recs)  #sắp xếp các trường
    result = char_rec(
        image,
        text_recs)  #nhận diện , đầu vào là file tọa độ và hình ảnh ban đầu
    return result, img_framed
Ejemplo n.º 3
0
def charRec(img, text_recs, adjust=False):
    """
    加载OCR模型,进行字符识别
    """
    results = {}
    xDim, yDim = img.shape[1], img.shape[0]

    for index, rec in enumerate(text_recs):
        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(img, degree, pt1, pt2, pt3, pt4)
        #dis(partImg)
        if partImg.shape[0] < 1 or partImg.shape[1] < 1 or partImg.shape[
                0] > partImg.shape[1]:  # 过滤异常图片
            continue
        text = recognizer.recognize(partImg)

        if len(text) > 0:
            results[index] = [rec]
            results[index].append(text)  # 识别文字

    return results

    # the input image is a one-channel-nparray imagedef ocr(image):
    # detect
    text_recs, img_framed, image = get_det_boxes(image)
    text_recs = sort_box(text_recs)
    result = charRec(image, text_recs)
    return result, img_framed