update
This commit is contained in:
@ -16,60 +16,13 @@ import shutil
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import cv2
|
||||
|
||||
from pathlib import Path
|
||||
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
|
||||
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
|
||||
|
||||
|
||||
|
||||
def showHist(err, correct):
|
||||
err = np.array(err)
|
||||
correct = np.array(correct)
|
||||
|
||||
fig, axs = plt.subplots(2, 1)
|
||||
axs[0].hist(err, bins=50, edgecolor='black')
|
||||
axs[0].set_xlim([0, 1])
|
||||
axs[0].set_title('err')
|
||||
|
||||
axs[1].hist(correct, bins=50, edgecolor='black')
|
||||
axs[1].set_xlim([0, 1])
|
||||
axs[1].set_title('correct')
|
||||
# plt.show()
|
||||
|
||||
return plt
|
||||
|
||||
def show_recall_prec(recall, prec, ths):
|
||||
# x = np.linspace(start=-0, stop=1, num=11, endpoint=True).tolist()
|
||||
fig = plt.figure(figsize=(10, 6))
|
||||
plt.plot(ths, recall, color='red', label='recall')
|
||||
plt.plot(ths, prec, color='blue', label='PrecisePos')
|
||||
plt.legend()
|
||||
plt.xlabel(f'threshold')
|
||||
# plt.ylabel('Similarity')
|
||||
plt.grid(True, linestyle='--', alpha=0.5)
|
||||
# plt.savefig('accuracy_recall_grid.png')
|
||||
# plt.show()
|
||||
# plt.close()
|
||||
|
||||
return plt
|
||||
|
||||
|
||||
def compute_recall_precision(err_similarity, correct_similarity):
|
||||
ths = np.linspace(0, 1, 51)
|
||||
recall, prec = [], []
|
||||
for th in ths:
|
||||
TP = len([num for num in correct_similarity if num >= th])
|
||||
FP = len([num for num in err_similarity if num >= th])
|
||||
if (TP+FP) == 0:
|
||||
prec.append(1)
|
||||
recall.append(0)
|
||||
else:
|
||||
prec.append(TP / (TP + FP))
|
||||
recall.append(TP / (len(err_similarity) + len(correct_similarity)))
|
||||
return recall, prec, ths
|
||||
from contrast.utils.tools import showHist, show_recall_prec, compute_recall_precision
|
||||
|
||||
|
||||
# =============================================================================
|
||||
@ -129,7 +82,7 @@ def read_tracking_imgs(imgspath):
|
||||
|
||||
return imgs_0, imgs_1
|
||||
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# def draw_tracking_boxes(imgs, tracks):
|
||||
# '''tracks: [x1, y1, x2, y2, track_id, score, cls, frame_index, box_index]
|
||||
@ -287,27 +240,9 @@ def save_tracking_imgpairs(pair, basepath, savepath):
|
||||
cv2.imwrite(imgpath, img)
|
||||
|
||||
|
||||
# def performance_evaluate(all_list, isshow=False):
|
||||
|
||||
# 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 = [float(s.strip()) for s in s_list['similarity']]
|
||||
|
||||
# if delete in barcodes[:1]:
|
||||
# corrpairs.append((seqdir, delete))
|
||||
# correct_barcode_list.append(delete)
|
||||
# correct_similarity.append(similarity[0])
|
||||
# else:
|
||||
# errpairs.append((seqdir, delete, barcodes[0]))
|
||||
# err_barcode_list.append(delete)
|
||||
# err_similarity.append(similarity[0])
|
||||
|
||||
def performance_evaluate(all_list, isshow=False):
|
||||
|
||||
corrpairs, correct_barcode_list, correct_similarity, errpairs, err_barcode_list, err_similarity = [], [], [], [], [], []
|
||||
def one2n_old(all_list):
|
||||
corrpairs, errpairs, correct_similarity, err_similarity = [], [], [], []
|
||||
for s_list in all_list:
|
||||
seqdir = s_list['SeqDir'].strip()
|
||||
delete = s_list['Deleted'].strip()
|
||||
@ -332,70 +267,136 @@ def performance_evaluate(all_list, isshow=False):
|
||||
matched_barcode = barcodes[index]
|
||||
if matched_barcode == delete:
|
||||
corrpairs.append((seqdir, delete))
|
||||
correct_barcode_list.append(delete)
|
||||
correct_similarity.append(max(similarity))
|
||||
else:
|
||||
errpairs.append((seqdir, delete, matched_barcode))
|
||||
err_barcode_list.append(delete)
|
||||
err_similarity.append(max(similarity))
|
||||
|
||||
'''3. 计算比对性能 '''
|
||||
if isshow:
|
||||
recall, prec, ths = compute_recall_precision(err_similarity, correct_similarity)
|
||||
show_recall_prec(recall, prec, ths)
|
||||
showHist(err_similarity, correct_similarity)
|
||||
|
||||
return errpairs, corrpairs, err_similarity, correct_similarity
|
||||
|
||||
return corrpairs, errpairs, correct_similarity, err_similarity
|
||||
|
||||
|
||||
def contrast_analysis(del_barcode_file, basepath, savepath, saveimgs=False):
|
||||
'''
|
||||
del_barcode_file: 测试数据文件,利用该文件进行算法性能分析
|
||||
|
||||
def one2n_new(all_list):
|
||||
corrpairs, correct_similarity, errpairs, 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']]
|
||||
events = [s.strip() for s in s_list['event']]
|
||||
types = [s.strip() for s in s_list['type']]
|
||||
|
||||
## =================== 读入相似度值
|
||||
similarity_comp, similarity_front = [], []
|
||||
for simil in s_list['similarity']:
|
||||
ss = [float(s.strip()) for s in simil.split(',')]
|
||||
|
||||
similarity_comp.append(ss[0])
|
||||
if len(ss)==3:
|
||||
similarity_front.append(ss[2])
|
||||
|
||||
if len(similarity_front):
|
||||
similarity = [s for s in similarity_front]
|
||||
else:
|
||||
similarity = [s for s in similarity_comp]
|
||||
|
||||
|
||||
index = similarity.index(max(similarity))
|
||||
matched_barcode = barcodes[index]
|
||||
if matched_barcode == delete:
|
||||
corrpairs.append((seqdir, events[index]))
|
||||
correct_similarity.append(max(similarity))
|
||||
else:
|
||||
idx = [i for i, name in enumerate(events) if name.split('_')[-1] == delete]
|
||||
idxmax, simimax = -1, -1
|
||||
# idxmax, simimax = k, similarity[k] for k in idx if similarity[k] > simimax
|
||||
for k in idx:
|
||||
if similarity[k] > simimax:
|
||||
idxmax = k
|
||||
simimax = similarity[k]
|
||||
|
||||
errpairs.append((seqdir, events[idxmax], events[index]))
|
||||
err_similarity.append(max(similarity))
|
||||
|
||||
|
||||
return errpairs, corrpairs, err_similarity, correct_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
|
||||
中元素为三元元组(取出,放入, 错误匹配)
|
||||
'''
|
||||
relative_paths = []
|
||||
|
||||
'''1. 读取 deletedBarcode 文件 '''
|
||||
all_list = read_deletedBarcode_file(del_barcode_file)
|
||||
|
||||
|
||||
'''2. 算法性能评估,并输出 (取出,删除, 错误匹配) 对 '''
|
||||
errpairs, corrpairs, _, _ = performance_evaluate(all_list)
|
||||
|
||||
'''3. 获取 (取出,删除, 错误匹配) 对应路径,保存相应轨迹图像'''
|
||||
relative_paths = []
|
||||
errpairs, corrpairs, _, _ = one2n_old(all_list)
|
||||
|
||||
'''3. 构造事件组合(取出,放入并删除, 错误匹配) 对应路径 '''
|
||||
for errpair in errpairs:
|
||||
GetoutPath, InputPath, ErrorPath = get_contrast_paths(errpair, basepath)
|
||||
relative_paths.append((GetoutPath, InputPath, ErrorPath))
|
||||
|
||||
|
||||
'''3. 获取 (取出,放入并删除, 错误匹配) 对应路径,保存相应轨迹图像'''
|
||||
if saveimgs:
|
||||
save_tracking_imgpairs(errpair, basepath, savepath)
|
||||
|
||||
return relative_paths
|
||||
|
||||
|
||||
def contrast_loop(fpath):
|
||||
def one2n_test():
|
||||
fpath = r'\\192.168.1.28\share\测试_202406\deletedBarcode\other'
|
||||
fpath = r'\\192.168.1.28\share\测试_202406\1030\images'
|
||||
|
||||
savepath = r'\\192.168.1.28\share\测试_202406\deletedBarcode\illustration'
|
||||
# savepath = r'D:\contrast\dataset\1_to_n\illustration'
|
||||
if not os.path.exists(savepath):
|
||||
os.mkdir(savepath)
|
||||
|
||||
if os.path.isfile(fpath):
|
||||
fpath, filename = os.path.split(fpath)
|
||||
|
||||
if os.path.isdir(fpath):
|
||||
filepaths = [os.path.join(fpath, f) for f in os.listdir(fpath)
|
||||
if f.find('.txt')>0
|
||||
and (f.find('deletedBarcode')>=0 or f.find('returnGoods')>=0)]
|
||||
elif os.path.isfile(fpath):
|
||||
filepaths = [fpath]
|
||||
else:
|
||||
return
|
||||
|
||||
|
||||
FileFormat = {}
|
||||
|
||||
BarLists, blists = {}, []
|
||||
for filename in os.listdir(fpath):
|
||||
file = os.path.splitext(filename)[0][15:]
|
||||
|
||||
filepath = os.path.join(fpath, filename)
|
||||
blist = read_deletedBarcode_file(filepath)
|
||||
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'
|
||||
blist = read_returnGoods_file(pth)
|
||||
else:
|
||||
return
|
||||
|
||||
|
||||
BarLists.update({file: blist})
|
||||
blists.extend(blist)
|
||||
|
||||
BarLists.update({file: blist})
|
||||
BarLists.update({"Total": blists})
|
||||
for file, blist in BarLists.items():
|
||||
errpairs, corrpairs, err_similarity, correct_similarity = performance_evaluate(blist)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
recall, prec, ths = compute_recall_precision(err_similarity, correct_similarity)
|
||||
|
||||
@ -411,25 +412,33 @@ def contrast_loop(fpath):
|
||||
# plt.close()
|
||||
|
||||
|
||||
def main():
|
||||
fpath = r'\\192.168.1.28\share\测试_202406\deletedBarcode\other'
|
||||
contrast_loop(fpath)
|
||||
|
||||
def main1():
|
||||
|
||||
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'
|
||||
savepath = r'D:\contrast\dataset\result'
|
||||
|
||||
# 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 = contrast_analysis(del_barcode_file, basepath, savepath)
|
||||
relative_path = get_relative_paths(del_barcode_file, basepath, savepath, saveimgs)
|
||||
except Exception as e:
|
||||
print(f'Error Type: {e}')
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
# main1()
|
||||
one2n_test()
|
||||
|
||||
# test_getreltpath()
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user