def init_delete_imgfile(log_oper,path_1,path_2,path_3,path_4):
    for root,dirs,files in os.walk(path_1["path_ref"]):
        img_path_list=[]
        for file in files:
            if os.path.splitext(file)[1]=='.jpg':
                img_path_list.append(os.path.join(path_1["path_ref"],file))
        delete_file(log_oper,img_path_list)
    #
    for root,dirs,files in os.walk(path_2["path_ref"]):
        img_path_list=[]
        for file in files:
            if os.path.splitext(file)[1]=='.jpg':
                img_path_list.append(os.path.join(path_2["path_ref"],file))
        delete_file(log_oper,img_path_list)
    #
    for root,dirs,files in os.walk(path_3["path_ref"]):
        img_path_list=[]
        for file in files:
            if os.path.splitext(file)[1]=='.jpg':
                img_path_list.append(os.path.join(path_3["path_ref"],file))
        delete_file(log_oper,img_path_list)
    #
    for root,dirs,files in os.walk(path_4["path_ref"]):
        img_path_list=[]
        for file in files:
            if os.path.splitext(file)[1]=='.jpg':
                img_path_list.append(os.path.join(path_4["path_ref"],file))
        delete_file(log_oper,img_path_list)
def thread_load_camimg3(log_oper,path):
    while True:
        try:
            for root,dirs,files in os.walk(path["path_ref"]):
                if len(files)>0:
                    img_path_list=[]
                    time.sleep(0.5)
                    for file in files:
                        if os.path.splitext(file)[1]=='.jpg':                            
                            if os.path.exists(os.path.join(path["path_ref"],file)):                                
                                img_info={}
                                img_info["img_path"]=os.path.join(path["path_ref"],file)
                                img_info["img_name"]=file
                                img_info['img_data']=cv2.imread(img_info["img_path"], 0)
                                if img_info['img_data'] is None:
                                    continue
                                image_cache_cam3.put(img_info)
                                img_path_list.append(img_info["img_path"])
                    delete_file(log_oper,img_path_list)
                else:
                    time.sleep(0.1)
        except Exception as err:
            log_oper.add_log("Error>>相机3图像载入线程中Bug->{}".format(err))
            continue
def steel_char_detect(path_1, path_2, path_3, path_4):
    try:
        Log_oper = Log("Log_ASC")

        Log_oper.add_log("Normal>>开始载入跟踪模型")
        steel_model = YOLO()
        Log_oper.add_log("Normal>>完成载入跟踪模型成功")

        Log_oper.add_log("Normal>>开始载入识别模型")
        phi = 2
        weighted_bifpn = True
        model_path = 'char_model/char_model.h5'
        image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
        image_size = image_sizes[phi]
        classes = {
            value['id']: value['name']
            for value in json.load(open('char_model/char_class.json',
                                        'r')).values()
        }
        num_classes = len(classes)
        score_threshold = 0.5
        colors = [
            np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)
        ]
        _, char_model = efficientdet(phi=phi,
                                     weighted_bifpn=weighted_bifpn,
                                     num_classes=num_classes,
                                     score_threshold=score_threshold)
        char_model.load_weights(model_path, by_name=True)
        char_model_param = {
            'image_size': image_size,
            'classes': classes,
            'score_threshold': score_threshold,
            'colors': colors
        }
        Log_oper.add_log("Normal>>完成载入识别模型成功")

        Log_oper.add_log("Normal>>开始载入mini识别模型")
        phi_mini = 0
        weighted_bifpn_mini = True
        model_path_mini = 'char_model_mini/char_model.h5'
        image_sizes_mini = (512, 640, 768, 896, 1024, 1280, 1408)
        image_size_mini = image_sizes_mini[phi_mini]
        classes_mini = {
            value['id']: value['name']
            for value in json.load(open('char_model_mini/char_class.json',
                                        'r')).values()
        }
        num_classes_mini = len(classes_mini)
        score_threshold_mini = 0.5
        colors_mini = [
            np.random.randint(0, 256, 3).tolist()
            for _ in range(num_classes_mini)
        ]
        _, char_model_mini = efficientdet(phi=phi_mini,
                                          weighted_bifpn=weighted_bifpn_mini,
                                          num_classes=num_classes_mini,
                                          score_threshold=score_threshold_mini)
        char_model_mini.load_weights(model_path_mini, by_name=True)
        char_model_param_mini = {
            'image_size': image_size_mini,
            'classes': classes_mini,
            'score_threshold': score_threshold_mini,
            'colors': colors_mini
        }
        Log_oper.add_log("Normal>>完成载入mini识别模型成功")

        Log_oper.add_log("Normal>>开始载入字符分类模型")
        table1 = get_convert_from_class_table(
            'char_model_class/steel_class_table_char.xml', Log_oper)
        table2, img_size, class_num, model_mark = get_convert_from_ini_config_file(
            'char_model_class/steel_classifier_interaction.ini', Log_oper)
        classifier = Classifier(20, 28,
                                'char_model_class/steel_model_classifier.h5')
        classifier.load_model()
        Log_oper.add_log("Normal>>完成载入字符分类模型成功")

        Log_oper.add_log("Normal>>开始创建二级连接")
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(('191.168.162.192', 12001))
        send_heartbeat = threading.Thread(target=thread_send_heartbeat_to_l2,
                                          args=(
                                              s,
                                              Log_oper,
                                          ))
        send_heartbeat.start()
        #recv_info = threading.Thread(target=thread_recv_info_from_l2,args=(s,Log_oper,))
        #recv_info.start()
        t_recv_info = HThreadRecvInfo((
            s,
            Log_oper,
        ), "recv")
        t_recv_info.start()
        Log_oper.add_log("Normal>>完成成功连接到二级")

        judge_num_sbm = 0
        SCD_SBM_Oper = SteelCharDetect(1, Log_oper, steel_model, char_model,
                                       char_model_param, char_model_mini,
                                       char_model_param_mini, classifier, s,
                                       10, path_1)
        judge_num_htf1 = 0
        SCD_HTF1_Oper = SteelCharDetect(2, Log_oper, steel_model, char_model,
                                        char_model_param, char_model_mini,
                                        char_model_param_mini, classifier, s,
                                        10, path_2)
        judge_num_htf2 = 0
        SCD_HTF2_Oper = SteelCharDetect(3, Log_oper, steel_model, char_model,
                                        char_model_param, char_model_mini,
                                        char_model_param_mini, classifier, s,
                                        10, path_3)
        judge_num_print = 0
        SCD_PRINT_Oper = SteelCharDetect(4, Log_oper, steel_model, char_model,
                                         char_model_param, char_model_mini,
                                         char_model_param_mini, classifier, s,
                                         10, path_4)

        steel_no_send = {
            "SBM": False,
            "HTF1": False,
            "HTF2": False,
            "PRINT": False
        }  #根据一级信号判断钢板是否经过
        last_recv_res = {
            "SBM": [1, 1, 1],
            "HTF1": [1, 1, 1],
            "HTF2": [1, 1, 1],
            "PRINT": [1, 1, 1]
        }
        time.sleep(
            5)  #延时一会,让二级信息接收到后,再对变量进行更新,不让其使用默认值,另外感觉recv_res得到的是变量的地址,会跟着变
        recv_res = t_recv_info.get_result()
        last_recv_res["SBM"] = copy.deepcopy(recv_res["SBM"])
        last_recv_res["HTF1"] = copy.deepcopy(recv_res["HTF1"])
        last_recv_res["HTF2"] = copy.deepcopy(recv_res["HTF2"])
        last_recv_res["PRINT"] = copy.deepcopy(recv_res["PRINT"])
        char_roi = None
        enable_l2_final_send = {
            "SBM": False,
            "HTF1": False,
            "HTF2": False,
            "PRINT": False
        }  #程序重启后钢板不在识别区时不发送数据
        last_recv_res_plan_counter = {
            "SBM": last_recv_res["SBM"][0],
            "HTF1": last_recv_res["HTF1"][0],
            "HTF2": last_recv_res["HTF2"][0],
            "PRINT": last_recv_res["PRINT"][0]
        }  #通过2级发送的第一位计数判断是否还给二级发送,如果计数已变化则不再发送
        #第一位:二级是否搜到计划 1为未搜到 0为搜到 第二位:钢板是否在识别区 1为在识别区 0为不在识别区 第三位:辊道转速情况 1为正转 0为停止 -1为倒转

        #清空图像目录
        for root, dirs, files in os.walk(path_1["path_ref"]):
            img_path_list = []
            for file in files:
                if os.path.splitext(file)[1] == '.jpg':
                    img_path_list.append(os.path.join(path_1["path_ref"],
                                                      file))
            delete_file(Log_oper, img_path_list)
        #
        for root, dirs, files in os.walk(path_2["path_ref"]):
            img_path_list = []
            for file in files:
                if os.path.splitext(file)[1] == '.jpg':
                    img_path_list.append(os.path.join(path_2["path_ref"],
                                                      file))
            delete_file(Log_oper, img_path_list)
        #
        for root, dirs, files in os.walk(path_3["path_ref"]):
            img_path_list = []
            for file in files:
                if os.path.splitext(file)[1] == '.jpg':
                    img_path_list.append(os.path.join(path_3["path_ref"],
                                                      file))
            delete_file(Log_oper, img_path_list)
        #
        for root, dirs, files in os.walk(path_4["path_ref"]):
            img_path_list = []
            for file in files:
                if os.path.splitext(file)[1] == '.jpg':
                    img_path_list.append(os.path.join(path_4["path_ref"],
                                                      file))
            delete_file(Log_oper, img_path_list)
        #
    except Exception as err:
        Log_oper.add_log("Error>>执行中出现错误{},程序自动重启".format(err))
        os.system('taskkill /IM ArNTSteelDefectsClassifier64.exe /F')

    #钢板状态更新
    def total_steel_state_update():
        #抛丸机
        if recv_res["SBM"][1] == 1:
            enable_l2_final_send["SBM"] = True
        if recv_res["SBM"][1] == 0 and last_recv_res["SBM"][1] == 1:  #钢板退出时执行
            if recv_res["SBM"][2] >= 0 and enable_l2_final_send["SBM"] is True:
                if steel_no_send["SBM"] is not True:
                    steel_no, steel_type, steel_size, img_path = SCD_SBM_Oper.get_send_char(
                        char_roi, cache_limit=False)
                    if steel_no is not None:
                        SCD_SBM_Oper.send_char_to_l2(steel_no, steel_type,
                                                     steel_size, img_path)
                    else:
                        if SCD_SBM_Oper.curr_char_img_info[1] is not None:
                            SCD_SBM_Oper.send_char_to_l2_not_steel_no()
                        else:
                            SCD_SBM_Oper.send_char_to_l2_not_img()
        elif recv_res["SBM"][1] == 1 and last_recv_res["SBM"][
                1] == 0:  #钢板重新进入后,上一次发送的板号置0
            steel_no_send["SBM"] = False
            last_recv_res_plan_counter["SBM"] = copy.deepcopy(
                recv_res["SBM"][0])
            SCD_SBM_Oper.last_steel_no = "00000000000000"
            SCD_SBM_Oper.curr_char_img_info = [None, None]
        last_recv_res["SBM"] = copy.deepcopy(recv_res["SBM"])
        #1号炉
        if recv_res["HTF1"][1] == 1:
            enable_l2_final_send["HTF1"] = True
        if recv_res["HTF1"][1] == 0 and last_recv_res["HTF1"][1] == 1:  #钢板退出时执行
            if recv_res["HTF1"][2] >= 0 and enable_l2_final_send[
                    "HTF1"] is True:
                if steel_no_send["HTF1"] is not True:
                    steel_no, steel_type, steel_size, img_path = SCD_HTF1_Oper.get_send_char(
                        char_roi, cache_limit=False)
                    if steel_no is not None:
                        SCD_HTF1_Oper.send_char_to_l2(steel_no, steel_type,
                                                      steel_size, img_path)
                    else:
                        if SCD_HTF1_Oper.curr_char_img_info[1] is not None:
                            SCD_HTF1_Oper.send_char_to_l2_not_steel_no()
                        else:
                            SCD_HTF1_Oper.send_char_to_l2_not_img()
        elif recv_res["HTF1"][1] == 1 and last_recv_res["HTF1"][
                1] == 0:  #钢板重新进入后,上一次发送的板号置0
            steel_no_send["HTF1"] = False
            last_recv_res_plan_counter["HTF1"] = copy.deepcopy(
                recv_res["HTF1"][0])
            #针对炉前来回倒板的情况,为避免重复识别发送
            #SCD_HTF1_Oper.last_steel_no="00000000000000"
            SCD_HTF1_Oper.curr_char_img_info = [None, None]
        last_recv_res["HTF1"] = copy.deepcopy(recv_res["HTF1"])
        #2号炉
        if recv_res["HTF2"][1] == 1:
            enable_l2_final_send["HTF2"] = True
        if recv_res["HTF2"][1] == 0 and last_recv_res["HTF2"][1] == 1:  #钢板退出时执行
            if recv_res["HTF2"][2] >= 0 and enable_l2_final_send[
                    "HTF2"] is True:
                if steel_no_send["HTF2"] is not True:
                    steel_no, steel_type, steel_size, img_path = SCD_HTF2_Oper.get_send_char(
                        char_roi, cache_limit=False)
                    if steel_no is not None:
                        SCD_HTF2_Oper.send_char_to_l2(steel_no, steel_type,
                                                      steel_size, img_path)
                    else:
                        if SCD_HTF2_Oper.curr_char_img_info[1] is not None:
                            SCD_HTF2_Oper.send_char_to_l2_not_steel_no()
                        else:
                            SCD_HTF2_Oper.send_char_to_l2_not_img()
        elif recv_res["HTF2"][1] == 1 and last_recv_res["HTF2"][
                1] == 0:  #钢板重新进入后,上一次发送的板号置0
            steel_no_send["HTF2"] = False
            last_recv_res_plan_counter["HTF2"] = copy.deepcopy(
                recv_res["HTF2"][0])
            #针对炉前来回倒板的情况,为避免重复识别发送
            #SCD_HTF2_Oper.last_steel_no="00000000000000"
            SCD_HTF2_Oper.curr_char_img_info = [None, None]
        last_recv_res["HTF2"] = copy.deepcopy(recv_res["HTF2"])
        #标印
        if recv_res["PRINT"][1] == 1:
            enable_l2_final_send["PRINT"] = True
        if recv_res["PRINT"][1] == 0 and last_recv_res["PRINT"][
                1] == 1:  #钢板退出时执行
            if recv_res["PRINT"][2] >= 0 and enable_l2_final_send[
                    "PRINT"] is True:
                if steel_no_send["PRINT"] is not True:
                    steel_no, steel_type, steel_size, img_path = SCD_PRINT_Oper.get_send_char(
                        char_roi, cache_limit=False)
                    if steel_no is not None:
                        SCD_PRINT_Oper.send_char_to_l2(steel_no, steel_type,
                                                       steel_size, img_path)
                    else:
                        if SCD_PRINT_Oper.curr_char_img_info[1] is not None:
                            SCD_PRINT_Oper.send_char_to_l2_not_steel_no()
                        else:
                            SCD_PRINT_Oper.send_char_to_l2_not_img()
        elif recv_res["PRINT"][1] == 1 and last_recv_res["PRINT"][
                1] == 0:  #钢板重新进入后,上一次发送的板号置0
            steel_no_send["PRINT"] = False
            last_recv_res_plan_counter["PRINT"] = copy.deepcopy(
                recv_res["PRINT"][0])
            SCD_PRINT_Oper.last_steel_no = "00000000000000"
            SCD_PRINT_Oper.curr_char_img_info = [None, None]
        last_recv_res["PRINT"] = copy.deepcopy(recv_res["PRINT"])

    while True:
        try:
            Log_oper.add_log("Normal>>获得文件夹图像路径列表")
            time.sleep(1)

            Log_oper.add_log(
                "Normal>>-----------------抛丸机前字符识别-----------------")
            for root, dirs, files in os.walk(path_1["path_ref"]):
                img_path_list = []

                if len(files) > 0:  #确定是否钢板已走完
                    judge_num_sbm = 0
                else:
                    judge_num_sbm = judge_num_sbm + 1
                num = 0
                for file in files:
                    if os.path.splitext(file)[1] == '.jpg':
                        num = num + 1
                        if num > 10:
                            break
                        img_path_list.append(
                            os.path.join(path_1["path_ref"], file))
                        char_label, char_roi, img_name, top_offset, bottom_offset = SCD_SBM_Oper.detect_steel(
                            file)
                        if char_roi is not None:
                            if char_label != 'tchar':
                                SCD_SBM_Oper.detect_char(
                                    char_roi, img_name, top_offset,
                                    bottom_offset)
                            else:
                                SCD_SBM_Oper.detect_char_mini(
                                    char_roi, img_name, top_offset,
                                    bottom_offset)
                        steel_no, steel_type, steel_size, img_path = SCD_SBM_Oper.get_send_char(
                            char_roi, cache_limit=True)
                        if steel_no is not None:
                            recv_res = t_recv_info.get_result()
                            if recv_res["SBM"][0] == last_recv_res_plan_counter[
                                    "SBM"] and recv_res["SBM"][
                                        2] >= 0:  #还未依据钢板号查到计划且辊道速度不为负
                                SCD_SBM_Oper.send_char_to_l2(
                                    steel_no, steel_type, steel_size, img_path)
                                steel_no_send["SBM"] = True

                recv_res = t_recv_info.get_result()  #一级状态
                total_steel_state_update()
                delete_file(Log_oper, img_path_list)

            Log_oper.add_log(
                "Normal>>-----------------一号加热炉前字符识别-----------------")
            for root, dirs, files in os.walk(path_2["path_ref"]):
                img_path_list = []

                if len(files) > 0:  #确定是否钢板已走完
                    judge_num_htf1 = 0
                else:
                    judge_num_htf1 = judge_num_htf1 + 1
                num = 0
                for file in files:
                    if os.path.splitext(file)[1] == '.jpg':
                        num = num + 1
                        if num > 10:
                            break
                        img_path_list.append(
                            os.path.join(path_2["path_ref"], file))
                        char_label, char_roi, img_name, top_offset, bottom_offset = SCD_HTF1_Oper.detect_steel(
                            file)
                        if char_roi is not None:
                            if char_label != 'tchar':
                                SCD_HTF1_Oper.detect_char(
                                    char_roi, img_name, top_offset,
                                    bottom_offset)
                            else:
                                SCD_HTF1_Oper.detect_char_mini(
                                    char_roi, img_name, top_offset,
                                    bottom_offset)
                        steel_no, steel_type, steel_size, img_path = SCD_HTF1_Oper.get_send_char(
                            char_roi, cache_limit=True)
                        if steel_no is not None:
                            recv_res = t_recv_info.get_result()
                            if recv_res["HTF1"][
                                    0] == last_recv_res_plan_counter[
                                        "HTF1"] and recv_res["HTF1"][
                                            2] >= 0:  #还未依据钢板号查到计划且辊道速度不为负
                                SCD_HTF1_Oper.send_char_to_l2(
                                    steel_no, steel_type, steel_size, img_path)
                                steel_no_send["HTF1"] = True

                recv_res = t_recv_info.get_result()  #一级状态
                total_steel_state_update()
                delete_file(Log_oper, img_path_list)

            Log_oper.add_log(
                "Normal>>-----------------二号加热炉前字符识别-----------------")
            for root, dirs, files in os.walk(path_3["path_ref"]):
                img_path_list = []

                if len(files) > 0:  #确定是否钢板已走完
                    judge_num_htf2 = 0
                else:
                    judge_num_htf2 = judge_num_htf2 + 1
                num = 0
                for file in files:
                    if os.path.splitext(file)[1] == '.jpg':
                        num = num + 1
                        if num > 10:
                            break
                        img_path_list.append(
                            os.path.join(path_3["path_ref"], file))
                        char_label, char_roi, img_name, top_offset, bottom_offset = SCD_HTF2_Oper.detect_steel(
                            file)
                        if char_roi is not None:
                            if char_label != 'tchar':
                                SCD_HTF2_Oper.detect_char(
                                    char_roi, img_name, top_offset,
                                    bottom_offset)
                            else:
                                SCD_HTF2_Oper.detect_char_mini(
                                    char_roi, img_name, top_offset,
                                    bottom_offset)
                        steel_no, steel_type, steel_size, img_path = SCD_HTF2_Oper.get_send_char(
                            char_roi, cache_limit=True)
                        if steel_no is not None:
                            recv_res = t_recv_info.get_result()
                            if recv_res["HTF2"][
                                    0] == last_recv_res_plan_counter[
                                        "HTF2"] and recv_res["HTF2"][
                                            2] >= 0:  #还未依据钢板号查到计划且辊道速度不为负
                                SCD_HTF2_Oper.send_char_to_l2(
                                    steel_no, steel_type, steel_size, img_path)
                                steel_no_send["HTF2"] = True

                recv_res = t_recv_info.get_result()  #一级状态
                total_steel_state_update()
                delete_file(Log_oper, img_path_list)

            Log_oper.add_log(
                "Normal>>-----------------喷印前字符识别-----------------")
            for root, dirs, files in os.walk(path_4["path_ref"]):
                img_path_list = []

                if len(files) > 0:  #确定是否钢板已走完
                    judge_num_print = 0
                else:
                    judge_num_print = judge_num_print + 1
                num = 0
                for file in files:
                    if os.path.splitext(file)[1] == '.jpg':
                        num = num + 1
                        if num > 10:
                            break
                        img_path_list.append(
                            os.path.join(path_4["path_ref"], file))
                        char_label, char_roi, img_name, top_offset, bottom_offset = SCD_PRINT_Oper.detect_steel(
                            file)
                        if char_roi is not None:
                            if char_label != 'tchar':
                                SCD_PRINT_Oper.detect_char(
                                    char_roi, img_name, top_offset,
                                    bottom_offset)
                            else:
                                SCD_PRINT_Oper.detect_char_mini(
                                    char_roi, img_name, top_offset,
                                    bottom_offset)
                        steel_no, steel_type, steel_size, img_path = SCD_PRINT_Oper.get_send_char(
                            char_roi, cache_limit=True)
                        if steel_no is not None:
                            recv_res = t_recv_info.get_result()
                            if recv_res["PRINT"][
                                    0] == last_recv_res_plan_counter[
                                        "PRINT"] and recv_res["PRINT"][
                                            2] >= 0:  #还未依据钢板号查到计划且辊道速度不为负
                                SCD_PRINT_Oper.send_char_to_l2(
                                    steel_no, steel_type, steel_size, img_path)
                                steel_no_send["PRINT"] = True

                recv_res = t_recv_info.get_result()  #一级状态
                total_steel_state_update()
                delete_file(Log_oper, img_path_list)

        except Exception as err:
            Log_oper.add_log("Error>>执行中出现错误{},程序自动重启".format(err))
            os.system('taskkill /IM ArNTSteelDefectsClassifier64.exe /F')
    steel_model.close_session()