From 333238c7b1d5b48cf9a6d9cc1e140dcd3cac9a75 Mon Sep 17 00:00:00 2001 From: liyubo Date: Tue, 18 Nov 2025 10:42:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=81=BE=E5=AE=B3=E6=95=B0=E6=8D=AE=E8=BE=93?= =?UTF-8?q?=E5=87=BA=EF=BC=9A=E7=BB=BC=E5=90=88=E6=98=8E=E7=BB=86=E8=A1=A8?= =?UTF-8?q?.xlsx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- predict/predict_yolo11seg.py | 6 +- util/smb.py | 6 +- util/yolo2pix_new.py | 201 ++++++++++++++++++++++++++++++++++- 3 files changed, 204 insertions(+), 9 deletions(-) diff --git a/predict/predict_yolo11seg.py b/predict/predict_yolo11seg.py index 6f3543e..a513f10 100644 --- a/predict/predict_yolo11seg.py +++ b/predict/predict_yolo11seg.py @@ -899,7 +899,8 @@ def predict_images_share_dir(task_id, pt_name, zip_url, user_name, pwd, output_d found_paths = scanner.find_folders_by_name( share_path=config['share'], folder_name='图像类', - start_dir=config['dir'] + start_dir=config['dir'], + max_depth=2 ) target_path = "" # 识别图片目录 @@ -911,7 +912,8 @@ def predict_images_share_dir(task_id, pt_name, zip_url, user_name, pwd, output_d found_paths = scanner.find_folders_by_name( share_path=config['share'], folder_name='Images', - start_dir=tmpConfig['dir'] + start_dir=tmpConfig['dir'], + max_depth=3 ) if len(found_paths) > 0: target_path = found_paths[0] diff --git a/util/smb.py b/util/smb.py index c4516e6..099205c 100644 --- a/util/smb.py +++ b/util/smb.py @@ -758,7 +758,8 @@ def get_road_dict(dir,user_name,pwd) : found_paths = scanner.find_files_by_name( share_path=config['share'], file_name='每公里指标明细表*.xls', - start_dir=config['dir'] + start_dir=config['dir'], + max_depth=1 ) print(f"\n找到 {len(found_paths)} 个 'fileindex.txt' 文件:") for i, path in enumerate(found_paths, 1): @@ -789,7 +790,8 @@ def get_pile_dict(dir,user_name,pwd) : found_paths = scanner.find_files_by_name( share_path=config['share'], file_name='fileindex.txt', - start_dir=config['dir'] + start_dir=config['dir'], + max_depth=8 ) print(f"\n找到 {len(found_paths)} 个 'fileindex.txt' 文件:") for i, path in enumerate(found_paths, 1): diff --git a/util/yolo2pix_new.py b/util/yolo2pix_new.py index d73150c..efc961e 100644 --- a/util/yolo2pix_new.py +++ b/util/yolo2pix_new.py @@ -6,6 +6,9 @@ import numpy as np from collections import defaultdict import smb import pandas as pd +from openpyxl import Workbook +from openpyxl.styles import Font, Alignment +from openpyxl.utils import get_column_letter import glob from datetime import datetime @@ -169,7 +172,8 @@ def yoloseg_to_grid_share_dir(road_dict,pile_dict,image_path,label_file,cover_ra cname = class_names[cls_id] if cls_id= len(group_list) : + break + return process_info_data + +def adjust_column_width(worksheet): + """ + 调整列宽函数 + """ + for col_idx in range(1, worksheet.max_column + 1): + max_length = 0 + column_letter = get_column_letter(col_idx) + + for row_idx in range(1, worksheet.max_row + 1): + try: + cell = worksheet.cell(row=row_idx, column=col_idx) + # 检查单元格是否在合并单元格范围内 + if any(cell.coordinate in merged_range for merged_range in worksheet.merged_cells.ranges): + continue # 跳过合并单元格 + + if cell.value is not None: + cell_length = len(str(cell.value)) + if cell_length > max_length: + max_length = cell_length + except: + continue + + if max_length > 0: + worksheet.column_dimensions[column_letter].width = max_length + 2 + +def create_multiple_sheets_with_multiple_headers(file_path, excel_data): + # 创建工作簿 + wb = Workbook() + + # 删除默认创建的sheet + wb.remove(wb.active) + + for sheet_data in excel_data: + # 创建工作表 + ws = wb.create_sheet(title=sheet_data['name']) + + # 设置多表头 + current_row = 1 + + # 多级表头 + for heads in sheet_data['headers'] : + for head in heads : + ws.merge_cells(f'{head[1]}{current_row}:{head[2]}{current_row}') + ws[f'{head[1]}{current_row}'] = head[0] + ws[f'{head[1]}{current_row}'].font = Font(bold=True, size=14) + ws[f'{head[1]}{current_row}'].alignment = Alignment(horizontal='center') + current_row += 1 + + # 列标题 + for col_idx, column_name in enumerate(sheet_data['columns'], 1): + cell = ws.cell(row=current_row, column=col_idx, value=column_name) + cell.font = Font(bold=True) + + current_row += 1 + + # 写入数据 + for row_data in sheet_data['data']: + for col_idx, value in enumerate(row_data, 1): + ws.cell(row=current_row, column=col_idx, value=value) + current_row += 1 + + # 自动调整列宽 + adjust_column_width(ws) + + # 保存文件 + wb.save(file_path) + print(f"文件已保存为 {file_path}") # ---------------- 主函数-共享目录 ---------------- def process_dir(road_dict,pile_dict,dir="output",cell_area=CELL_AREA,grid_width=GRID_WIDTH,grid_height=GRID_HEIGHT): @@ -367,7 +528,7 @@ def process_dir(road_dict,pile_dict,dir="output",cell_area=CELL_AREA,grid_width= if up_or_down == '下行' : group_list = list(group)[::-1] increment = float(-0.010) - tmp_pile_no = convert_special_format(group[len(group)-1][1]) + tmp_pile_no = convert_special_format(group_list[len(group_list)-1][1]) pile_no_start = tmp_pile_no if tmp_pile_no % increment != 0 : pile_no_end = tmp_pile_no + (tmp_pile_no % increment) @@ -381,7 +542,7 @@ def process_dir(road_dict,pile_dict,dir="output",cell_area=CELL_AREA,grid_width= tmp_end = pile_no_end while True : # 每10m一个区间,在区间内进行灾害计算 - pile_no, DR, counts, road_type = summary_data[index] + pile_no, DR, counts, road_type = group_list[index] cur_pile_no = convert_special_format(pile_no) if not in_interval(increment, cur_pile_no, tmp_start, tmp_end) : # 没在刻度内直接输出无病害数据 @@ -400,7 +561,7 @@ def process_dir(road_dict,pile_dict,dir="output",cell_area=CELL_AREA,grid_width= row = [f"{tmp_start:0.3f}", identify_width] subRows = [] while index < len(group_list): - pile_no, DR, counts, road_type = summary_data[index] + pile_no, DR, counts, road_type = group_list[index] cur_pile_no = convert_special_format(pile_no) tmp_row = [] @@ -452,7 +613,37 @@ def process_dir(road_dict,pile_dict,dir="output",cell_area=CELL_AREA,grid_width= data_list.append(excel_data) all_data = [headers] + data_list - smb.write_to_excel_pandas(all_data, img_file_path + '/病害明显列表.xlsx') + smb.write_to_excel_pandas(all_data, img_file_path + '/病害明细列表.xlsx') + + # 综合明细表.xlsx + heads = ['路线编码','起点','终点','车道编码','上下行','公路等级','路面类型','PQI','PQI等级','DR(%)','PCI','PCI等级','IRI','RQI','RQI等级','RD','RDI','RDI等级','SMTD','PBI','PBI等级','WR','PWI','PWI等级','备注'] + data1 = process_info(road_dict,pile_dict,summary_data,image_path,current_time,10) + data2 = process_info(road_dict,pile_dict,summary_data,image_path,current_time,100) + data3 = process_info(road_dict,pile_dict,summary_data,image_path,current_time,1000) + + excel_data = [ + { + 'name': '综合明细十米', + 'headers': [[['综合明细十米','A','Y']]], + 'columns': heads, + 'data': data1 + }, + { + 'name': '综合明细百米', + 'headers': [[['综合明细百米','A','Y']]], + 'columns': heads, + 'data': data2 + }, + { + 'name': '综合明细千米', + 'headers': [[['综合明细千米','A','Y']]], + 'columns': heads, + 'data': data3 + } + ] + create_multiple_sheets_with_multiple_headers(img_file_path + '/综合明细表.xlsx', excel_data) + + # ---------------- 主函数 ---------------- def process_zip(zip_path,pile_map_file,output_dir="output",cell_area=CELL_AREA,grid_width=GRID_WIDTH,grid_height=GRID_HEIGHT):