ocr_utils_old.py 68.7 KB
Newer Older
Wan Xia's avatar
Wan Xia committed
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 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
import glob
import json
import os
import shutil
import subprocess
import sys
import time
from collections import defaultdict
from datetime import datetime
from ftplib import FTP
from queue import Queue
from threading import Thread
import cv2
import numpy as np
import requests
import pickle
from PIL import Image, ImageDraw, ImageFont


# from tqdm import tqdm
# =================== sage utils======================================
def make_dir(target_path: str):
    if target_path:
        if os.path.exists(target_path):
            return
        else:
            parent_path, _ = os.path.split(target_path)
            if parent_path == '/':
                os.mkdir(target_path)
                return
            elif not os.path.exists(parent_path):
                make_dir(parent_path)
            os.mkdir(target_path)


def get_stime():
    return str(datetime.now()).split('.')[0].replace('-', '').replace(' ', '').replace(':', '')


def get_timestamp():
    return str(time.time() * 1000).split('.')[0]


def my_print(print_str, str_type='INFO'):
    print(f'{str(datetime.now())} || [{str_type}] : {print_str}\n')


class Logger(object):
    def __init__(self, filepath, stream=sys.stdout):
        self.terminal = stream
        self.log = open(filepath, 'a')
        self.log_dir_path, self.filename = os.path.split(filepath)
        self.file_date = self.filename[:14]
        self.filepath = filepath

    def write(self, message):
        self.terminal.write(message)
        self.terminal.flush()
        self.check_log()
        self.log.write(message)
        self.log.flush()

    def check_log(self):
        if (datetime.now() - datetime.strptime(self.file_date, '%Y%m%d%H%M%S')).days > 1:
            self.log.close()
            self.filename = get_stime()
            self.filepath = f'{os.path.join(self.log_dir_path, self.filename)}.log'
            self.file_date = self.filename[:14]
            self.log = open(self.filepath, 'a')
            check_log(self.log_dir_path)

    def flush(self):
        pass


def set_log(log_path, log_time, check_flag=False):
    if check_flag:
        check_log(log_path)
    sys.stdout = Logger(f'{os.path.join(log_path, log_time)}.log', sys.stdout)
    sys.stderr = Logger(f'{os.path.join(log_path, log_time)}.log', sys.stderr)


def check_log(log_path):
    if not os.path.exists(log_path):
        os.mkdir(log_path)
    else:
        logs = os.listdir(log_path)
        if len(logs) > 100:
            logs.sort(key=lambda x: float(x.split('.')[0]))
            for del_log in logs[:-50]:
                os.remove(os.path.join(log_path, del_log))


def cap_usb_cam(cam_index, buffsize, cam_exp):  # 使用生成器的方式控制相机拍照
    my_print(f'cam {cam_index} start')
    flag = False
    retry_cnt = 0
    while not flag:
        if retry_cnt > 10:
            my_print(f'open_flag={flag}, try to open cam {cam_index} exceed {retry_cnt} times', 'ERROR')
            break
        cap = cv2.VideoCapture(cam_index, cv2.CAP_V4L2)
        cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
        cap.set(cv2.CAP_PROP_BUFFERSIZE, buffsize)
        cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1)
        cap.set(cv2.CAP_PROP_EXPOSURE, cam_exp)
        my_print(f'EXPOSURE:{cap.get(cv2.CAP_PROP_EXPOSURE)}')
        flag = cap.isOpened()
        my_print(f'open_flag:{flag}')
        if not flag:
            try:
                cap.release()
            except Exception as e:
                pass
        time.sleep(1)
        retry_cnt += 1
    try:
        while True:
            if flag:
                buffsize_cnt = buffsize + 1
                while buffsize_cnt > 0:
                    try:
                        ret, f = cap.read()
                    except Exception as e:
                        my_print(f'cam {cam_index} broken, {e}', 'ERROR')
                        f = None
                        flag = False
                        break
                    buffsize_cnt -= 1
                my_print(f'cap read')
            else:
                f = None
            if not isinstance(f, np.ndarray):
                f = None
            yield f
    finally:
        cap.release()
        cv2.destroyAllWindows()


def cap_web_cam(rtsp, buffsize):  # 使用生成器的方式控制相机拍照
    my_print(f'{rtsp} start!', 'WEB_CAM_INFO')
    cap = cv2.VideoCapture(rtsp)
    # cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
    # cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
    cap.set(cv2.CAP_PROP_BUFFERSIZE, buffsize)
    # cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1)
    # cap.set(cv2.CAP_PROP_EXPOSURE, 75)
    # print('EXPOSURE: ', cap.get(cv2.CAP_PROP_EXPOSURE))
    flag = cap.isOpened()
    my_print(f'flag={flag}', 'WEB_CAM_INFO')
    my_print(f'cap opened!', 'WEB_CAM_INFO')
    try:
        while True:
            buffsize_cnt = buffsize + 1
            while buffsize_cnt > 0:
                ret, f = cap.read()
                buffsize_cnt -= 1
            my_print(f'cap read!', 'WEB_CAM_INFO')
            yield f
    finally:
        cap.release()
        cv2.destroyAllWindows()


def stitch_imgs(images):  # 拼接np格式图像的list
    # print('images:',images)
    diff_min_xy = None
    diff_max_xy = None
    min_xy = None
    max_xy = None
    my_print(f'stitching images...')
    stitcher = cv2.Stitcher_create()
    (status, stitched) = stitcher.stitch(images)
    stitched_img = None
    if status == 0:
        my_print(f'cropping...')
        stitched = cv2.copyMakeBorder(stitched, 20, 20, 20, 20,
                                      cv2.BORDER_CONSTANT, (0, 0, 0))
        # cv2.imwrite('stitched_o.png', stitched)
        # cus_show(stitched)
        gray = cv2.cvtColor(stitched, cv2.COLOR_BGR2GRAY)
        ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)  # 二值化拼接后的图
        # cus_show(thresh)
        # cv2.imwrite('thresh.png', thresh)
        cnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  # 寻找拼接图的外轮廓
        cnt = max(cnts, key=cv2.contourArea)
        min_v = float('inf')
        max_v = -float('inf')
        diff_min = float('inf')
        diff_max = -float('inf')
        for i in range(cnt.shape[0]):  # 寻找拼接图外轮廓的内接矩形的长宽起始坐标
            if cnt[i][0][0] + cnt[i][0][1] < min_v:
                min_v = cnt[i][0][0] + cnt[i][0][1]
                min_xy = cnt[i][0]
            if cnt[i][0][0] + cnt[i][0][1] > max_v:
                max_v = cnt[i][0][0] + cnt[i][0][1]
                max_xy = cnt[i][0]
            if cnt[i][0][0] - cnt[i][0][1] < diff_min:
                diff_min = cnt[i][0][0] - cnt[i][0][1]
                diff_min_xy = cnt[i][0]
            if cnt[i][0][0] - cnt[i][0][1] > diff_max:
                diff_max = cnt[i][0][0] - cnt[i][0][1]
                diff_max_xy = cnt[i][0]
        if min_xy is not None and max_xy is not None and diff_min_xy is not None and diff_max_xy is not None:
            min_xy = min_xy.tolist()
            max_xy = max_xy.tolist()
            diff_min_xy = diff_min_xy.tolist()
            diff_max_xy = diff_max_xy.tolist()
            x_start = np.array([diff_max_xy[-1], min_xy[-1]]).max()
            x_end = np.array([diff_min_xy[-1], max_xy[-1]]).min()
            y_start = np.array([diff_min_xy[0], min_xy[0]]).max()
            y_end = np.array([diff_max_xy[0], max_xy[0]]).min()
            stitched_img = stitched[x_start:x_end, y_start:y_end]
        # cv2.imwrite('./stitched.png', stitched_img)
        # cus_show(stitched)
    else:
        my_print(f'image stitching failed, stitch status:{status}', 'ERROR')
    return stitched_img


def check_book(book_indexs, shelf_id, book_service_ip, book_service_port, enable_barcode=False):
    from rapidfuzz import process, fuzz
    res = []
    try:
        my_print(f'checking books of {shelf_id=}', 'CHECK_BOOK_INFO')
        url = f'http://{book_service_ip}:{book_service_port}/book/shelfID?shelfID={shelf_id}'
        r = requests.get(url, timeout=2.)
        books = r.json()
    except Exception as e:
        my_print(f'request book error, {e}', 'CHECK_BOOK_ERROR')
        return res
    shelf_indexs = []
    shelf_title_dict = {}
    shelf_cn_dict = {}
    shelf_cn_title_dict = {}
    # 生成索书号集合,以及索书号与标题对应的字典
    for book in books:
        book_id = book['id']
        if book_id not in shelf_indexs:
            shelf_indexs.append(book_id)
            shelf_title_dict[book_id] = book['title']
            shelf_cn_dict[book_id] = book['callNumber']
            shelf_cn_title_dict[book['callNumber']] = book['title']
    book_callnumbers = list(shelf_cn_title_dict.keys())
    my_print(f'book_id to title ==> {shelf_title_dict}', 'CHECK_BOOK_INFO')
    my_print(f'book_id to callnumber ==> {shelf_cn_dict}', 'CHECK_BOOK_INFO')
    my_print(f'callnumber to title ==> {shelf_cn_title_dict}', 'CHECK_BOOK_INFO')
    # 模糊匹配
    for pos, box_w, book_index in book_indexs:
        book_barcode = ''
        if not enable_barcode:
            if shelf_indexs and book_index:  # match callnumber and detected book_index
                matched_index, matched_score, _ = process.extractOne(book_index, book_callnumbers, scorer=fuzz.WRatio)
                res_title = shelf_cn_title_dict[matched_index]
            else:
                my_print(f'book_id list is empty or detected callnumber is empty', 'CHECK_BOOK_INFO')
                matched_index, matched_score = book_index, 0.0
                res_title = 'unknown'
        else:
            if book_index in shelf_indexs:
                book_barcode = book_index
                matched_index = shelf_cn_dict[book_index]  # callnumber
                res_title = shelf_title_dict[book_index]
                matched_score = 99
            else:
                my_print(f'book_id list is empty or detected barcode not in book_id list', 'CHECK_BOOK_INFO')
                matched_index = book_index
                res_title = 'unknown'
                matched_score = 0.0
        # for callnumber, book_index=callnumber, book_barcode='', matched_index=matched_callnumber
        # for barcode, book_index=barcode, book_barcode=matched_barcode, matched_index=matched_callnumber or barcode
        res.append([pos, res_title, box_w, [book_index, book_barcode, matched_index, matched_score]])
    return res


# def cus_sort(res_list):  # 自定义排序,按检测框横向起始坐标排序
#     def cus_cmp(m1, m2):
#         m1_y = m1['bbox'][0]
#         m2_y = m2['bbox'][0]
#         return m1_y > m2_y
#
#     res_len = len(res_list)
#     if res_len > 1:
#         for i in range(res_len - 1, 0, -1):
#             for j in range(i):
#                 if cus_cmp(res_list[j], res_list[i]):
#                     temp = res_list[j]
#                     res_list[j] = res_list[i]
#                     res_list[i] = temp
#     return res_list


def cus_sort_t(res_list):  # 自定义排序,按检测框的位置由左到右,由上到下排序
    def cus_cmp(m1, m2):
        x_x = (m1[0][0][-1] + m1[0][2][-1]) / 2
        x_y = (m1[0][0][0] + m1[0][2][0]) / 2
        y_x = (m2[0][0][-1] + m2[0][2][-1]) / 2
        y_y = (m2[0][0][0] + m2[0][2][0]) / 2
        x_h = m1[0][3][-1] - m1[0][0][-1]
        # x_w = m1[0][1][0] - m1[0][0][0]
        y_h = m2[0][3][-1] - m2[0][0][-1]
        # y_w = m2[0][1][0] - m2[0][0][0]
        if abs(x_x - y_x) < max(x_h, y_h) * 0.5:
            return x_y > y_y
        else:
            return x_x > y_x

    res_len = len(res_list)
    if res_len > 1:
        for i in range(res_len - 1, 0, -1):
            for j in range(i):
                if cus_cmp(res_list[j], res_list[i]):
                    temp = res_list[j]
                    res_list[j] = res_list[i]
                    res_list[i] = temp
    return res_list


# def cum_level_path_sort(path_a, path_b):  # 自定义排序,按照任务时间对路径排序
#     shelf_path_a, level_a = os.path.split(path_a)
#     shelf_path_b, level_b = os.path.split(path_b)
#     task_path_a, shelf_a = os.path.split(shelf_path_a)
#     task_path_b, shelf_b = os.path.split(shelf_path_b)
#     _path_a, task_a = os.path.split(task_path_a)
#     _path_b, task_b = os.path.split(task_path_b)
#     a_time = float(task_a)
#     b_time = float(task_b)
#     return a_time - b_time


def concate_sub_res(total_det_res, sub_img, level_cls_model, book_index_model):  # 判断子图片是否包含图书,若包含则将预测结果添加到总结果里
    import paddlex as pdx
    for img_k in sorted(sub_img.keys()):
        cur_img = sub_img[img_k]
        level_cls_res = level_cls_model.predict(cur_img)
        if level_cls_res[0]['category'] == '0':
            my_print(f'blank sub_img')
            continue
        tmp_book_det_res = book_index_model.predict(cur_img)
        pdx.det.visualize(cur_img.copy(), tmp_book_det_res, threshold=0.2, save_dir='./output')
        index_det_res = []
        for tbdr in tmp_book_det_res:
            if tbdr['score'] > 0.2:
                tbdr['bbox'][0] += img_k
                index_det_res.append(tbdr['bbox'] + [tbdr['score']])
        total_det_res += index_det_res
    return total_det_res


def custom_cut(img, img_h, img_w, i, img_num, top_margin, left_margin, right_margin):
    if i == 0:
        img = img[int(img_h * top_margin):, int(img_w * left_margin[0]):]
    elif i == 1:
        if len(left_margin) > 1:
            img = img[int(img_h * top_margin):, int(img_w * left_margin[1]):]
    elif i == img_num - 2:
        if len(right_margin) > 1:
            img = img[int(img_h * top_margin):, :int(img_w * (1 - right_margin[1]))]
    elif i == img_num - 1:
        img = img[int(img_h * top_margin):, :int(img_w * (1 - right_margin[0]))]
    else:
        img = img[int(img_h * top_margin):, ]
    return img


def get_cn_det_res(book_index_det_model, img, det_score, ocr_path, task_id, shelf_id, level_id, i):
    import paddlex as pdx
    cn_det_res = book_index_det_model.predict(img)
    save_path = f'{ocr_path}/{task_id}/{shelf_id}/{level_id}/{i}'
    make_dir(save_path)
    visual_img = pdx.det.visualize(img.copy(), cn_det_res, threshold=det_score, save_dir=None)  # 部署时注释此行
    cv2.imwrite(f'{save_path}/visual_img.png', visual_img)  # 部署时注释此行
    temp_det_res = []
    my_print(f'start filter det_res', 'TH_OCR_INFO')
    for cn_res in cn_det_res:
        if cn_res['score'] > det_score:  # 置信度过滤
            temp_det_res.append(cn_res)
    cn_det_res = temp_det_res
    my_print(f'len_cn_det_res(score>{det_score}:{len(cn_det_res)}', 'TH_OCR_INFO')
    if len(cn_det_res) > 1:
        cn_det_res = cus_nms(cn_det_res)  # 过滤重合框
    my_print(f'len_cn_det_res(after nms):{len(cn_det_res)}', 'TH_OCR_INFO')
    visual_img = pdx.det.visualize(img.copy(), cn_det_res, threshold=det_score, save_dir=None)  # 部署时注释此行
    cv2.imwrite(f'{save_path}/visual_img_nms.png', visual_img)  # 部署时注释此行
    return cn_det_res


def get_barcode_det_res(img, ocr_path, task_id, shelf_id, level_id, i, str_len: list):
    from pyzbar import pyzbar
    cn_det_res = []
    barcode_list = []
    save_path = f'{ocr_path}/{task_id}/{shelf_id}/{level_id}/{i}'
    make_dir(save_path)
    img_res = img.copy()
    img_h, img_w = img.shape[:2]
    img_scale_x2 = cv2.resize(img.copy(), (img_w * 2, img_h * 2), interpolation=cv2.INTER_CUBIC)
    # my_print(f'img_scale h={img_scale.shape[0]}, w={img_scale.shape[1]}','DEBUG')
    img_scale_x2_v = cv2.rotate(img_scale_x2.copy(), cv2.ROTATE_90_COUNTERCLOCKWISE)
    result_h = pyzbar.decode(img_scale_x2)
    result_v = pyzbar.decode(img_scale_x2_v)
    for result in result_h:
        # my_print(f'{result.data}', 'DEBUG')
        # my_print(f'{len(result.data)} in {str_len}', 'DEBUG')
        if len(result.data) not in str_len:
            continue
        if result.data in barcode_list:
            continue
        barcode_list.append(result.data)
        left = result.rect.left
        top = result.rect.top
        width = result.rect.width
        height = result.rect.height
        if width < 40 or height < 40:
            # left -= 40
            width = 80
            # top -= 150
            height = 400
        else:
            width = width // 2
            height = height // 2
        left = left // 2
        top = top // 2
        cn_det_res.append({
            'category_id': str(result.data)[2:-1],
            'category': 'barcode',
            'bbox': [left, top, width, height],
            'score': 0.9999
        })
        cv2.circle(img_res, (left, top), 10, (0, 255, 0), 2)
    for result in result_v:
        if len(result.data) not in str_len:
            continue
        if result.data in barcode_list:
            continue
        barcode_list.append(result.data)
        top = result.rect.left
        left = img_w * 2 - result.rect.top
        width = result.rect.height
        height = result.rect.width
        if width < 40 or height < 40:
            # left -= 40
            width = 80
            # top -= 150
            height = 400
        else:
            width = width // 2
            height = height // 2
        left = left // 2
        top = top // 2
        cn_det_res.append({
            'category_id': str(result.data)[2:-1],
            'category': 'barcode',
            'bbox': [left, top, width, height],
            'score': 0.9999
        })
        cv2.circle(img_res, (left, top), 10, (0, 255, 0), 2)

        # cv2.rectangle(img_res, (result.rect.left, result.rect.top),
        #               (result.rect.left + result.rect.width, result.rect.top + result.rect.height), (0, 255, 0), 2)
    img_scale_x1 = cv2.resize(img.copy(), (img_w * 1, img_h * 1), interpolation=cv2.INTER_CUBIC)
    # my_print(f'img_scale h={img_scale.shape[0]}, w={img_scale.shape[1]}','DEBUG')
    img_scale_x1_v = cv2.rotate(img_scale_x1.copy(), cv2.ROTATE_90_COUNTERCLOCKWISE)
    result_h = pyzbar.decode(img_scale_x1)
    result_v = pyzbar.decode(img_scale_x1_v)
    for result in result_h:
        # my_print(f'{result.data}', 'DEBUG')
        # my_print(f'{len(result.data)} in {str_len}', 'DEBUG')
        if len(result.data) not in str_len:
            continue
        if result.data in barcode_list:
            continue
        barcode_list.append(result.data)
        left = result.rect.left
        top = result.rect.top
        width = result.rect.width
        height = result.rect.height
        if width < 40 or height < 40:
            # left -= 40
            width = 80
            # top -= 150
            height = 400
        # else:
        #     width = width // 2
        #     height = height // 2
        # left = left // 2
        # top = top // 2
        cn_det_res.append({
            'category_id': str(result.data)[2:-1],
            'category': 'barcode',
            'bbox': [left, top, width, height],
            'score': 0.9999
        })
        cv2.circle(img_res, (left, top), 10, (0, 255, 0), 2)
    for result in result_v:
        if len(result.data) not in str_len:
            continue
        if result.data in barcode_list:
            continue
        barcode_list.append(result.data)
        top = result.rect.left
        left = img_w * 2 - result.rect.top
        width = result.rect.height
        height = result.rect.width
        if width < 40 or height < 40:
            # left -= 40
            width = 80
            # top -= 150
            height = 400
        # else:
        #     width = width // 2
        #     height = height // 2
        # left = left // 2
        # top = top // 2
        cn_det_res.append({
            'category_id': str(result.data)[2:-1],
            'category': 'barcode',
            'bbox': [left, top, width, height],
            'score': 0.9999
        })
        cv2.circle(img_res, (left, top), 10, (0, 255, 0), 2)

        # cv2.rectangle(img_res, (result.rect.left, result.rect.top),
        #               (result.rect.left + result.rect.width, result.rect.top + result.rect.height), (0, 255, 0), 2)
    img_scale_x05 = cv2.resize(img.copy(), (img_w // 2, img_h // 2), interpolation=cv2.INTER_CUBIC)
    # my_print(f'img_scale h={img_scale.shape[0]}, w={img_scale.shape[1]}','DEBUG')
    img_scale_x05_v = cv2.rotate(img_scale_x2.copy(), cv2.ROTATE_90_COUNTERCLOCKWISE)
    result_h = pyzbar.decode(img_scale_x05)
    result_v = pyzbar.decode(img_scale_x05_v)
    for result in result_h:
        # my_print(f'{result.data}', 'DEBUG')
        # my_print(f'{len(result.data)} in {str_len}', 'DEBUG')
        if len(result.data) not in str_len:
            continue
        if result.data in barcode_list:
            continue
        barcode_list.append(result.data)
        left = result.rect.left
        top = result.rect.top
        width = result.rect.width
        height = result.rect.height
        if width < 40 or height < 40:
            # left -= 40
            width = 80
            # top -= 150
            height = 400
        else:
            width = width * 2
            height = height * 2
        left = left * 2
        top = top * 2
        cn_det_res.append({
            'category_id': str(result.data)[2:-1],
            'category': 'barcode',
            'bbox': [left, top, width, height],
            'score': 0.9999
        })
        cv2.circle(img_res, (left, top), 10, (0, 255, 0), 2)
    for result in result_v:
        if len(result.data) not in str_len:
            continue
        if result.data in barcode_list:
            continue
        barcode_list.append(result.data)
        top = result.rect.left
        left = img_w * 2 - result.rect.top
        width = result.rect.height
        height = result.rect.width
        if width < 40 or height < 40:
            # left -= 40
            width = 80
            # top -= 150
            height = 400
        else:
            width = width * 2
            height = height * 2
        left = left * 2
        top = top * 2
        cn_det_res.append({
            'category_id': str(result.data)[2:-1],
            'category': 'barcode',
            'bbox': [left, top, width, height],
            'score': 0.9999
        })
        cv2.circle(img_res, (left, top), 10, (0, 255, 0), 2)

        # cv2.rectangle(img_res, (result.rect.left, result.rect.top),
        #               (result.rect.left + result.rect.width, result.rect.top + result.rect.height), (0, 255, 0), 2)
    # if len(cn_det_res) > 1:
    # cn_det_res = cus_nms(cn_det_res)  # 过滤重合框
    cv2.imwrite(f'{save_path}/visual_img.png', img_res)
    return cn_det_res


def cus_nms(total_det_res):  # 自定义NMS算法,目的是为了去除重合的检测框,若对NMS算法不了解,请先了解
    len_det_res = len(total_det_res)
    del_res_index = []
    for i in range(len_det_res - 1):
        if i in del_res_index:
            continue
        for j in range(i + 1, len_det_res):
            b1_y_s, b1_x_s, b1_w, b1_h = total_det_res[i]['bbox'][:4]
            b1_x_end = b1_x_s + b1_h
            b1_y_end = b1_y_s + b1_w
            b2_y_s, b2_x_s, b2_w, b2_h = total_det_res[j]['bbox'][:4]
            b2_x_end = b2_x_s + b2_h
            b2_y_end = b2_y_s + b2_w
            b1_area = b1_h * b1_w
            b2_area = b2_h * b2_w
            if (b1_y_s < b2_y_end and b2_y_s < b1_y_end) or (b2_y_s < b1_y_end and b1_y_s < b2_y_end):
                min_x = max(b1_x_s, b2_x_s)
                min_y = max(b1_y_s, b2_y_s)
                max_x = min(b1_x_end, b2_x_end)
                max_y = min(b1_y_end, b2_y_end)
                overlap_area = (max_y - min_y) * (max_x - min_x)
                oa_ratio = {
                    overlap_area / b1_area: i,
                    overlap_area / b2_area: j
                }
                if max(oa_ratio.keys()) > 0.5:
                    del_res_index.append(oa_ratio[max(oa_ratio.keys())])
    temp_total_det_res = []
    for i, res in enumerate(total_det_res):
        if i in del_res_index:
            continue
        temp_total_det_res.append(res)
    return temp_total_det_res


def draw_ocr(img, total_det_res, book_indexs, book_res, size):  # 可视化每张图片的OCR结果
    for det_res in total_det_res:  # 绘制检测框
        y, x, w, h = det_res['bbox'][:4]
        cv2.rectangle(img, (int(y), int(x)), (int(y + w), int(x + h)), (0, 255, 0), 2)
    img_h, img_w = img.shape[:2]
    img_ro = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
    img_pil = Image.fromarray(cv2.cvtColor(img_ro, cv2.COLOR_BGR2RGB))  # 转化成pillow支持的图像格式绘制文字,支持中文
    pli_draw = ImageDraw.Draw(img_pil)
    fontText = ImageFont.truetype('msyh.ttc', size, encoding='utf-8')  # 加载字体
    for i, det_res in enumerate(total_det_res):
        if str(book_indexs[i]):
            # print('det_res:',det_res)
            y, x, w, h = det_res['bbox'][:4]
            center_x = x + h // 2
            center_y = y + w // 2
            score_x = x + h // 2 + h // 4
            score_y = y + w // 2
            pos_x = x
            pos_y = y + w
            title_x = x
            title_y = y + w * 0.25
            ocr_x = x + h // 2
            ocr_y = y + w
            pli_draw.text((center_x, img_w - center_y), str(i + 1), (255, 0, 255), font=fontText)
            pli_draw.text((pos_x, img_w - pos_y), str(round(book_res[i][0], 2)), (0, 180, 140), font=fontText)
            pli_draw.text((ocr_x, img_w - ocr_y), str(book_res[i][-1][1]), (0, 255, 0), font=fontText)
            pli_draw.text((title_x, img_w - title_y), str(book_res[i][1]), (255, 0, 0), font=fontText)
            pli_draw.text((ocr_x, img_w - title_y), str(book_res[i][-1][0]), (140, 180, 0), font=fontText)
            pli_draw.text((score_x, img_w - score_y), str(round(book_res[i][-1][-1], 1)), (0, 0, 255), font=fontText)
    img_ro = cv2.cvtColor(np.asarray(img_pil), cv2.COLOR_RGB2BGR)
    img_re = cv2.rotate(img_ro, cv2.ROTATE_90_CLOCKWISE)
    return img_re


def ftp_upload_connect(ftp, img_path, target_dir, ip, port, usr, psw):  # 创建ftp handle,并进入到指定的服务器目录
    level_path, img_name = os.path.split(img_path)
    shelf_path, level_id = os.path.split(level_path)
    task_path, shelf_id = os.path.split(shelf_path)
    save_path, task_id = os.path.split(task_path)
    retry_cnt = 0
    connect_flag = True
    while True:
        try:
            ftp.connect(ip, port)
            my_print('Connect to FTP successfully!!!', 'TH_UP_INFO')
            break
        except Exception as e:
            my_print(f'Fail to connect FTP!!!, {e}', 'TH_UP_ERROR')
            pass
        time.sleep(1)
        retry_cnt += 1
        if retry_cnt > 3:
            connect_flag = False
            break
    if connect_flag:
        try:
            ftp.login(usr, psw)
            # print(str(datetime.now()) + '||[INFO]: ftp login dir', ftp.dir())
        except Exception as e:
            connect_flag = False
            my_print(f'ftp login Fail, {e}', 'TH_UP_ERROR')
            pass
        try:
            ftp.mkd(target_dir)
            # print(str(datetime.now()) + '||[INFO]: mk video dir', ftp.dir())
        except Exception as e:
            pass
        try:
            ftp.cwd('./' + target_dir)
            # print(str(datetime.now()) + '||[INFO]: cd video dir ', ftp.dir())
        except Exception as e:
            connect_flag = False
            my_print(f'cd images Fail, {e}', 'TH_UP_ERROR')
            pass
        try:
            ftp.mkd(task_id)
            # print(str(datetime.now()) + '||[INFO]: mk task dir', ftp.dir())
        except Exception as e:
            pass
        try:
            ftp.cwd('./' + task_id)
            # print(str(datetime.now()) + '||[INFO]: cd task dir', ftp.dir())
        except Exception as e:
            connect_flag = False
            my_print(f'cd task Fail, {e}', 'TH_UP_ERROR')
            pass
        try:
            ftp.mkd(shelf_id)
            # print(str(datetime.now()) + '||[INFO]: mk task dir', ftp.dir())
        except Exception as e:
            pass
        try:
            ftp.cwd('./' + shelf_id)
            # print(str(datetime.now()) + '||[INFO]: cd task dir', ftp.dir())
        except Exception as e:
            connect_flag = False
            my_print(f'cd shelf Fail, {e}', 'TH_UP_ERROR')
            pass
        try:
            ftp.mkd(level_id)
            # print(str(datetime.now()) + '||[INFO]: mk task dir', ftp.dir())
        except Exception as e:
            pass
        try:
            ftp.cwd('./' + level_id)
            # print(str(datetime.now()) + '||[INFO]: cd task dir', ftp.dir())
        except Exception as e:
            connect_flag = False
            my_print(f'cd level Fail, {e}', 'TH_UP_ERROR')
            pass
    return connect_flag, ftp


def get_file_status(remote_host, file_path):
    try:
        response = requests.post(f'http://{remote_host}:8773/file_status',
                                 headers={'content-type': 'application/json'},
                                 data=json.dumps({
                                     'file_path': file_path
                                 }),
                                 timeout=2.)
    except Exception as e:
        my_print(f'query file status error,{e}', 'ERROR')
        return None, None
    if response.status_code == 200:
        res_content = json.loads(response.content)
        file_flag, file_size = res_content['flag'], res_content['size']
        return file_flag, file_size
    my_print(f'query file status code={response.status_code}', 'ERROR')
    return None, None


def upload_img(ftp, img_path, ftp_params):  # 上传图片的子线程
    remote_host, port, username, psw, target_path, remote_path = ftp_params
    my_print(f'uploading ==> {img_path}', 'TH_UP_IMG_INFO')
    level_path, img_name = os.path.split(img_path)
    i_name, i_upload, i_suffix = img_name.split('.')
    new_v_file = '.'.join([i_name, '1', i_suffix])
    new_img_path = os.path.join(level_path, new_v_file)
    flag, size = get_file_status(remote_host, level_path)
    if flag is None:
        time.sleep(3)
        return
    # flag, size = get_file_status(remote_host, f'{level_path}.ocred')
    # if flag is None:
    #     time.sleep(3)
    #     return
    connect_flag, ftp = ftp_upload_connect(ftp, img_path, remote_path, remote_host, port, username, psw)
    if connect_flag:
        local_img_size = os.path.getsize(img_path)
        if local_img_size > 0:
            buf_size = 4096
            remote_filename = '.'.join([i_name, '0', '0', i_suffix])
            try:
                remote_img_size = ftp.size(remote_filename)  # 尝试获取远程文件大小
                if remote_img_size == 0:
                    try:
                        ftp.delete(remote_filename)
                    except Exception as e:
                        my_print(f'try to delete remote_filename={remote_filename} with 0 size, {e}', 'TH_UP_IMG_ERROR')
            except Exception as e:
                remote_img_size = 0
                pass
            remote_upload_filename = '.'.join([i_name, '0', '1', i_suffix])
            flag, size = get_file_status(remote_host, f'{level_path}/{remote_upload_filename}')
            if flag is not None and flag == 1:
                try:
                    os.rename(img_path, new_img_path)
                except Exception as e:
                    pass
                try:
                    ftp.quit()
                    time.sleep(0.1)
                except Exception as e:
                    pass
                return
            remote_ocring_filename = '.'.join([i_name, '2', '1', i_suffix])
            flag, size = get_file_status(remote_host, f'{level_path}/{remote_ocring_filename}')
            if flag is not None and flag == 1:
                try:
                    os.rename(img_path, new_img_path)
                except Exception as e:
                    pass
                try:
                    ftp.quit()
                    time.sleep(0.1)
                except Exception as e:
                    pass
                return
            remote_ocred_filename = '.'.join([i_name, '1', '1', i_suffix])
            flag, size = get_file_status(remote_host, f'{level_path}/{remote_ocred_filename}')
            if flag is not None and flag == 1:
                try:
                    os.rename(img_path, new_img_path)
                except Exception as e:
                    pass
                try:
                    ftp.quit()
                    time.sleep(0.1)
                except Exception as e:
                    pass
                return
            my_print(f'local_size={local_img_size} remote_size={remote_img_size}', 'TH_UP_IMG_INFO')
            if local_img_size != remote_img_size:  # 服务器和本机同一文件大小不等,则视情况采取完整上传或续传
                fp = open(img_path, 'rb')
                if remote_img_size == 0:
                    try:
                        ftp.storbinary("STOR {}".format(remote_filename), fp, buf_size)  # 完整上传
                    except Exception as e:
                        my_print(f'ftp storbinary Fail', 'TH_UP_IMG_ERROR')
                        pass
                    fp.close()
                elif remote_img_size > local_img_size:
                    try:
                        ftp.delete(remote_filename)
                        my_print(f'remote file size > local size, has been deleted', 'TH_UP_IMG')
                    except Exception as e:
                        my_print(f'remote file size which > local size cannot be deleted, {e}', 'TH_UP_IMG_ERROR')
                        fp.close()
                    try:
                        ftp.quit()
                        time.sleep(0.1)
                    except Exception as e:
                        pass
                    return
                elif remote_img_size < local_img_size:
                    fp.seek(remote_img_size)
                    datasock = ''
                    esize = ''
                    try:
                        ftp.voidcmd('TYPE I')
                        datasock, esize = ftp.ntransfercmd("STOR {}".format(remote_filename), remote_img_size)  # 续传
                    except Exception as e:
                        my_print(f'ftp ntransfercmd Fail', 'TH_UP_IMG_ERROR')
                        try:
                            ftp.quit()
                            time.sleep(0.1)
                        except Exception as e:
                            my_print(f'ntransfercmd exception ftp quit Fail', 'TH_UP_IMG_ERROR')
                            pass
                        fp.close()
                        return
                    cmpsize = remote_img_size
                    while True:  # 续传
                        buf = fp.read(4096 * 1024)
                        if not len(buf):
                            my_print(f'no data [break]', 'TH_UP_IMG_INFO')
                            break
                        try:
                            datasock.sendall(buf)
                        except Exception as e:
                            my_print(f'data send Fail', 'TH_UP_IMG_ERROR')
                            break
                        cmpsize += len(buf)
                        if cmpsize == local_img_size:
                            my_print(f'file size equal [break]', 'TH_UP_IMG_INFO')
                            break
                    try:
                        datasock.close()
                    except Exception as e:
                        my_print(f'datasock close Fail', 'TH_UP_IMG_ERROR')
                        pass
                    try:
                        ftp.voidcmd('NOOP')
                    except Exception as e:
                        my_print(f'ftp NOOP Fail', 'TH_UP_IMG_ERROR')
                        pass
                    try:
                        ftp.voidresp()
                    except Exception as e:
                        my_print(f'ftp voidresp Fail', 'TH_UP_IMG_ERROR')
                        pass
                    fp.close()
            else:  # 两端文件大小一致,则修改文件名里的用于表示上传状态的占位符
                new_remote_filename = '.'.join([i_name, '0', '1', i_suffix])
                try:
                    flag, size = get_file_status(remote_host, f'{level_path}/{new_remote_filename}')
                    if flag is not None:
                        if flag == 0:
                            ftp.rename(remote_filename, new_remote_filename)
                            my_print(f'ftp rename {remote_filename} ==> {new_remote_filename} done', 'TH_UP_IMG_INFO')
                        else:
                            ftp.delete(remote_filename)
                except Exception as e:
                    my_print(f'ftp rename {remote_filename} ==> {new_remote_filename} Fail, {e}', 'TH_UP_IMG_ERROR')
                os.rename(img_path, new_img_path)
                my_print(f'rename {img_path} ==> {new_img_path} done', 'TH_UP_IMG_INFO')
            my_print(f'{img_path} uploaded', 'TH_UP_IMG_INFO')
        else:
            try:
                os.remove(img_path)
                my_print(f'{img_path} size is 0, has been deleted', 'TH_UP_IMG_INFO')
            except Exception as e:
                my_print(f'{img_path} size is 0, delete fail, {e}', 'TH_UP_IMG_ERROR')
                pass
    try:
        ftp.quit()
        time.sleep(0.1)
    except Exception as e:
        my_print(f'ftp quit Fail', 'TH_UP_IMG_ERROR')
        pass


def upload_image(remote_host, port, username, psw, target_path, remote_path, upload_max):  # 上传图片的总线程
    try:
        my_print(f'main_up is starting', 'TH_UP_INFO')
        ftp_params = [remote_host, port, username, psw, target_path, remote_path]
        upload_queue = Queue(maxsize=upload_max)
        while True:
            img_path_list = glob.glob(
                f'{target_path}/*/*/*/*.0.png')  # 未上传的图片集合 target_path/task_id/shelf_id/level_id/*.0.png
            if img_path_list:
                my_print(f'Found {len(img_path_list)} caped images ==> {img_path_list}', 'TH_UP_INFO')
                for img_path in img_path_list:
                    if not upload_queue.full():
                        tmp_th = Thread(target=upload_img, args=(FTP(), img_path, ftp_params,), daemon=True)  # 上传队列未满时
                        tmp_th.start()
                        upload_queue.put(tmp_th)
                        time.sleep(0.1)
                    else:
                        while True:  # 上传队列满时,循环等待
                            tmp_th = upload_queue.get()
                            if tmp_th.is_alive():
                                upload_queue.put(tmp_th)
                            else:
                                tmp_th = Thread(target=upload_img, args=(FTP(), img_path, ftp_params,), daemon=True)
                                tmp_th.start()
                                upload_queue.put(tmp_th)
                                break
                            time.sleep(0.5)
                while not upload_queue.empty():  # 等待上传队列为空
                    tmp_th = upload_queue.get()
                    if tmp_th.is_alive():
                        upload_queue.put(tmp_th)
                    else:
                        time.sleep(0.5)
            else:
                my_print(f'Found none caped img', 'TH_UP_INFO')
            time.sleep(10)
    except Exception as e:
        my_print(f'upload_image error, {e}', 'TH_UP_ERROR')


def monitor_overdue(target_path, days, sleep_time):  # 删除过期的任务图片文件夹
    while True:
        try:
            # f = open(cf_path, 'r')
            # param_dict = json.load(f)
            # f.close()
            # target_path = param_dict['target_path']
            # days = param_dict['days']
            # sleep_time = param_dict['sleep(min)']
            if os.path.exists(target_path):
                task_dirs = sorted(os.listdir(target_path), reverse=False)
                my_print(f'task_dirs={task_dirs}', 'TH_MON_INFO')
                for task_dir in task_dirs:
                    if os.path.isdir(os.path.join(target_path, task_dir)):
                        if len(task_dir) < 8:  # 19700101000000
                            continue
                        file_date = task_dir[:8]
                        if (datetime.now() - datetime.strptime(file_date, '%Y%m%d')).days <= int(days):  # 判断距离当前时间经过了几天
                            continue
                        my_print(
                            f"{task_dir} exists {(datetime.now() - datetime.strptime(file_date, '%Y%m%d')).days} days",
                            'TH_MON_INFO')
                        shutil.rmtree(os.path.join(target_path, task_dir))
                        my_print(f'{task_dir} deleted', 'TH_MON_INFO')
            time.sleep(float(sleep_time) * 60)
        except Exception as e:
            my_print(f'{e}', 'TH_MON_ERROR')