modify byte.trackerupdate, add list first_finded

This commit is contained in:
王庆刚
2024-05-25 18:35:53 +08:00
parent d6f3693d3f
commit dff029de20
17 changed files with 132 additions and 80 deletions

Binary file not shown.

View File

@ -206,7 +206,12 @@ def run(
handpose = hand_pose() handpose = hand_pose()
handlocals_dict = {} handlocals_dict = {}
boxes_and_imgs = [] boxes_and_imgs = []
BoxesFeats = []
track_boxes = np.empty((0, 9), dtype = np.float32) track_boxes = np.empty((0, 9), dtype = np.float32)
det_boxes = np.empty((0, 9), dtype = np.float32) det_boxes = np.empty((0, 9), dtype = np.float32)
@ -263,27 +268,40 @@ def run(
det = det.cpu().numpy() det = det.cpu().numpy()
det = np.concatenate([det[:, :4], np.arange(nd).reshape(-1, 1), det[:, 4:]], axis=-1) det = np.concatenate([det[:, :4], np.arange(nd).reshape(-1, 1), det[:, 4:]], axis=-1)
'''FeatFlag为相对于上一帧boxes当前boxes是否为静止的标志。''' #
# def static_estimate(box1, box2, TH1=8, TH2=12): def static_estimate(box1, box2, TH1=8, TH2=12):
# dij_abs = max(np.abs(box1 - box2)) dij_abs = max(np.abs(box1 - box2))
# dij_euc = max([np.linalg.norm((box1[:2] - box2[:2])), dij_euc = max([np.linalg.norm((box1[:2] - box2[:2])),
# np.linalg.norm((box1[2:4] - box2[2:4])) np.linalg.norm((box1[2:4] - box2[2:4]))
# ]) ])
# if dij_abs < TH1 and dij_euc < TH2: if dij_abs < TH1 and dij_euc < TH2:
# return True return True
# else: else:
# return False return False
# FeatFlag = [-1] * nd
# if len(boxes_and_imgs): nw = 3 # 向前递推检查的窗口大小
# detj = boxes_and_imgs[-1][0] nf = len(BoxesFeats) # 已经检测+特征提取的帧数
# frmj = boxes_and_imgs[-1][-1] feat_curr = [None] * nd # nd: 当前帧检测出的boxes数
# for ii in range(nd): for ii in range(nd):
# ## flag 中保存的是box索引 box = det[ii, :4]
# condt1 = frame-frmj==1
# flag = [idx for jj, idx in enumerate(detj[:, 4]) if condt1 and static_estimate(det[ii, :4], detj[jj, :4])] kk=1
# if len(flag) == 1: feat = None
# FeatFlag[ii] = flag[0] while kk <= nw and nf>=kk:
# boxes_and_imgs.append((det, im0, frame)) ki = -1 * kk
boxes_ = BoxesFeats[ki][0]
feats_ = BoxesFeats[ki][1]
flag = [jj for jj in range(len(boxes_)) if static_estimate(box, boxes_[jj, :4])]
if len(flag) == 1:
feat = feats_[flag[0]]
break
kk += 1
if feat is not None:
feat_curr[ii] = feat
## ================================================================ writed by WQG ## ================================================================ writed by WQG
@ -301,6 +319,9 @@ def run(
'''================== 1. 存储 dets/subimgs/features Dict =============''' '''================== 1. 存储 dets/subimgs/features Dict ============='''
imgs, features = inference_image(im0, tracks) imgs, features = inference_image(im0, tracks)
BoxesFeats.append((tracks, features))
imgdict = {} imgdict = {}
boxdict = {} boxdict = {}
featdict = {} featdict = {}
@ -504,17 +525,20 @@ def main_loop(opt):
if os.path.isdir(p): if os.path.isdir(p):
files = find_files_in_nested_dirs(p) files = find_files_in_nested_dirs(p)
# files = [#r"D:\datasets\ym\videos\标记视频\test_20240402-173935_6920152400975_back_174037372.mp4", # files = [r"D:\datasets\ym\广告板遮挡测试\8\6926636301004_20240508-175300_back_addGood_70f754088050_215_17327712807.mp4",
# r"D:\datasets\ym\videos\标记视频\test_20240402-173935_6920152400975_front_174037379.mp4"] # r"D:\datasets\ym\videos\标记视频\test_20240402-173935_6920152400975_back_174037372.mp4",
# r"D:\datasets\ym\videos\标记视频\test_20240402-173935_6920152400975_front_174037379.mp4",
# r"D:\datasets\ym\广告板遮挡测试\8\2500441577966_20240508-175946_front_addGood_70f75407b7ae_155_17788571404.mp4"
# ]
# files = [r"D:\datasets\ym\广告板遮挡测试\8\2500441577966_20240508-175946_front_addGood_70f75407b7ae_155_17788571404.mp4"] files = [r"D:\datasets\ym\videos\标记视频\test_20240402-173935_6920152400975_back_174037372.mp4"]
for file in files: for file in files:
optdict["source"] = file optdict["source"] = file
run(**optdict) run(**optdict)
k += 1 # k += 1
if k == 3: # if k == 3:
break # break
elif os.path.isfile(p): elif os.path.isfile(p):
optdict["source"] = p optdict["source"] = p
run(**vars(opt)) run(**vars(opt))

Binary file not shown.

View File

@ -216,7 +216,7 @@ class Track:
trajdist_min, trajdist_max, trajdist_min, trajdist_max,
trajlen_rate, trajdist_rate] trajlen_rate, trajdist_rate]
def compute_static_fids(self, det_y, STATIC_THRESH = 8): def pt_state_fids(self, det_y, STATIC_THRESH = 8):
''' '''
前摄时y一般选择为 box 的 y1 坐标,且需限定商品在购物车内。 前摄时y一般选择为 box 的 y1 坐标,且需限定商品在购物车内。
inputs inputs
@ -326,10 +326,13 @@ class doTracks:
idx = np.where(track_ids == t_id)[0] idx = np.where(track_ids == t_id)[0]
box = self.bboxes[idx, :] box = self.bboxes[idx, :]
assert len(set(box[:, 7])) == len(box), "Please check!!!"
lboxes.append(box) lboxes.append(box)
return lboxes return lboxes
'''
def classify(self): def classify(self):
tracks = self.tracks tracks = self.tracks
@ -354,10 +357,11 @@ class doTracks:
self.Static.extend(static_tracks) self.Static.extend(static_tracks)
'''剔除静止目标后的 tracks''' # 剔除静止目标后的 tracks
tracks = self.sub_tracks(tracks, static_tracks) tracks = self.sub_tracks(tracks, static_tracks)
return tracks return tracks
'''

View File

@ -51,40 +51,28 @@ class doBackTracks(doTracks):
# return lboxes # return lboxes
# ============================================================================= # =============================================================================
def classify(self): def classify(self):
''' '''功能:对 tracks 中元素分类 '''
功能:对 tracks 中元素分类
''' tracks = self.tracks
tracks = super().classify() # 提取手的frame_id并和动目标的frame_id 进行关联
hand_tracks = [t for t in tracks if t.cls==0]
self.Hands.extend(hand_tracks)
# tracks = self.tracks tracks = self.sub_tracks(tracks, hand_tracks)
# shopcart = self.shopcart
# # 提取手的frame_id并和动目标的frame_id 进行关联 # 提取小孩的track并计算状态left, right, incart
# hand_tracks = [t for t in tracks if t.cls==0] kid_tracks = [t for t in tracks if t.cls==9]
# self.Hands.extend(hand_tracks) kid_states = [self.kid_state(t) for t in kid_tracks]
# tracks = self.sub_tracks(tracks, hand_tracks) self.Kids = [x for x in zip(kid_tracks, kid_states)]
tracks = self.sub_tracks(tracks, kid_tracks)
static_tracks = [t for t in tracks if t.frnum>1 and t.is_static()]
self.Static.extend(static_tracks)
# # 提取小孩的track并计算状态left, right, incart '''剔除静止目标后的 tracks'''
# kid_tracks = [t for t in tracks if t.cls==9] tracks = self.sub_tracks(tracks, static_tracks)
# kid_states = [self.kid_state(t) for t in kid_tracks]
# self.Kids = [x for x in zip(kid_tracks, kid_states)]
# tracks = self.sub_tracks(tracks, kid_tracks)
# static_tracks = [t for t in tracks if t.frnum>1 and t.is_static()]
# self.Static.extend(static_tracks)
# '''剔除静止目标后的 tracks'''
# tracks = self.sub_tracks(tracks, static_tracks)
'''购物框边界外具有运动状态的干扰目标''' '''购物框边界外具有运动状态的干扰目标'''

View File

@ -31,7 +31,7 @@ class backTrack(Track):
'''运动点帧索引(运动帧两端的静止帧索引)''' '''运动点帧索引(运动帧两端的静止帧索引)'''
self.moving_index = self.compute_dynamic_fids() self.moving_index = self.compute_dynamic_fids()
self.static_dynamic_fids = self.compute_static_dynamic_fids() # self.static_index, self.moving_index = self.compute_static_dynamic_fids()
'''该函数依赖项: self.cornpoints定义 4 个商品位置变量: '''该函数依赖项: self.cornpoints定义 4 个商品位置变量:
self.Cent_isIncart, self.LB_isIncart, self.RB_isIncart self.Cent_isIncart, self.LB_isIncart, self.RB_isIncart
@ -250,13 +250,35 @@ class backTrack(Track):
return moving_index return moving_index
def compute_static_dynamic_fids(self): def compute_static_dynamic_fids(self):
static_dynamic_fids = []
for traj in self.trajectory:
static, dynamic = self.compute_static_fids(traj)
static_dynamic_fids.append((static, dynamic)) idx2 = self.trajlens.index(min(self.trajlens))
trajmin = self.trajectory[idx2]
return static_dynamic_fids static, dynamic = self.pt_state_fids(trajmin)
static = np.array(static)
dynamic = np.array(dynamic)
if static.size:
indx = np.argsort(static[:, 0])
static = static[indx]
if dynamic.size:
indx = np.argsort(dynamic[:, 0])
dynamic = dynamic[indx]
return static, dynamic
# =============================================================================
# static_dynamic_fids = []
# for traj in self.trajectory:
# static, dynamic = self.pt_state_fids(traj)
# static_dynamic_fids.append((static, dynamic))
#
# return static_dynamic_fids
# =============================================================================
@ -270,6 +292,10 @@ class backTrack(Track):
trajlen_rate, trajdist_rate] trajlen_rate, trajdist_rate]
''' '''
# print(f"TrackID: {self.tid}")
boxes = self.boxes
condt1 = self.feature[5] < 0.2 or self.feature[3] < 120 condt1 = self.feature[5] < 0.2 or self.feature[3] < 120
'''静态情况 2: 目标初始状态为静止,适当放宽关键点最小相对运动轨迹 < 0.5''' '''静态情况 2: 目标初始状态为静止,适当放宽关键点最小相对运动轨迹 < 0.5'''

View File

@ -31,8 +31,8 @@ class frontTrack(Track):
'''y1、y2静止状态区间值是 boxes 中对 axis=0 的索引,不是帧索引''' '''y1、y2静止状态区间值是 boxes 中对 axis=0 的索引,不是帧索引'''
det_y1 = np.diff(boxes[:, 1], axis=0) det_y1 = np.diff(boxes[:, 1], axis=0)
det_y2 = np.diff(boxes[:, 3], axis=0) det_y2 = np.diff(boxes[:, 3], axis=0)
self.static_y1, self.dynamic_y1 = self.compute_static_fids(det_y1) self.static_y1, self.dynamic_y1 = self.pt_state_fids(det_y1)
self.static_y2, self.dynamic_y2 = self.compute_static_fids(det_y2) self.static_y2, self.dynamic_y2 = self.pt_state_fids(det_y2)
self.isCornpoint = self.is_left_or_right_cornpoint() self.isCornpoint = self.is_left_or_right_cornpoint()
self.isBotmpoint = self.is_bottom_cornpoint() self.isBotmpoint = self.is_bottom_cornpoint()

View File

@ -90,7 +90,7 @@ def have_tracked():
k = 0 k = 0
gt = Profile() gt = Profile()
for filename in os.listdir(trackdict): for filename in os.listdir(trackdict):
filename = 'test_20240402-173935_6920152400975_front_174037379.pkl' filename = 'test_20240402-173935_6920152400975_back_174037372.pkl'
file, ext = os.path.splitext(filename) file, ext = os.path.splitext(filename)
filepath = os.path.join(trackdict, filename) filepath = os.path.join(trackdict, filename)
TracksDict = np.load(filepath, allow_pickle=True) TracksDict = np.load(filepath, allow_pickle=True)
@ -121,9 +121,9 @@ def have_tracked():
# k += 1 k += 1
# if k==1: if k==1:
# break break
if len(alltracks): if len(alltracks):
drawFeatures(alltracks, save_dir) drawFeatures(alltracks, save_dir)

View File

@ -215,6 +215,8 @@ class BYTETracker:
lost_stracks = [] lost_stracks = []
removed_stracks = [] removed_stracks = []
first_finded = []
scores = results.conf scores = results.conf
cls = results.cls cls = results.cls
@ -328,6 +330,9 @@ class BYTETracker:
continue continue
track.activate(self.kalman_filter, self.frame_id) track.activate(self.kalman_filter, self.frame_id)
activated_stracks.append(track) activated_stracks.append(track)
first_finded.append(track)
# Step 5: Update state # Step 5: Update state
for track in self.lost_stracks: for track in self.lost_stracks:
if self.frame_id - track.end_frame > self.max_time_lost: if self.frame_id - track.end_frame > self.max_time_lost:
@ -359,7 +364,7 @@ class BYTETracker:
for x in self.tracked_stracks if x.is_activated] for x in self.tracked_stracks if x.is_activated]
output2 = [x.tlwh_to_tlbr(x._tlwh).tolist() + [x.track_id, x.score, x.cls, x.frame_id, x.idx] output2 = [x.tlwh_to_tlbr(x._tlwh).tolist() + [x.track_id, x.score, x.cls, x.frame_id, x.idx]
for x in activated_stracks if x.first_find] for x in first_finded if x.first_find]
output = np.asarray(output1+output2, dtype=np.float32) output = np.asarray(output1+output2, dtype=np.float32)

View File

@ -11,6 +11,7 @@ import matplotlib.pyplot as plt
from sklearn.decomposition import PCA from sklearn.decomposition import PCA
from utils.annotator import TrackAnnotator from utils.annotator import TrackAnnotator
from utils.plotting import colors from utils.plotting import colors
from pathlib import Path
def plot_frameID_y2(vts): def plot_frameID_y2(vts):
@ -88,14 +89,18 @@ def draw_all_trajectories(vts, edgeline, save_dir, filename):
# ============================================================================= # =============================================================================
# '''1. tracks 5点轨迹''' # '''1. tracks 5点轨迹'''
# trackpth = save_dir.parent /Path("trajectory")/ Path(f"{file}")
# if not trackpth.exists():
# trackpth.mkdir(parents=True, exist_ok=True)
# for track in vts.tracks: # for track in vts.tracks:
# if track.cls != 0: # # if track.cls != 0:
# img = edgeline.copy() # img = edgeline.copy()
# img = draw5points(track, img) # img = draw5points(track, img)
# pth = save_dir.joinpath(f"{file}_{track.tid}.png") #
# cv2.imwrite(pth, img) # pth = trackpth.joinpath(f"{track.tid}.png")
# cv2.imwrite(str(pth), img)
#
# ============================================================================= # =============================================================================
'''2. all tracks 中心轨迹''' '''2. all tracks 中心轨迹'''
img1, img2 = edgeline.copy(), edgeline.copy() img1, img2 = edgeline.copy(), edgeline.copy()