This commit is contained in:
王庆刚
2024-11-08 08:52:56 +08:00
parent 5ecc1285d4
commit c47894ddc0
11 changed files with 562 additions and 644 deletions

View File

@ -21,7 +21,7 @@ import sys
sys.path.append(r"D:\DetectTracking")
from tracking.utils.plotting import Annotator, colors
from tracking.utils.read_data import extract_data, read_deletedBarcode_file, read_tracking_output, read_returnGoods_file
from tracking.utils.plotting import draw_tracking_boxes
from tracking.utils.plotting import draw_tracking_boxes, get_subimgs
from contrast.utils.tools import showHist, show_recall_prec, compute_recall_precision
@ -148,13 +148,18 @@ def get_contrast_paths(pair, basepath):
if len(input_folds):
indice = np.argsort(np.array(times))
input_fold = input_folds[indice[-1]]
inputpath = os.path.join(basepath, input_fold)
'''取出操作错误匹配的放入操作对应的文件夹'''
if len(errmatch_folds):
indice = np.argsort(np.array(errmatch_times))
errmatch_fold = errmatch_folds[indice[-1]]
errorpath = os.path.join(basepath, errmatch_fold)
'''放入事件文件夹地址、取出事件文件夹地址'''
getoutpath = os.path.join(basepath, getout_fold)
@ -163,33 +168,33 @@ def get_contrast_paths(pair, basepath):
return getoutpath, inputpath, errorpath
def save_tracking_imgpairs(pair, basepath, savepath):
def save_tracking_imgpairs(pairs, savepath):
'''
basepath: 原始测试数据文件夹的路径
pairs: 匹配事件对
savepath: 保存的目标文件夹
'''
def get_event_path(evtpath):
basepath, eventname = os.path.split(evtpath)
evt_path = ''
for filename in os.listdir(basepath):
if filename.find(eventname)==0:
evt_path = os.path.join(basepath, filename)
break
return evt_path
getoutpath, inputpath, errorpath = get_contrast_paths(pair, basepath)
if len(inputpath)==0:
return
getoutpath = get_event_path(pairs[0])
inputpath = get_event_path(pairs[1])
if len(pairs) == 3:
errorpath = get_event_path(pairs[2])
else:
errorpath = ''
'''==== 读取放入、取出事件对应的 Yolo输入的前后摄图像0后摄1前摄 ===='''
'''==== 读取放入、取出事件对应的 tracking 输出boxes, feats ===='''
if len(inputpath):
imgs_input_0, imgs_input_1 = read_tracking_imgs(inputpath)
input_data_0 = os.path.join(inputpath, '0_tracking_output.data')
input_data_1 = os.path.join(inputpath, '1_tracking_output.data')
boxes_input_0, feats_input_0 = read_tracking_output(input_data_0)
boxes_input_1, feats_input_1 = read_tracking_output(input_data_1)
ImgsInput_0 = draw_tracking_boxes(imgs_input_0, boxes_input_0)
ImgsInput_1 = draw_tracking_boxes(imgs_input_1, boxes_input_1)
''' 1. 读取放入、取出事件对应的 Yolo输入的前后摄图像0后摄1前摄
2. 读取放入、取出事件对应的 tracking 输出boxes, feats
3. boxes绘制并保存图像序列
4. 截取并保存轨迹子图
'''
if len(getoutpath):
imgs_getout_0, imgs_getout_1 = read_tracking_imgs(getoutpath)
@ -199,9 +204,28 @@ def save_tracking_imgpairs(pair, basepath, savepath):
boxes_output_1, feats_output_1 = read_tracking_output(getout_data_1)
ImgsGetout_0 = draw_tracking_boxes(imgs_getout_0, boxes_output_0)
ImgsGetout_1 = draw_tracking_boxes(imgs_getout_1, boxes_output_1)
if len(errorpath):
SubimgsGetout_0 = get_subimgs(imgs_getout_0, boxes_output_0)
SubimgsGetout_1 = get_subimgs(imgs_getout_1, boxes_output_1)
savedir = os.path.basename(getoutpath)
if len(inputpath):
imgs_input_0, imgs_input_1 = read_tracking_imgs(inputpath)
input_data_0 = os.path.join(inputpath, '0_tracking_output.data')
input_data_1 = os.path.join(inputpath, '1_tracking_output.data')
boxes_input_0, feats_input_0 = read_tracking_output(input_data_0)
boxes_input_1, feats_input_1 = read_tracking_output(input_data_1)
ImgsInput_0 = draw_tracking_boxes(imgs_input_0, boxes_input_0)
ImgsInput_1 = draw_tracking_boxes(imgs_input_1, boxes_input_1)
SubimgsInput_0 = get_subimgs(imgs_input_0, boxes_input_0)
SubimgsInput_1 = get_subimgs(imgs_input_1, boxes_input_1)
savedir = savedir + '+' + os.path.basename(inputpath)
if len(errorpath):
imgs_error_0, imgs_error_1 = read_tracking_imgs(errorpath)
error_data_0 = os.path.join(errorpath, '0_tracking_output.data')
@ -211,37 +235,61 @@ def save_tracking_imgpairs(pair, basepath, savepath):
ImgsError_0 = draw_tracking_boxes(imgs_error_0, boxes_error_0)
ImgsError_1 = draw_tracking_boxes(imgs_error_1, boxes_error_1)
SubimgsError_0 = get_subimgs(imgs_error_0, boxes_error_0)
SubimgsError_1 = get_subimgs(imgs_error_0, boxes_error_0)
savedir = pair[0] + pair[1]
if len(errorpath):
savedir = savedir + '_' + errorpath.split('_')[-1]
foldname = os.path.join(savepath, 'imgpairs', savedir)
if not os.path.exists(foldname):
os.makedirs(foldname)
for i, img in enumerate(ImgsInput_0):
imgpath = os.path.join(foldname, f'input_0_{i}.png')
cv2.imwrite(imgpath, img)
for i, img in enumerate(ImgsInput_1):
imgpath = os.path.join(foldname, f'input_1_{i}.png')
cv2.imwrite(imgpath, img)
for i, img in enumerate(ImgsGetout_0):
imgpath = os.path.join(foldname, f'getout_0_{i}.png')
cv2.imwrite(imgpath, img)
for i, img in enumerate(ImgsGetout_1):
imgpath = os.path.join(foldname, f'getout_1_{i}.png')
cv2.imwrite(imgpath, img)
savedir = savedir + '+' + os.path.basename(errorpath)
for i, img in enumerate(ImgsError_0):
imgpath = os.path.join(foldname, f'errMatch_0_{i}.png')
''' savepath\pairs\savedir\eventpairs\保存画框后的图像序列 '''
entpairs = os.path.join(savepath, 'pairs', savedir, 'eventpairs')
if not os.path.exists(entpairs):
os.makedirs(entpairs)
for fid, img in ImgsInput_0:
imgpath = os.path.join(entpairs, f'input_0_{fid}.png')
cv2.imwrite(imgpath, img)
for i, img in enumerate(ImgsError_1):
imgpath = os.path.join(foldname, f'errMatch_1_{i}.png')
for fid, img in ImgsInput_1:
imgpath = os.path.join(entpairs, f'input_1_{fid}.png')
cv2.imwrite(imgpath, img)
for fid, img in ImgsGetout_0:
imgpath = os.path.join(entpairs, f'getout_0_{fid}.png')
cv2.imwrite(imgpath, img)
for fid, img in ImgsGetout_1:
imgpath = os.path.join(entpairs, f'getout_1_{fid}.png')
cv2.imwrite(imgpath, img)
if 'ImgsError_0' in vars() and 'ImgsError_1' in vars():
for fid, img in ImgsError_0:
imgpath = os.path.join(entpairs, f'errMatch_0_{fid}.png')
cv2.imwrite(imgpath, img)
for fid, img in ImgsError_1:
imgpath = os.path.join(entpairs, f'errMatch_1_{fid}.png')
cv2.imwrite(imgpath, img)
''' savepath\pairs\savedir\subimgpairs\保存轨迹子图 '''
subimgpairs = os.path.join(savepath, 'pairs', savedir, 'subimgpairs')
if not os.path.exists(subimgpairs):
os.makedirs(subimgpairs)
for fid, bid, img in SubimgsGetout_0:
imgpath = os.path.join(subimgpairs, f'getout_0_{fid}_{bid}.png')
cv2.imwrite(imgpath, img)
for fid, bid, img in SubimgsGetout_1:
imgpath = os.path.join(subimgpairs, f'getout_1_{fid}_{bid}.png')
cv2.imwrite(imgpath, img)
for fid, bid, img in SubimgsInput_0:
imgpath = os.path.join(subimgpairs, f'input_0_{fid}_{bid}.png')
cv2.imwrite(imgpath, img)
for fid, bid, img in SubimgsInput_1:
imgpath = os.path.join(subimgpairs, f'input_1_{fid}_{bid}.png')
cv2.imwrite(imgpath, img)
if 'SubimgsError_0' in vars() and 'SubimgsError_1' in vars():
for fid, bid, img in SubimgsError_0:
imgpath = os.path.join(subimgpairs, f'errMatch_0_{fid}_{bid}.png')
cv2.imwrite(imgpath, img)
for fid, bid, img in SubimgsError_1:
imgpath = os.path.join(subimgpairs, f'errMatch_1_{fid}_{bid}.png')
cv2.imwrite(imgpath, img)
def one2n_old(all_list):
def one2n_deleted(all_list):
corrpairs, errpairs, correct_similarity, err_similarity = [], [], [], []
for s_list in all_list:
seqdir = s_list['SeqDir'].strip()
@ -277,8 +325,9 @@ def one2n_old(all_list):
def one2n_new(all_list):
corrpairs, correct_similarity, errpairs, err_similarity = [], [], [], []
def one2n_return(all_list, basepath):
corrpairs, corr_similarity, errpairs, err_similarity = [], [], [], []
for s_list in all_list:
seqdir = s_list['SeqDir'].strip()
delete = s_list['Deleted'].strip()
@ -305,7 +354,7 @@ def one2n_new(all_list):
matched_barcode = barcodes[index]
if matched_barcode == delete:
corrpairs.append((seqdir, events[index]))
correct_similarity.append(max(similarity))
corr_similarity.append(max(similarity))
else:
idx = [i for i, name in enumerate(events) if name.split('_')[-1] == delete]
idxmax, simimax = -1, -1
@ -314,49 +363,80 @@ def one2n_new(all_list):
if similarity[k] > simimax:
idxmax = k
simimax = similarity[k]
errpairs.append((seqdir, events[idxmax], events[index]))
if idxmax>-1:
input_event = events[idxmax]
else:
input_event = ''
errpairs.append((seqdir, input_event, events[index]))
err_similarity.append(max(similarity))
return errpairs, corrpairs, err_similarity, correct_similarity
return corrpairs, errpairs, corr_similarity, err_similarity
# def contrast_analysis(del_barcode_file, basepath, savepath, saveimgs=False):
def get_relative_paths(del_barcode_file, basepath, savepath, saveimgs=False):
'''
del_barcode_file:
deletedBarcode.txt 格式的 1:n 数据结果文件
returnGoods.txt格式数据文件不需要调用该函数one2n_old() 函数返回的 errpairs
中元素为三元元组(取出,放入, 错误匹配)
'''
def test_rpath_deleted():
'''deletedBarcode.txt 格式的 1:n 数据结果文件, returnGoods.txt格式数据文件不需要调用该函数'''
del_bfile = r'\\192.168.1.28\share\测试_202406\709\deletedBarcode.txt'
basepath = r'\\192.168.1.28\share\测试_202406\709'
savepath = r'D:\DetectTracking\contrast\result'
saveimgs = True
relative_paths = []
'''1. 读取 deletedBarcode 文件 '''
all_list = read_deletedBarcode_file(del_barcode_file)
all_list = read_deletedBarcode_file(del_bfile)
'''2. 算法性能评估,并输出 (取出,删除, 错误匹配) 对 '''
errpairs, corrpairs, _, _ = one2n_old(all_list)
corrpairs, errpairs, _, _ = one2n_deleted(all_list)
'''3. 构造事件组合(取出,放入并删除, 错误匹配) 对应路径 '''
for errpair in errpairs:
GetoutPath, InputPath, ErrorPath = get_contrast_paths(errpair, basepath)
relative_paths.append((GetoutPath, InputPath, ErrorPath))
pairs = (GetoutPath, InputPath, ErrorPath)
relative_paths.append(pairs)
print(InputPath)
'''3. 获取 (取出,放入并删除, 错误匹配) 对应路径,保存相应轨迹图像'''
if saveimgs:
save_tracking_imgpairs(errpair, basepath, savepath)
if saveimgs:
save_tracking_imgpairs(pairs, savepath)
return relative_paths
def one2n_test():
fpath = r'\\192.168.1.28\share\测试_202406\deletedBarcode\other'
fpath = r'\\192.168.1.28\share\测试_202406\1030\images'
def test_rpath_return():
return_bfile = r'\\192.168.1.28\share\测试_202406\1101\images\returnGoods.txt'
basepath = r'\\192.168.1.28\share\测试_202406\1101\images'
savepath = r'D:\DetectTracking\contrast\result'
all_list = read_returnGoods_file(return_bfile)
corrpairs, errpairs, _, _ = one2n_return(all_list, basepath)
for corrpair in corrpairs:
GetoutPath = os.path.join(basepath, corrpair[0])
InputPath = os.path.join(basepath, corrpair[1])
pairs = (GetoutPath, InputPath)
save_tracking_imgpairs(pairs, savepath)
for errpair in errpairs:
GetoutPath = os.path.join(basepath, errpair[0])
InputPath = os.path.join(basepath, errpair[1])
ErrorPath = os.path.join(basepath, errpair[2])
pairs = (GetoutPath, InputPath, ErrorPath)
save_tracking_imgpairs(pairs, savepath)
def test_one2n():
'''
1:n 性能测试
兼容 2 种 txt 文件格式returnGoods.txt, deletedBarcode.txt
fpath: 文件路径、或文件夹,其中包含多个 txt 文件
savepath: pr曲线保存路径
'''
# fpath = r'\\192.168.1.28\share\测试_202406\deletedBarcode\other' # deletedBarcode.txt
fpath = r'\\192.168.1.28\share\测试_202406\returnGoods\all' # returnGoods.txt
savepath = r'\\192.168.1.28\share\测试_202406\deletedBarcode\illustration'
if not os.path.exists(savepath):
os.mkdir(savepath)
if os.path.isdir(fpath):
filepaths = [os.path.join(fpath, f) for f in os.listdir(fpath)
@ -366,37 +446,27 @@ def one2n_test():
filepaths = [fpath]
else:
return
FileFormat = {}
if not os.path.exists(savepath):
os.mkdir(savepath)
BarLists, blists = {}, []
for pth in filepaths:
file = str(Path(pth).stem)
if file.find('deletedBarcode')>=0:
FileFormat[file] = 'deletedBarcode'
blist = read_deletedBarcode_file(pth)
elif file.find('returnGoods')>=0:
FileFormat[file] = 'returnGoods'
if file.find('returnGoods')>=0:
blist = read_returnGoods_file(pth)
else:
return
BarLists.update({file: blist})
blists.extend(blist)
BarLists.update({file: blist})
BarLists.update({"Total": blists})
if len(blists): BarLists.update({"Total": blists})
for file, blist in BarLists.items():
if FileFormat[file] == 'deletedBarcode':
_, _, err_similarity, correct_similarity = one2n_old(blist)
elif FileFormat[file] == 'returnGoods':
_, _, err_similarity, correct_similarity = one2n_new(blist)
else:
_, _, err_similarity, correct_similarity = one2n_old(blist)
if all(b['filetype']=="deletedBarcode" for b in blist):
_, _, correct_similarity, err_similarity = one2n_deleted(blist)
if all(b['filetype']=="returnGoods" for b in blists):
_, _, correct_similarity, err_similarity = one2n_return(blist)
recall, prec, ths = compute_recall_precision(err_similarity, correct_similarity)
@ -413,51 +483,16 @@ def one2n_test():
def test_getreltpath():
'''
适用于deletedBarcode.txt不适用于returnGoods.txt
'''
del_barcode_file = r'\\192.168.1.28\share\测试_202406\709\deletedBarcode.txt'
basepath = r'\\192.168.1.28\share\测试_202406\709'
# del_barcode_file = r'\\192.168.1.28\share\测试_202406\1030\images\returnGoods.txt'
# basepath = r'\\192.168.1.28\share\测试_202406\1030\images'
savepath = r'D:\contrast\dataset\result'
saveimgs = True
try:
relative_path = get_relative_paths(del_barcode_file, basepath, savepath, saveimgs)
except Exception as e:
print(f'Error Type: {e}')
if __name__ == '__main__':
one2n_test()
# test_getreltpath()
# test_one2n()
test_rpath_return() # returnGoods.txt
test_rpath_deleted() # deleteBarcode.txt
# try:
# test_rpath_return()
# test_rpath_deleted()
# except Exception as e:
# print(e)