bakeup
This commit is contained in:
@ -25,110 +25,14 @@ from tracking.utils.drawtracks import plot_frameID_y2, draw_all_trajectories
|
||||
from tracking.utils.read_data import extract_data, read_deletedBarcode_file, read_tracking_output
|
||||
|
||||
from contrast_analysis import contrast_analysis
|
||||
|
||||
from tracking.utils.annotator import TrackAnnotator
|
||||
|
||||
W, H = 1024, 1280
|
||||
Mode = 'front' #'back'
|
||||
ImgFormat = ['.jpg', '.jpeg', '.png', '.bmp']
|
||||
|
||||
def video2imgs(path):
|
||||
vpath = os.path.join(path, "videos")
|
||||
|
||||
k = 0
|
||||
have = False
|
||||
for filename in os.listdir(vpath):
|
||||
file, ext = os.path.splitext(filename)
|
||||
imgdir = os.path.join(path, file)
|
||||
if os.path.exists(imgdir):
|
||||
continue
|
||||
else:
|
||||
os.mkdir(imgdir)
|
||||
|
||||
vfile = os.path.join(vpath, filename)
|
||||
cap = cv2.VideoCapture(vfile)
|
||||
i = 0
|
||||
while True:
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
break
|
||||
|
||||
i += 1
|
||||
imgp = os.path.join(imgdir, file+f"_{i}.png")
|
||||
cv2.imwrite(imgp, frame)
|
||||
|
||||
print(filename+f": {i}")
|
||||
|
||||
|
||||
cap.release()
|
||||
|
||||
k+=1
|
||||
if k==1000:
|
||||
break
|
||||
|
||||
def draw_boxes():
|
||||
datapath = r'D:\datasets\ym\videos_test\20240530\1_tracker_inout(1).data'
|
||||
VideosData = read_tracker_input(datapath)
|
||||
|
||||
bboxes = VideosData[0][0]
|
||||
ffeats = VideosData[0][1]
|
||||
|
||||
videopath = r"D:\datasets\ym\videos_test\20240530\134458234-1cd970cf-f8b9-4e80-9c2e-7ca3eec83b81-1_seek0.10415589124891511.mp4"
|
||||
|
||||
cap = cv2.VideoCapture(videopath)
|
||||
i = 0
|
||||
while True:
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
break
|
||||
|
||||
|
||||
annotator = Annotator(frame.copy(), line_width=3)
|
||||
|
||||
|
||||
boxes = bboxes[i]
|
||||
|
||||
for *xyxy, conf, cls in reversed(boxes):
|
||||
label = f'{int(cls)}: {conf:.2f}'
|
||||
|
||||
color = colors(int(cls), True)
|
||||
annotator.box_label(xyxy, label, color=color)
|
||||
|
||||
img = annotator.result()
|
||||
|
||||
imgpath = r"D:\datasets\ym\videos_test\20240530\result\int8_front\{}.png".format(i+1)
|
||||
cv2.imwrite(imgpath, img)
|
||||
|
||||
print(f"Output: {i}")
|
||||
i += 1
|
||||
cap.release()
|
||||
|
||||
def read_imgs(imgspath, CamerType):
|
||||
imgs, frmIDs = [], []
|
||||
for filename in os.listdir(imgspath):
|
||||
file, ext = os.path.splitext(filename)
|
||||
flist = file.split('_')
|
||||
if len(flist)==4 and ext in ImgFormat:
|
||||
camID, frmID = flist[0], int(flist[-1])
|
||||
imgpath = os.path.join(imgspath, filename)
|
||||
img = cv2.imread(imgpath)
|
||||
|
||||
if camID==CamerType:
|
||||
imgs.append(img)
|
||||
frmIDs.append(frmID)
|
||||
|
||||
if len(frmIDs):
|
||||
indice = np.argsort(np.array(frmIDs))
|
||||
imgs = [imgs[i] for i in indice]
|
||||
|
||||
return imgs
|
||||
|
||||
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
'''调用tracking()函数,利用本地跟踪算法获取各目标轨迹,可以比较本地跟踪算法与现场跟踪算法的区别。'''
|
||||
def init_tracker(tracker_yaml = None, bs=1):
|
||||
"""
|
||||
Initialize tracker for object tracking during prediction.
|
||||
@ -177,38 +81,45 @@ def tracking(bboxes, ffeats):
|
||||
|
||||
return TrackBoxes, TracksDict
|
||||
|
||||
def read_imgs(imgspath, CamerType):
|
||||
'''
|
||||
inputs:
|
||||
imgspath;序列图像地址
|
||||
CamerType:相机类型,0:后摄,1:前摄
|
||||
outputs:
|
||||
imgs:图像序列
|
||||
功能:
|
||||
根据CamerType类型读取imgspath文件夹中的图像,并根据帧索引进行排序。
|
||||
do_tracking()中调用该函数,实现(1)读取imgs并绘制各目标轨迹框;(2)获取subimgs
|
||||
'''
|
||||
imgs, frmIDs = [], []
|
||||
for filename in os.listdir(imgspath):
|
||||
file, ext = os.path.splitext(filename)
|
||||
flist = file.split('_')
|
||||
if len(flist)==4 and ext in ImgFormat:
|
||||
camID, frmID = flist[0], int(flist[-1])
|
||||
imgpath = os.path.join(imgspath, filename)
|
||||
img = cv2.imread(imgpath)
|
||||
|
||||
if camID==CamerType:
|
||||
imgs.append(img)
|
||||
frmIDs.append(frmID)
|
||||
|
||||
|
||||
def do_tracker_tracking(fpath, save_dir):
|
||||
bboxes, ffeats, trackerboxes, tracker_feat_dict, trackingboxes, tracking_feat_dict = extract_data(fpath)
|
||||
tboxes, feats_dict = tracking(bboxes, ffeats)
|
||||
|
||||
CamerType = os.path.basename(fpath).split('_')[0]
|
||||
dirname = os.path.split(os.path.split(fpath)[0])[1]
|
||||
if CamerType == '1':
|
||||
vts = doFrontTracks(tboxes, feats_dict)
|
||||
vts.classify()
|
||||
if len(frmIDs):
|
||||
indice = np.argsort(np.array(frmIDs))
|
||||
imgs = [imgs[i] for i in indice]
|
||||
|
||||
plt = plot_frameID_y2(vts)
|
||||
plt.savefig('front_y2.png')
|
||||
# plt.close()
|
||||
elif CamerType == '0':
|
||||
vts = doBackTracks(tboxes, feats_dict)
|
||||
vts.classify()
|
||||
|
||||
filename = dirname+'_' + CamerType
|
||||
edgeline = cv2.imread("./shopcart/cart_tempt/edgeline.png")
|
||||
draw_all_trajectories(vts, edgeline, save_dir, filename)
|
||||
else:
|
||||
print("Please check data file!")
|
||||
|
||||
|
||||
return imgs
|
||||
|
||||
def do_tracking(fpath, savedir, event_name='images'):
|
||||
'''
|
||||
fpath: 算法各模块输出的data文件地址,匹配;
|
||||
savedir: 对 fpath 各模块输出的复现;
|
||||
分析具体视频时,需指定 fpath 和 savedir
|
||||
args:
|
||||
fpath: 算法各模块输出的data文件地址,匹配;
|
||||
savedir: 对 fpath 各模块输出的复现;
|
||||
分析具体视频时,需指定 fpath 和 savedir
|
||||
outputs:
|
||||
img_tracking:目标跟踪轨迹、本地轨迹分析算法的轨迹对比图
|
||||
abimg:现场轨迹分析算法、轨迹选择输出的对比图
|
||||
'''
|
||||
# fpath = r'D:\contrast\dataset\1_to_n\709\20240709-102758_6971558612189\1_track.data'
|
||||
# savedir = r'D:\contrast\dataset\result\20240709-102843_6958770005357_6971558612189\error_6971558612189'
|
||||
@ -231,8 +142,10 @@ def do_tracking(fpath, savedir, event_name='images'):
|
||||
bboxes, ffeats, trackerboxes, tracker_feat_dict, trackingboxes, tracking_feat_dict = extract_data(fpath)
|
||||
tracking_output_boxes, _ = read_tracking_output(tracking_output_path)
|
||||
|
||||
'''1.2 利用本地跟踪算法生成各商品轨迹'''
|
||||
# trackerboxes, tracker_feat_dict = tracking(bboxes, ffeats)
|
||||
|
||||
'''1.2 分别构造 2 个文件夹,(1) 存储画框后的图像; (2) 运动轨迹对应的 boxes子图'''
|
||||
'''1.3 分别构造 2 个文件夹,(1) 存储画框后的图像; (2) 运动轨迹对应的 boxes子图'''
|
||||
save_dir = os.path.join(savedir, event_name)
|
||||
subimg_dir = os.path.join(savedir, event_name + '_subimgs')
|
||||
if not os.path.exists(save_dir):
|
||||
@ -241,8 +154,6 @@ def do_tracking(fpath, savedir, event_name='images'):
|
||||
os.makedirs(subimg_dir)
|
||||
|
||||
|
||||
|
||||
|
||||
'''2. 执行轨迹分析, 保存轨迹分析前后的对比图示'''
|
||||
traj_graphic = event_name + '_' + CamerType
|
||||
if CamerType == '1':
|
||||
@ -344,24 +255,30 @@ def do_tracking(fpath, savedir, event_name='images'):
|
||||
|
||||
def tracking_simulate(eventpath, savepath):
|
||||
'''args:
|
||||
eventpath: 时间文件夹
|
||||
eventpath: 事件文件夹
|
||||
savepath: 存储文件夹
|
||||
遍历eventpath
|
||||
'''
|
||||
|
||||
'''1. 获取事件名'''
|
||||
event_names = os.path.basename(eventpath).strip().split('_')
|
||||
if len(event_names)==2 and len(event_names[1])>=8:
|
||||
enent_name = event_names[1]
|
||||
elif len(event_names)==2 and len(event_names[1])==0:
|
||||
enent_name = event_names[0]
|
||||
else:
|
||||
return
|
||||
# =============================================================================
|
||||
# '''1. 获取事件名'''
|
||||
# event_names = os.path.basename(eventpath).strip().split('_')
|
||||
# if len(event_names)==2 and len(event_names[1])>=8:
|
||||
# enent_name = event_names[1]
|
||||
# elif len(event_names)==2 and len(event_names[1])==0:
|
||||
# enent_name = event_names[0]
|
||||
# else:
|
||||
# return
|
||||
# =============================================================================
|
||||
|
||||
enent_name = os.path.basename(eventpath)[:15]
|
||||
|
||||
'''2. 依次读取 0/1_track.data 中数据,进行仿真'''
|
||||
illu_tracking, illu_select = [], []
|
||||
for filename in os.listdir(eventpath):
|
||||
# filename = '1_track.data'
|
||||
if filename.find("track.data") <= 0: continue
|
||||
if filename.find("track.data") < 0: continue
|
||||
|
||||
fpath = os.path.join(eventpath, filename)
|
||||
if not os.path.isfile(fpath): continue
|
||||
|
||||
@ -451,7 +368,7 @@ def main_loop():
|
||||
'''2. 循环执行操作事件:取出、放入、错误匹配'''
|
||||
for eventpath in tuple_paths:
|
||||
try:
|
||||
tracking_simulate(eventpath, savepath)
|
||||
tracking_simulate(eventpath, savepath)
|
||||
except Exception as e:
|
||||
print(f'Error! {eventpath}, {e}')
|
||||
|
||||
@ -462,29 +379,29 @@ def main_loop():
|
||||
|
||||
def main():
|
||||
'''
|
||||
eventpath: data文件地址,该 data 文件包括 Pipeline 各模块输出
|
||||
savepath: 包含二级目录,一级目录为轨迹图像;二级目录为与data文件对应的序列图像存储地址。
|
||||
eventPaths: data文件地址,该 data 文件包括 Pipeline 各模块输出
|
||||
SavePath: 包含二级目录,一级目录为轨迹图像;二级目录为与data文件对应的序列图像存储地址。
|
||||
'''
|
||||
EventPaths = r'\\192.168.1.28\share\测试_202406\0723\0723_2'
|
||||
SavePath = r'D:\contrast\dataset\result'
|
||||
eventPaths = r'\\192.168.1.28\share\测试_202406\0723\0723_3'
|
||||
savePath = r'D:\contrast\dataset\result'
|
||||
k=0
|
||||
for pathname in os.listdir(EventPaths):
|
||||
# pathname = "20240723-094731_6903148242797"
|
||||
|
||||
eventpath = os.path.join(EventPaths, pathname)
|
||||
savepath = os.path.join(SavePath, pathname)
|
||||
for pathname in os.listdir(eventPaths):
|
||||
pathname = "20240723-163121_6925282237668"
|
||||
|
||||
eventpath = os.path.join(eventPaths, pathname)
|
||||
savepath = os.path.join(savePath, pathname)
|
||||
if not os.path.exists(savepath):
|
||||
os.makedirs(savepath)
|
||||
|
||||
# tracking_simulate(eventpath, savepath)
|
||||
try:
|
||||
tracking_simulate(eventpath, savepath)
|
||||
except Exception as e:
|
||||
print(f'Error! {eventpath}, {e}')
|
||||
tracking_simulate(eventpath, savepath)
|
||||
# try:
|
||||
# tracking_simulate(eventpath, savepath)
|
||||
# except Exception as e:
|
||||
# print(f'Error! {eventpath}, {e}')
|
||||
|
||||
# k += 1
|
||||
# if k==10:
|
||||
# break
|
||||
k += 1
|
||||
if k==1:
|
||||
break
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Reference in New Issue
Block a user