update for bakeup

This commit is contained in:
王庆刚
2024-08-06 20:00:29 +08:00
parent 27d57b21d4
commit 5109400a57
19 changed files with 497 additions and 352 deletions

View File

@ -2,6 +2,8 @@
"""
Created on Sat Jul 27 14:07:25 2024
现场测试数据,在不同特征组合情况下的精度、召回率分析程序
@author: ym
"""
import os.path
@ -13,7 +15,8 @@ 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
from tracking.contrast_analysis import compute_recall_precision, show_recall_prec
from tracking.contrast_analysis import performance_evaluate
def compute_similar(feat1, feat2):
@ -25,10 +28,6 @@ def compute_similar(feat1, feat2):
return smean
def update_event(datapath):
'''一次购物事件,包含 8 个keys'''
@ -47,6 +46,38 @@ def update_event(datapath):
event['feats_compose'] = np.empty((0, 256), dtype=np.float64)
event['feats_select'] = np.empty((0, 256), dtype=np.float64)
# '''读取 track.data 文件中的数据'''
# fpath_0_track = os.path.join(datapath, '0_track.data')
# fpath_1_track = os.path.join(datapath, '1_track.data')
# if os.path.exists(fpath_0_track) and os.path.isfile(fpath_0_track):
# _, _, _, _, tracking_boxes, tracking_feat_dict = extract_data(fpath_0_track)
# event['back_tracking_boxes'] = tracking_boxes
# event['back_tracking_feats'] = tracking_feat_dict
# if os.path.exists(fpath_1_track) and os.path.isfile(fpath_1_track):
# _, _, _, _, tracking_boxes, tracking_feat_dict = extract_data(fpath_1_track)
# event['front_tracking_boxes'] = tracking_boxes
# event['front_tracking_feats'] = tracking_feat_dict
# =============================================================================
# '''================1. 直接指定读取,速度快======================================'''
# '''读取 tracking_output.data 文件中的数据'''
# fpath_0_tracking = os.path.join(datapath, '0_tracking_output.data')
# fpath_1_tracking = os.path.join(datapath, '1_tracking_output.data')
# if os.path.exists(fpath_0_tracking) and os.path.isfile(fpath_0_tracking):
# tracking_output_boxes, tracking_output_feats = read_tracking_output(fpath_0_tracking)
# event['back_sole_boxes'] = tracking_output_boxes
# event['back_sole_feats'] = tracking_output_feats
#
# if os.path.exists(fpath_1_tracking) and os.path.isfile(fpath_1_tracking):
# tracking_output_boxes, tracking_output_feats = read_tracking_output(fpath_1_tracking)
# event['front_sole_boxes'] = tracking_output_boxes
# event['front_sole_feats'] = tracking_output_feats
# =============================================================================
'''================2. 遍历方式读取(与以上方式二选一)======================================'''
'''读取当前事件的 data 文件'''
for filename in os.listdir(datapath):
# filename = '1_track.data'
@ -61,8 +92,6 @@ def update_event(datapath):
# 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)
@ -75,7 +104,6 @@ def update_event(datapath):
'''事件的特征表征方式选择'''
fs_feats = event['front_sole_feats']
bs_feats = event['back_sole_feats']
@ -100,50 +128,56 @@ def update_event(datapath):
'''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
# =============================================================================
# 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)
MatchList = []
k = 0
for s_list in bcdlist:
getout_fold = s_list['SeqDir'].strip()
getout_path = os.path.join(basepath, getout_fold)
'''取出事件文件夹不存在,跳出循环'''
if not os.path.exists(getout_path) and not os.path.isdir(getout_path):
continue
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['path'] = getout_path
getout_event['feats_compose'] = np.empty((0, 256), dtype=np.float64)
getout_event['feats_select'] = np.empty((0, 256), dtype=np.float64)
@ -164,20 +198,21 @@ def creatd_deletedBarcode_front(filepath):
infold = pathname.split('_')
if len(infold)!=2: continue
if len(infold[0])<=14 or len(infold[1])<=10: 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))
if len(input_folds)==0: continue
''' 根据时间排序,选择离取出操作最近时间的文件夹,作为取出操作应的放入操作所对应的文件夹 '''
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_fold = input_folds[times.index(max(times))]
input_path = os.path.join(basepath, input_fold)
if not os.path.exists(getout_path) and not os.path.isdir(getout_path):
continue
input_event['barcode'] = barcode
input_event['path'] = input_path
input_event['similarity'] = float(similarity[i])
@ -192,22 +227,23 @@ def creatd_deletedBarcode_front(filepath):
# k += 1
# if k==2:
# break
print('Step 1 Done!')
print('Step 1: Event init 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)
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!')
print('Step 2: Event update Done!')
results = []
for getout_event, InputList in MatchList:
getout_barcode = getout_event['barcode']
@ -242,10 +278,10 @@ def creatd_deletedBarcode_front(filepath):
# result[f'{input_barcode}'] = (input_event['similarity'], similar_comp, similar_selt)
results.append(result)
print('Step 3 Done!')
print('Step 3: Similarity conputation Done!')
wpath = os.path.split(filepath)[0]
wfile = os.path.join(wpath, 'deletedBarcodeTest.txt')
wfile = os.path.join(wpath, 'deletedBarcodeTest_x.txt')
with open(wfile, 'w', encoding='utf-8') as file:
for result in results:
@ -260,52 +296,13 @@ def creatd_deletedBarcode_front(filepath):
file.write(f'{key}: ')
file.write(f'{value[0]}, {value[1]:.3f}, {value[2]:.3f}\n')
print('Step 4 Done!')
print('Step 4: File writting 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):
def compute_precision(filepath, savepath):
fpath = os.path.split(filepath)[0]
@ -330,7 +327,7 @@ def compute_pres(filepath, savepath):
'''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_errpairs, front_corrpairs, front_err_similarity, front_correct_similarity = 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)
@ -341,56 +338,61 @@ def compute_pres(filepath, savepath):
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',
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\719_1\deletedBarcode.txt',
# r'\\192.168.1.28\share\测试_202406\0719\719_2\deletedBarcode.txt',
# r'\\192.168.1.28\share\测试_202406\0719\719_3\deletedBarcode.txt',
# r'\\192.168.1.28\share\测试_202406\0719\719_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_1\deletedBarcode.txt',
# r'\\192.168.1.28\share\测试_202406\0712\0712_2\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\702_pm_1\deletedBarcode.txt',
# r'\\192.168.1.28\share\测试_202406\702_pm_3\702_pm_3\deletedBarcode.txt',
# r'\\192.168.1.28\share\测试_202406\701_am\deletedBarcode.txt',
# r'\\192.168.1.28\share\测试_202406\701_pm\701_pm\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',
]
fplist = [#r'\\192.168.1.28\share\测试_202406\0723\0723_1\deletedBarcode.txt',
# r'\\192.168.1.28\share\测试_202406\0723\0723_3\deletedBarcode.txt',
r'\\192.168.1.28\share\测试_202406\0723\0723_3\deletedBarcodeTest.txt',
]
savepath = r'\\192.168.1.28\share\测试_202406\deletedBarcode\illustration'
for filepath in fplist:
print(filepath)
# creatd_deletedBarcode_front(filepath)
compute_precision(filepath, savepath)
try:
creatd_deletedBarcode_front(filepath)
compute_pres(filepath, savepath)
except Exception as e:
print(f'Error: {e}')
# try:
# creatd_deletedBarcode_front(filepath)
# compute_pres(filepath, savepath)
# except Exception as e:
# print(f'{filepath}, Error: {e}')
if __name__ == '__main__':
main()