20241217
This commit is contained in:
@ -84,82 +84,84 @@ def ft16_to_uint8(arr_ft16):
|
||||
return arr_uint8, arr_ft16_
|
||||
|
||||
|
||||
def plot_save_image(event, savepath):
|
||||
cameras = ('front', 'back')
|
||||
for camera in cameras:
|
||||
if camera == 'front':
|
||||
boxes = event.front_trackerboxes
|
||||
imgpaths = event.front_imgpaths
|
||||
else:
|
||||
boxes = event.back_trackerboxes
|
||||
imgpaths = event.back_imgpaths
|
||||
|
||||
def array2list(bboxes):
|
||||
'''[x1, y1, x2, y2, track_id, score, cls, frame_index, box_index]'''
|
||||
frame_ids = bboxes[:, 7].astype(int)
|
||||
fID = np.unique(bboxes[:, 7].astype(int))
|
||||
fboxes = []
|
||||
for f_id in fID:
|
||||
idx = np.where(frame_ids==f_id)[0]
|
||||
box = bboxes[idx, :]
|
||||
fboxes.append((f_id, box))
|
||||
return fboxes
|
||||
|
||||
fboxes = array2list(boxes)
|
||||
|
||||
for fid, fbox in fboxes:
|
||||
imgpath = imgpaths[int(fid-1)]
|
||||
|
||||
image = cv2.imread(imgpath)
|
||||
|
||||
annotator = Annotator(image.copy(), line_width=2)
|
||||
for i, *xyxy, tid, score, cls, fid, bid in enumerate(fbox):
|
||||
label = f'{int(id), int(cls)}'
|
||||
if tid >=0 and cls==0:
|
||||
color = colors(int(cls), True)
|
||||
elif tid >=0 and cls!=0:
|
||||
color = colors(int(id), True)
|
||||
else:
|
||||
color = colors(19, True) # 19为调色板的最后一个元素
|
||||
annotator.box_label(xyxy, label, color=color)
|
||||
|
||||
im0 = annotator.result()
|
||||
spath = os.path.join(savepath, Path(imgpath).name)
|
||||
cv2.imwrite(spath, im0)
|
||||
|
||||
|
||||
def save_event_subimg(event, savepath):
|
||||
'''
|
||||
功能: 保存一次购物事件的轨迹子图
|
||||
9 items: barcode, type, filepath, back_imgpaths, front_imgpaths,
|
||||
back_boxes, front_boxes, back_feats, front_feats,
|
||||
feats_compose, feats_select
|
||||
子图保存次序:先前摄、后后摄,以 k 为编号,和 "feats_compose" 中次序相同
|
||||
'''
|
||||
cameras = ('front', 'back')
|
||||
for camera in cameras:
|
||||
if camera == 'front':
|
||||
boxes = event.front_boxes
|
||||
imgpaths = event.front_imgpaths
|
||||
else:
|
||||
boxes = event.back_boxes
|
||||
imgpaths = event.back_imgpaths
|
||||
|
||||
for i, box in enumerate(boxes):
|
||||
x1, y1, x2, y2, tid, score, cls, fid, bid = box
|
||||
|
||||
imgpath = imgpaths[int(fid-1)]
|
||||
image = cv2.imread(imgpath)
|
||||
|
||||
subimg = image[int(y1/2):int(y2/2), int(x1/2):int(x2/2), :]
|
||||
|
||||
camerType, timeTamp, _, frameID = os.path.basename(imgpath).split('.')[0].split('_')
|
||||
subimgName = f"cam{camerType}_{i}_tid{int(tid)}_fid({int(fid)}, {frameID}).png"
|
||||
spath = os.path.join(savepath, subimgName)
|
||||
|
||||
cv2.imwrite(spath, subimg)
|
||||
# basename = os.path.basename(event['filepath'])
|
||||
print(f"Image saved: {os.path.basename(event.eventpath)}")
|
||||
# =============================================================================
|
||||
# def plot_save_image(event, savepath):
|
||||
# cameras = ('front', 'back')
|
||||
# for camera in cameras:
|
||||
# if camera == 'front':
|
||||
# boxes = event.front_trackerboxes
|
||||
# imgpaths = event.front_imgpaths
|
||||
# else:
|
||||
# boxes = event.back_trackerboxes
|
||||
# imgpaths = event.back_imgpaths
|
||||
#
|
||||
# def array2list(bboxes):
|
||||
# '''[x1, y1, x2, y2, track_id, score, cls, frame_index, box_index]'''
|
||||
# frame_ids = bboxes[:, 7].astype(int)
|
||||
# fID = np.unique(bboxes[:, 7].astype(int))
|
||||
# fboxes = []
|
||||
# for f_id in fID:
|
||||
# idx = np.where(frame_ids==f_id)[0]
|
||||
# box = bboxes[idx, :]
|
||||
# fboxes.append((f_id, box))
|
||||
# return fboxes
|
||||
#
|
||||
# fboxes = array2list(boxes)
|
||||
#
|
||||
# for fid, fbox in fboxes:
|
||||
# imgpath = imgpaths[int(fid-1)]
|
||||
#
|
||||
# image = cv2.imread(imgpath)
|
||||
#
|
||||
# annotator = Annotator(image.copy(), line_width=2)
|
||||
# for i, *xyxy, tid, score, cls, fid, bid in enumerate(fbox):
|
||||
# label = f'{int(id), int(cls)}'
|
||||
# if tid >=0 and cls==0:
|
||||
# color = colors(int(cls), True)
|
||||
# elif tid >=0 and cls!=0:
|
||||
# color = colors(int(id), True)
|
||||
# else:
|
||||
# color = colors(19, True) # 19为调色板的最后一个元素
|
||||
# annotator.box_label(xyxy, label, color=color)
|
||||
#
|
||||
# im0 = annotator.result()
|
||||
# spath = os.path.join(savepath, Path(imgpath).name)
|
||||
# cv2.imwrite(spath, im0)
|
||||
#
|
||||
#
|
||||
# def save_event_subimg(event, savepath):
|
||||
# '''
|
||||
# 功能: 保存一次购物事件的轨迹子图
|
||||
# 9 items: barcode, type, filepath, back_imgpaths, front_imgpaths,
|
||||
# back_boxes, front_boxes, back_feats, front_feats,
|
||||
# feats_compose, feats_select
|
||||
# 子图保存次序:先前摄、后后摄,以 k 为编号,和 "feats_compose" 中次序相同
|
||||
# '''
|
||||
# cameras = ('front', 'back')
|
||||
# for camera in cameras:
|
||||
# if camera == 'front':
|
||||
# boxes = event.front_boxes
|
||||
# imgpaths = event.front_imgpaths
|
||||
# else:
|
||||
# boxes = event.back_boxes
|
||||
# imgpaths = event.back_imgpaths
|
||||
#
|
||||
# for i, box in enumerate(boxes):
|
||||
# x1, y1, x2, y2, tid, score, cls, fid, bid = box
|
||||
#
|
||||
# imgpath = imgpaths[int(fid-1)]
|
||||
# image = cv2.imread(imgpath)
|
||||
#
|
||||
# subimg = image[int(y1/2):int(y2/2), int(x1/2):int(x2/2), :]
|
||||
#
|
||||
# camerType, timeTamp, _, frameID = os.path.basename(imgpath).split('.')[0].split('_')
|
||||
# subimgName = f"cam{camerType}_{i}_tid{int(tid)}_fid({int(fid)}, {frameID}).png"
|
||||
# spath = os.path.join(savepath, subimgName)
|
||||
#
|
||||
# cv2.imwrite(spath, subimg)
|
||||
# # basename = os.path.basename(event['filepath'])
|
||||
# print(f"Image saved: {os.path.basename(event.eventpath)}")
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def data_precision_compare(stdfeat, evtfeat, evtMessage, save=True):
|
||||
@ -296,7 +298,11 @@ def one2one_simi():
|
||||
if not os.path.exists(pairpath):
|
||||
os.makedirs(pairpath)
|
||||
try:
|
||||
save_event_subimg(event, pairpath)
|
||||
subimgpairs = event.save_event_subimg(pairpath)
|
||||
for subimgName, subimg in subimgpairs:
|
||||
spath = os.path.join(pairpath, subimgName)
|
||||
cv2.imwrite(spath, subimg)
|
||||
|
||||
except Exception as e:
|
||||
error_event.append(evtname)
|
||||
|
||||
@ -304,10 +310,16 @@ def one2one_simi():
|
||||
if not os.path.exists(img_path):
|
||||
os.makedirs(img_path)
|
||||
try:
|
||||
plot_save_image(event, img_path)
|
||||
imgpairs = event.plot_save_image(img_path)
|
||||
for imgname, img in imgpairs:
|
||||
spath = os.path.join(img_path, imgname)
|
||||
cv2.imwrite(spath, img)
|
||||
except Exception as e:
|
||||
error_event.append(evtname)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
errfile = os.path.join(subimgPath, f'error_event.txt')
|
||||
with open(errfile, 'w', encoding='utf-8') as f:
|
||||
@ -353,17 +365,16 @@ def one2one_simi():
|
||||
matrix = 1 - cdist(stdfeat, evtfeat, 'cosine')
|
||||
matrix[matrix < 0] = 0
|
||||
|
||||
|
||||
simi_mean = np.mean(matrix)
|
||||
simi_max = np.max(matrix)
|
||||
stdfeatm = np.mean(stdfeat, axis=0, keepdims=True)
|
||||
evtfeatm = np.mean(evtfeat, axis=0, keepdims=True)
|
||||
simi_mfeat = 1- np.maximum(0.0, cdist(stdfeatm, evtfeatm, 'cosine'))
|
||||
rltdata.append((label, stdbcd, evtname, simi_mean, simi_max, simi_mfeat[0,0]))
|
||||
|
||||
|
||||
'''================ float32、16、int8 精度比较与存储 ============='''
|
||||
# data_precision_compare(stdfeat, evtfeat, mergePairs[i], save=True)
|
||||
|
||||
|
||||
print("func: one2one_eval(), have finished!")
|
||||
|
||||
return rltdata
|
||||
@ -436,8 +447,11 @@ def gen_eventdict(sourcePath, saveimg=True):
|
||||
errEvents = []
|
||||
k = 0
|
||||
for source_path in sourcePath:
|
||||
bname = os.path.basename(source_path)
|
||||
evtpath, bname = os.path.split(source_path)
|
||||
|
||||
bname = r"20241126-135911-bdf91cf9-3e9a-426d-94e8-ddf92238e175_6923555210479"
|
||||
source_path = os.path.join(evtpath, bname)
|
||||
|
||||
pickpath = os.path.join(eventDataPath, f"{bname}.pickle")
|
||||
if os.path.isfile(pickpath): continue
|
||||
|
||||
@ -451,9 +465,9 @@ def gen_eventdict(sourcePath, saveimg=True):
|
||||
errEvents.append(source_path)
|
||||
print(e)
|
||||
|
||||
# k += 1
|
||||
# if k==10:
|
||||
# break
|
||||
k += 1
|
||||
if k==1:
|
||||
break
|
||||
|
||||
errfile = os.path.join(eventDataPath, f'error_events.txt')
|
||||
with open(errfile, 'w', encoding='utf-8') as f:
|
||||
|
Reference in New Issue
Block a user