def tracking(im, state, cap): """Return cx, cy""" state = SiamRPN_track(state, im) # track res = cxy_wh_2_rect(state['target_pos'], state['target_sz']) cx, cy = res[0] + res[2] / 2, res[1] + res[3] / 2 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) cap.write(im) cv2.waitKey(1) return cx, cy
def test(score): net = SiamRPNvot() net.load_state_dict( torch.load('/home/traker_hao/code/learn/train_RPN/model/30.model')) net.eval().cuda() version_name = 'jiasu' sequence_path = '/media/traker_hao/data/dataset/UAV1/sequences' init_path = '/media/traker_hao/data/dataset/UAV1/annotations' result_path = '/home/traker_hao/result/visdrone/' + version_name if os.path.exists(result_path) is False: os.mkdir(result_path) sequence_names = os.listdir(sequence_path) random.shuffle(sequence_names) #sequence_names.sort() i = 0 for sequence_name in sequence_names: print(sequence_name) #if sequence_name != 'Suv': #continue #sequence_name='uav0000054_00000_s' imagenames = os.listdir(sequence_path + '/' + sequence_name) imagenames.sort() print(i) i = i + 1 print(sequence_path + '/' + sequence_name) f = open( result_path + '/' + sequence_name + '_' + version_name + '.txt', 'w') inited = False fp = open(init_path + '/' + sequence_name + '.txt') j = 0 for imagename in imagenames: j = j + 1 image = cv2.imread(sequence_path + '/' + sequence_name + '/' + imagename) #init the tracker if inited is False: data = fp.readline() data = data.strip('\n') data = data.split(',') [cx, cy, w, h] = (int(data[0]) + int(data[2]) // 2, int(data[1]) + int(data[3]) // 2, int(data[2]), int(data[3])) #f.write(str(annos[0]['bbox'][0])+','+str(annos[0]['bbox'][1])+','+str(annos[0]['bbox'][2])+','+str(annos[0]['bbox'][3])+','+str(1.00)+'\n') f.write(data[0] + ',' + data[1] + ',' + data[2] + ',' + data[3] + '\n') target_pos, target_sz = np.array([cx, cy]), np.array([w, h]) state = SiamRPN_init(image, target_pos, target_sz, net) inited = True cv2.rectangle(image, (int(cx) - int(w) // 2, int(cy) - int(h) // 2), (int(cx) + int(w) // 2, int(cy) + int(h) // 2), (0, 255, 0), 3) cv2.putText(image, sequence_name, (50, 50), 0, 5e-3 * 200, (0, 255, 0), 2) cv2.putText(image, 'initing...', (100, 100), 0, 5e-3 * 200, (0, 255, 0), 2) image2 = cv2.resize(image, (960, 540)) cv2.imshow('aa2', image2) cv2.waitKey(1) else: data = fp.readline() data = data.strip('\n') data = data.split(',') try: truth = (int(data[0]), int(data[1]), int(data[0]) + int(data[2]), int(data[1]) + int(data[3])) except: truth = [0, 0, 0, 0] #update the tracker #print([cx, cy, w, h]) tic = cv2.getTickCount() t1 = time.time() state = SiamRPN_track(state, image) # track #state['target_sz'] = np.array( [int(data[2]), int(data[3])] ) toc = (cv2.getTickCount() - tic) / cv2.getTickFrequency() #print(1/toc) #mytracker.target_sz = np.array([int(truth[2]),int(truth[3])]) res = cxy_wh_2_rect(state['target_pos'], state['target_sz']) res = [int(l) for l in res] cv2.rectangle(image, (res[0], res[1]), (res[0] + res[2], res[1] + res[3]), (0, 255, 255), 2) #visualize the result cv2.rectangle(image, (int(truth[0]), int(truth[1])), (int(truth[2]), int(truth[3])), (0, 255, 0), 2) #mytracker.target_sz=np.array([int(data[2]),int(data[3])]) #cv2.putText(image, str(iou), (res[0] + res[2], res[1] + res[3]), 0, 5e-3*200, (0,255,0), 2) cv2.putText(image, sequence_name, (50, 50), 0, 5e-3 * 200, (0, 255, 0), 2) image2 = cv2.resize(image, (960, 540)) cv2.imshow('aa2', image2) if cv2.waitKey(1) == 97: break #if j>209: #cv2.waitKey(0) f.close()
# 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:运行检测分支并更新状态变量 res = cxy_wh_2_rect(state['target_pos'], state['target_sz']) # cxy_wh_2_rect:将坐标转换成矩形框的表示形式 handle.report(Rectangle(res[0], res[1], res[2], res[3])) # report:将跟踪结果报告给客户端 print(handle.result) print(handle.frames) del handle
# warm up for i in range(10): net.temple(torch.autograd.Variable(torch.ones(1, 3, 127, 127)).cuda(), \ torch.autograd.Variable(torch.ones(1, 3, 271, 271)).cuda()) net(torch.autograd.Variable(torch.ones(1, 3, 271, 271)).cuda()) for seq in OTB_seqs: _data_provider.pick_seq(seq) exemplar_path, exemplar_gt, cur_img_num = _data_provider.eval_pick_exemplar( ) exemplar = cv2.imread(exemplar_path) exemplar_pos, exemplar_sz = rect_2_cxy_wh(exemplar_gt) state = SiamRPN_init(exemplar, exemplar_pos, exemplar_sz, net) save_file = save_res_path + seq + '_ours.txt' tracking_res = open(save_file, 'w') for idx in range(cur_img_num): instance_path = _data_provider.eval_pick_instance() instance = cv2.imread(instance_path) state = SiamRPN_track(state, instance) print('seq:{}:{} , score:{}'.format(seq, idx, state['score'])) res = cxy_wh_2_rect(state['target_pos'], state['target_sz']) tracking_res.write('{},{},{},{}'.format(res[0], res[1], res[2], res[3])) tracking_res.write('\n') tracking_res.close()