You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.1 KiB
109 lines
3.1 KiB
import json
|
|
import os
|
|
from dataclasses import (
|
|
dataclass,
|
|
field, asdict
|
|
)
|
|
from typing import Dict, Optional
|
|
|
|
import numpy as np
|
|
from dataclasses_json import dataclass_json
|
|
|
|
import models.target
|
|
|
|
_file_path: str
|
|
|
|
|
|
@dataclass_json
|
|
@dataclass
|
|
class Server:
|
|
port: int = 0
|
|
|
|
@dataclass_json
|
|
@dataclass
|
|
class ConfigInfo:
|
|
server:Server
|
|
capture: int = 0
|
|
# 标靶配置
|
|
targets: Dict[int, models.target.CircleTarget] = field(default_factory=dict)
|
|
# 标靶透视矩阵
|
|
perspective: Dict[int, np.ndarray] = field(default_factory=dict)
|
|
|
|
class ConfigOperate:
|
|
_file_path: str
|
|
config_info: ConfigInfo
|
|
def __init__(self,path:str):
|
|
self._file_path = path
|
|
self.load2obj_sample()
|
|
|
|
|
|
def load2dict(self):
|
|
""""读取配置"""
|
|
if not os.path.exists(self._file_path):
|
|
raise FileNotFoundError(f"配置文件 {self._file_path} 不存在")
|
|
|
|
with open(self._file_path) as json_file:
|
|
config = json.load(json_file)
|
|
return config
|
|
|
|
def load2obj_sample2(self):
|
|
""""读取配置"""
|
|
dic=self.load2dict()
|
|
ts = dic["targets"]
|
|
capture = dic["capture"]
|
|
# 获取矩阵数据
|
|
matrix_dict = dic.get("perspective", {})
|
|
# n0=convert_to_ndarray(self.matrix_dict["0"])
|
|
# 将矩阵转换为字符串
|
|
# matrix_str = np.array2string(n0, precision=8, separator=', ', suppress_small=True)
|
|
for _,t in ts.items():
|
|
obj = models.target.TargetInfo(**t)
|
|
area = models.target.RectangleArea.from_dict(obj.rectangle_area)
|
|
thres = models.target.Threshold(**obj.threshold)
|
|
self.targets[obj.id] = models.target.CircleTarget(
|
|
obj.id,
|
|
obj.desc,
|
|
area,
|
|
obj.radius,
|
|
thres,
|
|
obj.base
|
|
)
|
|
return self.targets
|
|
|
|
def load2obj_sample(self):
|
|
dic=self.load2dict()
|
|
dict_str = json.dumps(dic)
|
|
self.config_info=ConfigInfo.from_json(dict_str)
|
|
|
|
def save2json_file(self):
|
|
json_str = self.config_info.to_json(indent=4)
|
|
""""更新配置"""
|
|
with open(self._file_path, 'w') as json_file:
|
|
json_file.write(json_str)
|
|
# json.dump(self, json_file, indent=4)
|
|
return None
|
|
|
|
|
|
def save_dict_config(self, dict_data:Dict):
|
|
""""更新配置"""
|
|
with open(self._file_path, 'w') as json_file:
|
|
json.dump(dict_data, json_file, indent=4)
|
|
return None
|
|
|
|
def update_dict_config(self, updates):
|
|
"""
|
|
更新配置文件中的特定字段。
|
|
:param file_path: 配置文件路径
|
|
:param updates: 包含更新内容的字典
|
|
"""
|
|
config_dict = self.load2dict()
|
|
config_dict.update(updates)
|
|
self.save_dict_config(config_dict)
|
|
|
|
def convert_to_ndarray(matrix_data):
|
|
"""
|
|
将 JSON 中的矩阵数据转换为 numpy ndarray。
|
|
:param matrix_data: JSON 中的矩阵数据(列表形式)
|
|
:return: numpy ndarray
|
|
"""
|
|
return np.array(matrix_data, dtype=np.float64)
|