modify pipeline.py
This commit is contained in:
Binary file not shown.
Binary file not shown.
@ -171,16 +171,6 @@ def calc_simil(event, stdfeat):
|
|||||||
return Similar
|
return Similar
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -345,20 +335,6 @@ def main():
|
|||||||
|
|
||||||
simi_matrix()
|
simi_matrix()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -136,8 +136,8 @@ def stdfeat_infer(imgPath, featPath, bcdSet=None):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
featpath = os.path.join(featPath, f"{bcd}.pickle")
|
featpath = os.path.join(featPath, f"{bcd}.pickle")
|
||||||
# if os.path.isfile(featpath):
|
if os.path.isfile(featpath):
|
||||||
# continue
|
continue
|
||||||
|
|
||||||
stdbDict = {}
|
stdbDict = {}
|
||||||
t1 = time.time()
|
t1 = time.time()
|
||||||
|
@ -24,10 +24,15 @@ def init_eventdict(sourcePath, stype="data"):
|
|||||||
# bname = r"20241126-135911-bdf91cf9-3e9a-426d-94e8-ddf92238e175_6923555210479"
|
# bname = r"20241126-135911-bdf91cf9-3e9a-426d-94e8-ddf92238e175_6923555210479"
|
||||||
|
|
||||||
source_path = os.path.join(sourcePath, bname)
|
source_path = os.path.join(sourcePath, bname)
|
||||||
if not os.path.isdir(source_path): continue
|
if stype=="data":
|
||||||
|
pickpath = os.path.join(eventDataPath, f"{bname}.pickle")
|
||||||
pickpath = os.path.join(eventDataPath, f"{bname}.pickle")
|
if not os.path.isdir(source_path) or os.path.isfile(pickpath):
|
||||||
if os.path.isfile(pickpath): continue
|
continue
|
||||||
|
if stype=="source":
|
||||||
|
pickpath = os.path.join(eventDataPath, bname)
|
||||||
|
if not os.path.isfile(source_path) or os.path.isfile(pickpath):
|
||||||
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
event = ShoppingEvent(source_path, stype)
|
event = ShoppingEvent(source_path, stype)
|
||||||
|
|
||||||
@ -86,20 +91,28 @@ def simi_calc(event, o2nevt, typee=None):
|
|||||||
feat1 = event.back_feats
|
feat1 = event.back_feats
|
||||||
feat2 = o2nevt.front_feats
|
feat2 = o2nevt.front_feats
|
||||||
|
|
||||||
|
'''自定义事件特征选择'''
|
||||||
|
if typee==3:
|
||||||
|
feat1 = event.feats_compose
|
||||||
|
feat2 = o2nevt.feats_compose
|
||||||
|
|
||||||
|
|
||||||
if len(feat1) and len(feat2):
|
if len(feat1) and len(feat2):
|
||||||
matrix = 1 - cdist(feat1[0], feat2[0], 'cosine')
|
matrix = 1 - cdist(feat1[0], feat2[0], 'cosine')
|
||||||
simi = np.mean(matrix)
|
simi = np.mean(matrix)
|
||||||
else:
|
else:
|
||||||
simi = None
|
simi = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return simi
|
return simi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def one2n_pr(evtDicts):
|
def one2n_pr(evtDicts, pattern=1):
|
||||||
|
'''
|
||||||
|
pattern:
|
||||||
|
1: process.data 中记录的相似度
|
||||||
|
2: 根据 process.data 中标记的 type 选择特征计算
|
||||||
|
3: 以其它方式选择特征计算
|
||||||
|
'''
|
||||||
|
|
||||||
tpevents, fnevents, fpevents, tnevents = [], [], [], []
|
tpevents, fnevents, fpevents, tnevents = [], [], [], []
|
||||||
tpsimi, fnsimi, tnsimi, fpsimi = [], [], [], []
|
tpsimi, fnsimi, tnsimi, fpsimi = [], [], [], []
|
||||||
errorFile_one2n = []
|
errorFile_one2n = []
|
||||||
@ -111,22 +124,30 @@ def one2n_pr(evtDicts):
|
|||||||
barcode = ndict["barcode"]
|
barcode = ndict["barcode"]
|
||||||
similar = ndict["similar"]
|
similar = ndict["similar"]
|
||||||
typee = ndict["type"].strip()
|
typee = ndict["type"].strip()
|
||||||
|
|
||||||
o2n_evt = [evt for name, evt in evtDicts.items() if name.find(nname[:15])==0]
|
|
||||||
if len(o2n_evt)==1:
|
|
||||||
o2nevt = o2n_evt[0]
|
|
||||||
else:
|
|
||||||
continue
|
|
||||||
|
|
||||||
simival = simi_calc(event, o2nevt, typee)
|
|
||||||
if simival==None:
|
|
||||||
continue
|
|
||||||
|
|
||||||
evt_names.append(nname)
|
evt_names.append(nname)
|
||||||
evt_barcodes.append(barcode)
|
evt_barcodes.append(barcode)
|
||||||
evt_similars.append(simival)
|
|
||||||
evt_types.append(typee)
|
evt_types.append(typee)
|
||||||
|
|
||||||
|
if pattern==1:
|
||||||
|
evt_similars.append(similar)
|
||||||
|
|
||||||
|
if pattern==2 or pattern==3:
|
||||||
|
o2n_evt = [evt for name, evt in evtDicts.items() if name.find(nname[:15])==0]
|
||||||
|
if len(o2n_evt)==1:
|
||||||
|
o2nevt = o2n_evt[0]
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if pattern==2:
|
||||||
|
simival = simi_calc(event, o2nevt, typee)
|
||||||
|
|
||||||
|
if pattern==3:
|
||||||
|
simival = simi_calc(event, o2nevt, typee=pattern)
|
||||||
|
|
||||||
|
if simival==None:
|
||||||
|
continue
|
||||||
|
evt_similars.append(simival)
|
||||||
|
|
||||||
if len(evt_names)==len(evt_barcodes) and len(evt_barcodes)==len(evt_similars) \
|
if len(evt_names)==len(evt_barcodes) and len(evt_barcodes)==len(evt_similars) \
|
||||||
and len(evt_similars)==len(evt_types) and len(evt_names)>0:
|
and len(evt_similars)==len(evt_types) and len(evt_names)>0:
|
||||||
@ -205,30 +226,25 @@ def one2n_pr(evtDicts):
|
|||||||
def main():
|
def main():
|
||||||
|
|
||||||
'''1. 生成事件字典并保存至 eventDataPath, 只需运行一次 '''
|
'''1. 生成事件字典并保存至 eventDataPath, 只需运行一次 '''
|
||||||
# init_eventdict(eventSourcePath)
|
init_eventdict(eventSourcePath, stype="source")
|
||||||
|
|
||||||
'''2. 读取事件字典 '''
|
'''2. 读取事件字典 '''
|
||||||
evtDicts = read_eventdict(eventDataPath)
|
evtDicts = read_eventdict(eventDataPath)
|
||||||
|
|
||||||
|
|
||||||
'''3. 1:n 比对事件评估 '''
|
'''3. 1:n 比对事件评估 '''
|
||||||
fpevents = one2n_pr(evtDicts)
|
fpevents = one2n_pr(evtDicts, pattern=3)
|
||||||
|
|
||||||
fpErrFile = str(Path(resultPath).joinpath("one2n_fp_Error.txt"))
|
fpErrFile = str(Path(resultPath).joinpath("one2n_fp_Error.txt"))
|
||||||
with open(fpErrFile, "w") as file:
|
with open(fpErrFile, "w") as file:
|
||||||
for item in fpevents:
|
for item in fpevents:
|
||||||
file.write(item + "\n")
|
file.write(item + "\n")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
eventSourcePath = r"\\192.168.1.28\share\测试视频数据以及日志\算法全流程测试\202412\images"
|
eventSourcePath = r"\\192.168.1.28\share\测试视频数据以及日志\算法全流程测试\202412\result\ShoppingDict_pkfile"
|
||||||
resultPath = r"\\192.168.1.28\share\测试视频数据以及日志\算法全流程测试\202412\result"
|
resultPath = r"\\192.168.1.28\share\测试视频数据以及日志\算法全流程测试\202412\result\contrast"
|
||||||
|
|
||||||
eventDataPath = os.path.join(resultPath, "evtobjs")
|
eventDataPath = os.path.join(resultPath, "evtobjs")
|
||||||
similPath = os.path.join(resultPath, "simidata")
|
similPath = os.path.join(resultPath, "simidata")
|
||||||
|
@ -194,6 +194,7 @@ def simi_calc(event, stdfeat):
|
|||||||
if len(evtfeat)==0 or len(stdfeat)==0:
|
if len(evtfeat)==0 or len(stdfeat)==0:
|
||||||
return None, None, None
|
return None, None, None
|
||||||
|
|
||||||
|
|
||||||
evtfeat /= np.linalg.norm(evtfeat, axis=1)[:, None]
|
evtfeat /= np.linalg.norm(evtfeat, axis=1)[:, None]
|
||||||
stdfeat /= np.linalg.norm(stdfeat, axis=1)[:, None]
|
stdfeat /= np.linalg.norm(stdfeat, axis=1)[:, None]
|
||||||
|
|
||||||
@ -305,8 +306,8 @@ def one2SN_pr(evtList, evtDict, stdDict):
|
|||||||
barcodes, similars = [], []
|
barcodes, similars = [], []
|
||||||
for stdbcd in bcd_selected:
|
for stdbcd in bcd_selected:
|
||||||
stdfeat = stdDict[stdbcd]
|
stdfeat = stdDict[stdbcd]
|
||||||
# simi_mean, simi_max, simi_mfeat = simi_calc(event, stdfeat)
|
simi_mean, simi_max, simi_mfeat = simi_calc(event, stdfeat)
|
||||||
simi_mean = calc_simil(event, stdfeat)
|
# simi_mean = calc_simil(event, stdfeat)
|
||||||
|
|
||||||
## 在event.front_feats和event.back_feats同时为空时,此处不需要保护
|
## 在event.front_feats和event.back_feats同时为空时,此处不需要保护
|
||||||
# if simi_mean==None:
|
# if simi_mean==None:
|
||||||
@ -629,10 +630,8 @@ if __name__ == '__main__':
|
|||||||
stdBarcodePath = r"D:\全实时\source_data\bcdpath"
|
stdBarcodePath = r"D:\全实时\source_data\bcdpath"
|
||||||
stdFeaturePath = r"D:\全实时\source_data\stdfeats"
|
stdFeaturePath = r"D:\全实时\source_data\stdfeats"
|
||||||
|
|
||||||
eventSourcePath = [r"D:\全实时\result\pipeline\pipeline"]
|
eventSourcePath = [r"\\192.168.1.28\share\测试视频数据以及日志\算法全流程测试\202412\result\ShoppingDict_pkfile"]
|
||||||
resultPath = r"D:\全实时\result\pipeline"
|
resultPath = r"\\192.168.1.28\share\测试视频数据以及日志\算法全流程测试\202412\result\contrast"
|
||||||
|
|
||||||
|
|
||||||
eventDataPath = os.path.join(resultPath, "evtobjs")
|
eventDataPath = os.path.join(resultPath, "evtobjs")
|
||||||
similPath = os.path.join(resultPath, "simidata")
|
similPath = os.path.join(resultPath, "simidata")
|
||||||
if not os.path.exists(eventDataPath):
|
if not os.path.exists(eventDataPath):
|
||||||
@ -640,7 +639,7 @@ if __name__ == '__main__':
|
|||||||
if not os.path.exists(similPath):
|
if not os.path.exists(similPath):
|
||||||
os.makedirs(similPath)
|
os.makedirs(similPath)
|
||||||
|
|
||||||
test_one2one()
|
# test_one2one()
|
||||||
|
|
||||||
test_one2SN()
|
test_one2SN()
|
||||||
|
|
||||||
|
Binary file not shown.
@ -76,7 +76,7 @@ def array2list(bboxes):
|
|||||||
|
|
||||||
class ShoppingEvent:
|
class ShoppingEvent:
|
||||||
def __init__(self, eventpath, stype="data"):
|
def __init__(self, eventpath, stype="data"):
|
||||||
'''stype: str, 'source', 'data', '''
|
'''stype: str, 'source', 'data', 'realtime', 共三种 '''
|
||||||
|
|
||||||
self.eventpath = eventpath
|
self.eventpath = eventpath
|
||||||
self.evtname = str(Path(eventpath).stem)
|
self.evtname = str(Path(eventpath).stem)
|
||||||
@ -167,20 +167,22 @@ class ShoppingEvent:
|
|||||||
|
|
||||||
|
|
||||||
def from_source_pkl(self, eventpath):
|
def from_source_pkl(self, eventpath):
|
||||||
|
|
||||||
with open(eventpath, 'rb') as f:
|
with open(eventpath, 'rb') as f:
|
||||||
ShoppingDict = pickle.load(f)
|
ShoppingDict = pickle.load(f)
|
||||||
|
|
||||||
|
|
||||||
self.eventpath = ShoppingDict["eventPath"]
|
self.eventpath = ShoppingDict["eventPath"]
|
||||||
self.evtname = ShoppingDict["eventName"]
|
self.evtname = ShoppingDict["eventName"]
|
||||||
self.barcode = ShoppingDict["barcode"]
|
self.barcode = ShoppingDict["barcode"]
|
||||||
|
|
||||||
|
if len(ShoppingDict["one2n"]):
|
||||||
|
self.one2n = ShoppingDict["one2n"]
|
||||||
|
|
||||||
'''=========== path of image and video =========== '''
|
'''=========== path of image and video =========== '''
|
||||||
self.back_videopath = ShoppingDict["backCamera"]["videoPath"]
|
self.back_videopath = ShoppingDict["backCamera"]["videoPath"]
|
||||||
self.front_videopath = ShoppingDict["frontCamera"]["videoPath"]
|
self.front_videopath = ShoppingDict["frontCamera"]["videoPath"]
|
||||||
self.back_imgpaths = ShoppingDict["backCamera"]["imagePaths"]
|
self.back_imgpaths = ShoppingDict["backCamera"]["imagePaths"]
|
||||||
self.front_imgpaths = ShoppingDict["frontCamera"]["imagePaths"]
|
self.front_imgpaths = ShoppingDict["frontCamera"]["imagePaths"]
|
||||||
|
|
||||||
|
|
||||||
'''===========对应于 0/1_track.data ============================='''
|
'''===========对应于 0/1_track.data ============================='''
|
||||||
backdata, back_outdata = self.kerndata(ShoppingDict, "backCamera")
|
backdata, back_outdata = self.kerndata(ShoppingDict, "backCamera")
|
||||||
|
111
events/evt_rename.py
Normal file
111
events/evt_rename.py
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Created on Thu Jan 9 14:34:41 2025
|
||||||
|
|
||||||
|
@author: ym
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
def read_deletedBarcode_file(filePath):
|
||||||
|
with open(filePath, 'r', encoding='utf-8') as f:
|
||||||
|
lines = f.readlines()
|
||||||
|
|
||||||
|
split_flag, all_list = False, []
|
||||||
|
dict, barcode_list, similarity_list = {}, [], []
|
||||||
|
clean_lines = [line.strip().replace("'", '').replace('"', '') for line in lines]
|
||||||
|
|
||||||
|
for i, line in enumerate(clean_lines):
|
||||||
|
if line.endswith(','):
|
||||||
|
line = line[:-1]
|
||||||
|
|
||||||
|
stripped_line = line.strip()
|
||||||
|
if not stripped_line:
|
||||||
|
if len(barcode_list): dict['barcode'] = barcode_list
|
||||||
|
if len(similarity_list): dict['similarity'] = similarity_list
|
||||||
|
if len(dict): all_list.append(dict)
|
||||||
|
|
||||||
|
split_flag = False
|
||||||
|
dict, barcode_list, similarity_list = {}, [], []
|
||||||
|
continue
|
||||||
|
|
||||||
|
if line.find(':')<0: continue
|
||||||
|
label = line.split(':')[0]
|
||||||
|
value = line.split(':')[1]
|
||||||
|
|
||||||
|
if label == 'SeqDir':
|
||||||
|
dict['SeqDir'] = value
|
||||||
|
dict['filetype'] = "deletedBarcode"
|
||||||
|
if label == 'Deleted':
|
||||||
|
dict['Deleted'] = value
|
||||||
|
if label == 'List':
|
||||||
|
split_flag = True
|
||||||
|
continue
|
||||||
|
if split_flag:
|
||||||
|
barcode_list.append(label)
|
||||||
|
similarity_list.append(value)
|
||||||
|
|
||||||
|
if len(barcode_list): dict['barcode'] = barcode_list
|
||||||
|
if len(similarity_list): dict['similarity'] = similarity_list
|
||||||
|
if len(dict): all_list.append(dict)
|
||||||
|
return all_list
|
||||||
|
|
||||||
|
|
||||||
|
def event_rename(path):
|
||||||
|
for filename in os.listdir(path):
|
||||||
|
fpath = os.path.join(path, filename)
|
||||||
|
if os.path.isfile(fpath): continue
|
||||||
|
flist = filename.split("_")
|
||||||
|
|
||||||
|
if len(flist)==3 and flist[-1].isdigit() and len(flist[-1])>=8:
|
||||||
|
print(f"{path}: renamed!")
|
||||||
|
return
|
||||||
|
|
||||||
|
dpath = os.path.join(path, "deletedBarcode.txt")
|
||||||
|
alist = read_deletedBarcode_file(dpath)
|
||||||
|
|
||||||
|
input_events, getout_events = [], []
|
||||||
|
for filename in os.listdir(path):
|
||||||
|
fpath = os.path.join(path, filename)
|
||||||
|
if os.path.isfile(fpath): continue
|
||||||
|
flist = filename.split("_")
|
||||||
|
|
||||||
|
bcd = flist[-1]
|
||||||
|
if len(flist)==2 and flist[0].find("2024")==0 and len(bcd)==0:
|
||||||
|
bcds = [dt['Deleted'] for dt in alist if dt['SeqDir'].strip()==filename]
|
||||||
|
if len(bcds)==1:
|
||||||
|
getout_events.append((filename, filename + bcds[0].strip()))
|
||||||
|
|
||||||
|
if len(flist)==2 and flist[0].find("2024")==0 and len(bcd)>=10:
|
||||||
|
input_events.append((filename, filename + '_' + bcd))
|
||||||
|
|
||||||
|
events = (input_events, getout_events)
|
||||||
|
for evts in events:
|
||||||
|
for name_old, name_new in evts:
|
||||||
|
path_old = os.path.join(path, name_old)
|
||||||
|
path_new = os.path.join(path, name_new)
|
||||||
|
try:
|
||||||
|
os.rename(path_old, path_new)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"发生错误:{e}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
path = r"\\192.168.1.28\share\测试视频数据以及日志\测试_202406\0910\images"
|
||||||
|
event_rename(path)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
209
pipeline.py
209
pipeline.py
@ -5,67 +5,48 @@ Created on Sun Sep 29 08:59:21 2024
|
|||||||
@author: ym
|
@author: ym
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
import sys
|
# import sys
|
||||||
import cv2
|
import cv2
|
||||||
import pickle
|
import pickle
|
||||||
import argparse
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from track_reid import parse_opt
|
|
||||||
from track_reid import yolo_resnet_tracker
|
from track_reid import yolo_resnet_tracker
|
||||||
# FILE = Path(__file__).resolve()
|
|
||||||
# ROOT = FILE.parents[0] # YOLOv5 root directory
|
|
||||||
# if str(ROOT) not in sys.path:
|
|
||||||
# sys.path.append(str(ROOT)) # add ROOT to PATH
|
|
||||||
# ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
|
||||||
|
|
||||||
from tracking.dotrack.dotracks_back import doBackTracks
|
from tracking.dotrack.dotracks_back import doBackTracks
|
||||||
from tracking.dotrack.dotracks_front import doFrontTracks
|
from tracking.dotrack.dotracks_front import doFrontTracks
|
||||||
from tracking.utils.drawtracks import plot_frameID_y2, draw_all_trajectories
|
from tracking.utils.drawtracks import plot_frameID_y2, draw_all_trajectories
|
||||||
from utils.getsource import get_image_pairs, get_video_pairs
|
from utils.getsource import get_image_pairs, get_video_pairs
|
||||||
|
from tracking.utils.read_data import read_similar
|
||||||
|
|
||||||
def get_interbcd_inputenents():
|
|
||||||
bcdpath = r"\\192.168.1.28\share\测试_202406\contrast\std_barcodes_2192"
|
|
||||||
eventpath = r"\\192.168.1.28\share\测试_202406\0918"
|
|
||||||
|
|
||||||
barcodes = []
|
def save_subimgs(imgdict, boxes, spath, ctype):
|
||||||
eventpaths = []
|
for i in range(len(boxes)):
|
||||||
for featname in os.listdir(bcdpath):
|
fid, bid = int(boxes[i, 7]), int(boxes[i, 8])
|
||||||
barcode, ext = os.path.splitext(featname)
|
if f"{fid}_{bid}" in imgdict.keys():
|
||||||
barcodes.append(barcode)
|
img = imgdict[f"{fid}_{bid}"]
|
||||||
|
imgpath = spath / f"{ctype}_{fid}_{bid}.png"
|
||||||
input_enents = []
|
cv2.imwrite(imgpath, img)
|
||||||
for root, dirs, files in os.walk(eventpath):
|
|
||||||
input_enent = [os.path.join(root, d) for d in dirs if d.split('_')[-1] in barcodes]
|
|
||||||
|
|
||||||
input_enents.extend(input_enent)
|
|
||||||
|
|
||||||
|
|
||||||
return input_enents
|
|
||||||
|
|
||||||
def pipeline(
|
def pipeline(
|
||||||
eventpath,
|
eventpath,
|
||||||
savepath = '',
|
savepath,
|
||||||
SourceType = "image", # video
|
SourceType,
|
||||||
stdfeat_path = None
|
weights
|
||||||
):
|
):
|
||||||
if SourceType == "video":
|
|
||||||
vpaths = get_video_pairs(eventpath)
|
|
||||||
elif SourceType == "image":
|
|
||||||
vpaths = get_image_pairs(eventpath)
|
|
||||||
'''
|
'''
|
||||||
eventpath: 单个事件的存储路径
|
eventpath: 单个事件的存储路径
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
'''======== 函数 yolo_resnet_tracker() 的参数字典 ========'''
|
if SourceType == "video":
|
||||||
opt = parse_opt()
|
vpaths = get_video_pairs(eventpath)
|
||||||
optdict = vars(opt)
|
elif SourceType == "image":
|
||||||
|
vpaths = get_image_pairs(eventpath)
|
||||||
|
|
||||||
|
optdict = {}
|
||||||
optdict["weights"] = r'D:\DetectTracking\ckpts\best_cls10_0906.pt'
|
optdict["weights"] = weights
|
||||||
optdict["is_save_img"] = True
|
|
||||||
optdict["is_save_video"] = True
|
|
||||||
|
|
||||||
event_tracks = []
|
event_tracks = []
|
||||||
|
|
||||||
@ -76,21 +57,35 @@ def pipeline(
|
|||||||
and evtname.split('_')[-1].isdigit() else ''
|
and evtname.split('_')[-1].isdigit() else ''
|
||||||
'''事件结果存储文件夹'''
|
'''事件结果存储文件夹'''
|
||||||
if not savepath:
|
if not savepath:
|
||||||
savepath = Path(__file__).resolve().parents[0] / "evtresult"
|
savepath = Path(__file__).resolve().parents[0] / "events_result"
|
||||||
|
|
||||||
save_dir_event = Path(savepath) / evtname
|
savepath_pipeline = Path(savepath) / Path("Yolos_Tracking") / evtname
|
||||||
|
|
||||||
pickpath = Path(savepath)/"pickfile"
|
|
||||||
if not pickpath.exists():
|
|
||||||
pickpath.mkdir(parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
|
"""ShoppingDict pickle 文件保存地址 """
|
||||||
|
savepath_spdict = Path(savepath) / "ShoppingDict_pkfile"
|
||||||
|
if not savepath_spdict.exists():
|
||||||
|
savepath_spdict.mkdir(parents=True, exist_ok=True)
|
||||||
|
pf_path = Path(savepath_spdict) / Path(str(evtname)+".pickle")
|
||||||
|
|
||||||
|
# if pf_path.exists():
|
||||||
|
# return
|
||||||
|
|
||||||
ShoppingDict = {"eventPath": eventpath,
|
ShoppingDict = {"eventPath": eventpath,
|
||||||
"eventName": evtname,
|
"eventName": evtname,
|
||||||
"barcode": barcode,
|
"barcode": barcode,
|
||||||
"eventType": '', # "input", "output", "other"
|
"eventType": '', # "input", "output", "other"
|
||||||
"frontCamera": {},
|
"frontCamera": {},
|
||||||
"backCamera": {}}
|
"backCamera": {},
|
||||||
|
"one2n": []
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
procpath = Path(eventpath).joinpath('process.data')
|
||||||
|
if procpath.is_file():
|
||||||
|
SimiDict = read_similar(procpath)
|
||||||
|
ShoppingDict["one2n"] = SimiDict['one2n']
|
||||||
|
|
||||||
|
|
||||||
for vpath in vpaths:
|
for vpath in vpaths:
|
||||||
'''相机事件字典构造'''
|
'''相机事件字典构造'''
|
||||||
@ -115,16 +110,20 @@ def pipeline(
|
|||||||
'''事件结果存储文件夹'''
|
'''事件结果存储文件夹'''
|
||||||
|
|
||||||
if isinstance(vpath, list):
|
if isinstance(vpath, list):
|
||||||
save_dir_video = save_dir_event / Path("images")
|
savepath_pipeline_imgs = savepath_pipeline / Path("images")
|
||||||
else:
|
else:
|
||||||
save_dir_video = save_dir_event / Path(str(Path(vpath).stem))
|
savepath_pipeline_imgs = savepath_pipeline / Path(str(Path(vpath).stem))
|
||||||
if not save_dir_video.exists():
|
if not savepath_pipeline_imgs.exists():
|
||||||
save_dir_video.mkdir(parents=True, exist_ok=True)
|
savepath_pipeline_imgs.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
savepath_pipeline_subimgs = savepath_pipeline / Path("subimgs")
|
||||||
|
if not savepath_pipeline_subimgs.exists():
|
||||||
|
savepath_pipeline_subimgs.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
'''Yolo + Resnet + Tracker'''
|
'''Yolo + Resnet + Tracker'''
|
||||||
optdict["source"] = vpath
|
optdict["source"] = vpath
|
||||||
optdict["save_dir"] = save_dir_video
|
optdict["save_dir"] = savepath_pipeline_imgs
|
||||||
|
|
||||||
yrtOut = yolo_resnet_tracker(**optdict)
|
yrtOut = yolo_resnet_tracker(**optdict)
|
||||||
|
|
||||||
@ -164,13 +163,30 @@ def pipeline(
|
|||||||
CameraEvent["tracking"] = vts
|
CameraEvent["tracking"] = vts
|
||||||
ShoppingDict["frontCamera"] = CameraEvent
|
ShoppingDict["frontCamera"] = CameraEvent
|
||||||
|
|
||||||
# pklpath = save_dir_event / "ShoppingDict.pkl"
|
|
||||||
# with open(str(pklpath), 'wb') as f:
|
|
||||||
# pickle.dump(ShoppingDict, f)
|
|
||||||
pf_path = Path(pickpath) / Path(str(evtname)+".pkl")
|
|
||||||
with open(str(pf_path), 'wb') as f:
|
with open(str(pf_path), 'wb') as f:
|
||||||
pickle.dump(ShoppingDict, f)
|
pickle.dump(ShoppingDict, f)
|
||||||
|
|
||||||
|
|
||||||
|
for CamerType, vts in event_tracks:
|
||||||
|
if len(vts.tracks)==0: continue
|
||||||
|
if CamerType == 'front':
|
||||||
|
yolos = ShoppingDict["frontCamera"]["yoloResnetTracker"]
|
||||||
|
ctype = 1
|
||||||
|
if CamerType == 'back':
|
||||||
|
yolos = ShoppingDict["backCamera"]["yoloResnetTracker"]
|
||||||
|
ctype = 0
|
||||||
|
|
||||||
|
imgdict = {}
|
||||||
|
for y in yolos:
|
||||||
|
imgdict.update(y["imgs"])
|
||||||
|
|
||||||
|
for track in vts.Residual:
|
||||||
|
if isinstance(track, np.ndarray):
|
||||||
|
save_subimgs(imgdict, track, savepath_pipeline_subimgs, ctype)
|
||||||
|
else:
|
||||||
|
save_subimgs(imgdict, track.boxes, savepath_pipeline_subimgs, ctype)
|
||||||
|
|
||||||
|
|
||||||
'''轨迹显示模块'''
|
'''轨迹显示模块'''
|
||||||
illus = [None, None]
|
illus = [None, None]
|
||||||
@ -181,24 +197,23 @@ def pipeline(
|
|||||||
edgeline = cv2.imread("./tracking/shopcart/cart_tempt/board_ftmp_line.png")
|
edgeline = cv2.imread("./tracking/shopcart/cart_tempt/board_ftmp_line.png")
|
||||||
|
|
||||||
h, w = edgeline.shape[:2]
|
h, w = edgeline.shape[:2]
|
||||||
nh, nw = h//2, w//2
|
# nh, nw = h//2, w//2
|
||||||
edgeline = cv2.resize(edgeline, (nw, nh), interpolation=cv2.INTER_AREA)
|
# edgeline = cv2.resize(edgeline, (nw, nh), interpolation=cv2.INTER_AREA)
|
||||||
|
|
||||||
img_tracking = draw_all_trajectories(vts, edgeline, save_dir_event, CamerType, draw5p=True)
|
img_tracking = draw_all_trajectories(vts, edgeline, savepath_pipeline, CamerType, draw5p=True)
|
||||||
illus[0] = img_tracking
|
illus[0] = img_tracking
|
||||||
|
|
||||||
|
|
||||||
plt = plot_frameID_y2(vts)
|
plt = plot_frameID_y2(vts)
|
||||||
plt.savefig(os.path.join(save_dir_event, "front_y2.png"))
|
plt.savefig(os.path.join(savepath_pipeline, "front_y2.png"))
|
||||||
|
|
||||||
if CamerType == 'back':
|
if CamerType == 'back':
|
||||||
edgeline = cv2.imread("./tracking/shopcart/cart_tempt/edgeline.png")
|
edgeline = cv2.imread("./tracking/shopcart/cart_tempt/edgeline.png")
|
||||||
|
|
||||||
h, w = edgeline.shape[:2]
|
h, w = edgeline.shape[:2]
|
||||||
nh, nw = h//2, w//2
|
# nh, nw = h//2, w//2
|
||||||
edgeline = cv2.resize(edgeline, (nw, nh), interpolation=cv2.INTER_AREA)
|
# edgeline = cv2.resize(edgeline, (nw, nh), interpolation=cv2.INTER_AREA)
|
||||||
|
|
||||||
img_tracking = draw_all_trajectories(vts, edgeline, save_dir_event, CamerType, draw5p=True)
|
img_tracking = draw_all_trajectories(vts, edgeline, savepath_pipeline, CamerType, draw5p=True)
|
||||||
illus[1] = img_tracking
|
illus[1] = img_tracking
|
||||||
|
|
||||||
illus = [im for im in illus if im is not None]
|
illus = [im for im in illus if im is not None]
|
||||||
@ -208,64 +223,25 @@ def pipeline(
|
|||||||
H, W = img_cat.shape[:2]
|
H, W = img_cat.shape[:2]
|
||||||
cv2.line(img_cat, (int(W/2), 0), (int(W/2), int(H)), (128, 128, 255), 3)
|
cv2.line(img_cat, (int(W/2), 0), (int(W/2), int(H)), (128, 128, 255), 3)
|
||||||
|
|
||||||
trajpath = os.path.join(save_dir_event, "traj.png")
|
trajpath = os.path.join(savepath_pipeline, "trajectory.png")
|
||||||
cv2.imwrite(trajpath, img_cat)
|
cv2.imwrite(trajpath, img_cat)
|
||||||
|
|
||||||
|
|
||||||
def main_loop():
|
|
||||||
bcdpath = r"\\192.168.1.28\share\测试_202406\contrast\std_barcodes_2192"
|
|
||||||
eventpath = r"\\192.168.1.28\share\测试_202406\0918\images1"
|
|
||||||
SourceType = "image" # video, image
|
|
||||||
|
|
||||||
barcodes = []
|
|
||||||
input_enents = []
|
|
||||||
output_events = []
|
|
||||||
|
|
||||||
|
|
||||||
'''1. 获得barcode标准特征集列表'''
|
|
||||||
for featname in os.listdir(bcdpath):
|
|
||||||
barcode, ext = os.path.splitext(featname)
|
|
||||||
if not barcode.isdigit() or len(barcode)<=8 or ext != ".pickle" :
|
|
||||||
continue
|
|
||||||
barcodes.append(barcode)
|
|
||||||
|
|
||||||
'''2. 构造(放入事件,标准特征)对'''
|
|
||||||
for filename in os.listdir(eventpath):
|
|
||||||
'''barcode为时间文件夹的最后一个字段'''
|
|
||||||
bcd = filename.split('_')[-1]
|
|
||||||
|
|
||||||
event_path = os.path.join(eventpath, filename)
|
|
||||||
stdfeat_path = None
|
|
||||||
if bcd in barcodes:
|
|
||||||
stdfeat_path = os.path.join(bcdpath, f"{bcd}.pickle")
|
|
||||||
input_enents.append((event_path, stdfeat_path))
|
|
||||||
|
|
||||||
parmDict = {}
|
|
||||||
parmDict["SourceType"] = "image"
|
|
||||||
parmDict["savepath"] = r"D:\contrast\detect"
|
|
||||||
for eventpath, stdfeat_path in input_enents:
|
|
||||||
parmDict["eventpath"] = eventpath
|
|
||||||
parmDict["stdfeat_path"] = stdfeat_path
|
|
||||||
|
|
||||||
pipeline(**parmDict)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
'''
|
'''
|
||||||
函数:pipeline(),遍历事件文件夹,选择类型 image 或 video,
|
函数:pipeline(),遍历事件文件夹,选择类型 image 或 video,
|
||||||
'''
|
'''
|
||||||
evtdir = r"\\192.168.1.28\share\测试视频数据以及日志\算法全流程测试\202412\images"
|
|
||||||
evtdir = Path(evtdir)
|
|
||||||
parmDict = {}
|
parmDict = {}
|
||||||
parmDict["savepath"] = r"D:\contrast\202412测试"
|
evtdir = r"\\192.168.1.28\share\测试视频数据以及日志\算法全流程测试\202412\images"
|
||||||
parmDict["SourceType"] = "video" # video, image
|
parmDict["SourceType"] = "video" # video, image
|
||||||
parmDict["stdfeat_path"] = None
|
parmDict["savepath"] = r"\\192.168.1.28\share\测试视频数据以及日志\算法全流程测试\202412\result"
|
||||||
|
parmDict["weights"] = r'D:\DetectTracking\ckpts\best_cls10_0906.pt'
|
||||||
k = 0
|
|
||||||
errEvents = []
|
evtdir = Path(evtdir)
|
||||||
|
k, errEvents = 0, []
|
||||||
for item in evtdir.iterdir():
|
for item in evtdir.iterdir():
|
||||||
if item.is_dir():
|
if item.is_dir():
|
||||||
# item = r"D:\exhibition\images\images2\images2"
|
# item = evtdir/Path("20241209-160201-b97f7a0e-7322-4375-9f17-c475500097e9_6926265317292")
|
||||||
parmDict["eventpath"] = item
|
parmDict["eventpath"] = item
|
||||||
# pipeline(**parmDict)
|
# pipeline(**parmDict)
|
||||||
|
|
||||||
@ -273,10 +249,9 @@ def main():
|
|||||||
pipeline(**parmDict)
|
pipeline(**parmDict)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
errEvents.append(str(item))
|
errEvents.append(str(item))
|
||||||
|
k+=1
|
||||||
# k+=1
|
if k==1:
|
||||||
# if k==1:
|
break
|
||||||
# break
|
|
||||||
|
|
||||||
errfile = os.path.join(parmDict["savepath"], f'error_events.txt')
|
errfile = os.path.join(parmDict["savepath"], f'error_events.txt')
|
||||||
with open(errfile, 'w', encoding='utf-8') as f:
|
with open(errfile, 'w', encoding='utf-8') as f:
|
||||||
|
@ -62,6 +62,7 @@ from hands.hand_inference import hand_pose
|
|||||||
|
|
||||||
from contrast.feat_extract.config import config as conf
|
from contrast.feat_extract.config import config as conf
|
||||||
from contrast.feat_extract.inference import FeatsInterface
|
from contrast.feat_extract.inference import FeatsInterface
|
||||||
|
|
||||||
ReIDEncoder = FeatsInterface(conf)
|
ReIDEncoder = FeatsInterface(conf)
|
||||||
|
|
||||||
IMG_FORMATS = '.bmp', '.dng', '.jpeg', '.jpg', '.mpo', '.png', '.tif', '.tiff', '.webp', '.pfm' # include image suffixes
|
IMG_FORMATS = '.bmp', '.dng', '.jpeg', '.jpg', '.mpo', '.png', '.tif', '.tiff', '.webp', '.pfm' # include image suffixes
|
||||||
@ -139,12 +140,8 @@ def init_trackers(tracker_yaml = None, bs=1):
|
|||||||
def yolo_resnet_tracker(
|
def yolo_resnet_tracker(
|
||||||
weights=ROOT / 'yolov5s.pt', # model path or triton URL
|
weights=ROOT / 'yolov5s.pt', # model path or triton URL
|
||||||
source=ROOT / 'data/images', # file/dir/URL/glob/screen/0(webcam)
|
source=ROOT / 'data/images', # file/dir/URL/glob/screen/0(webcam)
|
||||||
|
|
||||||
project=ROOT / 'runs/detect', # save results to project/name
|
|
||||||
name='exp', # save results to project/name
|
|
||||||
save_dir = '',
|
save_dir = '',
|
||||||
|
is_save_img = True,
|
||||||
is_save_img = False,
|
|
||||||
is_save_video = True,
|
is_save_video = True,
|
||||||
|
|
||||||
tracker_yaml = "./tracking/trackers/cfg/botsort.yaml",
|
tracker_yaml = "./tracking/trackers/cfg/botsort.yaml",
|
||||||
@ -154,19 +151,9 @@ def yolo_resnet_tracker(
|
|||||||
max_det=1000, # maximum detections per image
|
max_det=1000, # maximum detections per image
|
||||||
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
|
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
|
||||||
|
|
||||||
view_img=False, # show results
|
|
||||||
save_txt=False, # save results to *.txt
|
|
||||||
save_csv=False, # save results in CSV format
|
|
||||||
save_conf=False, # save confidences in --save-txt labels
|
|
||||||
save_crop=False, # save cropped prediction boxes
|
|
||||||
nosave=False, # do not save images/videos
|
|
||||||
update=False, # update all models
|
|
||||||
exist_ok=False, # existing project/name ok, do not increment
|
|
||||||
|
|
||||||
classes=None, # filter by class: --class 0, or --class 0 2 3
|
classes=None, # filter by class: --class 0, or --class 0 2 3
|
||||||
agnostic_nms=False, # class-agnostic NMS
|
agnostic_nms=False, # class-agnostic NMS
|
||||||
augment=False, # augmented inference
|
augment=False, # augmented inference
|
||||||
visualize=False, # visualize features
|
|
||||||
|
|
||||||
line_thickness=3, # bounding box thickness (pixels)
|
line_thickness=3, # bounding box thickness (pixels)
|
||||||
hide_labels=False, # hide labels
|
hide_labels=False, # hide labels
|
||||||
@ -208,7 +195,7 @@ def yolo_resnet_tracker(
|
|||||||
|
|
||||||
# Inference
|
# Inference
|
||||||
with dt[1]:
|
with dt[1]:
|
||||||
visualize = increment_path(project / Path(path).stem, mkdir=True) if visualize else False
|
# visualize = increment_path(project / Path(path).stem, mkdir=True) if visualize else False
|
||||||
pred = model(im, augment=augment, visualize=False)
|
pred = model(im, augment=augment, visualize=False)
|
||||||
|
|
||||||
# NMS
|
# NMS
|
||||||
@ -242,15 +229,16 @@ def yolo_resnet_tracker(
|
|||||||
# trackerBoxes = np.concatenate([trackerBoxes, tracks], axis=0)
|
# trackerBoxes = np.concatenate([trackerBoxes, tracks], axis=0)
|
||||||
'''================== 1. 存储 dets/subimgs/features Dict ============='''
|
'''================== 1. 存储 dets/subimgs/features Dict ============='''
|
||||||
imgs, features = ReIDEncoder.inference(im0, tracks)
|
imgs, features = ReIDEncoder.inference(im0, tracks)
|
||||||
featdict = {}
|
imgdict, featdict = {}, {}
|
||||||
for ii, bid in enumerate(tracks[:, 8]):
|
for ii, bid in enumerate(tracks[:, 8]):
|
||||||
featdict.update({f"{int(frameId)}_{int(bid)}": features[ii, :]}) # [f"feat_{int(bid)}"] = features[i, :]
|
featdict.update({f"{int(frameId)}_{int(bid)}": features[ii, :]}) # [f"feat_{int(bid)}"] = features[i, :]
|
||||||
|
imgdict.update({f"{int(frameId)}_{int(bid)}": imgs[ii]})
|
||||||
|
|
||||||
frameDict = {"path": path,
|
frameDict = {"path": path,
|
||||||
"fid": int(frameId),
|
"fid": int(frameId),
|
||||||
"bboxes": det,
|
"bboxes": det,
|
||||||
"tboxes": tracks,
|
"tboxes": tracks,
|
||||||
"imgs": imgs,
|
"imgs": imgdict,
|
||||||
"feats": featdict}
|
"feats": featdict}
|
||||||
yoloResnetTracker.append(frameDict)
|
yoloResnetTracker.append(frameDict)
|
||||||
|
|
||||||
|
54
说明文档.txt
54
说明文档.txt
@ -26,12 +26,56 @@
|
|||||||
|
|
||||||
4. 整体流程仿真
|
4. 整体流程仿真
|
||||||
pipeline.py
|
pipeline.py
|
||||||
SourceType: "image", "video", yolo+resent+tracker模块输入数据类型
|
需要指定是个变量:
|
||||||
|
(1) evtdir: 保存各个事件的文件夹
|
||||||
|
(2) SourceType: "image", "video", yolo+resent+tracker模块输入数据类型
|
||||||
|
(3) savepath: 结果数据保存位置
|
||||||
|
(4) weights: YoloV5 模型权重
|
||||||
|
|
||||||
|
savepath
|
||||||
|
(1) Yolos_Tracking
|
||||||
|
eventname文件夹,保存yolo+resent+tracker模块输出,包括:
|
||||||
|
(1) images 或 {videoname}
|
||||||
|
(2) subimgs
|
||||||
|
(3) trajectory
|
||||||
|
(4) front_y2.png
|
||||||
|
(5) trajectory.png
|
||||||
|
(2) ShoppingDict_pkfile
|
||||||
|
保存 ShoppingDict 字典的 pickle 文件,可用于构造 ShoppingEvent 对象。
|
||||||
|
|
||||||
|
(3) contrast
|
||||||
|
保存1:1、1:SN、1:n数据
|
||||||
|
|
||||||
|
(4) error_events.txt
|
||||||
|
|
||||||
|
事件字典结构:
|
||||||
|
ShoppingDict = {"eventPath": eventpath,
|
||||||
|
"eventName": evtname,
|
||||||
|
"barcode": barcode,
|
||||||
|
"eventType": '', # "input", "output", "other"
|
||||||
|
"frontCamera": {},
|
||||||
|
"backCamera": {},
|
||||||
|
"one2n": []
|
||||||
|
}
|
||||||
|
|
||||||
保存为 pickle 文件,该文件可用于 ShoppingEvent 对象构建。
|
相机字典:
|
||||||
|
CameraEvent = {"cameraType": '', # "front", "back"
|
||||||
|
"videoPath": '',
|
||||||
|
"imagePaths": [],
|
||||||
|
"yoloResnetTracker": [],
|
||||||
|
"tracking": [],
|
||||||
|
}
|
||||||
|
|
||||||
|
yoloResnetTracker,列表,元素 "frameDict" 为有关帧数据的字典结构
|
||||||
|
frameDict = {"path": path,
|
||||||
|
"fid": int(frameId),
|
||||||
|
"bboxes": det,
|
||||||
|
"tboxes": tracks,
|
||||||
|
"imgs": imgdict,
|
||||||
|
"feats": featdict}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user