UAV/post_pro/merge_obj.py

416 lines
17 KiB
Python
Raw Normal View History

2024-12-29 12:03:53 +08:00
import os
import logging
import numpy as np
from typing import Dict
import pandas as pd
2024-12-31 22:29:24 +08:00
import shutil
2025-01-02 20:11:47 +08:00
import time
2025-01-13 14:42:20 +08:00
import cv2
2024-12-29 12:03:53 +08:00
class MergeObj:
def __init__(self, output_dir: str):
self.output_dir = output_dir
self.logger = logging.getLogger('UAV_Preprocess.MergeObj')
2024-12-31 20:37:35 +08:00
def read_obj(self, file_path):
2025-01-02 10:36:16 +08:00
"""读取.obj文件返回顶点、纹理坐标、法线、面的列表和MTL文件名"""
vertices = [] # v
tex_coords = [] # vt
normals = [] # vn
faces = [] # f
face_materials = [] # 每个面对应的材质名称
mtl_file = None # mtl文件名
current_material = None # 当前使用的材质
2024-12-31 22:29:24 +08:00
2024-12-31 20:37:35 +08:00
with open(file_path, 'r') as file:
for line in file:
2025-01-02 10:36:16 +08:00
if line.startswith('#') or not line.strip():
continue
parts = line.strip().split()
if not parts:
2024-12-31 20:37:35 +08:00
continue
2025-01-02 10:36:16 +08:00
if parts[0] == 'mtllib': # MTL文件引用
mtl_file = parts[1]
elif parts[0] == 'usemtl': # 材质使用
current_material = parts[1]
elif parts[0] == 'v': # 顶点
2024-12-31 22:29:24 +08:00
vertices.append(
[float(parts[1]), float(parts[2]), float(parts[3])])
2025-01-02 10:36:16 +08:00
elif parts[0] == 'vt': # 纹理坐标
tex_coords.append([float(parts[1]), float(parts[2])])
elif parts[0] == 'vn': # 法线
normals.append(
[float(parts[1]), float(parts[2]), float(parts[3])])
2024-12-31 20:37:35 +08:00
elif parts[0] == 'f': # 面
2025-01-02 10:36:16 +08:00
# 处理面的索引 (v/vt/vn)
face_v = []
face_vt = []
face_vn = []
for p in parts[1:]:
indices = p.split('/')
face_v.append(int(indices[0]))
if len(indices) > 1 and indices[1]:
face_vt.append(int(indices[1]))
if len(indices) > 2:
face_vn.append(int(indices[2]))
faces.append((face_v, face_vt, face_vn))
face_materials.append(current_material) # 记录这个面使用的材质
return vertices, tex_coords, normals, faces, face_materials, mtl_file
def write_obj(self, file_path, vertices, tex_coords, normals, faces, face_materials, mtl_file=None):
"""将顶点、纹理坐标、法线和面写入到.obj文件"""
2024-12-31 20:37:35 +08:00
with open(file_path, 'w') as file:
2025-01-02 10:36:16 +08:00
# 写入MTL文件引用
if mtl_file:
file.write(f"mtllib {mtl_file}\n")
# 写入顶点
for v in vertices:
file.write(f"v {v[0]} {v[1]} {v[2]}\n")
# 写入纹理坐标
for vt in tex_coords:
file.write(f"vt {vt[0]} {vt[1]}\n")
# 写入法线
for vn in normals:
file.write(f"vn {vn[0]} {vn[1]} {vn[2]}\n")
# 写入面(按材质分组)
current_material = None
for face, material in zip(faces, face_materials):
# 如果材质发生变化写入新的usemtl
if material != current_material:
file.write(f"usemtl {material}\n")
current_material = material
face_str = "f"
for i in range(len(face[0])):
face_str += " "
face_str += str(face[0][i])
if face[1]:
face_str += f"/{face[1][i]}"
else:
face_str += "/"
if face[2]:
face_str += f"/{face[2][i]}"
else:
face_str += "/"
file.write(face_str + "\n")
2024-12-31 20:37:35 +08:00
def translate_vertices(self, vertices, translation):
"""平移顶点"""
return [[v[0] + translation[0], v[1] + translation[1], v[2] + translation[2]] for v in vertices]
2025-01-02 17:21:23 +08:00
def merge_two_objs(self, obj1_path: str, obj2_path: str, output_path: str, translation, grid_id1: tuple, grid_id2: tuple):
2024-12-31 20:37:35 +08:00
"""合并两个OBJ文件"""
2024-12-29 12:03:53 +08:00
try:
2024-12-31 20:37:35 +08:00
self.logger.info(f"开始合并OBJ模型:\n输入1: {obj1_path}\n输入2: {obj2_path}")
2024-12-31 22:29:24 +08:00
2024-12-31 20:37:35 +08:00
# 读取两个obj文件
2025-01-03 11:16:30 +08:00
vertices1, tex_coords1, normals1, faces1, face_materials1, mtl1 = self.read_obj(
obj1_path)
vertices2, tex_coords2, normals2, faces2, face_materials2, mtl2 = self.read_obj(
obj2_path)
2025-01-02 10:36:16 +08:00
# 读取MTL文件内容以获取正确的材质名称
src_dir1 = os.path.dirname(obj1_path)
src_dir2 = os.path.dirname(obj2_path)
mtl1_path = os.path.join(src_dir1, mtl1)
mtl2_path = os.path.join(src_dir2, mtl2)
2025-01-03 11:16:30 +08:00
2025-01-02 10:36:16 +08:00
# 读取并更新材质内容
materials1 = self.read_mtl(mtl1_path)
materials2 = self.read_mtl(mtl2_path)
2025-01-03 11:16:30 +08:00
2025-01-02 10:36:16 +08:00
# 创建材质名称映射使用与MTL文件相同的命名格式
2025-01-02 20:11:47 +08:00
material_map1 = {}
material_map2 = {}
2025-01-03 11:16:30 +08:00
2025-01-02 20:11:47 +08:00
# 处理第一个模型的材质映射
for old_name in materials1.keys():
2025-01-03 11:16:30 +08:00
if "grid_0_0" in obj1_path:
2025-01-02 20:11:47 +08:00
material_map1[old_name] = f"material_{grid_id1[0]}_{grid_id1[1]}_{old_name}"
2025-01-03 11:16:30 +08:00
else:
# 更新完一次后,之后就不用再更新了
material_map1[old_name] = old_name
2025-01-02 20:11:47 +08:00
# 处理第二个模型的材质映射
for old_name in materials2.keys():
2025-01-03 11:16:30 +08:00
material_map2[old_name] = f"material_{grid_id2[0]}_{grid_id2[1]}_{old_name}"
2024-12-31 22:29:24 +08:00
2024-12-31 20:37:35 +08:00
# 平移第二个模型的顶点
2025-01-03 11:16:30 +08:00
vertices2_translated = self.translate_vertices(
vertices2, translation)
2024-12-31 22:29:24 +08:00
2025-01-02 10:36:16 +08:00
# 计算偏移量
v_offset = len(vertices1)
vt_offset = len(tex_coords1)
vn_offset = len(normals1)
2024-12-31 22:29:24 +08:00
2025-01-02 10:36:16 +08:00
# 合并顶点、纹理坐标和法线
all_vertices = vertices1 + vertices2_translated
all_tex_coords = tex_coords1 + tex_coords2
all_normals = normals1 + normals2
2024-12-31 22:29:24 +08:00
2025-01-02 10:36:16 +08:00
# 调整第二个模型的面索引和材质名称
all_faces = faces1.copy()
all_face_materials = []
2025-01-03 11:16:30 +08:00
2025-01-02 10:36:16 +08:00
# 更新第一个模型的材质名称
for material in face_materials1:
2025-01-03 11:16:30 +08:00
all_face_materials.append(material_map1.get(material))
2025-01-02 10:36:16 +08:00
# 更新第二个模型的面索引和材质名称
for face, material in zip(faces2, face_materials2):
new_face_v = [f + v_offset for f in face[0]]
2025-01-03 11:16:30 +08:00
new_face_vt = [
f + vt_offset for f in face[1]] if face[1] else []
new_face_vn = [
f + vn_offset for f in face[2]] if face[2] else []
2025-01-02 10:36:16 +08:00
all_faces.append((new_face_v, new_face_vt, new_face_vn))
2025-01-03 11:16:30 +08:00
all_face_materials.append(material_map2.get(material))
2025-01-02 10:36:16 +08:00
# 写入合并后的obj文件使用与MTL文件相同的名称
2025-01-09 11:45:55 +08:00
mtl_filename = "textured_model.mtl" # 使用固定的MTL文件名
2025-01-03 11:16:30 +08:00
self.write_obj(output_path, all_vertices, all_tex_coords, all_normals,
all_faces, all_face_materials, mtl_filename)
2024-12-29 12:03:53 +08:00
self.logger.info(f"模型合并成功,已保存至: {output_path}")
except Exception as e:
self.logger.error(f"合并OBJ模型时发生错误: {str(e)}", exc_info=True)
raise
2025-01-02 10:36:16 +08:00
def read_mtl(self, mtl_path: str) -> dict:
"""读取MTL文件内容
Returns:
dict: 材质名称到材质信息的映射
"""
materials = {}
current_material = None
with open(mtl_path, 'r') as f:
content = f.read()
for line in content.strip().split('\n'):
if not line:
continue
parts = line.split()
if not parts:
continue
if parts[0] == 'newmtl':
current_material = parts[1]
materials[current_material] = []
elif current_material:
materials[current_material].append(line)
return materials
2024-12-31 22:29:24 +08:00
2025-01-02 10:36:16 +08:00
def copy_and_rename_texture(self, src_dir: str, dst_dir: str, grid_id: tuple) -> dict:
2025-01-13 14:42:20 +08:00
"""复制并重命名纹理文件对大于100MB的文件进行下采样
2024-12-31 22:29:24 +08:00
Args:
2025-01-02 10:36:16 +08:00
src_dir: 源纹理目录
dst_dir: 目标纹理目录
grid_id: 网格ID
Returns:
dict: 原始文件名到新文件名的映射
2024-12-31 22:29:24 +08:00
"""
2025-01-02 10:36:16 +08:00
texture_map = {}
2024-12-31 22:29:24 +08:00
os.makedirs(dst_dir, exist_ok=True)
2025-01-02 10:36:16 +08:00
2024-12-31 22:29:24 +08:00
for file in os.listdir(src_dir):
2025-01-02 10:36:16 +08:00
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
2025-01-13 14:42:20 +08:00
# 生成新的文件名
2024-12-31 22:29:24 +08:00
new_name = f"grid_{grid_id[0]}_{grid_id[1]}_{file}"
2025-01-02 10:36:16 +08:00
src_path = os.path.join(src_dir, file)
dst_path = os.path.join(dst_dir, new_name)
2025-01-13 14:42:20 +08:00
# 检查文件大小(以字节为单位)
file_size = os.path.getsize(src_path)
if file_size > 100 * 1024 * 1024: # 大于100MB
self.logger.info(f"纹理文件 {file} 大于100MB进行4倍下采样")
# 读取图像
img = cv2.imread(src_path, cv2.IMREAD_UNCHANGED)
if img is not None:
# 计算新的尺寸长宽各变为1/4
new_size = (img.shape[1] // 4, img.shape[0] // 4)
# 使用双三次插值进行下采样
resized_img = cv2.resize(img, new_size, interpolation=cv2.INTER_CUBIC)
# 保存压缩后的图像
if file.lower().endswith('.png'):
cv2.imwrite(dst_path, resized_img, [cv2.IMWRITE_PNG_COMPRESSION, 9])
else:
cv2.imwrite(dst_path, resized_img, [cv2.IMWRITE_JPEG_QUALITY, 95])
else:
self.logger.warning(f"无法读取图像文件: {src_path}")
shutil.copy2(src_path, dst_path)
else:
# 文件大小未超过100MB直接复制
shutil.copy2(src_path, dst_path)
2025-01-02 10:36:16 +08:00
texture_map[file] = new_name
2025-01-13 14:42:20 +08:00
self.logger.debug(f"处理纹理文件: {file} -> {new_name}")
2024-12-31 22:29:24 +08:00
2025-01-02 10:36:16 +08:00
return texture_map
def update_mtl_content(self, materials: dict, texture_map: dict, grid_id: tuple) -> dict:
"""更新材质内容,修改材质名称和纹理路径
2024-12-31 22:29:24 +08:00
Args:
2025-01-02 10:36:16 +08:00
materials: 原始材质信息
texture_map: 纹理文件映射
grid_id: 网格ID
2024-12-31 22:29:24 +08:00
Returns:
2025-01-02 10:36:16 +08:00
dict: 更新后的材质信息
2024-12-31 22:29:24 +08:00
"""
2025-01-02 10:36:16 +08:00
updated_materials = {}
2024-12-31 20:37:35 +08:00
2025-01-02 10:36:16 +08:00
for mat_name, content in materials.items():
# 为材质名称添加网格ID前缀与OBJ文件中的usemtl保持一致
new_mat_name = f"material_{grid_id[0]}_{grid_id[1]}_{mat_name}"
updated_content = []
for line in content:
if line.startswith('map_'): # 更新纹理文件路径
parts = line.split()
old_texture = parts[-1]
if old_texture in texture_map:
parts[-1] = texture_map[old_texture]
line = ' '.join(parts)
updated_content.append(line)
updated_materials[new_mat_name] = updated_content
2024-12-29 12:03:53 +08:00
2025-01-02 10:36:16 +08:00
return updated_materials
2024-12-29 12:03:53 +08:00
2025-01-02 10:36:16 +08:00
def merge_grid_obj(self, grid_points: Dict[tuple, pd.DataFrame], translations: Dict[tuple, tuple]):
"""合并所有网格的OBJ模型"""
2025-01-02 17:21:23 +08:00
if len(grid_points) == 1:
2025-01-13 14:32:36 +08:00
grid_id = list(grid_points.keys())[0]
shutil.copytree(os.path.join(self.output_dir,
f"grid_{grid_id[0]}_{grid_id[1]}",
"project",
"odm_texturing"),
os.path.join(self.output_dir, "texturing"))
os.rename(os.path.join(self.output_dir, "texturing", "odm_textured_model_geo.obj"),
os.path.join(self.output_dir, "texturing", "textured_model.obj"))
2025-01-02 17:21:23 +08:00
2024-12-29 12:03:53 +08:00
try:
2024-12-31 22:29:24 +08:00
# 创建输出目录
2025-01-09 11:45:55 +08:00
output_model_dir = os.path.join(self.output_dir, "texturing")
2024-12-31 22:29:24 +08:00
os.makedirs(output_model_dir, exist_ok=True)
2025-01-02 10:36:16 +08:00
# 获取所有有效的网格文件
grid_files = {}
2024-12-31 21:37:44 +08:00
for grid_id, points in grid_points.items():
2025-01-02 10:36:16 +08:00
base_dir = os.path.join(
2024-12-29 12:03:53 +08:00
self.output_dir,
2024-12-31 21:37:44 +08:00
f"grid_{grid_id[0]}_{grid_id[1]}",
2024-12-29 12:03:53 +08:00
"project",
2024-12-31 22:29:24 +08:00
"odm_texturing"
2024-12-29 12:03:53 +08:00
)
2025-01-02 10:36:16 +08:00
obj_path = os.path.join(base_dir, "odm_textured_model_geo.obj")
mtl_path = os.path.join(base_dir, "odm_textured_model_geo.mtl")
2024-12-29 12:03:53 +08:00
2025-01-02 10:36:16 +08:00
if not os.path.exists(obj_path) or not os.path.exists(mtl_path):
2025-01-03 11:16:30 +08:00
self.logger.warning(
f"网格 ({grid_id[0]},{grid_id[1]}) 的文件不存在")
2024-12-29 12:03:53 +08:00
continue
2025-01-02 10:36:16 +08:00
grid_files[grid_id] = {
'obj': obj_path,
'mtl': mtl_path,
'dir': base_dir
2024-12-31 22:29:24 +08:00
}
2025-01-02 10:36:16 +08:00
if not grid_files:
self.logger.error("没有找到有效的文件")
2024-12-31 22:29:24 +08:00
return
2025-01-02 10:36:16 +08:00
# 收集所有材质和纹理信息
all_materials = {}
for grid_id, files in grid_files.items():
# 复制并重命名纹理文件
texture_map = self.copy_and_rename_texture(
files['dir'],
2024-12-31 22:29:24 +08:00
output_model_dir,
grid_id
)
2025-01-03 11:16:30 +08:00
2025-01-02 10:36:16 +08:00
# 读取并更新MTL内容
materials = self.read_mtl(files['mtl'])
updated_materials = self.update_mtl_content(
materials,
texture_map,
grid_id
)
all_materials.update(updated_materials)
# 写入合并后的MTL文件
2025-01-09 11:45:55 +08:00
final_mtl = os.path.join(output_model_dir, "textured_model.mtl")
2025-01-02 10:36:16 +08:00
with open(final_mtl, 'w') as f:
for mat_name, content in all_materials.items():
f.write(f"newmtl {mat_name}\n")
for line in content:
f.write(f"{line}\n")
f.write("\n")
# 合并OBJ文件
reference_id = list(grid_files.keys())[0]
merged_obj = grid_files[reference_id]['obj']
2025-01-02 20:11:47 +08:00
temp_files = [] # 记录所有中间文件
2025-01-02 10:36:16 +08:00
for grid_id, files in list(grid_files.items())[1:]:
2024-12-31 22:29:24 +08:00
translation = translations[grid_id]
2025-01-02 10:36:16 +08:00
translation = (translation[0], translation[1], 0)
2024-12-31 22:29:24 +08:00
2025-01-02 20:11:47 +08:00
# 生成临时输出文件名
temp_output = os.path.join(
2024-12-31 22:29:24 +08:00
output_model_dir,
2025-01-02 20:11:47 +08:00
f"temp_merged_{int(time.time())}.obj"
2024-12-31 22:29:24 +08:00
)
2025-01-02 20:11:47 +08:00
temp_files.append(temp_output) # 添加到临时文件列表
2024-12-31 22:29:24 +08:00
2025-01-03 11:16:30 +08:00
self.merge_two_objs(
merged_obj, files['obj'], temp_output, translation, reference_id, grid_id)
2025-01-02 20:11:47 +08:00
merged_obj = temp_output
2024-12-31 22:29:24 +08:00
2025-01-02 10:36:16 +08:00
# 最终结果
2025-01-09 11:45:55 +08:00
final_obj = os.path.join(output_model_dir, "textured_model.obj")
2025-01-02 20:11:47 +08:00
try:
if os.path.exists(final_obj):
os.remove(final_obj)
os.rename(merged_obj, final_obj)
except Exception as e:
self.logger.warning(f"重命名最终文件失败: {str(e)}")
2024-12-31 22:29:24 +08:00
shutil.copy2(merged_obj, final_obj)
2025-01-02 20:11:47 +08:00
try:
os.remove(merged_obj)
except:
pass
# 清理所有临时文件
for temp_file in temp_files:
if os.path.exists(temp_file):
try:
os.remove(temp_file)
except Exception as e:
2025-01-03 11:16:30 +08:00
self.logger.warning(
f"删除临时文件失败: {temp_file}, 错误: {str(e)}")
2024-12-31 20:37:35 +08:00
2024-12-29 12:03:53 +08:00
self.logger.info(
2024-12-31 22:29:24 +08:00
f"模型合并完成,输出目录: {output_model_dir}\n"
2025-01-09 11:45:55 +08:00
f"- OBJ文件: textured_model.obj\n"
f"- MTL文件: textured_model.mtl\n"
2025-01-02 10:36:16 +08:00
f"- 纹理文件: {len(os.listdir(output_model_dir)) - 2}"
2024-12-29 12:03:53 +08:00
)
except Exception as e:
2025-01-02 10:36:16 +08:00
self.logger.error(f"合并过程中发生错误: {str(e)}", exc_info=True)
2024-12-31 14:23:45 +08:00
raise