pepeline_extract_subimg.py: extract subimg

This commit is contained in:
王庆刚
2024-10-07 17:27:00 +08:00
parent 390c5d2d94
commit dfb2272a15
6 changed files with 241 additions and 39 deletions

Binary file not shown.

View File

@ -404,8 +404,8 @@ def stdfeat_infer(imgPath, featPath, bcdSet=None):
feature_ft16 /= np.linalg.norm(feature_ft16, axis=1)[:, None]
# uint8, 两种策略1) 精度损失小, 2) 计算复杂度小
# stdfeat_uint8, _ = ft16_to_uint8(feature_ft16)
stdfeat_uint8 = (feature_ft16*128).astype(np.int8)
# feature_uint8, _ = ft16_to_uint8(feature_ft16)
feature_uint8 = (feature_ft16*128).astype(np.int8)
except Exception as e:
print(f"Error accured at: {filename}, with Exception is: {e}")
@ -414,32 +414,15 @@ def stdfeat_infer(imgPath, featPath, bcdSet=None):
##================== float32
stdbDict["barcode"] = barcode
stdbDict["imgpaths"] = imgpaths
stdbDict["feats"] = feature
stdbDict["feats_ft32"] = feature
stdbDict["feats_ft16"] = feature_ft16
stdbDict["feats_uint8"] = feature_uint8
# pkpath = os.path.join(featPath, f"{barcode}.pickle")
with open(pkpath, 'wb') as 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
##================== 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] = feature_ft16
t2 = time.time()
print(f"Barcode: {barcode}, need time: {t2-t1:.1f} secs")
@ -813,7 +796,7 @@ def main_std():
get_std_barcodeDict(std_sample_path, std_barcode_path)
# stdfeat_infer(std_barcode_path, std_feature_path, bcdSet=None)
stdfeat_infer(std_barcode_path, std_feature_path, bcdSet=None)
# fileList = []
# for filename in os.listdir(std_barcode_path):
@ -829,7 +812,7 @@ if __name__ == '__main__':
# main()
# main_std()
main_std()

View File

@ -6,6 +6,7 @@ Created on Sun Sep 29 08:59:21 2024
"""
import os
import cv2
import pickle
from pathlib import Path
from track_reid import parse_opt, yolo_resnet_tracker
@ -21,6 +22,31 @@ std_feature_path = r"\\192.168.1.28\share\测试_202406\contrast\std_features_21
opt = parse_opt()
optdict = vars(opt)
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 = []
eventpaths = []
for featname in os.listdir(bcdpath):
barcode, ext = os.path.splitext(featname)
barcodes.append(barcode)
input_enents = []
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 get_video_pairs(vpath):
vdieopath = []
for filename in os.listdir(vpath):
@ -29,13 +55,16 @@ def get_video_pairs(vpath):
vdieopath.append(os.path.join(vpath, filename))
return vdieopath
def pipeline(eventpath, stdfeat_path):
# eventpath = r"\\192.168.1.28\share\测试_202406\0918\images1\20240918-110822-1bc3902e-5a8e-4e23-8eca-fb3f02738551_6938314601726"
def pipeline():
eventpath = r"\\192.168.1.28\share\测试_202406\0918\images1\20240918-110913-c3a7e4d9-23d4-4a6f-a23f-a2eeee510536_6939947701616"
savepath = r"D:\contrast\detect"
optdict["project"] = savepath
eventname = os.path.basename(eventpath)
barcode = eventname.split('_')[-1]
vpaths = get_video_pairs(eventpath)
event_tracks = []
@ -75,9 +104,19 @@ def pipeline():
if CamerType == 'front':
pass
'''前后摄轨迹选择'''
if stdfeat_path is not None:
with open(stdfeat_path, 'rb') as f:
featDict = pickle.load(f)
for featname in os.listdir(std_feature_path):
pass
@ -88,7 +127,63 @@ def pipeline():
def main():
pipeline()
bcdpath = r"\\192.168.1.28\share\测试_202406\contrast\std_barcodes_2192"
eventpath = r"\\192.168.1.28\share\测试_202406\0918\images1"
barcodes = []
input_enents = []
output_events = []
# input_enents = get_interbcd_inputenents()
# k = 0
# for event in input_enents:
# pipeline(event)
# k += 1
# if k ==1:
# break
'''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)
for filename in os.listdir(eventpath):
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))
for eventpath, stdfeat_path in input_enents:
pipeline(eventpath, stdfeat_path)
if __name__ == "__main__":

125
pipeline_extract_subimg.py Normal file
View File

@ -0,0 +1,125 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 29 08:59:21 2024
@author: ym
"""
import os
import cv2
import pickle
from pathlib import Path
from track_reid import parse_opt, yolo_resnet_tracker
from tracking.dotrack.dotracks_back import doBackTracks
from tracking.dotrack.dotracks_front import doFrontTracks
IMGFORMATS = '.bmp', '.jpeg', '.jpg', 'png', 'tif', 'tiff', 'webp', 'pfm'
VIDFORMATS = '.avi', '.gif', '.m4v', '.mkv', '.mov', '.mp4', '.ts', '.wmv'
std_feature_path = r"\\192.168.1.28\share\测试_202406\contrast\std_features_2192_ft32vsft16"
opt = parse_opt()
optdict = vars(opt)
def pipeline(eventpath, savepath):
# eventpath = r"\\192.168.1.28\share\测试_202406\0918\images1\20240918-110822-1bc3902e-5a8e-4e23-8eca-fb3f02738551_6938314601726"
optdict["project"] = savepath
'''Yolo + Resnet + Tracker'''
optdict["source"] = eventpath
optdict["save_dir"] = savepath
optdict["nosave"] = False
tracksdict = yolo_resnet_tracker(**optdict)
bboxes = tracksdict['TrackBoxes']
basename = os.path.basename(eventpath)
base, ext = os.path.splitext(basename)
if base.find('front')>=0:
vts = doFrontTracks(bboxes, tracksdict)
vts.classify()
if base.find('back')>=0:
vts = doBackTracks(bboxes, tracksdict)
vts.classify()
tracks = [t for t in vts.tracks if t.cls>0 and not t.is_static()]
# tracks = [t for t in vts.tracks if t.cls>0]
for track in tracks:
# for track in vts.Residual:
for *xyxy, tid, conf, cls, fid, bid in track.boxes:
img = tracksdict[f'frame_{int(fid)}']["imgs"][int(bid)]
imgpth = savepath / Path(f'{base}_tid-{int(tid)}_fid-{int(fid)}_bid-{int(bid)}.jpg')
cv2.imwrite(imgpth, img)
return len(vts.Residual)
def main():
videopath = r"\\192.168.1.28\share\上海中环店采集视频\21-25\videos\1\back"
savepath = r"D:\contrast\barcodes"
vpaths = []
for root, dirs, files in os.walk(videopath):
vpth = [os.path.join(root, f) for f in files if os.path.splitext(f)[-1] in VIDFORMATS]
vpaths.extend(vpth)
manual_txt = os.path.join(savepath, 'manual_videos.txt')
file = open(manual_txt, 'a', encoding='utf-8')
manual = []
k = 0
for vpath in vpaths:
videoname = os.path.basename(vpath)
vname, ext = os.path.splitext(videoname)
barcode = videoname.split('_')[0]
subpath = os.path.join(savepath, barcode, vname)
subpath = Path(subpath)
if not subpath.exists():
subpath.mkdir(parents=True, exist_ok=True)
ntract = pipeline(vpath, subpath)
if ntract==0:
manual.append(vpath)
file.write(vpath)
file.write("\n")
print(f"{videoname} done!!!")
k += 1
if k==10:
break
file.close()
if __name__ == "__main__":
main()

View File

@ -264,12 +264,11 @@ def yolo_resnet_tracker(
im0 = annotator.result()
save_path_img, ext = os.path.splitext(save_path)
if save_img:
if dataset.mode == 'image':
imgpath = save_path_img + f"_{dataset}.png"
else:
imgpath = save_path_img + f"_{dataset.frame}.png"
cv2.imwrite(Path(imgpath), im0)
# if dataset.mode == 'image':
# imgpath = save_path_img + f"_{dataset}.png"
# else:
# imgpath = save_path_img + f"_{dataset.frame}.png"
# cv2.imwrite(Path(imgpath), im0)
if vid_path[i] != save_path: # new video
vid_path[i] = save_path