def main(): # load net net_file = join(realpath(dirname(__file__)), 'SiamRPNBIG.model') net = SiamRPNBIG() net.load_state_dict(torch.load(net_file)) net.eval().cuda() # warm up for i in range(10): net.temple( torch.autograd.Variable(torch.FloatTensor(1, 3, 127, 127)).cuda()) net(torch.autograd.Variable(torch.FloatTensor(1, 3, 255, 255)).cuda()) # start to track handle = vot.VOT("polygon") Polygon = handle.region() cx, cy, w, h = get_axis_aligned_bbox(Polygon) image_file = handle.frame() if not image_file: sys.exit(0) target_pos, target_sz = np.array([cx, cy]), np.array([w, h]) im = cv2.imread(image_file) # HxWxC state = SiamRPN_init(im, target_pos, target_sz, net) # init tracker while True: image_file = handle.frame() if not image_file: break im = cv2.imread(image_file) # HxWxC state = SiamRPN_track(state, im) # track res = cxy_wh_2_rect(state['target_pos'], state['target_sz']) handle.report(Rectangle(res[0], res[1], res[2], res[3]))
def main(): vid_file = os.path.expanduser("~/Videos/VID_20190327_195111.mp4") cap = cv2.VideoCapture(vid_file) # load net net = SiamRPNvot() net.load_state_dict( torch.load(join(realpath(dirname(__file__)), 'SiamRPNVOT.model'))) net.eval().cuda() # # image and init box # image_files = sorted(glob.glob('./bag/*.jpg')) init_rbox = [ 334.02, 128.36, 438.19, 188.78, 396.39, 260.83, 292.23, 200.41 ] [cx, cy, w, h] = get_axis_aligned_bbox(init_rbox) # tracker init target_pos, target_sz = np.array([cx, cy]), np.array([w, h]) ret, im = cap.read() state = SiamRPN_init(im, target_pos, target_sz, net, use_gpu=True) toc = 0 while (True): # Capture frame-by-frame ret, im = cap.read() tic = cv2.getTickCount() state = SiamRPN_track(state, im, use_gpu=True) # track toc += cv2.getTickCount() - tic res = cxy_wh_2_rect(state['target_pos'], state['target_sz']) res = [int(l) for l in res] cv2.rectangle(im, (res[0], res[1]), (res[0] + res[2], res[1] + res[3]), (0, 255, 255), 3) cv2.imshow('SiamRPN', im) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
def OTB_run(gt_rects, frame_paths, tracker): tic = time.time() # tracking loop res = [] for idx, img_path in enumerate(frame_paths): print('Frame', idx) img = np.array(Image.open(frame_paths[idx]).convert('RGB')) if idx == 0: if len(gt_rects[0]) == 8: init_bbox = get_axis_aligned_bbox(np.array(gt_rects[0])) else: init_bbox = gt_rects[0] pred_bbox = tracker.initialize(img, init_bbox) else: pred_bbox = tracker.track(img) res.append(pred_bbox) fps = len(res) / (time.time() - tic) success_overlap = compute_success_overlap(gt_rects, res) print('success overlap: %.4f, fps:%.2f' % (success_overlap.mean(), fps))
def SiamRPN_load(image, boxes, txt_path): if not os.path.exists(txt_path): os.makedirs(txt_path) multiTracker = cv2.MultiTracker_create() net = SiamRPNotb() net.load_state_dict( torch.load(join(realpath(dirname(__file__)), 'SiamRPNOTB.model'))) net.eval().cuda() states, labels = [], [] for bbox in boxes: #init_rbox = [bbox[0],bbox[1],bbox[0]+bbox[2],bbox[1],bbox[0]+bbox[2],bbox[1]+bbox[3],bbox[0],bbox[1]+bbox[3]] init_rbox = [ bbox[0], bbox[1], bbox[2], bbox[1], bbox[2], bbox[3], bbox[0], bbox[3] ] [cx, cy, w, h] = get_axis_aligned_bbox(init_rbox) # print(cx, cy, w, h,'-----',init_rbox) target_pos, target_sz = np.array([cx, cy]), np.array([w, h]) states.append(SiamRPN_init(image, target_pos, target_sz, net)) labels.append(bbox[-1]) return states, labels
parser.add_argument('--model', metavar='model', default='SiamRPNPPRes50', type=str, help='which model to use.') args = parser.parse_args() # load net net = eval(args.model)() load_net('./{}.pth'.format(args.model), net) net.eval().cuda() # image and init box image_files = sorted(glob.glob('./testData/*.jpg')) init_rbox = [3641, 1778, 3810, 1778, 3810, 2313, 3641, 2313] [cx, cy, w, h] = get_axis_aligned_bbox(init_rbox) # tracker init target_pos, target_sz = np.array([cx, cy]), np.array([w, h]) im = cv2.imread(image_files[0]) # HxWxC state = SiamRPN_init(im, target_pos, target_sz, net, args.model) # tracking and visualization toc = 0 for f, image_file in enumerate(image_files): im = cv2.imread(image_file) # print(im.shape) tic = cv2.getTickCount() state = SiamRPN_track(state, im) # track toc += cv2.getTickCount() - tic res = cxy_wh_2_rect(state['target_pos'], state['target_sz'])
# load net net_file = join(realpath(dirname(__file__)), 'SiamRPNBIG.model') net = SiamRPNBIG() net.load_state_dict(torch.load(net_file)) net.eval().cuda() # warm up for i in range(10): net.temple(torch.autograd.Variable(torch.FloatTensor(1, 3, 127, 127)).cuda()) net(torch.autograd.Variable(torch.FloatTensor(1, 3, 255, 255)).cuda()) # start to track handle = vot.VOT("polygon") Polygon = handle.region() cx, cy, w, h = get_axis_aligned_bbox(Polygon) image_file = handle.frame() if not image_file: sys.exit(0) target_pos, target_sz = np.array([cx, cy]), np.array([w, h]) im = cv2.imread(image_file) # HxWxC state = SiamRPN_init(im, target_pos, target_sz, net) # init tracker while True: image_file = handle.frame() if not image_file: break im = cv2.imread(image_file) # HxWxC state = SiamRPN_track(state, im) # track
net(torch.autograd.Variable(torch.FloatTensor(1, 3, 255, 255)).cuda()) # image_files = sorted(glob.glob('./bag/*.jpg')) # image_files = sorted(glob.glob('./helicopter/*.jpg')) # image_files = sorted(glob.glob('./ant/*.jpg')) # image_files = sorted(glob.glob('./basketball/*.jpg')) # image_files = sorted(glob.glob('./crossing/*.jpg')) image_files = sorted(glob.glob('./CarScale/*.jpg')) # start to track 来给第一帧的图像初始化坐标用的,在第一帧以后的后续帧,用来更新坐标信息 # init_rbox = [334.02,128.36,438.19,188.78,396.39,260.83,292.23,200.41] # init_rbox = [345.86,216.9,576.78,222.71,572.99,373.17,342.07,367.35] # init_rbox = [137.21,458.36,156.83,460.78,148.35,529.41,128.72,526.99] # init_rbox = [423,169,423,273,463,169,463,273] # init_rbox = [201,153,201,203,223,153,223,203] init_rbox = [6,166,6,189,48,171,48,192] [cx, cy, w, h] = get_axis_aligned_bbox(init_rbox) # 第一帧起始box。将坐标数据转换成 RPN 的格式 if not image_files: sys.exit(0) target_pos, target_sz = np.array([cx, cy]), np.array([w, h]) # box中心坐标和box高和宽 im = cv2.imread(image_files[0]) # HxWxC # SiamRPN_init 构造状态结构体并运行模板分支 state = SiamRPN_init(im, target_pos, target_sz, net) # init tracker for f, image_file in enumerate(image_files): if not image_file: break im = cv2.imread(image_file) # HxWxC # 运行检测分支并更新状态变量
files = sorted(files) images = [ dir_name + "/" + file_name for file_name in files if (file_name[-3:] == "jpg") ] gt_txt = dir_name + "/" + "groundtruth.txt" # with open(gt_txt, 'r') as f: # gt_box_str = f.readline() # gt_box = map(float,list(gt_box_str.split(','))) region_type = "polygon" region = convert_region(parse_region(open(gt_txt, 'r').readline()), region_type) #Polygon = region cx, cy, w, h = get_axis_aligned_bbox(region) frame = 0 image_file = images[frame] if not image_file: sys.exit(0) target_pos, target_sz = np.array([cx, cy]), np.array([w, h]) im = cv2.imread(image_file) # HxWxC cv2.imshow("Tracking", im) cv2.waitKey(1) print("SiamRPN init") state = SiamRPN_init(im, target_pos, target_sz, net) # init tracker print("begin tracking") while True: frame = frame + 1
video_length = vot.get_frame_length(video_name) #ground truth bounding box gts = vot.get_gts(video_name) frame_tags = vot.get_frame_tags(video_name) video_frames = vot.get_frames(video_name) flow_dir = os.path.join(flow_dirs, video_name + '.txt') img_dir = os.path.join(vot_dir, video_name,'color') confidence_dir = os.path.join('/home/jianingq/backward_flow_confidence_vot/',video_name) #initialize network # image and init box init_rbox = gts[0] if(len(init_rbox) == 4): [cx, cy], [w, h] = rect_2_cxy_wh(init_rbox) else: [cx, cy, w, h] = get_axis_aligned_bbox(init_rbox) # tracker init target_pos, target_sz = np.array([cx, cy]), np.array([w, h]) im = video_frames[0]# HxWxC state = SiamRPN_init(im, target_pos, target_sz, net) detection_box = [int(cx-w/2),int(cy-h/2),int(cx+w/2),int(cy+h/2)] for i in range(0,video_length - 1): #track im1 = np.copy(video_frames[i]) im2 = np.copy(video_frames[i + 1]) entropy_data = np.load(os.path.join(confidence_dir,format(i+1, '08')+'_entropy.npy')) flow = np.load(os.path.join(confidence_dir,format(i+1, '08')+'_flow.npy')) warped_im1 = flow_warp(im1, flow)
# load net net_file = join(realpath(dirname(__file__)), 'SiamRPNBIG.model') net = SiamRPNBIG() net.load_state_dict(torch.load(net_file)) # 加载模型参数 net.eval().cuda() # 对模型进行验证,cuda(device=None) 将所有模型参数和缓冲区移动到GPU # warm up # 预热,运行模板和检测分支10次,先屏蔽掉 # for i in range(10): # net.temple(torch.autograd.Variable(torch.FloatTensor(1, 3, 127, 127)).cuda()) # selonsy:cuda()表示使用GPU进行计算,FloatTensor(1, 3, 127, 127):浮点型四维张量 # net(torch.autograd.Variable(torch.FloatTensor(1, 3, 255, 255)).cuda()) # start to track handle = vot.VOT("polygon") Polygon = handle.region() # region:将配置消息发送到客户端并接收初始化区域和第一个图像的路径。其返回值为初始化区域。 cx, cy, w, h = get_axis_aligned_bbox(Polygon) # get_axis_aligned_bbox:将坐标数据转换成 RPN 的格式 image_file = handle.frame() # frame 函数从客户端获取帧(图像路径) if not image_file: sys.exit(0) target_pos, target_sz = np.array([cx, cy]), np.array([w, h]) im = cv2.imread(image_file) # HxWxC state = SiamRPN_init(im, target_pos, target_sz, net) # init tracker,SiamRPN_init:构造状态结构体并运行模板分支 # 从第一帧开始跟踪,表示很奇怪,难道直接给定的不准确么? # selonsy:改进点 while True: # 进入跟踪循环 image_file = handle.frame() if not image_file: break im = cv2.imread(image_file) # HxWxC state = SiamRPN_track(state, im) # track,SiamRPN_track:运行检测分支并更新状态变量