update for bakeup

This commit is contained in:
王庆刚
2024-07-22 20:16:45 +08:00
parent e986ec060b
commit 16543107f3
16 changed files with 179 additions and 52 deletions

View File

@ -10,6 +10,43 @@ sys.path.append(r"D:\DetectTracking")
from tracking.utils.read_data import extract_data, read_deletedBarcode_file, read_tracking_output
from tracking.utils.plotting import draw_tracking_boxes
VideoFormat = ['.mp4', '.avi']
def video2imgs(videopath, savepath):
k = 0
have = False
for filename in os.listdir(videopath):
file, ext = os.path.splitext(filename)
if ext not in VideoFormat:
continue
basename = os.path.basename(videopath)
imgbase = basename + '_' + file
imgdir = os.path.join(savepath, imgbase)
if not os.path.exists(imgdir):
os.mkdir(imgdir)
video = os.path.join(videopath, filename)
cap = cv2.VideoCapture(video)
i = 0
while True:
ret, frame = cap.read()
if not ret:
break
imgp = os.path.join(imgdir, file+f"_{i}.png")
i += 1
cv2.imwrite(imgp, frame)
cap.release()
print(filename + f" haved resolved")
k+=1
if k==1000:
break
def showHist(err, correct):
err = np.array(err)
correct = np.array(correct)
@ -22,24 +59,28 @@ def showHist(err, correct):
axs[1].hist(correct, bins=50, edgecolor='black')
axs[1].set_xlim([0, 1])
axs[1].set_title('correct')
plt.show()
# plt.show()
return plt
def showgrid(recall, prec, ths):
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('threshold')
plt.xlabel(f'threshold')
# plt.ylabel('Similarity')
plt.grid(True, linestyle='--', alpha=0.5)
plt.savefig('accuracy_recall_grid.png')
plt.show()
# 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, 11)
ths = np.linspace(0, 1, 51)
recall, prec = [], []
for th in ths:
TP = len([num for num in correct_similarity if num >= th])
@ -50,9 +91,7 @@ def compute_recall_precision(err_similarity, correct_similarity):
else:
prec.append(TP / (TP + FP))
recall.append(TP / (len(err_similarity) + len(correct_similarity)))
showgrid(recall, prec, ths)
return recall, prec
return recall, prec, ths
# =============================================================================
@ -291,7 +330,8 @@ def performance_evaluate(all_list, isshow=False):
'''3. 计算比对性能 '''
if isshow:
compute_recall_precision(err_similarity, correct_similarity)
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
@ -306,8 +346,7 @@ def contrast_analysis(del_barcode_file, basepath, savepath, saveimgs=False):
'''1. 读取 deletedBarcode 文件 '''
all_list = read_deletedBarcode_file(del_barcode_file)
'''2. 算法性能评估,并输出 (取出,删除, 错误匹配) 对 '''
errpairs, corrpairs, _, _ = performance_evaluate(all_list)
@ -319,12 +358,62 @@ def contrast_analysis(del_barcode_file, basepath, savepath, saveimgs=False):
if saveimgs:
save_tracking_imgpairs(errpair, basepath, savepath)
return relative_paths
def contrast_loop(fpath):
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)
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)
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)
recall, prec, ths = compute_recall_precision(err_similarity, correct_similarity)
plt1 = show_recall_prec(recall, prec, ths)
# plt1.show()
plt.xlabel(f'threshold, Num: {len(blist)}')
plt1.savefig(os.path.join(savepath, file+'_pr.png'))
# plt1.close()
# plt2 = showHist(err_similarity, correct_similarity)
# plt2.show()
# plt2.savefig(os.path.join(savepath, file+'_hist.png'))
# plt.close()
def main1():
fpath = r'D:\contrast\dataset\1_to_n\0719'
fpath = r'\\192.168.1.28\share\测试_202406\deletedBarcode\good'
contrast_loop(fpath)
def main():
del_barcode_file = 'D:/contrast/dataset/compairsonResult/deletedBarcode_20240709_pm.txt'
basepath = r'D:\contrast\dataset\1_to_n\709'
@ -335,13 +424,18 @@ def main():
except Exception as e:
print(f'Error Type: {e}')
def resolve_vidoes():
videopath = r"\\192.168.1.28\share\测试_202406\0719\719_1\20240719-103533_"
savepath = r"D:\contrast\result"
video2imgs(videopath, savepath)
if __name__ == '__main__':
main()
# main()
main1()
# resolve_vidoes()