38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
from dataclasses import dataclass, fields
|
|
|
|
|
|
@dataclass
|
|
class Camera_Para:
|
|
fx: float
|
|
fy: float
|
|
cx: int
|
|
cy: int
|
|
|
|
|
|
def read_camera_params(file_path) -> Camera_Para:
|
|
# 初始化默认参数
|
|
params = Camera_Para(fx=0.0, fy=0.0, cx=0, cy=0)
|
|
|
|
# 获取 dataclass 的字段信息(用于类型转换)
|
|
field_types = {f.name: f.type for f in fields(params)}
|
|
|
|
with open(file_path, 'r') as file:
|
|
for line in file:
|
|
line = line.strip()
|
|
if line and '=' in line:
|
|
key, value = line.split('=', 1)
|
|
key = key.strip()
|
|
value = value.strip()
|
|
|
|
# 检查字段是否存在
|
|
if key in field_types:
|
|
try:
|
|
# 根据字段类型转换值
|
|
if field_types[key] == float:
|
|
setattr(params, key, float(value))
|
|
elif field_types[key] == int:
|
|
setattr(params, key, int(value))
|
|
except ValueError:
|
|
print(f"Warning: Could not convert '{value}' to {field_types[key]} for '{key}'")
|
|
|
|
return params |