Example #1
0
def ocr_id_card_image(image_cv, detect_angle=False):
    H, W = image_cv.shape[:2]

    time_take = time.time()

    _, result, angle = model.model(
        image_cv,
        detectAngle=detect_angle,  # 是否进行文字方向检测,通过web传参控制
        config=dict(
            MAX_HORIZONTAL_GAP=50,  # 字符之间的最大间隔,用于文本行的合并
            MIN_V_OVERLAPS=0.6,
            MIN_SIZE_SIM=0.6,
            TEXT_PROPOSALS_MIN_SCORE=0.1,
            TEXT_PROPOSALS_NMS_THRESH=0.3,
            TEXT_LINE_NMS_THRESH=0.7),  # 文本行之间测iou值
        leftAdjust=True,  # 对检测的文本行进行向左延伸
        rightAdjust=True,  # 对检测的文本行进行向右延伸
        alpha=0.01)  # 对检测的文本行进行向右、左延伸的倍数

    # print('[ocr_id_card] result', result)
    # print('[ocr_id_card] angle', angle)

    res = idcard.idcard(result)
    res = res.res
    res = [{'text': res[key], 'name': key, 'box': {}} for key in res]

    print('[ocr_id_card] res', res)
    time_take = time.time() - time_take
    print('[ocr_id_card] time_take', time_take)
Example #2
0
    def POST(self):
        data = web.data()
        data = json.loads(data)
        billModel = data.get('billModel', '')
        imgString = data['imgString'].encode().split(b';base64,')[-1]
        imgString = base64.b64decode(imgString)
        jobid = uuid.uuid1().__str__()
        path = '/tmp/{}.jpg'.format(jobid)
        with open(path, 'wb') as f:
            f.write(imgString)
        img = Image.open(path).convert("RGB")
        W, H = img.size
        timeTake = time.time()
        _, result, angle = model.model(
            img,
            detectAngle=DETECTANGLE,  ##是否进行文字方向检测
            config=dict(
                MAX_HORIZONTAL_GAP=100,  ##字符之间的最大间隔,用于文本行的合并
                MIN_V_OVERLAPS=0.7,
                MIN_SIZE_SIM=0.7,
                TEXT_PROPOSALS_MIN_SCORE=0.1,
                TEXT_PROPOSALS_NMS_THRESH=0.3,
                TEXT_LINE_NMS_THRESH=0.99,  ##文本行之间测iou值
                MIN_RATIO=1.0,
                LINE_MIN_SCORE=0.2,
                TEXT_PROPOSALS_WIDTH=0,
                MIN_NUM_PROPOSALS=0,
            ),
            leftAdjust=True,  ##对检测的文本行进行向左延伸
            rightAdjust=True,  ##对检测的文本行进行向右延伸
            alph=0.2,  ##对检测的文本行进行向右、左延伸的倍数
            ifadjustDegree=False  ##是否先小角度调整文字倾斜角度
        )

        if billModel == '' or billModel == '通用OCR':
            result = union_rbox(result, 0.2)
            res = [{
                'text': x['text'],
                'name': str(i)
            } for i, x in enumerate(result)]
        elif billModel == '火车票':
            res = trainTicket.trainTicket(result)
            res = res.res
            res = [{'text': res[key], 'name': key} for key in res]

        elif billModel == '身份证':

            res = idcard.idcard(result)
            res = res.res
            res = [{'text': res[key], 'name': key} for key in res]

        timeTake = time.time() - timeTake

        os.remove(path)
        return json.dumps({
            'res': res,
            'timeTake': round(timeTake, 4)
        },
                          ensure_ascii=False)
Example #3
0
    def format_text(self, textbox, img, angle, billModel='general_OCR', CommandID= ''):
        """
        格式化各种图片提取的文本
        :param textbox: 提取的文本框(包括坐标和文本内容)
        :param img: 原图
        :param angle: 原图需要旋转的角度
        :param billModel: 图片类型,方便格式化
        :param CommandID: 判断来自网页的请求(文本展示),还是返回给服务器的请求
        :return: res: json格式的格式化结果
        """
        if billModel == '' or billModel == 'general_OCR':
            result = union_rbox(textbox, 0.2)
            res = [{'text': x['text'],
                    'name': str(i),
                    'box': {'cx': x['cx'],
                            'cy': x['cy'],
                            'w': x['w'],
                            'h': x['h'],
                            'angle': x['degree']

                            }
                    } for i, x in enumerate(result)]
            res = adjust_box_to_origin(img, angle, res)  ##修正box
        elif billModel == 'trainticket':
            res = trainTicket.trainTicket(textbox)
            res = res.res
            if CommandID != '':
                res = {key: res[key] for key in res}
            else:
                res = [{'text': res[key], 'name': key, 'box': {}} for key in res]
        elif billModel == 'idcard':
            res = idcard.idcard(textbox)
            res = res.res
            if CommandID != '':
                res = {key: res[key] for key in res}
            else:
                res = [{'text': res[key], 'name': key, 'box': {}} for key in res]
        elif billModel == 'invoice':
            res = invoice.invoice(textbox)
            res = res.res
            if CommandID != '':
                res = {key: res[key] for key in res}
            else:
                res = [{'text': res[key], 'name': key, 'box': {}} for key in res]
        elif billModel == 'bankcard':
            res = bankcard.bankcard(textbox)
            res = res.res
            if CommandID != '':
                res = {key: res[key] for key in res}
            else:
                res = [{'text': res[key], 'name': key, 'box': {}} for key in res]
        elif billModel == 'licenseplate':
            if CommandID != '':
                res = {'carNo': list(textbox), 'picUrl': '', 'picName': ''}
            else:
                res = [{'text': text, 'name': 'carNo', 'box': {}} for text in list(textbox)]

        return res
Example #4
0
def result(img):
    back = {}
    img = Image.open(BytesIO(img)).convert('RGB')
    img = np.array(img)
    result = text_predict(img)
    back['文本'] = list(map(lambda x: x['text'], result))
    res = trainTicket.trainTicket(result)
    back['火车票'] = str(res)
    res = idcard.idcard(result)
    back['身份证'] = str(res)
    return back
Example #5
0
def idCardOCR():

    if 'image' not in request.files:
        return {"error": "'image' not found"}

    img = Image.open(request.files["image"]).convert("RGB")
    img = np.array(img)

    result = text_predict(img)
    res = idcard.idcard(result)
    res = res.res
    res = [{'text': res[key], 'name':key} for key in res]
    return {"result": res}
Example #6
0
def idcard_ocr(imgpath):
    time.sleep(0.5)
    img = cv2.imread(imgpath)
    _,result,angle= model.model(img,detectAngle=True,config=dict(MAX_HORIZONTAL_GAP=50,MIN_V_OVERLAPS=0.6,MIN_SIZE_SIM=0.6,TEXT_PROPOSALS_MIN_SCORE=0.1,TEXT_PROPOSALS_NMS_THRESH=0.3,TEXT_LINE_NMS_THRESH = 0.7),leftAdjust=True,rightAdjust=True,alph=0.01)

    res = idcard.idcard(result)
    res = res.res
    res =[ {'text':res[key],'name':key,'box':{}} for key in res]

    out_str = {'姓名':None, '性别':None, '民族':None, '出生年月':None, '身份证号码':None, '身份证地址':None}
    for n in res:                                                                                                                                                                           1,1           Top
        key_ = n['name']
        value_ = n['text']
        out_str[key_] = value_
        print(out_str)
Example #7
0
def main():
 	billModel = '通用OCR'
 	path = 'test/t3.jpg'
 	img = Image.open(path).convert("RGB")
 	W,H = img.size
 	timeTake = time.time()
 	_,result,angle= model.model(img,
                                    detectAngle=DETECTANGLE,##是否进行文字方向检测
                                    config=dict(MAX_HORIZONTAL_GAP=100,##字符之间的最大间隔,用于文本行的合并
                                    MIN_V_OVERLAPS=0.7,
                                    MIN_SIZE_SIM=0.7,
                                    TEXT_PROPOSALS_MIN_SCORE=0.1,
                                    TEXT_PROPOSALS_NMS_THRESH=0.3,
                                    TEXT_LINE_NMS_THRESH = 0.99,##文本行之间测iou值
                                    MIN_RATIO=1.0,
                                    LINE_MIN_SCORE=0.2,
                                    TEXT_PROPOSALS_WIDTH=0,
                                    MIN_NUM_PROPOSALS=0,                                               
                ),
                                    leftAdjust=True,##对检测的文本行进行向左延伸
                                    rightAdjust=True,##对检测的文本行进行向右延伸
                                    alph=0.2,##对检测的文本行进行向右、左延伸的倍数
                                    ifadjustDegree=False##是否先小角度调整文字倾斜角度
                                   )
    
 	if billModel=='' or billModel=='通用OCR':
            result = union_rbox(result,0.2)
            res = [{'text':x['text'],'name':str(i)} for i,x in enumerate(result)]
 	elif billModel=='火车票':
            res = trainTicket.trainTicket(result)
            res = res.res
            res =[ {'text':res[key],'name':key} for key in res]

 	elif billModel=='身份证':

            res = idcard.idcard(result)
            res = res.res
            res =[ {'text':res[key],'name':key} for key in res]
 	print(result)
Example #8
0
    def POST(self):
        t = time.time()
        data = web.data()
        uidJob = uuid.uuid1().__str__()

        data = json.loads(data)
        billModel = data.get('billModel', '')
        textAngle = data.get('textAngle', False)  ##文字检测
        textLine = data.get('textLine', False)  ##只进行单行识别

        imgString = data['imgString'].encode().split(b';base64,')[-1]
        img = base64_to_PIL(imgString)
        if img is not None:
            img = np.array(img)

        H, W = img.shape[:2]

        while time.time() - t <= TIMEOUT:
            if os.path.exists(filelock):
                continue
            else:
                with open(filelock, 'w') as f:
                    f.write(uidJob)

                if textLine:
                    ##单行识别
                    partImg = Image.fromarray(img)
                    text = crnn.predict(partImg.convert('L'))
                    res = [{
                        'text': text,
                        'name': '0',
                        'box': [0, 0, W, 0, W, H, 0, H]
                    }]
                    os.remove(filelock)
                    break

                else:
                    detectAngle = textAngle
                    result, angle = model.model(
                        img,
                        scale=scale,
                        maxScale=maxScale,
                        detectAngle=detectAngle,  ##是否进行文字方向检测,通过web传参控制
                        MAX_HORIZONTAL_GAP=100,  ##字符之间的最大间隔,用于文本行的合并
                        MIN_V_OVERLAPS=0.6,
                        MIN_SIZE_SIM=0.6,
                        TEXT_PROPOSALS_MIN_SCORE=0.1,
                        TEXT_PROPOSALS_NMS_THRESH=0.3,
                        TEXT_LINE_NMS_THRESH=0.99,  ##文本行之间测iou值
                        LINE_MIN_SCORE=0.1,
                        leftAdjustAlph=0.01,  ##对检测的文本行进行向左延伸
                        rightAdjustAlph=0.01,  ##对检测的文本行进行向右延伸
                    )

                    if billModel == '' or billModel == '通用OCR':
                        result = union_rbox(result, 0.2)
                        res = [{
                            'text': x['text'],
                            'name': str(i),
                            'box': {
                                'cx': x['cx'],
                                'cy': x['cy'],
                                'w': x['w'],
                                'h': x['h'],
                                'angle': x['degree']
                            }
                        } for i, x in enumerate(result)]
                        res = adjust_box_to_origin(img, angle, res)  ##修正box

                    elif billModel == '火车票':
                        res = trainTicket.trainTicket(result)
                        res = res.res
                        res = [{
                            'text': res[key],
                            'name': key,
                            'box': {}
                        } for key in res]

                    elif billModel == '身份证':

                        res = idcard.idcard(result)
                        res = res.res
                        res = [{
                            'text': res[key],
                            'name': key,
                            'box': {}
                        } for key in res]

                    os.remove(filelock)
                    break

        timeTake = time.time() - t

        return json.dumps({
            'res': res,
            'timeTake': round(timeTake, 4)
        },
                          ensure_ascii=False)
Example #9
0
    def POST(self):
        t = time.time()
        data = web.data()
        uidJob = uuid.uuid1().__str__()

        data = json.loads(data)
        # print(data)
        billModel = data.get("billModel", "")
        # textAngle = data.get('textAngle',False)##文字检测
        textLine = data.get("textLine", False)  ##只进行单行识别

        imgString = data["imgString"].encode().split(b";base64,")[-1]
        img = base64_to_PIL(imgString)
        if img is not None:
            img = np.array(img)

        H, W = img.shape[:2]

        while time.time() - t <= TIMEOUT:
            if os.path.exists(filelock):
                continue
            else:
                with open(filelock, "w") as f:
                    f.write(uidJob)

                if textLine:
                    ##单行识别
                    partImg = Image.fromarray(img)
                    text = crnn_handle.predict(partImg)
                    res = [{
                        "text": text,
                        "name": "0",
                        "box": [0, 0, W, 0, W, H, 0, H]
                    }]
                    os.remove(filelock)
                    break

                else:
                    # detectAngle = textAngle
                    result = text_predict(img)

                    if billModel == "" or billModel == "通用OCR":
                        # result = union_rbox(result,0.2)
                        res = [{
                            "text": x["text"],
                            "name": str(i),
                            "box": {
                                "cx": x["cx"],
                                "cy": x["cy"],
                                "w": x["w"],
                                "h": x["h"],
                                "angle": x["degree"],
                            },
                        } for i, x in enumerate(result)]
                        # res = adjust_box_to_origin(img,angle, res)##修正box

                    elif billModel == "火车票":
                        res = trainTicket.trainTicket(result)
                        res = res.res
                        res = [{
                            "text": res[key],
                            "name": key,
                            "box": {}
                        } for key in res]

                    elif billModel == "身份证":

                        res = idcard.idcard(result)
                        res = res.res
                        res = [{
                            "text": res[key],
                            "name": key,
                            "box": {}
                        } for key in res]

                    os.remove(filelock)
                    break

        timeTake = time.time() - t

        return json.dumps({
            "res": res,
            "timeTake": round(timeTake, 4)
        },
                          ensure_ascii=False)
Example #10
0
    def POST(self):
        data = web.data()
        data = json.loads(data)
        billModel = data.get('billModel', '')
        textAngle = data.get('textAngle', False)  # 文字检测
        textLine = data.get('textLine', False)  # 只进行单行识别

        imgString = data['imgString'].encode().split(b';base64,')[-1]
        imgString = base64.b64decode(imgString)
        jobid = uuid.uuid1().__str__()
        path = 'test/{}.jpg'.format(jobid)
        with open(path, 'wb') as f:
            f.write(imgString)
        img = cv2.imread(path)  # GBR
        H, W = img.shape[:2]
        timeTake = time.time()

        if textLine:
            # 单行识别
            partImg = Image.fromarray(img)
            text = model.crnnOcr(partImg.convert('L'))
            res = [{
                'text': text,
                'name': '0',
                'box': [0, 0, W, 0, W, H, 0, H]
            }]
        else:
            detectAngle = textAngle  # 是否进行文字方向检测
            _, result, angle = model.model(
                img,
                file_name="test.jpg",
                detectAngle=detectAngle,  # 是否进行文字方向检测,通过web传参控制
                config=dict(
                    MAX_HORIZONTAL_GAP=50,  # 字符之间的最大间隔,用于文本行的合并
                    MIN_V_OVERLAPS=0.6,
                    MIN_SIZE_SIM=0.6,
                    TEXT_PROPOSALS_MIN_SCORE=0.1,
                    TEXT_PROPOSALS_NMS_THRESH=0.3,
                    TEXT_LINE_NMS_THRESH=0.7),  # 文本行之间测iou值
                leftAdjust=True,  # 对检测的文本行进行向左延伸
                rightAdjust=True,  # 对检测的文本行进行向右延伸
                alpha=0.01)  # 对检测的文本行进行向右、左延伸的倍数

            print('[POST] result', result)
            print('[POST] angle', angle)

            if billModel == '' or billModel == '通用OCR':
                result = union_rbox(result, 0.2)
                res = [{
                    'text': x['text'],
                    'name': str(i),
                    'box': {
                        'cx': x['cx'],
                        'cy': x['cy'],
                        'w': x['w'],
                        'h': x['h'],
                        'angle': x['degree']
                    }
                } for i, x in enumerate(result)]
                res = adjust_box_to_origin(img, angle, res)  # 修正box

            elif billModel == '火车票':
                res = trainTicket.trainTicket(result)
                res = res.res
                res = [{
                    'text': res[key],
                    'name': key,
                    'box': {}
                } for key in res]

            elif billModel == '身份证':
                res = idcard.idcard(result)
                res = res.res
                res = [{
                    'text': res[key],
                    'name': key,
                    'box': {}
                } for key in res]

        timeTake = time.time() - timeTake

        os.remove(path)
        return json.dumps({
            'res': res,
            'timeTake': round(timeTake, 4)
        },
                          ensure_ascii=False)
Example #11
0
def ocr():
    if request.method == "POST":
        request_start_time = time.time()
        uid_job = uuid.uuid1().__str__()
        data = request.json

        # 模型参数
        bill_model = data.get("billModel", "")

        # 文字检测
        # text_angle = data.get('textAngle', False)

        # 只进行单行识别
        text_line = data.get("textLine", False)

        img_str = data["imgString"].encode().split(b";base64,")[-1]
        img = base64_to_PIL(img_str)
        if img is None:
            response_time = time.time() - request_start_time
            return jsonify({"res": [], "timeTake": round(response_time, 4)})
        else:
            img = np.array(img)
            h, w = img.shape[:2]

            final_result: list = []
            while time.time() - request_start_time <= TIMEOUT:
                if os.path.exists(file_lock):
                    continue
                else:
                    with open(file_lock, "w") as f:
                        f.write(uid_job)
                    if text_line:
                        # 单行识别
                        part_img = Image.fromarray(img)
                        text = crnn_handle.predict(part_img)
                        final_result = [{
                            "text": text,
                            "name": "0",
                            "box": [0, 0, w, 0, w, h, 0, h]
                        }]
                        os.remove(file_lock)
                        break
                    else:
                        result = text_predict(img)
                        if bill_model == "" or bill_model == "通用OCR":
                            final_result = [{
                                "text": x["text"],
                                "name": str(i),
                                "box": {
                                    "cx": x["cx"],
                                    "cy": x["cy"],
                                    "w": x["w"],
                                    "h": x["h"],
                                    "angle": x["degree"],
                                },
                            } for i, x in enumerate(result)]
                        elif bill_model == "火车票":
                            train_ticket_result = trainTicket.trainTicket(
                                result)
                            result = train_ticket_result.res
                            final_result = [{
                                "text": result[key],
                                "name": key,
                                "box": {}
                            } for key in result]
                        elif bill_model == "身份证":
                            id_card_result = idcard.idcard(result)
                            result = id_card_result.res
                            final_result = [{
                                "text": result[key],
                                "name": key,
                                "box": {}
                            } for key in result]
                        os.remove(file_lock)
                        break

            response_time = time.time() - request_start_time
            return jsonify({
                "res": final_result,
                "timeTake": round(response_time, 4)
            })
Example #12
0
def upload():
    t = time.time()
    parser = reqparse.RequestParser()
    parser.add_argument('file')
    args = parser.parse_args()
    # data = request.json
    uidJob = uuid.uuid1().__str__()
    file = args['file']
    # data = json.loads(data)
    billModel = '通用OCR'
    textAngle = True  # 文字检测
    imgString = file.encode().split(b';base64,')[-1]
    img = base64_to_PIL(imgString)
    if img is not None:
        img = np.array(img)
    else:
        return 0

    H, W = img.shape[:2]

    while time.time() - t <= TIMEOUT:
        if os.path.exists(filelock):
            continue
        else:
            with open(filelock, 'w') as f:
                f.write(uidJob)
            detectAngle = textAngle
            result, angle = model.model(img,
                                        scale=scale,
                                        maxScale=maxScale,
                                        detectAngle=detectAngle,  # 是否进行文字方向检测,通过web传参控制
                                        MAX_HORIZONTAL_GAP=100,  # 字符之间的最大间隔,用于文本行的合并
                                        MIN_V_OVERLAPS=0.6,
                                        MIN_SIZE_SIM=0.6,
                                        TEXT_PROPOSALS_MIN_SCORE=0.1,
                                        TEXT_PROPOSALS_NMS_THRESH=0.3,
                                        TEXT_LINE_NMS_THRESH=0.99,  # 文本行之间测iou值
                                        LINE_MIN_SCORE=0.1,
                                        leftAdjustAlph=0.01,  # 对检测的文本行进行向左延伸
                                        rightAdjustAlph=0.01,  # 对检测的文本行进行向右延伸
                                        )

            if billModel == '' or billModel == '通用OCR':
                result = union_rbox(result, 0.2)
                res = {'name': '', 'position': '', 'company': '', 'local': '', 'email': '', 'phone': '',
                       'other': ''}
                # 对公司进行提取
                companyKeyword = ['公司', 'company', '银行', 'Bank', '集团', 'Group', '商行', 'Factory', 'CO', 'LTD']
                for i, x in enumerate(result):
                    for ck in companyKeyword:
                        if ck in x['text']:
                            res['company'] = x['text']
                            result.pop(i)
                            break
                    if res['company']:
                        break
                name = ''
                name_height = 0
                pop_data = 0
                # 对姓名进行处理
                for i, x in enumerate(result):
                    if 'name' in x['text'] or '姓名' in x['text']:
                        res['name'] = x['text'].replace('name', '')
                        res['name'] = res['name'].replace('姓名', '')
                        res['name'] = res['name'].replace(':', '')
                        result.pop(i)
                        break
                    else:
                        # 正则取中文,进行分别进行中文名和英文名的判断
                        text_list = re.findall(r'[\u4e00-\u9fa5]', x['text'])
                        if text_list:
                            if name_height < x['h'] and 1 < len(text_list) < 5:
                                name_height = x['h']
                                name = x['text']
                                pop_data = i
                        else:
                            strUpper = x['text'].upper()
                            if name_height < x['h'] and strUpper != x['text']:
                                name_height = x['h']
                                name = x['text']
                                pop_data = i
                    res['name'] = name
                result.pop(pop_data)
                # 对职位进行提取
                pos = ['GM', 'VP', 'HRD', 'OD', 'MD', 'OM', 'PM', 'BM', 'DM', 'RM', 'AAD', 'ACD', 'AD', 'AE',
                       'AP', 'ASM', 'VC', 'CWO', 'COO', 'CXO', 'CYO', 'CZO', 'PS', 'Manager', 'Engineer']
                posNum = "".join(filter(str.isdigit, x['text']))
                for i, x in enumerate(result[pop_data:pop_data + 3]):
                    if '职位' in x['text'] or 'position' in x['text']:
                        res['position'] = x['text'].replace('职位', '')
                        res['position'] = res['position'].replace('position', '')
                        res['position'] = res['position'].replace(':', '')
                        result.pop(i)
                        break
                if not res['position']:
                    for i, x in enumerate(result[pop_data:pop_data + 3]):
                        for ch in x['text']:
                            if u'\u4e00' <= ch <= u'\u9fff' and len(posNum) == 0:
                                res['position'] = x['text']
                                result.pop(i)
                                break
                        if res['position']:
                            break
                        for p in pos:
                            if p in x['text']:
                                res['position'] = x['text']
                                result.pop(i)
                                break
                        if res['position']:
                            break
                # 对邮箱进行提取
                for i, x in enumerate(result):
                    if '@' in x['text'] or '邮箱' in x['text'] or 'email' in x['text']:
                        res['email'] = x['text'].replace('邮箱', '')
                        res['email'] = res['email'].replace('email', '')
                        res['email'] = res['email'].replace(':', '')
                        result.pop(i)
                        break
                # 对地址进行提取
                localKeyword = ['市', '省', '区', '号', '路', '岛', '地址', 'sheng', 'shi', 'qu', 'hao', 'lu']
                for i, x in enumerate(result):
                    for k in localKeyword:
                        if k in x['text'] or 'Add' in x['text'] or 'add' in x['text']:
                            res['local'] = x['text'].replace('地址', '')
                            res['local'] = res['local'].replace('Add', '')
                            res['local'] = res['local'].replace(':', '')
                            result.pop(i)
                            break
                    if res['local']:
                        break

                # 对手机号码进行提取
                for i, x in enumerate(result):
                    if '-' not in x['text'] and '-' not in x['text']:
                        telephone = "".join(filter(str.isdigit, x['text']))
                        if 11 <= len(telephone) <= 13:
                            res['phone'] += x['text'].replace('电话', '') + ';'
                            res['phone'] = res['phone'].replace('Tel', '')
                            res['phone'] = res['phone'].replace('手机', '')
                            res['phone'] = res['phone'].replace(':', '')
                            result.pop(i)
                # 其他文本提取
                for i, x in enumerate(result):
                    res['other'] += x['text'] + ';'

            elif billModel == '火车票':
                res = trainTicket.trainTicket(result)
                res = res.res
                res = [{'text': res[key], 'name': key, 'box': {}} for key in res]

            elif billModel == '身份证':

                res = idcard.idcard(result)
                res = res.res
                res = [{'text': res[key], 'name': key, 'box': {}} for key in res]

            os.remove(filelock)
            break

    timeTake = time.time() - t
    return jsonify({
        "code": "0000",
        "msg": "成功",
        "data": res
    })
Example #13
0

def result(img):
    back = {}
    img = Image.open(BytesIO(img)).convert('RGB')
    img = np.array(img)
    result = text_predict(img)
    back['文本'] = list(map(lambda x: x['text'], result))
    res = trainTicket.trainTicket(result)
    back['火车票'] = str(res)
    res = idcard.idcard(result)
    back['身份证'] = str(res)
    return back


if __name__ == '__main__':
    img = './test/idcard-demo.jpg'
    img = Image.open(img).convert('RGB')
    # img.show()
    img = np.array(img)
    text = text_predict(img)
    print('文本预测:', list(map(lambda x: x['text'], text)))

    # 火车票
    res = trainTicket.trainTicket(text)
    print('火车票预测:', res)

    # 身份证
    res = idcard.idcard(text)
    print('身份证预测:', res)
Example #14
0
def run_ocr(img, billModel, textLine):
    t = time.time()
    H, W = img.shape[:2]
    res = ''
    uidJob = uuid.uuid1().__str__()

    while time.time() - t <= TIMEOUT:
        if os.path.exists(filelock):
            continue
        else:
            with open(filelock, 'w') as f:
                f.write(uidJob)

            if textLine:
                ##单行识别
                partImg = Image.fromarray(img)
                text = crnn_handle.predict(partImg)
                res = [{
                    'text': text,
                    'name': '0',
                    'box': [0, 0, W, 0, W, H, 0, H]
                }]
                os.remove(filelock)
                break

            else:
                # detectAngle = textAngle
                result = text_predict(img)

                if billModel == '' or billModel == '通用OCR':
                    # result = union_rbox(result,0.2)
                    res = [{
                        'text': x['text'],
                        'name': str(i),
                        'box': {
                            'cx': x['cx'],
                            'cy': x['cy'],
                            'w': x['w'],
                            'h': x['h'],
                            'angle': x['degree']
                        }
                    } for i, x in enumerate(result)]
                    # res = adjust_box_to_origin(img,angle, res)##修正box

                elif billModel == '火车票':
                    res = trainTicket.trainTicket(result)
                    res = res.res
                    res = [{
                        'text': res[key],
                        'name': key,
                        'box': {}
                    } for key in res]

                elif billModel == '身份证':

                    res = idcard.idcard(result)
                    res = res.res
                    res = [{
                        'text': res[key],
                        'name': key,
                        'box': {}
                    } for key in res]

                os.remove(filelock)
                break

    return res
Example #15
0
    def getWordRecognition(self, img_file, bill_model):
        billModel = bill_model
        textAngle = True ##文字检测
        textLine = False ##只进行单行识别
        text = ''

        file_name = os.path.basename(img_file)
        file_path = os.path.dirname(img_file)

        # img = cv2.imread(img_file)##GBR
        img,is_exif,H,W = self.setExif(img_file)
        if is_exif is True:
            textAngle = False

        # H,W = img.shape[:2]
        timeTake = time.time()
        if textLine:
            ##单行识别
            partImg = Image.fromarray(img)
            text = model.crnnOcr(partImg.convert('L'))
            res =[ {'text':text,'name':'0','box':[0,0,W,0,W,H,0,H]} ]
        else:
            detectAngle = textAngle
            _,result,angle= model(img,
                                        detectAngle=detectAngle,##是否进行文字方向检测,通过web传参控制
                                        config=dict(MAX_HORIZONTAL_GAP=50,##字符之间的最大间隔,用于文本行的合并
                                        MIN_V_OVERLAPS=0.6,
                                        MIN_SIZE_SIM=0.6,
                                        TEXT_PROPOSALS_MIN_SCORE=0.1,
                                        TEXT_PROPOSALS_NMS_THRESH=0.3,
                                        TEXT_LINE_NMS_THRESH = 0.7,##文本行之间测iou值
                                                ),
                                        leftAdjust=True,##对检测的文本行进行向左延伸
                                        rightAdjust=True,##对检测的文本行进行向右延伸
                                        alph=0.01,##对检测的文本行进行向右、左延伸的倍数
                                       )



            if billModel=='' or billModel=='通用OCR' :
                text = self.getTextList(img,angle, result)
                result = union_rbox(result,0.2)
                res = [{'text':x['text'],
                        'name':str(i),
                        'box':{'cx':x['cx'],
                               'cy':x['cy'],
                               'w':x['w'],
                               'h':x['h'],
                               'angle':x['degree']

                              }
                       } for i,x in enumerate(result)]
                res = adjust_box_to_origin(np.copy(img),angle, res)##修正box
                com_res = res

            elif billModel=='身份证':

                res = idcard.idcard(result)
                res = res.res
                res =[ {'text':res[key],'name':key,'box':{}} for key in res]
                text = self.getTextList(img,angle, result)
                result = union_rbox(result,0.2)
                com_res = [{'text':x['text'],
                        'name':str(i),
                        'box':{'cx':x['cx'],
                               'cy':x['cy'],
                               'w':x['w'],
                               'h':x['h'],
                               'angle':x['degree']
                              }
                       } for i,x in enumerate(result)]
                com_res = adjust_box_to_origin(np.copy(img),angle, com_res)##修正box
            
            elif billModel=='驾驶证':

                res = drivinglicense.drivinglicense(result)
                res = res.res
                res =[ {'text':res[key],'name':key,'box':{}} for key in res]
                text = self.getTextList(img,angle, result)
                result = union_rbox(result,0.2)
                com_res = [{'text':x['text'],
                        'name':str(i),
                        'box':{'cx':x['cx'],
                               'cy':x['cy'],
                               'w':x['w'],
                               'h':x['h'],
                               'angle':x['degree']
                              }
                       } for i,x in enumerate(result)]
                com_res = adjust_box_to_origin(np.copy(img),angle, com_res)##修正box

            elif billModel=='行驶证':

                res = vehiclelicense.vehiclelicense(result)
                res = res.res
                res =[ {'text':res[key],'name':key,'box':{}} for key in res]
                text = self.getTextList(img,angle, result)
                result = union_rbox(result,0.2)
                com_res = [{'text':x['text'],
                        'name':str(i),
                        'box':{'cx':x['cx'],
                               'cy':x['cy'],
                               'w':x['w'],
                               'h':x['h'],
                               'angle':x['degree']
                              }
                       } for i,x in enumerate(result)]
                com_res = adjust_box_to_origin(np.copy(img),angle, com_res)##修正box

            elif billModel=='营业执照':

                res = businesslicense.businesslicense(result)
                res = res.res
                res =[ {'text':res[key],'name':key,'box':{}} for key in res]
                text = self.getTextList(img,angle, result)
                result = union_rbox(result,0.2)
                com_res = [{'text':x['text'],
                        'name':str(i),
                        'box':{'cx':x['cx'],
                               'cy':x['cy'],
                               'w':x['w'],
                               'h':x['h'],
                               'angle':x['degree']
                              }
                       } for i,x in enumerate(result)]
                com_res = adjust_box_to_origin(np.copy(img),angle, com_res)##修正box
        
            elif billModel=='银行卡':
                res = bankcard.bankcard(result)
                res = res.res
                res =[ {'text':res[key],'name':key,'box':{}} for key in res]
                text = self.getTextList(img,angle, result)
                result = union_rbox(result,0.2)
                com_res = [{'text':x['text'],
                        'name':str(i),
                        'box':{'cx':x['cx'],
                               'cy':x['cy'],
                               'w':x['w'],
                               'h':x['h'],
                               'angle':x['degree']
                              }
                       } for i,x in enumerate(result)]
                com_res = adjust_box_to_origin(np.copy(img),angle, com_res)##修正box

            elif billModel=='手写体':
                result = union_rbox(result,0.2)
                res = [{'text':x['text'],
                        'name':str(i),
                        'box':{'cx':x['cx'],
                               'cy':x['cy'],
                               'w':x['w'],
                               'h':x['h'],
                               'angle':x['degree']

                              }
                       } for i,x in enumerate(result)]
                res = adjust_box_to_origin(np.copy(img),angle, res)##修正box
                text = self.getTextList(img,angle, result)

            elif billModel=='车牌':
                res = vehicleplate.vehicleplate(result)
                res = res.res
                res =[ {'text':res[key],'name':key,'box':{}} for key in res]
                text = self.getTextList(img,angle, result)
                result = union_rbox(result,0.2)
                com_res = [{'text':x['text'],
                        'name':str(i),
                        'box':{'cx':x['cx'],
                               'cy':x['cy'],
                               'w':x['w'],
                               'h':x['h'],
                               'angle':x['degree']
                              }
                       } for i,x in enumerate(result)]
                com_res = adjust_box_to_origin(np.copy(img),angle, com_res)##修正box
            
            elif billModel=='名片':
                res = businesscard.businesscard(result)
                res = res.res
                res =[ {'text':res[key],'name':key,'box':{}} for key in res]
                text = self.getTextList(img,angle, result)
                result = union_rbox(result,0.2)
                com_res = [{'text':x['text'],
                        'name':str(i),
                        'box':{'cx':x['cx'],
                               'cy':x['cy'],
                               'w':x['w'],
                               'h':x['h'],
                               'angle':x['degree']
                              }
                       } for i,x in enumerate(result)]
                com_res = adjust_box_to_origin(np.copy(img),angle, com_res)##修正box
            
        
        timeTake = time.time()-timeTake

        #draw box in to original image
        drawBoxes = []
        draw_filename = file_name.split('.')[0] + '_drawed.' + file_name.split('.')[1]
        drawPath = os.path.join(file_path,draw_filename)
        drawUrl = settings.FILE_URL +  settings.MEDIA_URL + 'photos' + '/' + draw_filename
        # img =  cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
        if len(com_res) > 0:
            for arr in com_res:
               drawBoxes.append(arr["box"])
            drawImg = draw_boxes(np.array(img),drawBoxes)
            cv2.imwrite(drawPath, drawImg)

        return {'res':res,'timeTake':round(timeTake,4), 'text':text, 'com_res': com_res, 'drawUrl': drawUrl}
Example #16
0
    model = TextOcrModel(ocr, text_detect, angle_detect)
    # arguments
    textAngle = False
    # load image
    img = cv2.imread(args["image_file"])

    # predict
    detectAngle = textAngle
    result, angle = model.model(
        img,
        scale=scale,
        maxScale=maxScale,
        detectAngle=detectAngle,  ##是否进行文字方向检测,通过web传参控制
        MAX_HORIZONTAL_GAP=100,  ##字符之间的最大间隔,用于文本行的合并
        MIN_V_OVERLAPS=0.6,
        MIN_SIZE_SIM=0.6,
        TEXT_PROPOSALS_MIN_SCORE=0.1,
        TEXT_PROPOSALS_NMS_THRESH=0.3,
        TEXT_LINE_NMS_THRESH=0.99,  ##文本行之间测iou值
        LINE_MIN_SCORE=0.1,
        leftAdjustAlph=0.01,  ##对检测的文本行进行向左延伸
        rightAdjustAlph=0.01,  ##对检测的文本行进行向右延伸
    )

    res = idcard.idcard(result)
    # res.res 是idcard类的一个用于存放各类信息集合的列表属性
    res = res.res
    res = [{'text': res[key], 'attribute': key, 'box': {}} for key in res]
    print(res)
    fps.stop()
    print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
Example #17
0
    def POST(self):
        t = time.time()
        data = web.data()
        uidJob = uuid.uuid1().__str__()
        
        data = json.loads(data)
        # print(data)
        billModel = data.get('billModel','')
        # textAngle = data.get('textAngle',False)##文字检测
        textLine = data.get('textLine',False)##只进行单行识别



        imgString = data['imgString'].encode().split(b';base64,')[-1]
        img = base64_to_PIL(imgString)
        if img is not None:
            img = np.array(img)
            
        H,W = img.shape[:2]

        while time.time()-t<=TIMEOUT:
            if os.path.exists(filelock):
                continue
            else:
                with open(filelock,'w') as f:
                    f.write(uidJob)
                                                
                if textLine:
                    ##单行识别
                    partImg = Image.fromarray(img)
                    text    = crnn_handle.predict(partImg)
                    res =[ {'text':text,'name':'0','box':[0,0,W,0,W,H,0,H]} ]
                    os.remove(filelock)
                    break
                        
                else:
                    # detectAngle = textAngle
                    result= text_predict(img)
        
        
        
                    if billModel=='' or billModel=='通用OCR' :
                        # result = union_rbox(result,0.2)
                        res = [{'text':x['text'],
                                'name':str(i),
                                'box':{'cx':x['cx'],
                                       'cy':x['cy'],
                                       'w':x['w'],
                                       'h':x['h'],
                                       'angle':x['degree']
        
                                      }
                               } for i,x in enumerate(result)]
                        # res = adjust_box_to_origin(img,angle, res)##修正box
        
                    elif billModel=='火车票':
                        res = trainTicket.trainTicket(result)
                        res = res.res
                        res =[ {'text':res[key],'name':key,'box':{}} for key in res]
        
                    elif billModel=='身份证':
        
                        res = idcard.idcard(result)
                        res = res.res
                        res =[ {'text':res[key],'name':key,'box':{}} for key in res]
                        
                    os.remove(filelock)
                    break
            
        
        timeTake = time.time()-t
         
        return json.dumps({'res':res,'timeTake':round(timeTake,4)},ensure_ascii=False)
Example #18
0
    def getWordRecognition(self, img_file, bill_model):
        billModel = bill_model
        textAngle = True  ##文字检测
        textLine = False  ##只进行单行识别

        img = cv2.imread(img_file)  ##GBR
        H, W = img.shape[:2]
        timeTake = time.time()
        if textLine:
            ##单行识别
            partImg = Image.fromarray(img)
            text = model.crnnOcr(partImg.convert('L'))
            res = [{
                'text': text,
                'name': '0',
                'box': [0, 0, W, 0, W, H, 0, H]
            }]
        else:
            detectAngle = textAngle
            _, result, angle = model(
                img,
                detectAngle=detectAngle,  ##是否进行文字方向检测,通过web传参控制
                config=dict(
                    MAX_HORIZONTAL_GAP=50,  ##字符之间的最大间隔,用于文本行的合并
                    MIN_V_OVERLAPS=0.6,
                    MIN_SIZE_SIM=0.6,
                    TEXT_PROPOSALS_MIN_SCORE=0.1,
                    TEXT_PROPOSALS_NMS_THRESH=0.3,
                    TEXT_LINE_NMS_THRESH=0.7,  ##文本行之间测iou值
                ),
                leftAdjust=True,  ##对检测的文本行进行向左延伸
                rightAdjust=True,  ##对检测的文本行进行向右延伸
                alph=0.01,  ##对检测的文本行进行向右、左延伸的倍数
            )

            if billModel == '' or billModel == '通用OCR':
                result = union_rbox(result, 0.2)
                res = [{
                    'text': x['text'],
                    'name': str(i),
                    'box': {
                        'cx': x['cx'],
                        'cy': x['cy'],
                        'w': x['w'],
                        'h': x['h'],
                        'angle': x['degree']
                    }
                } for i, x in enumerate(result)]
                res = adjust_box_to_origin(img, angle, res)  ##修正box

            elif billModel == '身份证':

                res = idcard.idcard(result)
                res = res.res
                res = [{
                    'text': res[key],
                    'name': key,
                    'box': {}
                } for key in res]

            elif billModel == '驾驶证':

                res = drivinglicense.drivinglicense(result)
                res = res.res
                res = [{
                    'text': res[key],
                    'name': key,
                    'box': {}
                } for key in res]

            elif billModel == '行驶证':

                res = vehiclelicense.vehiclelicense(result)
                res = res.res
                res = [{
                    'text': res[key],
                    'name': key,
                    'box': {}
                } for key in res]

            elif billModel == '银行卡':
                res = bankcard.bankcard(result)
                res = res.res
                res = [{
                    'text': res[key],
                    'name': key,
                    'box': {}
                } for key in res]

            elif billModel == '手写体':
                result = union_rbox(result, 0.2)
                res = [{
                    'text': x['text'],
                    'name': str(i),
                    'box': {
                        'cx': x['cx'],
                        'cy': x['cy'],
                        'w': x['w'],
                        'h': x['h'],
                        'angle': x['degree']
                    }
                } for i, x in enumerate(result)]
                res = adjust_box_to_origin(img, angle, res)  ##修正box

            elif billModel == '车牌':
                res = vehicleplate.vehicleplate(result)
                res = res.res
                res = [{
                    'text': res[key],
                    'name': key,
                    'box': {}
                } for key in res]

        timeTake = time.time() - timeTake

        return {'res': res, 'timeTake': round(timeTake, 4)}