modified for site test
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
tracking/utils/__pycache__/readData.cpython-39.pyc
Normal file
BIN
tracking/utils/__pycache__/readData.cpython-39.pyc
Normal file
Binary file not shown.
BIN
tracking/utils/__pycache__/read_data.cpython-39.pyc
Normal file
BIN
tracking/utils/__pycache__/read_data.cpython-39.pyc
Normal file
Binary file not shown.
BIN
tracking/utils/__pycache__/read_pipeline_data.cpython-39.pyc
Normal file
BIN
tracking/utils/__pycache__/read_pipeline_data.cpython-39.pyc
Normal file
Binary file not shown.
@ -80,15 +80,32 @@ def plot_frameID_y2(vts):
|
||||
return plt
|
||||
|
||||
|
||||
def draw_all_trajectories(vts, edgeline, save_dir, filename):
|
||||
def draw_all_trajectories(vts, edgeline, save_dir, file, draw5p=False):
|
||||
'''显示四种类型结果'''
|
||||
file, ext = os.path.splitext(filename)
|
||||
# file, ext = os.path.splitext(filename)
|
||||
# edgeline = cv2.imread("./shopcart/cart_tempt/edgeline.png")
|
||||
# edgeline2 = edgeline1.copy()
|
||||
# edgeline = np.concatenate((edgeline1, edgeline2), exis = 1)
|
||||
|
||||
'''1. tracks 5点轨迹'''
|
||||
trackpth = save_dir.parent /Path("trajectory")/ Path(f"{file}")
|
||||
if not isinstance(save_dir, Path): save_dir = Path(save_dir)
|
||||
|
||||
''' all tracks 中心轨迹'''
|
||||
img1, img2 = edgeline.copy(), edgeline.copy()
|
||||
img1 = drawTrack(vts.tracks, img1)
|
||||
img2 = drawTrack(vts.Residual, img2)
|
||||
|
||||
img = np.concatenate((img1, img2), axis = 1)
|
||||
H, W = img.shape[:2]
|
||||
cv2.line(img, (int(W/2), 0), (int(W/2), H), (128, 255, 128), 2)
|
||||
|
||||
imgpth = save_dir.joinpath(f"{file}_show.png")
|
||||
cv2.imwrite(str(imgpth), img)
|
||||
|
||||
if not draw5p:
|
||||
return
|
||||
|
||||
''' tracks 5点轨迹'''
|
||||
trackpth = save_dir / Path("trajectory") / Path(f"{file}")
|
||||
if not trackpth.exists():
|
||||
trackpth.mkdir(parents=True, exist_ok=True)
|
||||
for track in vts.tracks:
|
||||
@ -106,23 +123,9 @@ def draw_all_trajectories(vts, edgeline, save_dir, filename):
|
||||
|
||||
pth = trackpth.joinpath(f"{track.tid}_.png")
|
||||
cv2.imwrite(str(pth), img)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
'''2. all tracks 中心轨迹'''
|
||||
img1, img2 = edgeline.copy(), edgeline.copy()
|
||||
|
||||
img1 = drawTrack(vts.tracks, img1)
|
||||
img2 = drawTrack(vts.Residual, img2)
|
||||
|
||||
img = np.concatenate((img1, img2), axis = 1)
|
||||
H, W = img.shape[:2]
|
||||
cv2.line(img, (int(W/2), 0), (int(W/2), H), (128, 255, 128), 2)
|
||||
|
||||
pth = save_dir.joinpath(f"{file}_show.png")
|
||||
cv2.imwrite(str(pth), img)
|
||||
|
||||
# =============================================================================
|
||||
# '''3. moving tracks 中心轨迹'''
|
||||
# filename2 = f"{file}_show_r.png"
|
||||
@ -134,13 +137,11 @@ def draw_all_trajectories(vts, edgeline, save_dir, filename):
|
||||
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# '''5. tracks 时序:trajmin、trajmax、arearate、incartrate'''
|
||||
# plt = drawtracefeat(vts)
|
||||
# pth = save_dir.joinpath(f"{file}_x.png")
|
||||
# plt.savefig(pth)
|
||||
# plt.close('all')
|
||||
# =============================================================================
|
||||
'''5. tracks 时序:trajmin、trajmax、arearate、incartrate'''
|
||||
# plt = drawtracefeat(vts)
|
||||
# pth = save_dir.joinpath(f"{file}_x.png")
|
||||
# plt.savefig(pth)
|
||||
# plt.close('all')
|
||||
|
||||
|
||||
|
||||
|
@ -15,17 +15,18 @@ def readDict(boxes, TracksDict):
|
||||
for i in range(boxes.shape[0]):
|
||||
tid, fid, bid = int(boxes[i, 4]), int(boxes[i, 7]), int(boxes[i, 8])
|
||||
|
||||
feat = TracksDict[f"frame_{fid}"]["feats"][bid]
|
||||
img = TracksDict[f"frame_{fid}"]["imgs"][bid]
|
||||
trackdict = TracksDict[f"frame_{fid}"]
|
||||
if "feats" in trackdict:
|
||||
feat = trackdict["feats"][bid]
|
||||
feats.append(feat)
|
||||
|
||||
box = TracksDict[f"frame_{fid}"]["boxes"][bid]
|
||||
if "boxes" in trackdict:
|
||||
box = trackdict["boxes"][bid]
|
||||
assert (box[:4].astype(int) == boxes[i, :4].astype(int)).all(), f"Please check: frame_{fid}"
|
||||
|
||||
assert (box[:4].astype(int) == boxes[i, :4].astype(int)).all(), f"Please check: frame_{fid}"
|
||||
|
||||
feats.append(feat)
|
||||
|
||||
# img = TracksDict[fid][f'{bid}_img']
|
||||
# cv2.imwrite(f'./data/imgs/{tid}_{fid}_{bid}.png', img)
|
||||
if "imgs" in trackdict:
|
||||
img = trackdict["imgs"][bid]
|
||||
cv2.imwrite(f'./data/imgs/{tid}_{fid}_{bid}.png', img)
|
||||
|
||||
return np.asarray(feats, dtype=np.float32)
|
||||
|
||||
@ -59,13 +60,12 @@ def track_equal_track(atrack, btrack):
|
||||
''' 2. 轨迹特征相似度判断'''
|
||||
feat = np.concatenate((afeat, bfeat), axis=0)
|
||||
|
||||
emb_simil = 1-np.maximum(0.0, cdist(feat, feat, 'cosine'))
|
||||
emb_ = 1-cdist(np.mean(afeat, axis=0)[None, :], np.mean(bfeat, axis=0)[None, :], 'cosine')
|
||||
emb_simil = 1 - np.maximum(0.0, cdist(feat, feat, 'cosine'))
|
||||
emb_ = 1 - np.maximum(0.0, cdist(np.mean(afeat, axis=0)[None, :], np.mean(bfeat, axis=0)[None, :], 'cosine'))/2
|
||||
|
||||
if emb_[0, 0]<0.66:
|
||||
return False
|
||||
|
||||
|
||||
''' 3. 轨迹空间iou'''
|
||||
alabel = np.array([0] * afids.size, dtype=np.int_)
|
||||
blabel = np.array([1] * bfids.size, dtype=np.int_)
|
||||
@ -93,7 +93,7 @@ def track_equal_track(atrack, btrack):
|
||||
|
||||
af, bf = afeat[a, :], bfeat[b, :]
|
||||
|
||||
emb_ab = 1-cdist(af[None, :], bf[None, :], 'cosine')
|
||||
emb_ab = 1 - np.maximum(0.0, cdist(af[None, :], bf[None, :], 'cosine'))
|
||||
|
||||
|
||||
xa1, ya1 = abox[0] - abox[2]/2, abox[1] - abox[3]/2
|
||||
@ -113,7 +113,22 @@ def track_equal_track(atrack, btrack):
|
||||
|
||||
ious.append(inter/union)
|
||||
embs.append(emb_ab[0, 0])
|
||||
|
||||
|
||||
''' 4. 和同一手部关联,如何将该代码和 iou 部分相融合,需进一步完善'''
|
||||
# ahands = np.array(atrack.Hands)
|
||||
# bhands = np.array(btrack.Hands)
|
||||
# ahids = ahands[:, 0]
|
||||
# bhids = bhands[:, 0]
|
||||
# interhid = set(ahids).intersection(set(bhids))
|
||||
# for hid in interhid:
|
||||
# aidx = ahands[:, 0] == hid
|
||||
# bidx = bhands[:, 0] == hid
|
||||
|
||||
# ahfids = ahids[aidx, 1]
|
||||
# bhfids = bhids[bidx, 1]
|
||||
|
||||
|
||||
|
||||
|
||||
cont = False if len(interfid) else True # fid 无交集
|
||||
cont1 = all(emb > 0.5 for emb in embs)
|
||||
|
@ -1,4 +1,4 @@
|
||||
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||||
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||||
|
||||
import contextlib
|
||||
import math
|
||||
@ -284,5 +284,59 @@ def boxing_img(det, img, line_width=3):
|
||||
imgx = annotator.result()
|
||||
|
||||
return imgx
|
||||
|
||||
def draw_tracking_boxes(imgs, tracks, scale=2):
|
||||
'''tracks: [x1, y1, x2, y2, track_id, score, cls, frame_index, box_index]
|
||||
0 1 2 3 4 5 6 7 8
|
||||
关键:
|
||||
(1) imgs中的次序和 track 中的 fid 对应
|
||||
(2) img 尺度小对于xyxy减半
|
||||
|
||||
'''
|
||||
|
||||
def array2list(bboxes):
|
||||
track_fids = np.unique(bboxes[:, 7].astype(int))
|
||||
track_fids.sort()
|
||||
|
||||
lboxes = []
|
||||
for f_id in track_fids:
|
||||
# print(f"The ID is: {t_id}")
|
||||
idx = np.where(bboxes[:, 7] == f_id)[0]
|
||||
box = bboxes[idx, :]
|
||||
lboxes.append(box)
|
||||
|
||||
assert len(set(box[:, 4])) == len(box), "Please check!!!"
|
||||
|
||||
return lboxes
|
||||
|
||||
bboxes = array2list(tracks)
|
||||
|
||||
if len(bboxes)!=len(imgs):
|
||||
return []
|
||||
|
||||
subimgs = []
|
||||
for i, boxes in enumerate(bboxes):
|
||||
annotator = Annotator(imgs[i].copy())
|
||||
for *xyxy, tid, conf, cls, fid, bid in boxes:
|
||||
label = f'id:{int(tid)}_{int(cls)}_{conf:.2f}'
|
||||
|
||||
if cls==0:
|
||||
color = colors(int(cls), True)
|
||||
elif tid>0 and cls!=0:
|
||||
color = colors(int(tid), True)
|
||||
else:
|
||||
color = colors(19, True) # 19为调色板的最后一个元素
|
||||
|
||||
pt2 = [p/scale for p in xyxy]
|
||||
annotator.box_label(pt2, label, color=color)
|
||||
|
||||
img = annotator.result()
|
||||
subimgs.append(img)
|
||||
|
||||
return subimgs
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -12,10 +12,12 @@ class Boxes:
|
||||
"""Initialize the Boxes class."""
|
||||
if boxes.ndim == 1:
|
||||
boxes = boxes[None, :]
|
||||
n = boxes.shape[-1]
|
||||
assert n in (6, 7, 8), f'expected `n` in [6, 7], but got {n}' # xyxyb, track_id, conf, cls
|
||||
m, n = boxes.shape
|
||||
assert n in (6, 7), f'expected `n` in [6, 7], but got {n}' # xyxy, track_id, conf, cls
|
||||
|
||||
'''对每一个box进行编号,利用该编号可以索引对应 feature'''
|
||||
self.data = np.concatenate([boxes[:, :4], np.arange(m).reshape(-1, 1), boxes[:, 4:]], axis=-1)
|
||||
|
||||
self.data = boxes
|
||||
self.orig_shape = orig_shape
|
||||
|
||||
def cpu(self):
|
||||
@ -30,10 +32,9 @@ class Boxes:
|
||||
"""Return the boxes in xyxy format."""
|
||||
return self.data[:, :4]
|
||||
|
||||
|
||||
@property
|
||||
def xyxyb(self):
|
||||
"""Return the boxes in xyxyb format."""
|
||||
"""Return the boxes in xyxyb format."""
|
||||
return self.data[:, :5]
|
||||
|
||||
@property
|
||||
|
236
tracking/utils/read_data.py
Normal file
236
tracking/utils/read_data.py
Normal file
@ -0,0 +1,236 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Fri Jul 5 13:59:21 2024
|
||||
func: extract_data()
|
||||
读取 Pipeline 各模块的数据,在 read_pipeline_data.py(马晓慧)的基础上完成接口改造
|
||||
|
||||
@author: ym
|
||||
"""
|
||||
import numpy as np
|
||||
import re
|
||||
import os
|
||||
|
||||
|
||||
|
||||
|
||||
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 find_samebox_in_array(arr, target):
|
||||
|
||||
for i, st in enumerate(arr):
|
||||
if st[:4] == target[:4]:
|
||||
return i
|
||||
return -1
|
||||
|
||||
|
||||
def extract_data(datapath):
|
||||
bboxes, ffeats = [], []
|
||||
|
||||
trackerboxes = np.empty((0, 9), dtype=np.float64)
|
||||
trackerfeats = np.empty((0, 256), dtype=np.float64)
|
||||
|
||||
boxes, feats, tboxes, tfeats = [], [], [], []
|
||||
with open(datapath, 'r', encoding='utf-8') as lines:
|
||||
for line in lines:
|
||||
line = line.strip() # 去除行尾的换行符和可能的空白字符
|
||||
if not line: # 跳过空行
|
||||
continue
|
||||
|
||||
if line.find("CameraId")>=0:
|
||||
if len(boxes): bboxes.append(np.array(boxes))
|
||||
if len(feats): ffeats.append(np.array(feats))
|
||||
if len(tboxes):
|
||||
trackerboxes = np.concatenate((trackerboxes, np.array(tboxes)))
|
||||
if len(tfeats):
|
||||
trackerfeats = np.concatenate((trackerfeats, np.array(tfeats)))
|
||||
|
||||
boxes, feats, tboxes, tfeats = [], [], [], []
|
||||
|
||||
if line.find("box:") >= 0 and line.find("output_box:") < 0:
|
||||
box = line[line.find("box:") + 4:].strip()
|
||||
boxes.append(str_to_float_arr(box))
|
||||
|
||||
if line.find("feat:") >= 0:
|
||||
feat = line[line.find("feat:") + 5:].strip()
|
||||
feats.append(str_to_float_arr(feat))
|
||||
|
||||
if line.find("output_box:") >= 0:
|
||||
box = str_to_float_arr(line[line.find("output_box:") + 11:].strip())
|
||||
tboxes.append(box) # 去掉'output_box:'并去除可能的空白字符
|
||||
index = find_samebox_in_array(boxes, box)
|
||||
if index >= 0:
|
||||
# feat_f = str_to_float_arr(input_feats[index])
|
||||
feat_f = feats[index]
|
||||
norm_f = np.linalg.norm(feat_f)
|
||||
feat_f = feat_f / norm_f
|
||||
tfeats.append(feat_f)
|
||||
|
||||
if len(boxes): bboxes.append(np.array(boxes))
|
||||
if len(feats): ffeats.append(np.array(feats))
|
||||
if len(tboxes): trackerboxes = np.concatenate((trackerboxes, np.array(tboxes)))
|
||||
if len(tfeats): trackerfeats = np.concatenate((trackerfeats, np.array(tfeats)))
|
||||
|
||||
assert(len(bboxes)==len(ffeats)), "Error at Yolo output!"
|
||||
assert(len(trackerboxes)==len(trackerfeats)), "Error at tracker output!"
|
||||
|
||||
tracker_feat_dict = {}
|
||||
for i in range(len(trackerboxes)):
|
||||
tid, fid, bid = int(trackerboxes[i, 4]), int(trackerboxes[i, 7]), int(trackerboxes[i, 8])
|
||||
if f"frame_{fid}" not in tracker_feat_dict:
|
||||
tracker_feat_dict[f"frame_{fid}"]= {"feats": {}}
|
||||
tracker_feat_dict[f"frame_{fid}"]["feats"].update({bid: trackerfeats[i, :]})
|
||||
|
||||
|
||||
boxes, trackingboxes= [], []
|
||||
tracking_flag = False
|
||||
with open(datapath, 'r', encoding='utf-8') as lines:
|
||||
for line in lines:
|
||||
line = line.strip() # 去除行尾的换行符和可能的空白字符
|
||||
if not line: # 跳过空行
|
||||
continue
|
||||
if tracking_flag:
|
||||
if line.find("tracking_") >= 0:
|
||||
tracking_flag = False
|
||||
else:
|
||||
box = str_to_float_arr(line)
|
||||
boxes.append(box)
|
||||
if line.find("tracking_") >= 0:
|
||||
tracking_flag = True
|
||||
if len(boxes):
|
||||
trackingboxes.append(np.array(boxes))
|
||||
boxes = []
|
||||
|
||||
if len(boxes):
|
||||
trackingboxes.append(np.array(boxes))
|
||||
|
||||
tracking_feat_dict = {}
|
||||
for i, boxes in enumerate(trackingboxes):
|
||||
for box in boxes:
|
||||
tid, fid, bid = int(box[4]), int(box[7]), int(box[8])
|
||||
if f"track_{tid}" not in tracking_feat_dict:
|
||||
tracking_feat_dict[f"track_{tid}"]= {"feats": {}}
|
||||
tracking_feat_dict[f"track_{tid}"]["feats"].update({f"{fid}_{bid}": tracker_feat_dict[f"frame_{fid}"]["feats"][bid]})
|
||||
|
||||
return bboxes, ffeats, trackerboxes, tracker_feat_dict, trackingboxes, tracking_feat_dict
|
||||
|
||||
def read_tracking_output(filepath):
|
||||
boxes = []
|
||||
feats = []
|
||||
with open(filepath, 'r', encoding='utf-8') as file:
|
||||
for line in file:
|
||||
line = line.strip() # 去除行尾的换行符和可能的空白字符
|
||||
|
||||
if not line:
|
||||
continue
|
||||
|
||||
if line.endswith(','):
|
||||
line = line[:-1]
|
||||
|
||||
data = np.array([float(x) for x in line.split(",")])
|
||||
if data.size == 9:
|
||||
boxes.append(data)
|
||||
if data.size == 256:
|
||||
feats.append(data)
|
||||
|
||||
return np.array(boxes), np.array(feats)
|
||||
|
||||
|
||||
def read_deletedBarcode_file(filePth):
|
||||
with open(filePth, 'r', encoding='utf-8') as f:
|
||||
lines = f.readlines()
|
||||
|
||||
split_flag, all_list = False, []
|
||||
dict, barcode_list, similarity_list = {}, [], []
|
||||
|
||||
clean_lines = [line.strip().replace("'", '').replace('"', '') for line in lines]
|
||||
for line in clean_lines:
|
||||
stripped_line = line.strip()
|
||||
if not stripped_line:
|
||||
if len(barcode_list): dict['barcode'] = barcode_list
|
||||
if len(similarity_list): dict['similarity'] = similarity_list
|
||||
if len(dict): all_list.append(dict)
|
||||
|
||||
split_flag = False
|
||||
dict, barcode_list, similarity_list = {}, [], []
|
||||
continue
|
||||
|
||||
# print(line)
|
||||
label = line.split(':')[0]
|
||||
value = line.split(':')[1]
|
||||
if label == 'SeqDir':
|
||||
dict['SeqDir'] = value
|
||||
if label == 'Deleted':
|
||||
dict['Deleted'] = value
|
||||
if label == 'List':
|
||||
split_flag = True
|
||||
continue
|
||||
if split_flag:
|
||||
barcode_list.append(label)
|
||||
similarity_list.append(value)
|
||||
|
||||
if len(barcode_list): dict['barcode'] = barcode_list
|
||||
if len(similarity_list): dict['similarity'] = similarity_list
|
||||
if len(dict): all_list.append(dict)
|
||||
return all_list
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
files_path = 'D:/contrast/dataset/1_to_n/709/20240709-112658_6903148351833/'
|
||||
|
||||
# 遍历目录下的所有文件和目录
|
||||
for filename in os.listdir(files_path):
|
||||
filename = '1_track.data'
|
||||
file_path = os.path.join(files_path, filename)
|
||||
if os.path.isfile(file_path) and filename.find("track.data")>0:
|
||||
extract_data(file_path)
|
||||
|
||||
print("Done")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
250
tracking/utils/read_pipeline_data.py
Normal file
250
tracking/utils/read_pipeline_data.py
Normal file
@ -0,0 +1,250 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Tue May 21 15:25:23 2024
|
||||
读取 Pipeline 各模块的数据,主代码由 马晓慧 完成
|
||||
|
||||
@author: ieemoo-zl003
|
||||
"""
|
||||
|
||||
import os
|
||||
import numpy as np
|
||||
|
||||
# 替换为你的目录路径
|
||||
files_path = 'D:/contrast/dataset/1_to_n/709/20240709-112658_6903148351833/'
|
||||
|
||||
def str_to_float_arr(s):
|
||||
# 移除字符串末尾的逗号(如果存在)
|
||||
if s.endswith(','):
|
||||
s = s[:-1]
|
||||
|
||||
# 使用split()方法分割字符串,然后将每个元素转化为float
|
||||
float_array = np.array([float(x) for x in s.split(",")])
|
||||
return float_array
|
||||
|
||||
def extract_tracker_input_boxes_feats(file_name):
|
||||
boxes = []
|
||||
feats = []
|
||||
with open(file_name, 'r', encoding='utf-8') as file:
|
||||
for line in file:
|
||||
line = line.strip() # 去除行尾的换行符和可能的空白字符
|
||||
|
||||
# 跳过空行
|
||||
if not line:
|
||||
continue
|
||||
|
||||
# 检查是否以'box:'或'feat:'开始
|
||||
if line.find("box:") >= 0 and line.find("output_box:") < 0:
|
||||
box = line[line.find("box:") + 4:].strip()
|
||||
boxes.append(str_to_float_arr(box)) # 去掉'box:'并去除可能的空白字符
|
||||
|
||||
if line.find("feat:") >= 0:
|
||||
feat = line[line.find("feat:") + 5:].strip()
|
||||
feats.append(str_to_float_arr(feat)) # 去掉'box:'并去除可能的空白字符
|
||||
|
||||
return np.array(boxes), np.array(feats)
|
||||
|
||||
def find_string_in_array(arr, target):
|
||||
"""
|
||||
在字符串数组中找到目标字符串对应的行(索引)。
|
||||
|
||||
参数:
|
||||
arr -- 字符串数组
|
||||
target -- 要查找的目标字符串
|
||||
|
||||
返回:
|
||||
目标字符串在数组中的索引。如果未找到,则返回-1。
|
||||
"""
|
||||
tg = [float(t) for k, t in enumerate(target.split(',')) if k<4][:4]
|
||||
for i, st in enumerate(arr):
|
||||
st = [float(s) for k, s in enumerate(target.split(',')) if k<4][:4]
|
||||
|
||||
if st == tg:
|
||||
return i
|
||||
|
||||
# if st[:20] == target[:20]:
|
||||
# return i
|
||||
return -1
|
||||
|
||||
def find_samebox_in_array(arr, target):
|
||||
|
||||
for i, st in enumerate(arr):
|
||||
if all(st[:4] == target[:4]):
|
||||
return i
|
||||
return -1
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def extract_tracker_output_boxes_feats(read_file_name):
|
||||
|
||||
input_boxes, input_feats = 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:
|
||||
box = str_to_float_arr(line[line.find("output_box:") + 11:].strip())
|
||||
boxes.append(box) # 去掉'output_box:'并去除可能的空白字符
|
||||
index = find_samebox_in_array(input_boxes, box)
|
||||
if index >= 0:
|
||||
# feat_f = str_to_float_arr(input_feats[index])
|
||||
feat_f = 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, np.array(boxes), np.array(feats)
|
||||
|
||||
def extract_tracking_output_boxes_feats(read_file_name):
|
||||
tracker_boxes, tracker_feats, input_boxes, input_feats = extract_tracker_output_boxes_feats(read_file_name)
|
||||
boxes = []
|
||||
feats = []
|
||||
|
||||
tracking_flag = False
|
||||
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
|
||||
else:
|
||||
box = str_to_float_arr(line)
|
||||
boxes.append(box)
|
||||
index = find_samebox_in_array(input_boxes, box)
|
||||
if index >= 0:
|
||||
feats.append(input_feats[index])
|
||||
# 检查是否以tracking_'开始
|
||||
if line.find("tracking_") >= 0:
|
||||
tracking_flag = True
|
||||
|
||||
assert(len(tracker_boxes)==len(tracker_feats)), "Error at Yolo output"
|
||||
assert(len(input_boxes)==len(input_feats)), "Error at tracker output"
|
||||
assert(len(boxes)==len(feats)), "Error at tracking output"
|
||||
|
||||
return tracker_boxes, tracker_feats, input_boxes, input_feats, np.array(boxes), np.array(feats)
|
||||
|
||||
def read_tracking_input(datapath):
|
||||
with open(datapath, 'r') as file:
|
||||
lines = file.readlines()
|
||||
|
||||
data = []
|
||||
for line in lines:
|
||||
data.append([s for s in line.split(',') if len(s)>=3])
|
||||
# data.append([float(s) for s in line.split(',') if len(s)>=3])
|
||||
|
||||
# data = np.array(data, dtype = np.float32)
|
||||
try:
|
||||
data = np.array(data, dtype = np.float32)
|
||||
except Exception as e:
|
||||
data = np.array([], dtype = np.float32)
|
||||
print('DataError for func: read_tracking_input()')
|
||||
|
||||
|
||||
return data
|
||||
|
||||
|
||||
|
||||
def read_tracker_input(datapath):
|
||||
with open(datapath, 'r') as file:
|
||||
lines = file.readlines()
|
||||
Videos = []
|
||||
FrameBoxes, FrameFeats = [], []
|
||||
boxes, feats = [], []
|
||||
|
||||
timestamp = []
|
||||
t1 = None
|
||||
for line in lines:
|
||||
if line.find('CameraId') >= 0:
|
||||
t = int(line.split(',')[1].split(':')[1])
|
||||
timestamp.append(t)
|
||||
|
||||
if len(boxes) and len(feats):
|
||||
FrameBoxes.append(np.array(boxes, dtype = np.float32))
|
||||
FrameFeats.append(np.array(feats, dtype = np.float32))
|
||||
boxes, feats = [], []
|
||||
|
||||
if t1 and t - t1 > 1e3:
|
||||
Videos.append((FrameBoxes, FrameFeats))
|
||||
FrameBoxes, FrameFeats = [], []
|
||||
t1 = int(line.split(',')[1].split(':')[1])
|
||||
|
||||
if line.find('box') >= 0:
|
||||
box = line.split(':', )[1].split(',')[:-1]
|
||||
boxes.append(box)
|
||||
|
||||
|
||||
if line.find('feat') >= 0:
|
||||
feat = line.split(':', )[1].split(',')[:-1]
|
||||
feats.append(feat)
|
||||
|
||||
FrameBoxes.append(np.array(boxes, dtype = np.float32))
|
||||
FrameFeats.append(np.array(feats, dtype = np.float32))
|
||||
Videos.append((FrameBoxes, FrameFeats))
|
||||
|
||||
# TimeStamp = np.array(timestamp, dtype = np.int64)
|
||||
# DimesDiff = np.diff((TimeStamp))
|
||||
# sorted_indices = np.argsort(TimeStamp)
|
||||
# TimeStamp_sorted = TimeStamp[sorted_indices]
|
||||
# DimesDiff_sorted = np.diff((TimeStamp_sorted))
|
||||
|
||||
return Videos
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
files_path = 'D:/contrast/dataset/1_to_n/709/20240709-112658_6903148351833/'
|
||||
|
||||
# 遍历目录下的所有文件和目录
|
||||
for filename in os.listdir(files_path):
|
||||
# 构造完整的文件路径
|
||||
file_path = os.path.join(files_path, filename)
|
||||
if os.path.isfile(file_path) and filename.find("track.data")>0:
|
||||
tracker_boxes, tracker_feats, tracking_boxes, tracking_feats, output_boxes, output_feats = extract_tracking_output_boxes_feats(file_path)
|
||||
|
||||
print("Done")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user