folder_wsc_process.py 31 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
import os
import re
import time
import shutil
import base64
import signal
import requests
import traceback
from django import db
from PIL import Image
from datetime import datetime
from django.core.management import BaseCommand
from multiprocessing import Process
import numpy as np
from fuzzywuzzy import fuzz
from shapely.geometry import Polygon

from settings import conf
from common.mixins import LoggerMixin
from common.tools.pdf_to_img import PDFHandler
from apps.doc import consts
from apps.doc.exceptions import OCR1Exception, OCR4Exception
from apps.doc.ocr.wb import BSWorkbook, PatternFill
from apps.doc.models import OfflineReport
from apps.doc.named_enum import OfflineFailureReason


class Finder:
    """Summary
    
    Attributes:
        ocr_results (TYPE): Description
    """
    
    def __init__(self, ocr_results=None):
        self.ocr_results = ocr_results
        self.init_result = {
            "合同编号列表": [],
            "经销商名称_Page3": "",
            "经销商名称_Page38": "",
            "经销商统一社会信用代码或公司注册号": "",
            "保证人": "",
            "综合授信额度金额英文": "",
            "综合授信额度金额中文": "",
            "综合授信额度期限开始日期英文": '',
            "综合授信额度期限截止日期英文": '',
            "综合授信额度期限开始日期中文": '',
            "综合授信额度期限截止日期中文": '',
            "保证金比例英文": "",
            "保证金比例中文": "",
            "其他约定与条件英文": "",
            "其他约定与条件中文": "",
        }

    def get_line(self, ocr_results, key_string):
        # 根据指定关键词, 找出与关键词同处一行的字符
        top, bottom = -1, -1
        # 首先找到这个关键词所在的 Bbox
        for key in ocr_results:
            bbox, text = ocr_results[key]
            if key_string in text:
                top, bottom = min(bbox[1::2]), max(bbox[1::2])
                break
        line_text = []
        # 然后找到一行
        for key in ocr_results:
            bbox, text = ocr_results[key]
            if top < np.mean(bbox[1::2]) < bottom:
                line_text.append([bbox, text])
        # 从左到右排序
        lines = ''
        if len(line_text) > 0:
            line_text = sorted(line_text, key=lambda x: x[0][0], reverse=False)
            lines = ''.join([i[1] for i in line_text])
        return lines

    def page_predict(self, ocr_results, page_template):
        classes = []
        for pno in ocr_results:
            ocr_texts = ''
            for key in ocr_results[pno]:
                bbox, text = ocr_results[pno][key]
                ocr_texts += text
            pattern = re.compile("[^\u4e00-\u9fa5]")        # 匹配不是中文的其他字符
            ocr_texts = pattern.sub('', ocr_texts)
            score = fuzz.ratio(page_template, ocr_texts)/100.
            classes.append([pno, score])
        pred = sorted(classes, key=lambda x: x[1], reverse=True)[0]
        return pred

    def get_top_key(self, ocr_results, key_string):          # 加入过滤词典
        """找到与 key_string 最匹配的字段的 key
        """
        if len(ocr_results) == 0:
            return -1, -1
        ratio_list = [[fuzz.ratio(key_string, ocr_results[key][1]), key] for key in ocr_results]
        top_key = sorted(ratio_list, key=lambda x: x[0])[-1]
        return top_key

    def get_top_iou(self, ocr_results, poly):
        """求最大IoU
        """
        iou_list = []
        for key in ocr_results:
            bbox, text = ocr_results[key]
            g = Polygon(np.array(bbox).reshape((-1, 2)))
            p = Polygon(np.array(poly).reshape((-1, 2)))
            if not g.is_valid or not p.is_valid:
                continue
            inter = Polygon(g).intersection(Polygon(p)).area
            union = g.area + p.area - inter
            iou = inter/union
            iou_list.append([iou, key])
        if len(iou_list) == 0:
            return -1, -1
        top_iou = sorted(iou_list, key=lambda x: x[0])[-1]
        return top_iou

    def get_key_value(self, ocr_results, key_string):
        """根据 key 查找 value
        """
        value = ''
        tmp_ocr_results = {}
        for key in ocr_results:
            bbox, text = ocr_results[key]
            # 定制化规则, 比如过滤一些词呀什么的
            # 该例中, 我们要去掉非中文字符
            pattern = re.compile("[^\u4e00-\u9fa5]")        # 匹配不是中文的其他字符
            text = pattern.sub('', text) 
            tmp_ocr_results[key] = [bbox, text]
        # 先根据 key_string 找到 key 的位置所在, 再判断该位置是否包含 value
        # 若不包含 value, 则往右边一个单位查找 value
        ratio, key = self.get_top_key(tmp_ocr_results, key_string)
        if ratio > 50:
            bbox, text = ocr_results[key]
            words = text.strip(key_string).split(':')[-1]
            if len(words) == 0:
                # 将 bbox 右移一个单位
                x0, y0, x1, y1, x2, y2, x3, y3 = bbox
                rw = abs(x0-x1)
                anchor = [x0+rw, y0, x1+rw, y1, x2+rw, y2, x3+rw, y3]
                iou, key = self.get_top_iou(ocr_results, anchor)
                if ratio > 0.3:
                    bbox, text = ocr_results[key]
                    words = text.strip(key_string).split(':')[-1]
                    value = words
            else:
                value = words
        return value

    def get_contract_No(self):
        """提取左上角的合同编号字段
        """
        contract_No_list = []
        for pno in self.ocr_results:
            # 请务必保证 OCR 结果不为空, 否则直接返回空屁
            if len(self.ocr_results[pno]) > 0:
                contract_No = self.get_key_value(self.ocr_results[pno], '合同编号')
            else:
                contract_No = ''
            # 临时解决 S 识别成 8 的问题
            # TODO!!!
            contract_No_list.append(contract_No)
        return contract_No_list

    def get_info_in_page_3(self):
        """提取第三页上的经销商名称,和经销商统一社会信用代码或公司注册号
        """
        dealer_name = ''
        dealer_No = ''
        template = r"""合同编号宝马汽车金融中国有限公司甲方宝马汽车金融中国有限公司地址中国北京市朝阳区东三环北路霞光里号佳程
                       广场座层乙方统一社会信用代码或公司注册号地址鉴于甲方是一家依照中国法律合法组建和存续的汽车金融公司愿意
                       为宝马中国汽车贸易有限公司以下简称宝马中国及华晨宝马汽车有限公司以下简称华晨宝马在中国大陆的宝马集团经
                       销商提供汽车批售融资服务乙方是一家依据中国法律合法组建和存续与宝马中国和或华晨宝马签署了授权销售合同具
                       有专营进口和或国产宝马集团产品合法资格的企业本着自愿平等互惠互利的原则甲乙双方经充分协商签署本综合授信
                       额度合同本合同达成如下条款综合授信额度合同版本""".replace(" ", "").replace("\n", "")
        # 首先找到第三页纸, 我们阈值设为0.5
        pno, score = self.page_predict(self.ocr_results, template)
        if score > 0.5:
            if len(self.ocr_results[pno]) > 0:
                # print(self.ocr_results[pno])
                # 在所有字段中搜索乙方
                for key in self.ocr_results[pno]:
                    bbox, text = self.ocr_results[pno][key]
                    if '乙方:' in text:
                        words = text.split(':')[-1].replace('【', '[').replace('{', '[').replace('】', ']')
                        dealer_name = words
                words = self.get_key_value(self.ocr_results[pno], '统一社会信用代码或公司注册号')
                dealer_No = words.replace('O', '0')
        return dealer_name, dealer_No

    def get_info_in_page_38(self):
        """提取第38页上的经销商名称
        """
        dealer_name = ''
        template = r"""宝马汽车金融中国有限公司合同编号签署页甲方宝马汽车金融中国有限公司盖章姓名姓名职务职务日期乙方汽车销售服务
                       有限公司盖章姓名姓名职务职务日期综合授信额度合同版本""".replace(" ", "").replace("\n", "")
        # 首先找到第38页纸, 我们阈值设为0.5
        pno, score = self.page_predict(self.ocr_results, template)
        if score > 0.5:
            if len(self.ocr_results[pno]) > 0:
                for key in self.ocr_results[pno]:
                    bbox, text = self.ocr_results[pno][key]
                    if '乙方:' in text:
                        words = text.split(':')[-1].replace('【', '[').replace('{', '[')
                        words = re.sub(r'[(())盖章《]', "", words)
                        dealer_name = words
        return dealer_name

    def get_guarantor(self):
        """提取第10页上保证人段落,所见即所得
        """
        guarantor = '[/]'
        all_texts = ''
        for pno in self.ocr_results:
            for key in self.ocr_results[pno]:
                bbox, text = self.ocr_results[pno][key]
                all_texts += text
        searchObj = re.search( r'保证人\[(.*?)\]与甲方', all_texts)
        if searchObj:
            words = f'[{searchObj.group(1)}]'
            words = words.replace('【', '[').replace('】', ']').replace(',', ',').replace('(', '(').replace(')', ')')
            guarantor = words
        return guarantor

    def get_info_in_page_39(self):
        """提取综合授信合同上的一些字段
        """
        # Amount of General Credit Line
        amount_eng = ''
        amount_chn = ''
        term_start_eng = ''
        term_end_eng = ''
        term_start_chn = ''
        term_end_chn = ''
        deposit_eng = ''
        deposit_chn = ''
        template = r"""合同编号中国有限公司宝马汽车金融综合授信额度合同附件确认函综合授信额度金额本合同项下的综合授信额度为人民币
                       大写综合授信额度下面各个业务的授信额度将由甲方以授信额度通知函的方式时不时的通知乙方本合同项下的综合授信额
                       度可以由甲方根据乙方的信用和财务状况自行决定随时调整本合同项下的综合授信额度应为在本确认函第条的期间内双方
                       在确认函中确认的授信额度以及甲方向乙方时不时通过书面授信额度通知函以及临时授信额度通知函中沟通的额度的总和
                       综合授信额度期限从至或者由甲方向乙方通过书面形式在授信额度通知函中沟通的更短期间保证金甲方对乙方的最低保证
                       金要求为综合授信额度的实际执行的保证金比例以甲方不时另行书面通知根据最新的经销商融资或保证金相关政策或活动
                       为准综合授信额度合同版本""".replace(" ", "").replace("\n", "")
        # 首先找到综合授信合同第一面, 我们阈值设为0.5
        pno, score = self.page_predict(self.ocr_results, template)
        if score > 0.5:
            if len(self.ocr_results[pno]) > 0:
                # 根据关键词,找这一行字符
                lines = ''
                for i in ['RMB', 'CNY']:
                    lines += self.get_line(self.ocr_results[pno], i)
                # searchObj = re.search( r'RMB(.*?)in', lines)
                searchObj = re.search(r'[0-9,.]+', lines)
                if searchObj:
                    words = searchObj.group()
                    amount_eng = words
                lines = self.get_line(self.ocr_results[pno], '人民币')
                searchObj = re.search( r'大写(.*?)综合', lines)
                if searchObj:
                    words = searchObj.group(1)
                    pattern = re.compile("[^\u4e00-\u9fa5]")        # 匹配不是中文的其他字符
                    words = pattern.sub('', words)
                    words = words.replace("仔", "仟").replace("任", "仟")
                    words = words.replace("值", "佰")
                    words = words.replace("拐", "捌")
                    words = words.replace("查", "壹")
                    words = words.replace("政", "玖")
                    words = words.replace("垒", "叁")
                    amount_chn = words
                
                lines = self.get_line(self.ocr_results[pno], 'ending')
                if len(lines) > 0:
                    start, end = lines.split('ending')
                    searchStart = re.search( r'[0-9]+-[0-9a-zA-Z]+-[0-9]{4}', start)
                    if searchStart:
                        words = searchStart.group()
                        term_start_eng = words
                    searchEnd = re.search( r'[0-9]+-[0-9a-zA-Z]+-[0-9]{4}', end)
                    if searchEnd:
                        words = searchEnd.group()
                        term_end_eng = words
                lines = self.get_line(self.ocr_results[pno], '至')
                if len(lines) > 0:
                    start, end = lines.split('至')
                    searchStart = re.search( r'[0-9]{4}-[0-9]+-[0-9]+', start)
                    if searchStart:
                        words = searchStart.group()
                        term_start_chn = words
                    searchEnd = re.search( r'[0-9]{4}-[0-9]+-[0-9]+', end)
                    if searchEnd:
                        words = searchEnd.group()
                        term_end_chn = words
                
                lines = self.get_line(self.ocr_results[pno], 'above')
                searchObj = re.search( r'aboveto([0-9]+)', lines.replace('O', '0').replace('too', 'to0'))
                if searchObj:
                    words = searchObj.group(1)
                    deposit_eng = f'{words}%'
                lines = self.get_line(self.ocr_results[pno], '授信额度的')
                searchObj = re.search( r'授信额度的([0-9]+)', lines.replace('O', '0').replace('_', ''))
                if searchObj:
                    words = searchObj.group(1)
                    deposit_chn = f'{words}%'
        return amount_eng, amount_chn, term_start_eng, term_end_eng, \
                    term_start_chn, term_end_chn, deposit_eng, deposit_chn

    def get_other_arrangements_and_conditions(self):
        """获取其它约定与条件文本段落
        """
        other_arrangements_and_conditions_eng = ''
        other_arrangements_and_conditions_chn = ''
        all_texts = ''
        for pno in self.ocr_results:
            for key in self.ocr_results[pno]:
                all_texts += self.ocr_results[pno][key][1]
        searchObj = re.search(r'Conditions:(.*?)其他', all_texts, re.I)
        if searchObj:
            words = searchObj.group(1)
            pattern = re.compile("[\u4e00-\u9fa5]")        # 去除中文字符
            words = pattern.sub('', words)
            other_arrangements_and_conditions_eng = words
        searchObj = re.search(r'条件:(.*?)General', all_texts, re.I)
        if searchObj:
            words = searchObj.group(1)
            other_arrangements_and_conditions_chn = words
        return other_arrangements_and_conditions_eng, other_arrangements_and_conditions_chn

    def get_info(self):
        # 按照文档页码返回一个合同编号列表,依次表示每一页上识别到的合同编号
        contract_No_list = self.get_contract_No()
        self.init_result["合同编号列表"] = contract_No_list
        dealer_name, dealer_No = self.get_info_in_page_3()
        self.init_result["经销商名称_Page3"] = dealer_name
        self.init_result["经销商统一社会信用代码或公司注册号"] = dealer_No
        dealer_name = self.get_info_in_page_38()
        self.init_result["经销商名称_Page38"] = dealer_name
        guarantor = self.get_guarantor()
        self.init_result["保证人"] = guarantor
        amount_eng, amount_chn, term_start_eng, term_end_eng, \
            term_start_chn, term_end_chn, deposit_eng, deposit_chn = self.get_info_in_page_39()
        self.init_result["综合授信额度金额英文"] = amount_eng
        self.init_result["综合授信额度金额中文"] = amount_chn
        self.init_result["综合授信额度期限开始日期英文"] = term_start_eng
        self.init_result["综合授信额度期限截止日期英文"] = term_end_eng
        self.init_result["综合授信额度期限开始日期中文"] = term_start_chn
        self.init_result["综合授信额度期限截止日期中文"] = term_end_chn
        self.init_result["保证金比例英文"] = deposit_eng
        self.init_result["保证金比例中文"] = deposit_chn
        words_eng, words_chn = self.get_other_arrangements_and_conditions()
        self.init_result["其他约定与条件英文"] = words_eng
        self.init_result["其他约定与条件中文"] = words_chn
        return self.init_result


class TIFFHandler:

    def __init__(self, path, img_save_path):
        self.path = path
        self.img_save_path = img_save_path
        self.img_path_list = []

    def extract_image(self):
        os.makedirs(self.img_save_path, exist_ok=True)
        tiff = Image.open(self.path)
        tiff.load()

        for i in range(tiff.n_frames):
            try:
                save_path = os.path.join(self.img_save_path, 'page_{0}.jpeg'.format(i))
                tiff.seek(i)
                tiff.save(save_path)
                self.img_path_list.append(save_path)
            except EOFError:
                break


class Command(BaseCommand, LoggerMixin):

    def __init__(self):
        super().__init__()
        self.log_base = '[folder wsc process]'
        # 处理文件开关
        self.switch = True
        self.sheet_name = 'Wholesales Contract'
        self.finder = Finder()
        # 睡眠时间
        self.sleep_time = float(conf.SLEEP_SECOND_FOLDER)
        # input folder
        self.input_dir = conf.WSC_DIR
        # ocr相关
        self.go_ocr_url = conf.WSC_GO_URL
        self.amount_fill = PatternFill("solid", fgColor="00FFFF00")
        # 优雅退出信号:15
        signal.signal(signal.SIGTERM, self.signal_handler)

    def signal_handler(self, sig, frame):
        self.switch = False  # 停止处理文件

    @staticmethod
    def parse_img_path(img_path):
        # 'page_{0}_img_{1}.{2}'.format(pno, img_index, ext)
        img_name, _ = os.path.splitext(os.path.basename(img_path))
        if re.match(r'page_\d+_img_\d+', img_name):
            part_list = img_name.split('_')
            return img_name, int(part_list[1])+1, int(part_list[3])+1
        else:
            return img_name, 1, 1

    @staticmethod
    def get_path(name, img_output_dir, wb_output_dir, pdf_output_dir):
        time_stamp = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
        new_name = '{0}_{1}'.format(time_stamp, name)
        img_save_path = os.path.join(img_output_dir, new_name)
        pdf_save_path = os.path.join(pdf_output_dir, new_name)
        excel_name = '{0}.xlsx'.format(os.path.splitext(new_name)[0])
        excel_path = os.path.join(wb_output_dir, excel_name)
        return img_save_path, excel_path, pdf_save_path

    @staticmethod
    def get_mode_code(code_list):
        result_dict = {}
        for code in code_list:
            if code in result_dict:
                result_dict[code] += 1
            else:
                result_dict[code] = 1
        if len(result_dict) == 1:
            return None
        else:
            return sorted(result_dict.items(), key=lambda x:x[1], reverse=True)[0][0]

    def res_process(self, all_res, excel_path):
        try:
            self.finder.ocr_results = all_res
            results = self.finder.get_info()

            wb = BSWorkbook(set(), set(), set(), set(), set())
            ws = wb.create_sheet(self.sheet_name)
            row_idx = 0
            code_idx = 1
            mode_code = None
            for write_field, field_value in results.items():
                row_idx += 1
                if isinstance(field_value, list):
                    if write_field == '合同编号列表':
                        code_idx = row_idx
                        mode_code = self.get_mode_code(field_value)
                    ws.append((write_field, *field_value))
                else:
                    ws.append((write_field, field_value))

            if isinstance(mode_code, str):
                for cell in ws[code_idx]:
                    if cell.value == '合同编号列表':
                        continue
                    if cell.value != mode_code:
                        cell.fill = self.amount_fill

            wb.remove_base_sheet()
            wb.save(excel_path)
        except Exception as e:
            self.folder_log.error('{0} [wb build error] [path={1}] [error={2}]'.format(
                self.log_base, excel_path, traceback.format_exc()))

    def ocr_process(self, img_path):
        if os.path.exists(img_path):
            # TODO 图片验证
            with open(img_path, 'rb') as f:
                base64_data = base64.b64encode(f.read())
                # 获取解码后的base64值
                file_data = base64_data.decode()
            json_data = {
                "file": file_data,
            }

            for times in range(consts.RETRY_TIMES):
                try:
                    ocr_response = requests.post(self.go_ocr_url, json=json_data)
                    if ocr_response.status_code != 200:
                        raise OCR1Exception('{0} go status code: {1}'.format(self.log_base, ocr_response.status_code))
                except Exception as e:
                    self.folder_log.warn('{0} [go failed] [times={1}] [img_path={2}] [error={3}]'.format(
                        self.log_base, times, img_path, traceback.format_exc()))
                else:
                    ocr_res = ocr_response.json()
                    self.folder_log.info('{0} [ocr success] [img={1}]'.format(
                        self.log_base, img_path))
                    return ocr_res
            else:
                self.folder_log.warn('{0} [go failed] [img_path={1}]'.format(self.log_base, img_path))

    def get_pno(self, img_path):
        img_name, _ = os.path.splitext(os.path.basename(img_path))
        return int(img_name.split('_')[1])
                
    def images_process(self, img_path_list, excel_path):
        all_res = {}
        for img_path in img_path_list:
            ocr_res = self.ocr_process(img_path)
            pno = self.get_pno(img_path)
            all_res[pno] = ocr_res
        self.res_process(all_res, excel_path)

    def pdf_process(self, name, path, img_output_dir, wb_output_dir, pdf_output_dir):
        if os.path.exists(path):
            try:
                img_save_path, excel_path, pdf_save_path = self.get_path(name, img_output_dir, wb_output_dir, pdf_output_dir)
                self.folder_log.info('{0} [pdf to img start] [path={1}]'.format(self.log_base, path))
                pdf_handler = PDFHandler(path, img_save_path)
                pdf_handler.extract_image()
                self.folder_log.info('{0} [pdf to img end] [path={1}]'.format(self.log_base, path))
            except Exception as e:
                self.folder_log.error('{0} [pdf to img error] [path={1}] [error={2}]'.format(
                    self.log_base, path, traceback.format_exc()))
                raise e
            else:
                self.images_process(pdf_handler.img_path_list, excel_path)
                shutil.move(path, pdf_save_path)

    def tif_process(self, name, path, img_output_dir, wb_output_dir, tiff_output_dir):
        if os.path.exists(path):
            try:
                img_save_path, excel_path, tiff_save_path = self.get_path(name, img_output_dir, wb_output_dir, tiff_output_dir)
                self.folder_log.info('{0} [tiff to img start] [path={1}]'.format(self.log_base, path))
                tiff_handler = TIFFHandler(path, img_save_path)
                tiff_handler.extract_image()
                self.folder_log.info('{0} [tiff to img end] [path={1}]'.format(self.log_base, path))
            except Exception as e:
                self.folder_log.error('{0} [tiff to img error] [path={1}] [error={2}]'.format(
                    self.log_base, path, traceback.format_exc()))
                raise e
            else:
                self.images_process(tiff_handler.img_path_list, excel_path)
                shutil.move(path, tiff_save_path)

    def img_process(self, name, path, classify, wb_output_dir, img_output_dir, pdf_output_dir):
        try:
            img_save_path, excel_path, _ = self.get_path(name, img_output_dir, wb_output_dir, pdf_output_dir)
        except Exception as e:
            self.folder_log.error('{0} [get path error] [path={1}] [error={2}]'.format(
                self.log_base, path, traceback.format_exc()))
        else:
            ocr_res = self.ocr_process(path, classify)
            all_res = {path: ocr_res}
            self.res_process(all_res, classify, excel_path)
            shutil.move(path, img_save_path)

    def folder_process(self, input_dir):
        while not os.path.isdir(input_dir):
            self.folder_log.info('{0} [input dir is not dir] [input_dir={1}]'.format(self.log_base, input_dir))
            if self.switch:
                time.sleep(self.sleep_time)
                continue
            else:
                return
        output_dir = os.path.join(os.path.dirname(input_dir), 'Output')
        img_output_dir = os.path.join(output_dir, 'image')
        wb_output_dir = os.path.join(output_dir, 'excel')
        pdf_output_dir = os.path.join(output_dir, 'pdf')
        tiff_output_dir = os.path.join(output_dir, 'tiff')
        failed_output_dir = os.path.join(output_dir, 'failed')
        os.makedirs(output_dir, exist_ok=True)
        os.makedirs(img_output_dir, exist_ok=True)
        os.makedirs(wb_output_dir, exist_ok=True)
        os.makedirs(pdf_output_dir, exist_ok=True)
        os.makedirs(tiff_output_dir, exist_ok=True)
        os.makedirs(failed_output_dir, exist_ok=True)
        os_error_filename_set = set()
        while self.switch:
            # if not os.path.isdir(input_dir):
            #     self.folder_log.info('{0} [input dir is not dir] [input_dir={1}]'.format(self.log_base, input_dir))
            #     time.sleep(self.sleep_time)
            #     continue
            # 1. 从input dir获取pdf or image
            list_dir = os.listdir(input_dir)
            if not list_dir and len(os_error_filename_set) == 0:
                self.folder_log.info('{0} [input dir empty] [input_dir={1}]'.format(self.log_base, input_dir))
                time.sleep(self.sleep_time)
                continue
            all_file_set = set(list_dir)
            true_file_set = all_file_set - os_error_filename_set
            if len(true_file_set) == 0 and len(os_error_filename_set) > 0:
                true_file_set.add(os_error_filename_set.pop())
            for name in true_file_set:
                time.sleep(10)  # 防止文件较大时,读取到不完整文件
                path = os.path.join(input_dir, name)
                is_success = True
                failure_reason = OfflineFailureReason.OS_ERROR.value
                start_time = time.time()

                try:
                    if not os.path.exists(path):
                        self.folder_log.info('{0} [path is not exists] [path={1}]'.format(self.log_base, path))
                        continue
                    elif os.path.isfile(path):
                        self.folder_log.info('{0} [file start] [path={1}]'.format(self.log_base, path))
                        if name.endswith('.pdf') or name.endswith('.PDF'):
                            self.pdf_process(name, path, img_output_dir, wb_output_dir, pdf_output_dir)
                        elif name.endswith('.tif') or name.endswith('.TIF') or name.endswith('.tiff') or \
                                name.endswith('.TIFF'):
                            self.tif_process(name, path, img_output_dir, wb_output_dir, tiff_output_dir)
                        else:
                            self.folder_log.info('{0} [path is not pdf or tif] [path={1}]'.format(
                                self.log_base, input_dir))
                            failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name))
                            shutil.move(path, failed_path)
                        self.folder_log.info('{0} [file end] [path={1}]'.format(self.log_base, path))
                    else:
                        self.folder_log.info('{0} [path is dir] [path={1}]'.format(self.log_base, path))
                        failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name))
                        shutil.move(path, failed_path)
                except OSError:
                    is_success = False
                    failure_reason = OfflineFailureReason.OS_ERROR.value
                    os_error_filename_set.add(name)
                    self.folder_log.error('{0} [os error] [path={1}] [error={2}]'.format(
                        self.log_base, path, traceback.format_exc()))
                except Exception as e:
                    is_success = False
                    failure_reason = OfflineFailureReason.PROCESS_ERROR.value
                    try:
                        self.folder_log.error('{0} [file error] [path={1}] [error={2}]'.format(self.log_base, path,
                                                                                               traceback.format_exc()))
                        failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name))
                        shutil.move(path, failed_path)
                    except Exception as e:
                        failure_reason = OfflineFailureReason.OS_ERROR.value
                        os_error_filename_set.add(name)
                        self.folder_log.error('{0} [file move error] [path={1}] [error={2}]'.format(
                            self.log_base, path, traceback.format_exc()))
                finally:
                    end_time = time.time()
                    try:
                        OfflineReport.objects.create(
                            input_folder=input_dir,
                            doc_type=consts.FOLDER_WSC_CLASSIFY,
                            file_name=name,
                            status=is_success,
                            failure_reason=failure_reason,
                            duration=int(end_time - start_time)
                        )
                    except Exception as e:
                        self.folder_log.error('{0} [db save failed] [path={1}] [error={2}]'.format(
                            self.log_base, path, traceback.format_exc()))

    def handle(self, *args, **kwargs):
        db.close_old_connections()
        self.folder_process(self.input_dir)
        self.folder_log.info('{0} [stop safely]'.format(self.log_base))