guoqing bakeup
This commit is contained in:
@ -40,6 +40,7 @@ from scipy.spatial.distance import cdist
|
||||
import matplotlib.pyplot as plt
|
||||
import shutil
|
||||
from datetime import datetime
|
||||
from openpyxl import load_workbook, Workbook
|
||||
|
||||
# Vit版resnet, 和现场特征不一致,需将resnet_vit中文件提出
|
||||
# from config import config as conf
|
||||
@ -56,7 +57,7 @@ from tracking.utils.read_data import extract_data, read_tracking_output, read_de
|
||||
|
||||
from config import config as conf
|
||||
from model import resnet18 as resnet18
|
||||
from test_ori import inference_image
|
||||
from feat_inference import inference_image
|
||||
|
||||
|
||||
IMG_FORMAT = ['.bmp', '.jpg', '.jpeg', '.png']
|
||||
@ -100,13 +101,13 @@ def creat_shopping_event(eventPath, subimgPath=False):
|
||||
|
||||
'''================ 0. 检查 filename 及 eventPath 正确性和有效性 ================'''
|
||||
nmlist = eventName.split('_')
|
||||
if eventName.find('2024')<0 or len(nmlist)!=2 or len(nmlist[0])!=15 or len(nmlist[1])<11:
|
||||
# if eventName.find('2024')<0 or len(nmlist)!=2 or len(nmlist[0])!=15 or len(nmlist[1])<11:
|
||||
# return
|
||||
if eventName.find('2024')<0 or len(nmlist)!=2 or len(nmlist[1])<11:
|
||||
return
|
||||
if not os.path.isdir(eventPath):
|
||||
return
|
||||
|
||||
|
||||
|
||||
'''================ 1. 构造事件描述字典,暂定 9 items ==============='''
|
||||
event = {}
|
||||
event['barcode'] = eventName.split('_')[1]
|
||||
@ -293,10 +294,10 @@ def get_std_barcodeDict(bcdpath, savepath):
|
||||
imgpaths.append(imgpath)
|
||||
stdBarcodeDict[barcode].extend(imgpaths)
|
||||
|
||||
# pickpath = os.path.join(savepath, f"{barcode}.pickle")
|
||||
# with open(pickpath, 'wb') as f:
|
||||
# pickle.dump(stdBarcodeDict, f)
|
||||
# print(f"Barcode: {barcode}")
|
||||
pickpath = os.path.join(savepath, f"{barcode}.pickle")
|
||||
with open(pickpath, 'wb') as f:
|
||||
pickle.dump(stdBarcodeDict, f)
|
||||
print(f"Barcode: {barcode}")
|
||||
|
||||
# k += 1
|
||||
# if k == 10:
|
||||
@ -352,7 +353,6 @@ def batch_inference(imgpaths, batch):
|
||||
feature = featurize(group, conf.test_transform, model, conf.device)
|
||||
features.append(feature)
|
||||
features = np.concatenate(features, axis=0)
|
||||
|
||||
return features
|
||||
|
||||
def stdfeat_infer(imgPath, featPath, bcdSet=None):
|
||||
@ -371,14 +371,20 @@ def stdfeat_infer(imgPath, featPath, bcdSet=None):
|
||||
stdBarcodeDict = {}
|
||||
stdBarcodeDict_ft16 = {}
|
||||
|
||||
|
||||
'''4处同名: (1)barcode原始图像文件夹; (2)imgPath中的 .pickle 文件名、该pickle文件中字典的key值'''
|
||||
|
||||
k = 0
|
||||
for filename in os.listdir(imgPath):
|
||||
bcd, ext = os.path.splitext(filename)
|
||||
pkpath = os.path.join(featPath, f"{bcd}.pickle")
|
||||
|
||||
if os.path.isfile(pkpath): continue
|
||||
if bcdSet is not None and bcd not in bcdSet:
|
||||
continue
|
||||
|
||||
filepath = os.path.join(imgPath, filename)
|
||||
|
||||
|
||||
stdbDict = {}
|
||||
stdbDict_ft16 = {}
|
||||
stdbDict_uint8 = {}
|
||||
@ -399,12 +405,8 @@ def stdfeat_infer(imgPath, featPath, bcdSet=None):
|
||||
|
||||
# uint8, 两种策略,1) 精度损失小, 2) 计算复杂度小
|
||||
# stdfeat_uint8, _ = ft16_to_uint8(feature_ft16)
|
||||
|
||||
stdfeat_uint8 = (feature_ft16*128).astype(np.int8)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error accured at: {filename}, with Exception is: {e}")
|
||||
|
||||
@ -414,33 +416,30 @@ def stdfeat_infer(imgPath, featPath, bcdSet=None):
|
||||
stdbDict["imgpaths"] = imgpaths
|
||||
stdbDict["feats"] = feature
|
||||
|
||||
pkpath = os.path.join(featPath, f"{barcode}.pickle")
|
||||
# pkpath = os.path.join(featPath, f"{barcode}.pickle")
|
||||
with open(pkpath, 'wb') as f:
|
||||
pickle.dump(stdbDict, f)
|
||||
pickle.dump(stdbDict, f)
|
||||
stdBarcodeDict[barcode] = feature
|
||||
|
||||
|
||||
##================== float16
|
||||
stdbDict_ft16["barcode"] = barcode
|
||||
stdbDict_ft16["imgpaths"] = imgpaths
|
||||
stdbDict_ft16["feats"] = feature_ft16
|
||||
pkpath_ft16 = os.path.join(featPath, f"{barcode}_ft16.pickle")
|
||||
with open(pkpath_ft16, 'wb') as f:
|
||||
pickle.dump(stdbDict_ft16, f)
|
||||
stdBarcodeDict_ft16[barcode] = pkpath_ft16
|
||||
# stdbDict_ft16["barcode"] = barcode
|
||||
# stdbDict_ft16["imgpaths"] = imgpaths
|
||||
# stdbDict_ft16["feats"] = feature_ft16
|
||||
# pkpath_ft16 = os.path.join(featPath, f"{barcode}_ft16.pickle")
|
||||
# with open(pkpath_ft16, 'wb') as f:
|
||||
# pickle.dump(stdbDict_ft16, f)
|
||||
# stdBarcodeDict_ft16[barcode] = pkpath_ft16
|
||||
|
||||
|
||||
|
||||
##================== uint8
|
||||
stdbDict_uint8["barcode"] = barcode
|
||||
stdbDict_uint8["imgpaths"] = imgpaths
|
||||
stdbDict_uint8["feats"] = stdfeat_uint8
|
||||
pkpath_uint8 = os.path.join(featPath, f"{barcode}_uint8.pickle")
|
||||
with open(pkpath_uint8, 'wb') as f:
|
||||
pickle.dump(stdbDict_uint8, f)
|
||||
# stdBarcodeDict_ft16[barcode] = pkpath_ft16
|
||||
|
||||
|
||||
# stdbDict_uint8["barcode"] = barcode
|
||||
# stdbDict_uint8["imgpaths"] = imgpaths
|
||||
# stdbDict_uint8["feats"] = stdfeat_uint8
|
||||
# pkpath_uint8 = os.path.join(featPath, f"{barcode}_uint8.pickle")
|
||||
# with open(pkpath_uint8, 'wb') as f:
|
||||
# pickle.dump(stdbDict_uint8, f)
|
||||
|
||||
t2 = time.time()
|
||||
print(f"Barcode: {barcode}, need time: {t2-t1:.1f} secs")
|
||||
@ -448,7 +447,7 @@ def stdfeat_infer(imgPath, featPath, bcdSet=None):
|
||||
# if k == 10:
|
||||
# break
|
||||
|
||||
|
||||
##================== float32
|
||||
# pickpath = os.path.join(featPath, f"barcode_features_{k}.pickle")
|
||||
# with open(pickpath, 'wb') as f:
|
||||
# pickle.dump(stdBarcodeDict, f)
|
||||
@ -456,7 +455,7 @@ def stdfeat_infer(imgPath, featPath, bcdSet=None):
|
||||
##================== float16
|
||||
# pickpath_ft16 = os.path.join(featPath, f"barcode_features_ft16_{k}.pickle")
|
||||
# with open(pickpath_ft16, 'wb') as f:
|
||||
# pickle.dump(stdBarcodeDict_ft16, f)
|
||||
# pickle.dump(stdBarcodeDict_ft16, f)
|
||||
|
||||
return
|
||||
|
||||
@ -477,7 +476,8 @@ def contrast_performance_evaluate(resultPath):
|
||||
# ]
|
||||
|
||||
evtList = [(p.stem, p.stem.split('_')[1]) for p in Path(eventFeatPath).iterdir()
|
||||
if p.is_file()
|
||||
if p.is_file()
|
||||
and str(p).find('240910')>0
|
||||
and p.suffix=='.pickle'
|
||||
and len(p.stem.split('_'))==2
|
||||
and p.stem.split('_')[1].isdigit()
|
||||
@ -487,7 +487,7 @@ def contrast_performance_evaluate(resultPath):
|
||||
barcodes = set([bcd for _, bcd in evtList])
|
||||
|
||||
'''标准特征集图像样本经特征提取并保存,运行一次后无需再运行'''
|
||||
# stdfeat_infer(stdBarcodePath, stdFeaturePath, barcodes)
|
||||
stdfeat_infer(stdBarcodePath, stdFeaturePath, barcodes)
|
||||
|
||||
'''========= 构建用于比对的标准特征字典 ============='''
|
||||
stdDict = {}
|
||||
@ -639,6 +639,7 @@ def compute_precise_recall(pickpath):
|
||||
file, ext = os.path.splitext(pickfile)
|
||||
|
||||
if ext != '.pickle': return
|
||||
if file.find('ft16') < 0: return
|
||||
|
||||
with open(pickpath, 'rb') as f:
|
||||
results = pickle.load(f)
|
||||
@ -717,7 +718,8 @@ def generate_event_and_stdfeatures():
|
||||
|
||||
|
||||
'''=========================== 2. 提取并存储事件特征 ========================'''
|
||||
eventDatePath = [# r'\\192.168.1.28\share\测试_202406\0723\0723_1',
|
||||
eventDatePath = [r'\\192.168.1.28\share\测试_202406\0910\images',
|
||||
# r'\\192.168.1.28\share\测试_202406\0723\0723_1',
|
||||
# r'\\192.168.1.28\share\测试_202406\0723\0723_2',
|
||||
# r'\\192.168.1.28\share\测试_202406\0723\0723_3',
|
||||
# r'\\192.168.1.28\share\测试_202406\0722\0722_01',
|
||||
@ -751,12 +753,12 @@ def generate_event_and_stdfeatures():
|
||||
# break
|
||||
|
||||
## 保存轨迹中 boxes 子图
|
||||
# for event in eventList:
|
||||
# basename = os.path.basename(event['filepath'])
|
||||
# savepath = os.path.join(subimgPath, basename)
|
||||
# if not os.path.exists(savepath):
|
||||
# os.makedirs(savepath)
|
||||
# save_event_subimg(event, savepath)
|
||||
for event in eventList:
|
||||
basename = os.path.basename(event['filepath'])
|
||||
savepath = os.path.join(subimgPath, basename)
|
||||
if not os.path.exists(savepath):
|
||||
os.makedirs(savepath)
|
||||
save_event_subimg(event, savepath)
|
||||
|
||||
print("eventList have generated and features have saved!")
|
||||
|
||||
@ -789,23 +791,23 @@ def ft16_to_uint8(arr_ft16):
|
||||
return arr_uint8, arr_ft16_
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
generate_event_and_stdfeatures()
|
||||
# generate_event_and_stdfeatures()
|
||||
|
||||
# contrast_performance_evaluate(resultPath)
|
||||
# for filename in os.listdir(resultPath):
|
||||
# if filename.find('.pickle') < 0: continue
|
||||
# # if filename.find('0909') < 0: continue
|
||||
# pickpath = os.path.join(resultPath, filename)
|
||||
# compute_precise_recall(pickpath)
|
||||
contrast_performance_evaluate(resultPath)
|
||||
for filename in os.listdir(resultPath):
|
||||
if filename.find('.pickle') < 0: continue
|
||||
if filename.find('0911') < 0: continue
|
||||
pickpath = os.path.join(resultPath, filename)
|
||||
compute_precise_recall(pickpath)
|
||||
|
||||
|
||||
def main_std():
|
||||
std_sample_path = r"\\192.168.1.28\share\已标注数据备份\对比数据\barcode\barcode_500_1979_已清洗"
|
||||
std_sample_path = r"\\192.168.1.28\share\已标注数据备份\对比数据\barcode\barcode_500_2192_已清洗"
|
||||
std_barcode_path = r"\\192.168.1.28\share\测试_202406\contrast\std_barcodes_2192"
|
||||
std_feature_path = r"\\192.168.1.28\share\测试_202406\contrast\std_features_2192_ft32vsft16"
|
||||
|
||||
@ -824,7 +826,8 @@ def main_std():
|
||||
# print("done")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
# main()
|
||||
|
||||
|
||||
# main_std()
|
||||
|
||||
|
Reference in New Issue
Block a user