如何标准化 num_of_lanes 值,最小车道数为 1,最大车道数为 6,我能够创建数据,但如何才能标准化 num_of_lanes,而不是根据其值将它们从 1 到 6 进行标准化。我希望显示保存在 .pkl 和 .txt 文件中的值,但改为标准化值。
这是我的代码
import os
import xml.etree.ElementTree as ET
import pickle
# Define the Directories
annotations_dir =
attributes_dir =
output_dir =
# Create subdirectories for pkl and txt within the output directory
output_dir_pkl = os.path.join(output_dir, 'pkl')
output_dir_txt = os.path.join(output_dir, 'txt')
# Check if output subfolders exist, if not, create them
if not os.path.exists(output_dir_pkl):
os.makedirs(output_dir_pkl)
if not os.path.exists(output_dir_txt):
os.makedirs(output_dir_txt)
# Get a sorted list of XML files to maintain order in the annotations directory
xml_files = sorted([f for f in os.listdir(annotations_dir) if f.endswith('.xml')])
# Get a sorted list of attribute XML files to maintain order
num_of_lanes_files = sorted([f for f in os.listdir(attributes_dir) if f.endswith('_attributes.xml')])
# Parse the num_of_lanes information from the attribute XML files
num_of_lanes_mapping = {}
default_num_of_lanes_per_video = {}
for num_of_lanes_file in num_of_lanes_files:
num_of_lanes_tree = ET.parse(os.path.join(attributes_dir, num_of_lanes_file))
num_of_lanes_root = num_of_lanes_tree.getroot()
video_name = num_of_lanes_file.split('_attributes')[0]
default_num_of_lanes = None
for pedestrian in num_of_lanes_root.findall('.//pedestrian'):
ped_id = pedestrian.get('id')
old_id = pedestrian.get('old_id')
num_of_lanes = pedestrian.get('num_lanes')
num_of_lanes_mapping[(video_name, ped_id, old_id)] = num_of_lanes
if default_num_of_lanes is None:
default_num_of_lanes = num_of_lanes
default_num_of_lanes_per_video[video_name] = default_num_of_lanes
# Process main XML files
for filename in xml_files:
file_path = os.path.join(annotations_dir, filename)
tree = ET.parse(file_path)
root = tree.getroot()
video_name = root.find('.//name').text if root.find('.//name') is not None else filename.split('.xml')[0]
num_of_lanes_data = {}
for track in root.findall('.//track'):
for box in track.findall('box'):
frame_num = str(box.get('frame')).zfill(5)
ped_id = box.find("./attribute[@name='id']").text
old_id = box.find("./attribute[@name='old_id']").text
key = (video_name, ped_id, old_id)
ped_num_of_lanes = num_of_lanes_mapping.get(key, default_num_of_lanes_per_video[video_name])
frame_id = f"{video_name}_{frame_num}_{ped_id}"
num_of_lanes_data[frame_id] = {'num_lanes': ped_num_of_lanes}
# Save data in pkl and txt files
pkl_filename = f"{video_name}_num_of_lanes_data.pkl"
with open(os.path.join(output_dir_pkl, pkl_filename), 'wb') as pkl_file:
pickle.dump(num_of_lanes_data, pkl_file)
txt_filename = f"{video_name}_num_of_lanes_data.txt"
with open(os.path.join(output_dir_txt, txt_filename), 'w') as txt_file:
for frame_id, data in num_of_lanes_data.items():
txt_file.write(f"{frame_id}: num_of_lanes: {data['num_lanes']}\n")
要标准化
num_of_lanes
值,您可以使用以下公式:
归一化值 = 值-最小值/(最大值-最小值)
import os
import xml.etree.ElementTree as ET
import pickle
# Define the Directories
annotations_dir =
attributes_dir =
output_dir =
# Define min and max values for normalization
MIN_NUM_LANES = 1
MAX_NUM_LANES = 6
# Check if output subfolders exist, if not, create them
output_dir_pkl = os.path.join(output_dir, 'pkl')
output_dir_txt = os.path.join(output_dir, 'txt')
# Ensure the output subdirectories exist
os.makedirs(output_dir_pkl, exist_ok=True)
os.makedirs(output_dir_txt, exist_ok=True)
# Get a sorted list of attribute XML files to maintain order
num_of_lanes_files = sorted([f for f in os.listdir(attributes_dir) if f.endswith('_attributes.xml')])
for num_of_lanes_file in num_of_lanes_files:
num_of_lanes_tree = ET.parse(os.path.join(attributes_dir, num_of_lanes_file))
num_of_lanes_root = num_of_lanes_tree.getroot()
video_name = num_of_lanes_file.split('_attributes')[0]
# Assuming you extract num_of_lanes value here; Example:
# num_of_lanes = int(num_of_lanes_root.find('SomeXPath').text)
# For demonstration, let's assume num_of_lanes is directly available
normalized_num_of_lanes = {} # Dictionary to store normalized values
for child in num_of_lanes_root:
# Example of extracting num_of_lanes, replace with actual extraction logic
num_of_lanes = int(child.text) # Example placeholder
normalized_value = (num_of_lanes - MIN_NUM_LANES) / (MAX_NUM_LANES - MIN_NUM_LANES)
normalized_num_of_lanes[child.tag] = normalized_value
# Save normalized values to PKL
pkl_filename = os.path.join(output_dir_pkl, f"{video_name}.pkl")
with open(pkl_filename, 'wb') as pkl_file:
pickle.dump(normalized_num_of_lanes, pkl_file)
# Save normalized values to TXT
txt_filename = os.path.join(output_dir_txt, f"{video_name}.txt")
with open(txt_filename, 'w') as txt_file:
for key, value in normalized_num_of_lanes.items():
txt_file.write(f"{key}: {value}\n")
print("Normalization and saving completed.")