for bakeup
This commit is contained in:
Binary file not shown.
@ -397,7 +397,7 @@ def contrast_loop(fpath):
|
||||
|
||||
plt1 = show_recall_prec(recall, prec, ths)
|
||||
# plt1.show()
|
||||
plt.xlabel(f'threshold, Num: {len(blist)}')
|
||||
plt1.xlabel(f'threshold, Num: {len(blist)}')
|
||||
plt1.savefig(os.path.join(savepath, file+'_pr.png'))
|
||||
# plt1.close()
|
||||
|
||||
@ -408,13 +408,12 @@ def contrast_loop(fpath):
|
||||
|
||||
|
||||
|
||||
def main1():
|
||||
fpath = r'D:\contrast\dataset\1_to_n\0719'
|
||||
fpath = r'\\192.168.1.28\share\测试_202406\deletedBarcode\good'
|
||||
def main():
|
||||
fpath = r'\\192.168.1.28\share\测试_202406\deletedBarcode\other'
|
||||
|
||||
contrast_loop(fpath)
|
||||
|
||||
def main():
|
||||
def main1():
|
||||
del_barcode_file = 'D:/contrast/dataset/compairsonResult/deletedBarcode_20240709_pm.txt'
|
||||
basepath = r'D:\contrast\dataset\1_to_n\709'
|
||||
savepath = r'D:\contrast\dataset\result'
|
||||
@ -431,9 +430,9 @@ def resolve_vidoes():
|
||||
video2imgs(videopath, savepath)
|
||||
|
||||
if __name__ == '__main__':
|
||||
# main()
|
||||
main1()
|
||||
main()
|
||||
|
||||
# main1()
|
||||
# resolve_vidoes()
|
||||
|
||||
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -79,7 +79,7 @@ class ShoppingCart:
|
||||
|
||||
class Track:
|
||||
'''抽象基类,不能实例化对象'''
|
||||
def __init__(self, boxes, features, imgshape=(1024, 1280)):
|
||||
def __init__(self, boxes, features=None, imgshape=(1024, 1280)):
|
||||
'''
|
||||
boxes: [x1, y1, x2, y2, track_id, score, cls, frame_index, box_index]
|
||||
0 1 2 3 4 5 6 7 8
|
||||
|
@ -55,10 +55,14 @@ class doBackTracks(doTracks):
|
||||
# tracks = self.sub_tracks(tracks, out_trcak)
|
||||
|
||||
|
||||
[self.associate_with_hand(htrack, gtrack) for htrack in hand_tracks for gtrack in tracks]
|
||||
|
||||
'''轨迹循环归并'''
|
||||
# merged_tracks = self.merge_tracks(tracks)
|
||||
merged_tracks = self.merge_tracks_loop(tracks)
|
||||
|
||||
[self.associate_with_hand(htrack, gtrack) for htrack in hand_tracks for gtrack in tracks]
|
||||
|
||||
|
||||
tracks = [t for t in merged_tracks if t.frnum > 1]
|
||||
|
||||
self.merged_tracks = merged_tracks
|
||||
|
@ -45,10 +45,12 @@ class doFrontTracks(doTracks):
|
||||
'''剔除静止目标后的 tracks'''
|
||||
tracks = self.sub_tracks(tracks, static_tracks)
|
||||
|
||||
[self.associate_with_hand(htrack, gtrack) for htrack in hand_tracks for gtrack in tracks]
|
||||
# [self.associate_with_hand(htrack, gtrack) for htrack in hand_tracks for gtrack in tracks]
|
||||
'''轨迹循环归并'''
|
||||
merged_tracks = self.merge_tracks_loop(tracks)
|
||||
|
||||
[self.associate_with_hand(htrack, gtrack) for htrack in hand_tracks for gtrack in merged_tracks]
|
||||
|
||||
tracks = [t for t in merged_tracks if t.frnum > 1]
|
||||
|
||||
# for gtrack in tracks:
|
||||
|
91
tracking/dotrack/track_select.py
Normal file
91
tracking/dotrack/track_select.py
Normal file
@ -0,0 +1,91 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Mon Jul 29 10:28:21 2024
|
||||
未来需将这一部分和轨迹分析代码集成
|
||||
|
||||
@author: ym
|
||||
"""
|
||||
import numpy as np
|
||||
import cv2
|
||||
from scipy.spatial.distance import cdist
|
||||
|
||||
class TProp:
|
||||
def __init__(self, boxes):
|
||||
|
||||
self.boxes = boxes
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class TProp:
|
||||
'''抽象基类,不能实例化对象'''
|
||||
def __init__(self, boxes):
|
||||
'''
|
||||
boxes: [x1, y1, x2, y2, track_id, score, cls, frame_index, box_index]
|
||||
0 1 2 3 4 5 6 7 8
|
||||
'''
|
||||
# assert len(set(boxes[:, 4].astype(int))) == 1, "For a Track, track_id more than 1"
|
||||
# assert len(set(boxes[:, 6].astype(int))) == 1, "For a Track, class number more than 1"
|
||||
|
||||
self.boxes = boxes
|
||||
|
||||
'''5个关键点(中心点、左上点、右上点、左下点、右下点 )坐标'''
|
||||
self.compute_cornpoints()
|
||||
|
||||
'''5个关键点轨迹特征,可以在子类中实现,降低顺序处理时的计算量
|
||||
(中心点、左上点、右上点、左下点、右下点 )轨迹特征'''
|
||||
self.compute_cornpts_feats()
|
||||
|
||||
self.distmax = max(self.trajdist)
|
||||
|
||||
|
||||
def compute_cornpoints(self):
|
||||
'''
|
||||
cornpoints 共10项,分别是个点的坐标值(x, y)
|
||||
(center, top_left, top_right, bottom_left, bottom_right)
|
||||
'''
|
||||
boxes = self.boxes
|
||||
cornpoints = np.zeros((self.frnum, 10))
|
||||
cornpoints[:,0] = (boxes[:, 0] + boxes[:, 2]) / 2
|
||||
cornpoints[:,1] = (boxes[:, 1] + boxes[:, 3]) / 2
|
||||
cornpoints[:,2], cornpoints[:,3] = boxes[:, 0], boxes[:, 1]
|
||||
cornpoints[:,4], cornpoints[:,5] = boxes[:, 2], boxes[:, 1]
|
||||
cornpoints[:,6], cornpoints[:,7] = boxes[:, 0], boxes[:, 3]
|
||||
cornpoints[:,8], cornpoints[:,9] = boxes[:, 2], boxes[:, 3]
|
||||
|
||||
self.cornpoints = cornpoints
|
||||
def compute_cornpts_feats(self):
|
||||
'''
|
||||
'''
|
||||
trajectory = []
|
||||
trajlens = []
|
||||
trajdist = []
|
||||
trajrects = []
|
||||
for k in range(5):
|
||||
# diff_xy2 = np.power(np.diff(self.cornpoints[:, 2*k:2*(k+1)], axis = 0), 2)
|
||||
# trajlen = np.sum(np.sqrt(np.sum(diff_xy2, axis = 1)))
|
||||
|
||||
X = self.cornpoints[:, 2*k:2*(k+1)]
|
||||
|
||||
traj = np.linalg.norm(np.diff(X, axis=0), axis=1)
|
||||
trajectory.append(traj)
|
||||
|
||||
trajlen = np.sum(traj)
|
||||
trajlens.append(trajlen)
|
||||
|
||||
ptdist = np.max(cdist(X, X))
|
||||
trajdist.append(ptdist)
|
||||
|
||||
'''最小外接矩形:
|
||||
rect[0]: 中心(x, y)
|
||||
rect[1]: (w, h)
|
||||
rect[0]: 旋转角度 (-90°, 0]
|
||||
'''
|
||||
rect = cv2.minAreaRect(X.astype(np.int64))
|
||||
trajrects.append(rect)
|
||||
|
||||
self.trajectory = trajectory
|
||||
self.trajlens = trajlens
|
||||
self.trajdist = trajdist
|
||||
self.trajrects = trajrects
|
418
tracking/feat_select.py
Normal file
418
tracking/feat_select.py
Normal file
@ -0,0 +1,418 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Sat Jul 27 14:07:25 2024
|
||||
|
||||
@author: ym
|
||||
"""
|
||||
import os.path
|
||||
import numpy as np
|
||||
from scipy.spatial.distance import cdist
|
||||
import matplotlib.pyplot as plt
|
||||
import sys
|
||||
sys.path.append(r"D:\DetectTracking")
|
||||
from tracking.utils.read_data import extract_data, read_deletedBarcode_file, read_tracking_output
|
||||
from tracking.dotrack.dotracks import Track
|
||||
|
||||
from tracking.contrast_analysis import performance_evaluate, compute_recall_precision, show_recall_prec
|
||||
|
||||
def compute_similar(feat1, feat2):
|
||||
|
||||
if len(feat1)==0 or len(feat2)==0:
|
||||
return 0
|
||||
|
||||
similar = 1 - np.maximum(0.0, cdist(feat1, feat2, metric = 'cosine'))
|
||||
smean = np.mean(similar)
|
||||
|
||||
return smean
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def update_event(datapath):
|
||||
'''一次购物事件,包含 8 个keys'''
|
||||
event = {}
|
||||
# event['front_tracking_boxes'] = []
|
||||
# event['front_tracking_feats'] = {}
|
||||
# event['back_tracking_boxes'] = []
|
||||
# event['back_tracking_feats'] = {}
|
||||
|
||||
event['back_sole_boxes'] = np.empty((0, 9), dtype=np.float64)
|
||||
event['front_sole_boxes'] = np.empty((0, 9), dtype=np.float64)
|
||||
|
||||
event['back_sole_feats'] = np.empty((0, 256), dtype=np.float64)
|
||||
event['front_sole_feats'] = np.empty((0, 256), dtype=np.float64)
|
||||
|
||||
event['feats_compose'] = np.empty((0, 256), dtype=np.float64)
|
||||
event['feats_select'] = np.empty((0, 256), dtype=np.float64)
|
||||
|
||||
'''读取当前事件的 data 文件'''
|
||||
for filename in os.listdir(datapath):
|
||||
# filename = '1_track.data'
|
||||
fpath = os.path.join(datapath, filename)
|
||||
CamerType = filename.split('_')[0]
|
||||
# if os.path.isfile(fpath) and filename.find("track.data")>0:
|
||||
# bboxes, ffeats, trackerboxes, tracker_feat_dict, tracking_boxes, tracking_feat_dict = extract_data(fpath)
|
||||
|
||||
# if CamerType == '0':
|
||||
# event['back_tracking_boxes'] = tracking_boxes
|
||||
# event['back_tracking_feats'] = tracking_feat_dict
|
||||
# elif CamerType == '1':
|
||||
# event['front_tracking_boxes'] = tracking_boxes
|
||||
# event['front_tracking_feats'] = tracking_feat_dict
|
||||
|
||||
|
||||
|
||||
if os.path.isfile(fpath) and filename.find("tracking_output.data")>0:
|
||||
tracking_output_boxes, tracking_output_feats = read_tracking_output(fpath)
|
||||
if CamerType == '0':
|
||||
event['back_sole_boxes'] = tracking_output_boxes
|
||||
event['back_sole_feats'] = tracking_output_feats
|
||||
elif CamerType == '1':
|
||||
event['front_sole_boxes'] = tracking_output_boxes
|
||||
event['front_sole_feats'] = tracking_output_feats
|
||||
|
||||
|
||||
'''事件的特征表征方式选择'''
|
||||
|
||||
fs_feats = event['front_sole_feats']
|
||||
bs_feats = event['back_sole_feats']
|
||||
|
||||
'''1. 如果前后摄均没有轨迹选择输出,返回'''
|
||||
condt1 = len(fs_feats) + len(bs_feats) == 0
|
||||
if condt1:
|
||||
return event
|
||||
|
||||
|
||||
'''2. 构造综合特征'''
|
||||
feats_compose = np.empty((0, 256), dtype=np.float64)
|
||||
if len(fs_feats):
|
||||
feats_compose = np.concatenate((feats_compose, fs_feats), axis=0)
|
||||
if len(bs_feats):
|
||||
feats_compose = np.concatenate((feats_compose, bs_feats), axis=0)
|
||||
event['feats_compose'] = feats_compose
|
||||
|
||||
'''3. 构造前摄特征'''
|
||||
if len(fs_feats):
|
||||
event['feats_select'] = fs_feats
|
||||
return event
|
||||
|
||||
|
||||
'''4. 从前摄输出轨迹中选取特定轨迹对应的特征'''
|
||||
ftrboxes = event['front_tracking_boxes']
|
||||
ftrfeats = event['front_tracking_feats']
|
||||
|
||||
condt2 = len(ftrboxes) + len(ftrfeats) == 0
|
||||
condt3 = len(ftrfeats) != len(ftrboxes)
|
||||
if condt2 or condt3:
|
||||
return event
|
||||
|
||||
bprops = []
|
||||
for boxes in ftrboxes:
|
||||
track = Track(boxes)
|
||||
bprops.append(max(track.trajdist))
|
||||
|
||||
index = bprops.index(max(bprops))
|
||||
box_select = ftrboxes[index]
|
||||
tid = int(box_select[0, 4])
|
||||
|
||||
feat_select = ftrfeats[f"track_{tid}"]
|
||||
feats_select = np.empty((0, 256), dtype=np.float64)
|
||||
for fid_bid, feat in feat_select['feats'].items():
|
||||
feats_select = np.concatenate((feats_select, feat[None, :]), axis=0)
|
||||
event['feats_select'] = feats_select
|
||||
|
||||
return event
|
||||
|
||||
|
||||
def creatd_deletedBarcode_front(filepath):
|
||||
# filepath = r'\\192.168.1.28\share\测试_202406\0723\0723_1\deletedBarcode.txt'
|
||||
|
||||
basepath, _ = os.path.split(filepath)
|
||||
|
||||
MatchList = []
|
||||
|
||||
bcdlist = read_deletedBarcode_file(filepath)
|
||||
|
||||
k = 0
|
||||
for s_list in bcdlist:
|
||||
getout_fold = s_list['SeqDir'].strip()
|
||||
day, hms = getout_fold.strip('_').split('-')
|
||||
|
||||
''' 生成取出事件字典 '''
|
||||
getout_event = {}
|
||||
getout_event['barcode'] = s_list['Deleted'].strip()
|
||||
getout_event['path'] = os.path.join(basepath, getout_fold)
|
||||
|
||||
getout_event['feats_compose'] = np.empty((0, 256), dtype=np.float64)
|
||||
getout_event['feats_select'] = np.empty((0, 256), dtype=np.float64)
|
||||
|
||||
|
||||
InputList = []
|
||||
barcodes = [s.strip() for s in s_list['barcode']]
|
||||
similarity = [float(s.strip()) for s in s_list['similarity']]
|
||||
for i, barcode in enumerate(barcodes):
|
||||
|
||||
''' 生成放入事件字典 '''
|
||||
input_event = {}
|
||||
|
||||
input_folds, times = [], []
|
||||
for pathname in os.listdir(basepath):
|
||||
if pathname.endswith('_'): continue
|
||||
if os.path.isfile(os.path.join(basepath, pathname)):continue
|
||||
|
||||
infold = pathname.split('_')
|
||||
if len(infold)!=2: continue
|
||||
|
||||
day1, hms1 = infold[0].split('-')
|
||||
|
||||
if day1==day and infold[1]==barcode and int(hms1)<int(hms):
|
||||
input_folds.append(pathname)
|
||||
times.append(int(hms1))
|
||||
|
||||
''' 根据时间排序,选择离取出操作最近时间的文件夹,作为取出操作应的放入操作所对应的文件夹 '''
|
||||
input_path = ''
|
||||
if len(input_folds):
|
||||
indice = np.argsort(np.array(times))
|
||||
input_fold = input_folds[indice[-1]]
|
||||
input_path = os.path.join(basepath, input_fold)
|
||||
|
||||
input_event['barcode'] = barcode
|
||||
input_event['path'] = input_path
|
||||
input_event['similarity'] = float(similarity[i])
|
||||
input_event['feats_compose'] = np.empty((0, 256), dtype=np.float64)
|
||||
input_event['feats_select'] = np.empty((0, 256), dtype=np.float64)
|
||||
|
||||
|
||||
InputList.append(input_event)
|
||||
|
||||
MatchList.append((getout_event, InputList))
|
||||
|
||||
# k += 1
|
||||
# if k==2:
|
||||
# break
|
||||
print('Step 1 Done!')
|
||||
|
||||
for getout_event, InputList in MatchList:
|
||||
getout_path = getout_event['path']
|
||||
if os.path.exists(getout_path) and os.path.isdir(getout_path):
|
||||
event = update_event(getout_path)
|
||||
getout_event.update(event)
|
||||
|
||||
for input_event in InputList:
|
||||
input_path = input_event['path']
|
||||
if os.path.exists(input_path) and os.path.isdir(input_path):
|
||||
event = update_event(input_path)
|
||||
input_event.update(event)
|
||||
|
||||
print('Step 2 Done!')
|
||||
|
||||
results = []
|
||||
for getout_event, InputList in MatchList:
|
||||
getout_barcode = getout_event['barcode']
|
||||
getout_feats_compose = getout_event['feats_compose']
|
||||
getout_feats_select = getout_event['feats_select']
|
||||
|
||||
if len(getout_feats_select):
|
||||
outfeats_select = getout_feats_select.copy()
|
||||
else:
|
||||
outfeats_select = getout_feats_compose.copy()
|
||||
|
||||
result = {}
|
||||
result['SeqDir'] = os.path.split(getout_event['path'])[1]
|
||||
result['Deleted'] = getout_barcode
|
||||
result['List'] = {}
|
||||
for input_event in InputList:
|
||||
input_barcode = input_event['barcode']
|
||||
|
||||
input_feats_compose = input_event['feats_compose']
|
||||
input_feats_select = input_event['feats_select']
|
||||
|
||||
if len(input_feats_select):
|
||||
infeats_select = input_feats_select.copy()
|
||||
else:
|
||||
infeats_select = input_feats_compose.copy()
|
||||
|
||||
similar_comp = compute_similar(getout_feats_compose, input_feats_compose)
|
||||
similar_selt = compute_similar(outfeats_select, infeats_select)
|
||||
|
||||
'''现场测试相似度,组合特征相似度,前摄选择特征相似度'''
|
||||
result['List'][f'{input_barcode}'] = (input_event['similarity'], similar_comp, similar_selt)
|
||||
# result[f'{input_barcode}'] = (input_event['similarity'], similar_comp, similar_selt)
|
||||
results.append(result)
|
||||
|
||||
print('Step 3 Done!')
|
||||
|
||||
wpath = os.path.split(filepath)[0]
|
||||
wfile = os.path.join(wpath, 'deletedBarcodeTest.txt')
|
||||
with open(wfile, 'w', encoding='utf-8') as file:
|
||||
for result in results:
|
||||
|
||||
SeqDir = result['SeqDir']
|
||||
Deleted = result['Deleted']
|
||||
|
||||
file.write('\n')
|
||||
file.write(f'SeqDir: {SeqDir}\n')
|
||||
file.write(f'Deleted: {Deleted}\n')
|
||||
file.write('List:\n')
|
||||
for key, value in result['List'].items():
|
||||
file.write(f'{key}: ')
|
||||
file.write(f'{value[0]}, {value[1]:.3f}, {value[2]:.3f}\n')
|
||||
|
||||
print('Step 4 Done!')
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def front_performance_evaluate(all_list):
|
||||
|
||||
corrpairs, correct_barcode_list, correct_similarity, errpairs, err_barcode_list, err_similarity = [], [], [], [], [], []
|
||||
|
||||
for s_list in all_list:
|
||||
seqdir = s_list['SeqDir'].strip()
|
||||
delete = s_list['Deleted'].strip()
|
||||
barcodes = [s.strip() for s in s_list['barcode']]
|
||||
|
||||
similarity, front_similarity = [], []
|
||||
for simil in s_list['similarity']:
|
||||
ss = [float(s.strip()) for s in simil.split(',')]
|
||||
|
||||
similarity.append(ss[0])
|
||||
if len(ss)==3:
|
||||
front_similarity.append(ss[2])
|
||||
|
||||
# similarity = [float(s.strip()) for s in s_list['similarity']]
|
||||
|
||||
|
||||
index = front_similarity.index(max(front_similarity))
|
||||
matched_barcode = barcodes[index]
|
||||
if matched_barcode == delete:
|
||||
corrpairs.append((seqdir, delete))
|
||||
correct_barcode_list.append(delete)
|
||||
correct_similarity.append(max(front_similarity))
|
||||
else:
|
||||
errpairs.append((seqdir, delete, matched_barcode))
|
||||
err_barcode_list.append(delete)
|
||||
err_similarity.append(max(front_similarity))
|
||||
|
||||
|
||||
|
||||
|
||||
return errpairs, corrpairs, err_similarity, correct_similarity
|
||||
|
||||
|
||||
|
||||
def compute_pres(filepath, savepath):
|
||||
|
||||
|
||||
fpath = os.path.split(filepath)[0]
|
||||
_, basefile = os.path.split(fpath)
|
||||
|
||||
'''1. 综合前后摄特征的相似度比对性能'''
|
||||
fpath1 = os.path.join(fpath, 'deletedBarcode.txt')
|
||||
blist1 = read_deletedBarcode_file(fpath1)
|
||||
errpairs, corrpairs, err_similarity, correct_similarity = performance_evaluate(blist1)
|
||||
recall, prec, ths = compute_recall_precision(err_similarity, correct_similarity)
|
||||
|
||||
|
||||
|
||||
os.path.split(fpath1)
|
||||
plt1 = show_recall_prec(recall, prec, ths)
|
||||
# plt1.show()
|
||||
plt1.xlabel(f'threshold, Num: {len(blist1)}')
|
||||
plt1.title(basefile + ', compose')
|
||||
plt1.savefig(os.path.join(savepath, basefile+'_pr.png'))
|
||||
plt1.close()
|
||||
|
||||
'''2. 优先选取前摄特征的相似度比对性能'''
|
||||
fpath2 = os.path.join(fpath, 'deletedBarcodeTest.txt')
|
||||
blist2 = read_deletedBarcode_file(fpath2)
|
||||
front_errpairs, front_corrpairs, front_err_similarity, front_correct_similarity = front_performance_evaluate(blist2)
|
||||
front_recall, front_prec, front_ths = compute_recall_precision(front_err_similarity, front_correct_similarity)
|
||||
|
||||
plt2 = show_recall_prec(front_recall, front_prec, front_ths)
|
||||
# plt2.show()
|
||||
plt2.xlabel(f'threshold, Num: {len(blist2)}')
|
||||
plt1.title(basefile + ', front')
|
||||
plt2.savefig(os.path.join(savepath, basefile+'_pr_front.png'))
|
||||
plt2.close()
|
||||
|
||||
def main():
|
||||
fplist = [r'\\192.168.1.28\share\测试_202406\0723\0723_1\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0723\0723_2\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0723\0723_3\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0722\0722_01\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0722\0722_02\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0719\0719_1\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0719\0719_2\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0719\0719_3\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0719\0719_4\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0718\0718-1\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0718\0718-2\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0717\0717-1\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0717\0717-2\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0717\0717-3\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0716\0716_1\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0716\0716_2\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0716\0716_3\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0715\0715_1\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0715\0715_2\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0715\0715_3\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0712\0712_2\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\0712\0712_3\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\711\images01\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\711\images02\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\710\images_1\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\710\images_2\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\709\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\705\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\703\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\702_pm_1\images\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\702_pm_2\images\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\702_pm_3\images\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\702_am\images\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\702_pm\images\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\701_am\images\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\628\1\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\628\2\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\627\deletedBarcode.txt',
|
||||
r'\\192.168.1.28\share\测试_202406\625\deletedBarcode.txt',
|
||||
]
|
||||
|
||||
savepath = r'\\192.168.1.28\share\测试_202406\deletedBarcode\illustration'
|
||||
for filepath in fplist:
|
||||
print(filepath)
|
||||
|
||||
try:
|
||||
creatd_deletedBarcode_front(filepath)
|
||||
compute_pres(filepath, savepath)
|
||||
except Exception as e:
|
||||
print(f'Error: {e}')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -211,59 +211,45 @@ def do_tracking(fpath, savedir):
|
||||
'''
|
||||
# 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'
|
||||
|
||||
|
||||
'''1.1 构造 0/1_tracking_output.data 文件地址,读取文件数据'''
|
||||
imgpath, dfname = os.path.split(fpath)
|
||||
CamerType = dfname.split('_')[0]
|
||||
|
||||
bboxes, ffeats, trackerboxes, tracker_feat_dict, trackingboxes, tracking_feat_dict = extract_data(fpath)
|
||||
|
||||
tracking_output_path = os.path.join(imgpath, CamerType + '_tracking_output.data')
|
||||
if not os.path.isfile(fpath):
|
||||
print(f"Can't find {dfname} file!")
|
||||
return
|
||||
if not os.path.isfile(tracking_output_path):
|
||||
print(f"Can't find {CamerType}_tracking_output.data file!")
|
||||
return
|
||||
|
||||
if not os.path.isfile(tracking_output_path): return
|
||||
|
||||
bboxes, ffeats, trackerboxes, tracker_feat_dict, trackingboxes, tracking_feat_dict = extract_data(fpath)
|
||||
tracking_output_boxes, _ = read_tracking_output(tracking_output_path)
|
||||
|
||||
|
||||
|
||||
'''存储画框后的 img'''
|
||||
'''1.2 分别构造 2 个文件夹,(1) 存储画框后的图像; (2) 运动轨迹对应的 boxes子图'''
|
||||
save_dir, basename = os.path.split(savedir)
|
||||
if not os.path.exists(savedir):
|
||||
os.makedirs(savedir)
|
||||
|
||||
'''存储轨迹对应的 boxes子图'''
|
||||
os.makedirs(savedir)
|
||||
|
||||
subimg_dir = os.path.join(save_dir, basename.split('_')[0] + '_subimgs')
|
||||
if not os.path.exists(subimg_dir):
|
||||
os.makedirs(subimg_dir)
|
||||
|
||||
|
||||
''' 读取 fpath 中 track.data 文件对应的图像 '''
|
||||
|
||||
'''1.3 读取 fpath 中 track.data 文件对应的图像 imgs '''
|
||||
imgs = read_imgs(imgpath, CamerType)
|
||||
|
||||
''' 在 imgs 上画框并保存,如果 trackerboxes 的帧数和 imgs 数不匹配,返回原图'''
|
||||
imgs_dw = draw_tracking_boxes(imgs, trackerboxes)
|
||||
|
||||
if len(imgs_dw)==0:
|
||||
imgs_dw = [img for img in imgs]
|
||||
print(f"fpath: {imgpath}, savedir: {savedir}。Tracker输出的图像数和 imgs 中图像数不相等,无法一一匹配并画框")
|
||||
|
||||
for i in range(len(imgs_dw)):
|
||||
img_savepath = os.path.join(savedir, CamerType + "_" + f"{i}.png")
|
||||
# img = imgs_dw[i]
|
||||
cv2.imwrite(img_savepath, imgs_dw[i])
|
||||
|
||||
if not isinstance(savedir, Path):
|
||||
savedir = Path(savedir)
|
||||
save_dir = savedir.parent
|
||||
|
||||
|
||||
'''2. 执行轨迹分析, 保存轨迹分析前后的对比图示'''
|
||||
traj_graphic = basename + '_' + CamerType
|
||||
if CamerType == '1':
|
||||
vts = doFrontTracks(trackerboxes, tracker_feat_dict)
|
||||
vts.classify()
|
||||
|
||||
plt = plot_frameID_y2(vts)
|
||||
ftpath = save_dir.joinpath(f"{traj_graphic}_front_y2.png")
|
||||
plt.savefig(str(ftpath))
|
||||
# ftpath = os.path.join(save_dir, f"{traj_graphic}_front_y2.png")
|
||||
# plt.savefig(ftpath)
|
||||
plt.close()
|
||||
elif CamerType == '0':
|
||||
vts = doBackTracks(trackerboxes, tracker_feat_dict)
|
||||
@ -272,28 +258,13 @@ def do_tracking(fpath, savedir):
|
||||
edgeline = cv2.imread("./shopcart/cart_tempt/edgeline.png")
|
||||
img = draw_all_trajectories(vts, edgeline, save_dir, traj_graphic)
|
||||
|
||||
imgpth = save_dir.joinpath(f"{traj_graphic}_show.png")
|
||||
cv2.imwrite(str(imgpth), img)
|
||||
# imgpth = os.path.join(save_dir, f"{traj_graphic}_.png")
|
||||
# cv2.imwrite(str(imgpth), img)
|
||||
else:
|
||||
print("Please check data file!")
|
||||
|
||||
for track in vts.Residual:
|
||||
for *xyxy, tid, conf, cls, fid, bid in track.boxes:
|
||||
img = imgs[int(fid-1)]
|
||||
x1, y1, x2, y2 = int(xyxy[0]/2), int(xyxy[1]/2), int(xyxy[2]/2), int(xyxy[3]/2)
|
||||
subimg = img[y1:y2, x1:x2]
|
||||
|
||||
subimg_path = os.path.join(subimg_dir, f'{CamerType}_{int(tid)}_{int(fid-1)}_{int(bid)}.png' )
|
||||
|
||||
cv2.imwrite(subimg_path, subimg)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
'''================== 现场测试的 tracking() 算法输出 =================='''
|
||||
|
||||
'''3 tracking() 算法输出后多轨迹选择问题分析'''
|
||||
if CamerType == '1':
|
||||
aline = cv2.imread("./shopcart/cart_tempt/board_ftmp_line.png")
|
||||
elif CamerType == '0':
|
||||
@ -320,16 +291,54 @@ def do_tracking(fpath, savedir):
|
||||
abH, abW = abimg.shape[:2]
|
||||
cv2.line(abimg, (int(abW/2), 0), (int(abW/2), abH), (128, 255, 128), 2)
|
||||
|
||||
algpath = save_dir.joinpath(f"{traj_graphic}_Alg.png")
|
||||
cv2.imwrite(str(algpath), abimg)
|
||||
|
||||
return
|
||||
# algpath = os.path.join(save_dir, f"{traj_graphic}_alg.png")
|
||||
# cv2.imwrite(str(algpath), abimg)
|
||||
|
||||
'''4. 画框后的图像和子图保存,若imgs数与tracker中fid数不匹配,只保存原图,不保存子图'''
|
||||
'''4.1 imgs数 < trackerboxes 的 max(fid),返回原图'''
|
||||
if len(imgs) < np.max(trackerboxes[:,7]):
|
||||
for i in range(len(imgs)):
|
||||
img_savepath = os.path.join(savedir, CamerType + "_" + f"{i}.png")
|
||||
cv2.imwrite(img_savepath, imgs[i])
|
||||
print(f"fpath: {fpath}, len(imgs) = {len(imgs)} < Tracker max(fid) = {int(np.max(trackerboxes[:,7]))}, 无法匹配画框")
|
||||
return
|
||||
|
||||
'''4.2 在 imgs 上画框并保存'''
|
||||
imgs_dw = draw_tracking_boxes(imgs, trackerboxes)
|
||||
for fid, img in imgs_dw:
|
||||
img_savepath = os.path.join(savedir, CamerType + "_fid_" + f"{fid}.png")
|
||||
cv2.imwrite(img_savepath, img)
|
||||
|
||||
# =============================================================================
|
||||
# '''4.3.1 保存轨迹分析对应的子图'''
|
||||
# for track in vts.Residual:
|
||||
# for *xyxy, tid, conf, cls, fid, bid in track.boxes:
|
||||
# img = imgs[int(fid-1)]
|
||||
# x1, y1, x2, y2 = int(xyxy[0]/2), int(xyxy[1]/2), int(xyxy[2]/2), int(xyxy[3]/2)
|
||||
# subimg = img[y1:y2, x1:x2]
|
||||
#
|
||||
# subimg_path = os.path.join(subimg_dir, f'{CamerType}_tid{int(tid)}_{int(fid-1)}_{int(bid)}.png' )
|
||||
# cv2.imwrite(subimg_path, subimg)
|
||||
# =============================================================================
|
||||
|
||||
'''4.3.2 保存轨迹选择对应的子图'''
|
||||
for track in tracking_output_boxes:
|
||||
for *xyxy, tid, conf, cls, fid, bid in track:
|
||||
img = imgs[int(fid-1)]
|
||||
x1, y1, x2, y2 = int(xyxy[0]/2), int(xyxy[1]/2), int(xyxy[2]/2), int(xyxy[3]/2)
|
||||
subimg = img[y1:y2, x1:x2]
|
||||
|
||||
subimg_path = os.path.join(subimg_dir, f'{CamerType}_tid{int(tid)}_{int(fid-1)}_{int(bid)}.png' )
|
||||
cv2.imwrite(subimg_path, subimg)
|
||||
|
||||
|
||||
return abimg
|
||||
|
||||
|
||||
def main_loop():
|
||||
del_barcode_file = r'\\192.168.1.28\share\测试_202406\deletedBarcode\bad\deletedBarcode_0719_4.txt'
|
||||
basepath = r'\\192.168.1.28\share\测试_202406\0719\719_4' # 测试数据文件夹地址
|
||||
SavePath = r'D:\contrast\dataset\result' # 结果保存地址
|
||||
del_barcode_file = r'\\192.168.1.28\share\测试_202406\0723\0723_2\deletedBarcode.txt'
|
||||
basepath = r'\\192.168.1.28\share\测试_202406\0723\0723_2' # 测试数据文件夹地址
|
||||
SavePath = r'D:\contrast\dataset\resultx' # 结果保存地址
|
||||
prefix = ["getout_", "input_", "error_"]
|
||||
|
||||
|
||||
@ -354,11 +363,16 @@ def main_loop():
|
||||
savepath = os.path.join(SavePath, sdir)
|
||||
if not os.path.exists(savepath):
|
||||
os.makedirs(savepath)
|
||||
|
||||
|
||||
for path in tuple_paths:
|
||||
'''============= 分别指定指定存储、读取对应的文件夹 ============='''
|
||||
# if sdir.find('094631_6904724022444_6976075000082') < 0: continue
|
||||
# if path.find('094631_') < 0: continue
|
||||
|
||||
imgs = []
|
||||
for filename in os.listdir(path):
|
||||
# filename = '1_track.data'
|
||||
fpath = os.path.join(path, filename)
|
||||
|
||||
if os.path.isfile(fpath) and filename.find("track.data")>0:
|
||||
enent_name = ''
|
||||
|
||||
@ -368,35 +382,56 @@ def main_loop():
|
||||
enent_name = prefix[i] + name
|
||||
break
|
||||
spath = os.path.join(savepath, enent_name)
|
||||
do_tracking(fpath, spath)
|
||||
|
||||
k +=1
|
||||
if k==1:
|
||||
break
|
||||
|
||||
|
||||
# abimg = do_tracking(fpath, spath)
|
||||
# imgs.append(abimg)
|
||||
try:
|
||||
abimg = do_tracking(fpath, spath)
|
||||
imgs.append(abimg)
|
||||
if len(imgs) == 2:
|
||||
Img = np.concatenate((imgs[0], imgs[1]), axis = 0)
|
||||
|
||||
H, W = Img.shape[:2]
|
||||
cv2.line(Img, (0, int(H/2)), (int(W), int(H/2)), (128, 255, 128), 2)
|
||||
else:
|
||||
Img = imgs[0]
|
||||
|
||||
imgpath = os.path.join(savepath, enent_name + '_alg.png')
|
||||
cv2.imwrite(imgpath, Img)
|
||||
|
||||
except Exception as e:
|
||||
print(f'Error! {fpath}, {e}')
|
||||
|
||||
|
||||
|
||||
# k +=1
|
||||
# if k==1:
|
||||
# break
|
||||
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
'''
|
||||
fpath: data文件,包括 Pipeline 各模块输出
|
||||
fpath: data文件地址,该 data 文件包括 Pipeline 各模块输出
|
||||
save_dir:需包含二级目录,其中一级目录为轨迹图像;
|
||||
二级目录为与data文件对应的序列图像存储地址。
|
||||
'''
|
||||
|
||||
fpath = r'\\192.168.1.28\share\测试_202406\0719\719_4\20240719-164209_\0_track.data'
|
||||
save_dir = r'D:\contrast\dataset\result\20240719-164209_6971284204320_6902890247777\getout'
|
||||
fpath = r'\\192.168.1.28\share\测试_202406\0723\0723_1\20240723-101506_6906839615771\1_track.data'
|
||||
save_dir = r'D:\contrast\dataset\result\20240723-101506_\images'
|
||||
|
||||
do_tracking(fpath, save_dir)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
# main()
|
||||
main_loop()
|
||||
|
||||
except Exception as e:
|
||||
print(f'Error: {e}')
|
||||
|
||||
main_loop()
|
||||
# main()
|
||||
# try:
|
||||
# main_loop()
|
||||
# except Exception as e:
|
||||
# print(f'Error: {e}')
|
||||
|
||||
|
||||
|
||||
|
Binary file not shown.
Binary file not shown.
@ -286,7 +286,8 @@ def boxing_img(det, img, line_width=3):
|
||||
return imgx
|
||||
|
||||
def draw_tracking_boxes(imgs, tracks, scale=2):
|
||||
'''tracks: [x1, y1, x2, y2, track_id, score, cls, frame_index, box_index]
|
||||
'''需要确保 imgs 覆盖tracks中的帧ID数
|
||||
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 对应
|
||||
@ -311,12 +312,13 @@ def draw_tracking_boxes(imgs, tracks, scale=2):
|
||||
|
||||
bboxes = array2list(tracks)
|
||||
|
||||
if len(bboxes)!=len(imgs):
|
||||
return []
|
||||
# if len(bboxes)!=len(imgs):
|
||||
# return False, imgs
|
||||
|
||||
subimgs = []
|
||||
for i, boxes in enumerate(bboxes):
|
||||
annotator = Annotator(imgs[i].copy())
|
||||
fid = int(boxes[0, 7])
|
||||
annotator = Annotator(imgs[fid-1].copy())
|
||||
for *xyxy, tid, conf, cls, fid, bid in boxes:
|
||||
label = f'id:{int(tid)}_{int(cls)}_{conf:.2f}'
|
||||
|
||||
@ -331,7 +333,7 @@ def draw_tracking_boxes(imgs, tracks, scale=2):
|
||||
annotator.box_label(pt2, label, color=color)
|
||||
|
||||
img = annotator.result()
|
||||
subimgs.append(img)
|
||||
subimgs.append((fid-1, img))
|
||||
|
||||
return subimgs
|
||||
|
||||
|
@ -110,14 +110,17 @@ def extract_data(datapath):
|
||||
|
||||
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]})
|
||||
try:
|
||||
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]})
|
||||
except Exception as e:
|
||||
print(f'Path: {datapath}, Error: {e}')
|
||||
|
||||
return bboxes, ffeats, trackerboxes, tracker_feat_dict, trackingboxes, tracking_feat_dict
|
||||
|
||||
|
Reference in New Issue
Block a user