import os
from json import load, dumps
from pathlib import Path
from traceback import format_exc
from log_color.log_color import Logger
from progress.bar import Bar
def get_file_list(input_path, file_list):
"""
@param input_path:文件输入路径
@param file_list:文件列表
@return:
"""
with os.scandir(input_path) as it:
for entry in it:
if entry.is_file():
file_list.append(entry.path)
elif entry.is_dir():
get_file_list(entry.path, file_list)
class ProcessData:
def __init__(self, src, dst):
self.src = Path(src)
self.dst = Path(dst)
if self.dst.exists():
self.logger = Logger(name=os.path.join(dst, "$NAME$.log"))
else:
self.logger = Logger(name="$NAME$.log")
def start(self):
try:
if self.check_path_exist():
self.process_data()
except Exception as e:
self.logger.error(f"运行失败:{format_exc()}:{e}")
def process_data(self):
json_file_list = [file for file in self.src.rglob("*.json")]
with Bar(max=len(json_file_list), suffix='%(index)d/%(max)d in %(elapsed)ds (eta:%(eta_td)s)') as bar:
for file in json_file_list:
try:
output_file = self.dst.joinpath(file.relative_to(self.src))
output_file.parent.mkdir(parents=True, exist_ok=True)
self.convert(file, output_file)
except Exception as e:
self.logger.error(f"{file}运行失败,跳过这个文件。{e}\n{format_exc()}")
finally:
bar.next()
def convert(self, file, output_file):
json_content = self.read_json(file)
result_json = {}
self.generate_json(result_json, output_file)
def check_path_exist(self):
"""
:return:True/False
"""
for input_path in [self.src, self.dst]:
if not input_path.exists() or input_path != "":
self.logger.error(f"{input_path}不存在或者为空")
return False
return True
@staticmethod
def get_bbox(coordinates):
"""
:param coordinates: 平台点列表
:return: 返回bbox格式,左上x,y,宽,高
"""
x = int(coordinates[0][0])
y = int(coordinates[0][1])
w = int(abs(coordinates[2][0] - x))
h = int(abs(coordinates[2][1] - y))
return [x, y, w, h]
@staticmethod
def get_xys(coordinates):
"""
:param coordinates:平台点列表
:return:返回[x0,y0,x2,y2]
"""
x0 = coordinates[0][0]
y0 = coordinates[0][1]
x2 = coordinates[2][0]
y2 = coordinates[2][1]
return [x0, y0, x2, y2]
def get_coordinates(self, coordinates):
"""
@param coordinates:列表
@return:列表
"""
if len(coordinates) > 1:
return coordinates
elif len(coordinates) == 1:
return self.get_coordinates(coordinates[0])
def read_json(self, file_path):
"""
@param file_path:json文件路径
@return:json内容
"""
file_json = None
try:
with open(file_path, mode='r', encoding="utf-8") as f:
file_json = load(f)
except Exception as e:
self.logger.error(f"读取{file_path}失败:{e}")
finally:
return file_json
def generate_json(self, content_dic, file_path):
"""
@param content_dic:字典内容
@param file_path:生成的json文件路径
"""
try:
with open(file_path, "w", encoding="utf-8") as f:
f.write(dumps(content_dic, indent=4, ensure_ascii=False))
except Exception as e:
self.logger.error(f"生成{file_path}失败:{e}")
if __name__ == '__main__':
while True:
print("**** start ****")
input_folder = input("请输入平台标注结果文件夹:").strip("\"")
output_folder = input("请输入结果保存文件夹:").strip("\"")
# input_folder = r"F:\Task\2022\01\廉博\运动人25点标注\data"
# output_folder = r"F:\Task\2022\01\廉博\运动物25点标注\result"
pd = ProcessData(src=input_folder, dst=output_folder)
if pd.check_path_exist():
pd.start()
else:
continue
print("**** finished ****", end="\r")
c = input("请输入q(不区分大小写)回车退出,按其他任意键回车继续:")
if c.lower() == "q":
break