How can I normalize the num_of_lanes values, min num of lanes is 1 and the max is 6, I was able to have the data created but how can I have the num_of_lanes normalized instead of having them from 1-6 based on their values. I would like to have the values displayed as they are saved in .pkl and .txt files but having the normalized values instead.
Here's my code
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")