153 lines
5.3 KiB
Python
153 lines
5.3 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
Created on Tue May 21 15:25:23 2024
|
||
|
||
@author: ieemoo-zl003
|
||
"""
|
||
|
||
import os
|
||
import numpy as np
|
||
|
||
# 替换为你的目录路径
|
||
files_path = 'D:/Project/ieemoo/kmeans/comparisonData/deletedBarcode_10_0709_am/err_pair/6902088146356/20240709-105804_6902088146356'
|
||
|
||
|
||
def str_to_float_arr(s):
|
||
# 移除字符串末尾的逗号(如果存在)
|
||
if s.endswith(','):
|
||
s = s[:-1]
|
||
|
||
# 使用split()方法分割字符串,然后将每个元素转化为float
|
||
float_array = [float(x) for x in s.split(",")]
|
||
return float_array
|
||
|
||
|
||
def extract_tracker_input_boxes_feats(file_name):
|
||
framesId = []
|
||
boxes = []
|
||
feats = []
|
||
|
||
|
||
frame_id = 0
|
||
with open(file_name, 'r', encoding='utf-8') as file:
|
||
for line in file:
|
||
line = line.strip() # 去除行尾的换行符和可能的空白字符
|
||
|
||
# 跳过空行
|
||
if not line:
|
||
continue
|
||
if line.find("frameId") >= 0:
|
||
frame_id = line[line.find("frameId:") + 8:].strip()
|
||
|
||
# 检查是否以'box:'或'feat:'开始
|
||
if line.find("box:") >= 0 and line.find("output_box:") < 0:
|
||
boxes.append(line[line.find("box:") + 4:].strip()) # 去掉'box:'并去除可能的空白字符
|
||
framesId.append(frame_id)
|
||
|
||
if line.find("feat:") >= 0:
|
||
feats.append(line[line.find("feat:") + 5:].strip()) # 去掉'box:'并去除可能的空白字符
|
||
|
||
return boxes, feats, framesId
|
||
|
||
|
||
def find_string_in_array(arr, target):
|
||
"""
|
||
在字符串数组中找到目标字符串对应的行(索引)。
|
||
|
||
参数:
|
||
arr -- 字符串数组
|
||
target -- 要查找的目标字符串
|
||
|
||
返回:
|
||
目标字符串在数组中的索引。如果未找到,则返回-1。
|
||
"""
|
||
parts = target.split(',')
|
||
box_substrings = ','.join(parts[:4])
|
||
conf_substring = ','.join(parts[5:6])
|
||
for i, s in enumerate(arr):
|
||
if s.find(box_substrings) >= 0 and s.find(conf_substring[:7]) >= 0:
|
||
return i
|
||
return -1
|
||
|
||
|
||
def extract_tracker_output_boxes_feats(read_file_name):
|
||
input_boxes, input_feats, framesId = extract_tracker_input_boxes_feats(read_file_name)
|
||
|
||
boxes = []
|
||
feats = []
|
||
with open(read_file_name, 'r', encoding='utf-8') as file:
|
||
for line in file:
|
||
line = line.strip() # 去除行尾的换行符和可能的空白字符
|
||
|
||
# 跳过空行
|
||
if not line:
|
||
continue
|
||
|
||
# 检查是否以'output_box:'开始
|
||
if line.find("output_box:") >= 0:
|
||
boxes_str = line[line.find("output_box:") + 11:].strip()
|
||
boxes.append(boxes_str) # 去掉'output_box:'并去除可能的空白字符
|
||
index = find_string_in_array(input_boxes, boxes_str)
|
||
feat_f = str_to_float_arr(input_feats[index])
|
||
norm_f = np.linalg.norm(feat_f)
|
||
feat_f = feat_f / norm_f
|
||
feats.append(feat_f)
|
||
return input_boxes, input_feats, boxes, feats, framesId
|
||
|
||
|
||
def extract_tracking_output_boxes_feats(read_file_name):
|
||
tracker_boxes, tracker_feats, input_boxes, input_feats, framesId = extract_tracker_output_boxes_feats(
|
||
read_file_name)
|
||
boxes = []
|
||
feats = []
|
||
boxes_frames_id = []
|
||
tracking_flag = False
|
||
tracking_num_cnt = 0
|
||
with open(read_file_name, 'r', encoding='utf-8') as file:
|
||
for line in file:
|
||
line = line.strip() # 去除行尾的换行符和可能的空白字符
|
||
|
||
# 跳过空行
|
||
if not line:
|
||
continue
|
||
|
||
if tracking_flag:
|
||
if line.find("tracking_") >= 0:
|
||
tracking_flag = False
|
||
tracking_num_cnt = tracking_num_cnt + 1
|
||
else:
|
||
boxes.append(line)
|
||
index = find_string_in_array(input_boxes, line)
|
||
feats.append(input_feats[index])
|
||
|
||
if tracking_num_cnt == 0:
|
||
index = find_string_in_array(tracker_boxes, line)
|
||
boxes_frames_id.append(framesId[index])
|
||
# 检查是否以tracking_'开始
|
||
if line.find("tracking_") >= 0:
|
||
tracking_flag = True
|
||
|
||
return tracker_boxes, tracker_feats, input_boxes, input_feats, boxes, feats, boxes_frames_id
|
||
|
||
def find_index_feats(files_path):
|
||
# 遍历目录下的所有文件和目录
|
||
all_boxes, boboxes_frames_ids, Boxes, framesIds =[],[],[],[]
|
||
for filename in os.listdir(files_path):
|
||
# 构造完整的文件路径
|
||
file_path = os.path.join(files_path, filename)
|
||
# 判断是否是文件
|
||
if os.path.isfile(file_path):
|
||
# 打开文件
|
||
if filename.endswith('data') and (not 'tracking' in filename):
|
||
tracker_boxes, tracker_feats, input_boxes, input_feats, boxes, feats, boxes_frames_id = extract_tracking_output_boxes_feats(file_path)
|
||
box, feats, framesId = extract_tracker_input_boxes_feats(file_path)
|
||
Boxes += box
|
||
framesIds += framesId
|
||
all_boxes += boxes[:len(boxes_frames_id)]
|
||
boboxes_frames_ids += boxes_frames_id
|
||
# print(all_boxes)
|
||
# print(boboxes_frames_ids)
|
||
return all_boxes, boboxes_frames_ids, tracker_boxes, Boxes, framesIds
|
||
|
||
if __name__ == '__main__':
|
||
find_index_feats(files_path) |