def make_training_sample_BBShift_(self, bbParams, dbg=False): """generate training samples based on bbparams""" bbox_curr_gt = self.bbox_curr_gt_ bbox_curr_shift = BoundingBox(0, 0, 0, 0) bbox_curr_shift = bbox_curr_gt.shift( self.img_curr_, bbParams.lamda_scale, bbParams.lamda_shift, bbParams.min_scale, bbParams.max_scale, True, bbox_curr_shift) rand_search_region, rand_search_location, edge_spacing_x, edge_spacing_y = cropPadImage( bbox_curr_shift, self.img_curr_, dbg=self._dbg, viz=self._viz) bbox_curr_gt = self.bbox_curr_gt_ bbox_gt_recentered = BoundingBox(0, 0, 0, 0) bbox_gt_recentered = bbox_curr_gt.recenter(rand_search_location, edge_spacing_x, edge_spacing_y, bbox_gt_recentered) if dbg: env = self._env + '_make_training_sample_bbshift' viz = self._viz curr_img_bbox = draw.bbox(self.img_curr_, bbox_curr_gt) recentered_img = draw.bbox(rand_search_region, bbox_gt_recentered) viz.plot_image_opencv(curr_img_bbox, 'curr shifted bbox', env=env) viz.plot_image_opencv(recentered_img, 'recentered shifted bbox', env=env) bbox_gt_recentered.scale(rand_search_region) bbox_gt_scaled = bbox_gt_recentered return rand_search_region, self.target_pad_, bbox_gt_scaled
def track(self): """Track""" vid_frames = self._vid_frames[0] num_frames = len(vid_frames) f_path = vid_frames[0] frame_0 = image_io.load(f_path) prev = np.asarray(frame_0) global image image = prev while True: # prev_out = cv2.cvtColor(prev, cv2.COLOR_RGB2BGR) prev_out = np.copy(prev) cv2.imshow('image', prev_out) key = cv2.waitKey(1) & 0xFF if key == ord('s'): (x1, y1), (x2, y2) = refPt[0], refPt[1] bbox_0 = BoundingBox(x1, y1, x2, y2) break elif key == ord('r'): (x1, y1), (x2, y2) = refPt[0], refPt[1] bbox_0 = BoundingBox(x1, y1, x2, y2) break for i in range(1, num_frames): f_path = vid_frames[i] frame_1 = image_io.load(f_path) curr = np.asarray(frame_1) bbox_0 = self._track(curr, prev, bbox_0) bbox = bbox_0 prev = curr if cv2.waitKey(1) & 0xFF == ord('p'): while True: image = curr cv2.imshow("image", curr) key = cv2.waitKey(0) & 0xFF if key == ord("s"): (x1, y1), (x2, y2) = refPt[0], refPt[1] bbox_0 = BoundingBox(x1, y1, x2, y2) break curr_dbg = np.copy(curr) curr_dbg = cv2.rectangle(curr_dbg, (int(bbox.x1), int(bbox.y1)), (int(bbox.x2), int(bbox.y2)), (255, 255, 0), 2) # curr_dbg = cv2.cvtColor(curr_dbg, cv2.COLOR_RGB2BGR) cv2.imshow('image', curr_dbg) # cv2.imwrite('./output/{:04d}.png'.format(i), curr_dbg) cv2.waitKey(20)
def vis_images(self, prev, curr, gt_bb, pred_bb, prefix='train'): def unnormalize(image, mean, std): image = np.transpose(image, (1, 2, 0)) * std + mean image = image.astype(np.float32) return image for i in range(0, prev.shape[0]): _mean = np.array([104, 117, 123]) _std = np.ones_like(_mean) prev_img = prev[i].cpu().detach().numpy() curr_img = curr[i].cpu().detach().numpy() prev_img = unnormalize(prev_img, _mean, _std) curr_img = unnormalize(curr_img, _mean, _std) gt_bb_i = BoundingBox(*gt_bb[i].cpu().detach().numpy().tolist()) gt_bb_i.unscale(curr_img) curr_img = draw.bbox(curr_img, gt_bb_i, color=(255, 255, 255)) pred_bb_i = BoundingBox( *pred_bb[i].cpu().detach().numpy().tolist()) pred_bb_i.unscale(curr_img) curr_img = draw.bbox(curr_img, pred_bb_i) out = np.concatenate( (prev_img[np.newaxis, ...], curr_img[np.newaxis, ...]), axis=0) out = np.transpose(out, [0, 3, 1, 2]) self._viz.plot_images_np(out, title='sample_{}'.format(i), env='goturn_{}'.format(prefix))
def __load_annotation_file(self, alov_sub_dir, ann_file, ext='jpg'): ''' @alov_sub_dir: subdirectory of images directory @ann_file: annotation file to fetch the bounding boxes from @ext: image extension ''' vid_path = self._imgs_dir.joinpath(alov_sub_dir).joinpath( ann_file.stem) all_frames = vid_path.glob('*.{}'.format(ext)) objVid = video(vid_path) objVid._all_frames = sorted(all_frames) with open(ann_file) as f: data = f.read().rstrip().split('\n') for bb in data: frame_num, ax, ay, bx, by, cx, cy, dx, dy = [ float(i) for i in bb.split() ] frame_num = int(frame_num) x1 = min(ax, min(bx, min(cx, dx))) - 1 y1 = min(ay, min(by, min(cy, dy))) - 1 x2 = max(ax, max(bx, max(cx, dx))) - 1 y2 = max(ay, max(by, max(cy, dy))) - 1 bbox = BoundingBox(x1, y1, x2, y2) objFrame = frame(frame_num - 1, bbox) objVid._annotations.append(objFrame) if alov_sub_dir not in self._cats.keys(): self._cats[alov_sub_dir] = [] self._cats[alov_sub_dir].append(objVid)
def track(self): """Track""" print("start tracking") vid_frames = self._vid_frames[0] num_frames = len(vid_frames) f_path = vid_frames[0] frame_0 = image_io.load(f_path) prev = np.asarray(frame_0) global image image = prev refPt = None while True: # prev_out = cv2.cvtColor(prev, cv2.COLOR_RGB2BGR) prev_out = np.copy(prev) cv2.imshow('image', prev_out) key = cv2.waitKey(100) & 0xFF if key == ord('s') or key == ord('r'): bboxes_0 = [] for refPt in refPts: (x1, y1), (x2, y2) = refPt[0], refPt[1] bboxes_0.append(BoundingBox(x1, y1, x2, y2)) break for i in range(1, num_frames): print("frame: ", i) f_path = vid_frames[i] frame_1 = image_io.load(f_path) curr = np.asarray(frame_1) curr_dbg = np.copy(curr) for index, bbox_0 in enumerate(bboxes_0): bboxes_0[index] = self._track(curr, prev, bbox_0) bbox = bboxes_0[index] # Star's note: useless code so far... # if cv2.waitKey(1) & 0xFF == ord('p'): # while True: # image = curr # cv2.imshow("image", curr) # key = cv2.waitKey(0) & 0xFF # if key == ord("s"): # (x1, y1), (x2, y2) = refPt[0], refPt[1] # bbox_0 = BoundingBox(x1, y1, x2, y2) # break curr_dbg = cv2.rectangle(curr_dbg, (int(bbox.x1), int(bbox.y1)), (int(bbox.x2), int(bbox.y2)), bb_colors[index], 2) prev = curr # curr_dbg = cv2.cvtColor(curr_dbg, cv2.COLOR_RGB2BGR) cv2.imshow('image', curr_dbg) # cv2.imwrite('./output/{:04d}.png'.format(i), curr_dbg) cv2.waitKey(20)
def set_init_rect(self, req): try: print(req) self.init_rect = BoundingBox(req.xmin, req.ymin, req.xmax, req.ymax) print('Set init rect success.') return True except Exception as e: print(e) print('Set init rect wrong.') return False
def track(self, image_curr, objRegressor): """TODO: Docstring for tracker. :returns: TODO """ target_pad, _, _, _ = cropPadImage(self.bbox_prev_tight, self.image_prev) cur_search_region, search_location, edge_spacing_x, edge_spacing_y = cropPadImage( self.bbox_curr_prior_tight, image_curr) bbox_estimate = objRegressor.regress(cur_search_region, target_pad) bbox_estimate = BoundingBox(bbox_estimate[0, 0], bbox_estimate[0, 1], bbox_estimate[0, 2], bbox_estimate[0, 3]) # plt.imshow(cur_search_region) # plt.show() # plt.imshow(target_pad) # plt.show() # Inplace correction of bounding box bbox_estimate.unscale(cur_search_region) bbox_estimate.uncenter(image_curr, search_location, edge_spacing_x, edge_spacing_y) self.image_prev = image_curr self.bbox_prev_tight = bbox_estimate self.bbox_curr_prior_tight = bbox_estimate return bbox_estimate
def make_true_sample(self): """Generate true target:search_region pair""" curr_prior_tight = self.bbox_prev_gt_ target_pad = self.target_pad_ # To find out the region in which we need to search in the # current frame, we use the previous frame bbox to get the # region in which we can make the search output = cropPadImage(curr_prior_tight, self.img_curr_, self._dbg, self._viz) curr_search_region, curr_search_location, edge_spacing_x, edge_spacing_y = output bbox_curr_gt = self.bbox_curr_gt_ bbox_curr_gt_recentered = BoundingBox(0, 0, 0, 0) bbox_curr_gt_recentered = bbox_curr_gt.recenter( curr_search_location, edge_spacing_x, edge_spacing_y, bbox_curr_gt_recentered) if self._dbg: env = self._env + '_make_true_sample' search_dbg = draw.bbox(self.img_curr_, curr_search_location) search_dbg = draw.bbox(search_dbg, bbox_curr_gt, color=(255, 255, 0)) self._viz.plot_image_opencv(search_dbg, 'search_region', env=env) recentered_img = draw.bbox(curr_search_region, bbox_curr_gt_recentered, color=(255, 255, 0)) self._viz.plot_image_opencv(recentered_img, 'cropped_search_region', env=env) del recentered_img del search_dbg bbox_curr_gt_recentered.scale(curr_search_region) return curr_search_region, target_pad, bbox_curr_gt_recentered
def _track(self, curr_frame, prev_frame, rect): """track current frame @curr_frame: current frame @prev_frame: prev frame @rect: bounding box of previous frame """ prev_bbox = rect target_pad, _, _, _ = cropPadImage(prev_bbox, prev_frame) cur_search_region, search_location, edge_spacing_x, edge_spacing_y = cropPadImage( prev_bbox, curr_frame) if self._dbg: self._viz.plot_image_opencv(target_pad, 'target') self._viz.plot_image_opencv(cur_search_region, 'current') target_pad_in = self.preprocess(target_pad, mean=None).unsqueeze(0) cur_search_region_in = self.preprocess(cur_search_region, mean=None).unsqueeze(0) # GPU support target_pad_in = target_pad_in.to(gpu_device) cur_search_region_in = cur_search_region_in.to(gpu_device) pred_bb = self._model.forward(target_pad_in, cur_search_region_in) if self._dbg: prev_bbox.scale(prev_frame) x1, y1, x2, y2 = prev_bbox.x1, prev_bbox.y1, prev_bbox.x2, prev_bbox.y2 prev_bbox = torch.tensor([x1, y1, x2, y2]).unsqueeze(0) target_dbg = target_pad_in.clone() cur_search_region_dbg = cur_search_region_in.clone() self.vis_images(target_dbg, cur_search_region_dbg, prev_bbox, pred_bb) pred_bb = BoundingBox(*pred_bb[0].cpu().detach().numpy().tolist()) pred_bb.unscale(cur_search_region) pred_bb.uncenter(curr_frame, search_location, edge_spacing_x, edge_spacing_y) x1, y1, x2, y2 = int(pred_bb.x1), int(pred_bb.y1), int( pred_bb.x2), int(pred_bb.y2) pred_bb = BoundingBox(x1, y1, x2, y2) return pred_bb
def trackAllRealTimeSimu_OnVideo(self, inputVideoPath, save_tracking=False, show_tracking=True): """Track the objects in the video Simulates real time by taking the frame according to computation time of each itteration. """ objRegressor = self.regressor objTracker = self.tracker vid = cv2.VideoCapture(inputVideoPath) FPS = vid.get(cv2.CAP_PROP_FPS) ok, frame_0 = vid.read() if not ok: print('Couldnt read first frame. Exit.') exit() if save_tracking: inputName = inputVideoPath.split('/')[-1:][0].split('.')[:-1][0] tracker_type = 'GoturnGPU' output_video_name = '../output/' + inputName +'_'+ tracker_type + time.strftime("_%-Y-%m-%d_%H-%M")+'.mp4' fourcc = cv2.VideoWriter_fourcc(*'mp4v') width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) outVideo = cv2.VideoWriter(output_video_name, fourcc, int(FPS), (width, height)) box_0 = cv2.selectROI(frame_0) cv2.destroyAllWindows() num_frames = int(vid.get(cv2.CAP_PROP_FRAME_COUNT)) bbox = BoundingBox(x1=box_0[0], y1=box_0[1], x2=box_0[0] + box_0[2], y2=box_0[1] + box_0[3]) # Init Goturn objTracker.init(frame_0, bbox, objRegressor) TimeSimulation = 0 color = (50, 255, 50) for i in range(0,num_frames): # Simulate real time by taking the next frame TimeSimuFrame = TimeSimulation * FPS NumframeSimu = int(TimeSimuFrame) print('Tracking on frame:' + str(NumframeSimu) + '/' + str(num_frames)) vid.set(1, NumframeSimu) ok, frame = vid.read() if not ok: break ### Start timer timeA = cv2.getTickCount() ### Update Tracker bbox = objTracker.track(frame, objRegressor) ### Calculate Frames per second (FPS) timeB = cv2.getTickCount() fps = cv2.getTickFrequency() /(timeB - timeA) TimeSimulation += 1/fps ### Draw bounding box ImageDraw = frame.copy() ImageDraw = cv2.rectangle(ImageDraw, (int(bbox.x1), int(bbox.y1)), (int(bbox.x2), int(bbox.y2)), color, 2) # Display FPS on frame cv2.putText(ImageDraw, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, color, 2) if show_tracking: cv2.imshow('Results', ImageDraw) cv2.waitKey(1) if save_tracking: outVideo.write(ImageDraw) print('Total processing time =' + str(TimeSimulation)) print('Average FPS =' + str(i/TimeSimulation)) if save_tracking: outVideo.release()
def trackAll(self, inputVideo,save_tracking=False,show_tracking=True): """Track the objects in the video """ objRegressor = self.regressor objTracker = self.tracker vid = cv2.VideoCapture(inputVideo) ok, frame_0 =vid.read() if not ok: print('Couldnt read first frame') exit() if save_tracking: movie_number =0 tracker_type ='GoturnGPU' fourcc = cv2.VideoWriter_fourcc(*'DIVX') width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) FPS = vid.get(cv2.CAP_PROP_FPS) output_video_name = 'output_videos/movie_' + str(movie_number) + time.strftime( "_%d%m%Y_%H%M%S") + '_' + tracker_type + '.avi' outVideo = cv2.VideoWriter(output_video_name, fourcc, int(FPS), (width, height)) box_0 = cv2.selectROI(frame_0) cv2.destroyAllWindows() num_frames = int(vid.get(cv2.CAP_PROP_FRAME_COUNT)) bbox_0 = BoundingBox(x1=box_0[0], y1=box_0[1], x2=box_0[0] +box_0[2], y2=box_0[1] +box_0[3]) objTracker.init(frame_0, bbox_0, objRegressor) timerI = cv2.getTickCount() for i in range(1, num_frames-1): # vid.set(1,i) print('Tracking on frame:'+str(i)+'/'+str(num_frames)) ok,frame = vid.read() if not ok: break ### Start timer timer = cv2.getTickCount() ### Update Tracker bbox = objTracker.track(frame, objRegressor) ### Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) ### Draw bounding box ImageDraw = frame.copy() ImageDraw = cv2.rectangle(ImageDraw, (int(bbox.x1), int(bbox.y1)), (int(bbox.x2), int(bbox.y2)), (255, 0, 0), 2) # Display FPS on frame cv2.putText(ImageDraw, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 0, 0), 2) if show_tracking: cv2.imshow('Results', ImageDraw) cv2.waitKey(1) if save_tracking: outVideo.write(ImageDraw) timerF = cv2.getTickCount() Time_proc = (timerF-timerI)/cv2.getTickFrequency() print('Total processing time ='+ str(int(Time_proc))+'sec') print('Average FPS ='+ str(i / Time_proc)) vid.release() if save_tracking: outVideo.release()
def trackAllRealTimeSimu(self, input_folder, inputVideo, save_tracking=False, show_tracking=True, Frame_int_Goturn=0): """Track the objects in the video Simulates real time by taking the frame according to computation time of each itteration. """ objRegressor = self.regressor objTracker = self.tracker vid = cv2.VideoCapture(input_folder + inputVideo) FPS = vid.get(cv2.CAP_PROP_FPS) ok, frame_0 = vid.read() if not ok: print('Couldnt read first frame. Exit.') exit() if save_tracking: movie_number = 0 tracker_type = 'GoturnGPU' fourcc = cv2.VideoWriter_fourcc(*'DIVX') width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) output_video_name = 'output_videos/movie_' + inputVideo + str( movie_number) + time.strftime( "_%d%m%Y_%H%M%S") + '_' + tracker_type + '.avi' outVideo = cv2.VideoWriter(output_video_name, fourcc, int(FPS), (width, height)) box_0 = cv2.selectROI(frame_0) cv2.destroyAllWindows() num_frames = int(vid.get(cv2.CAP_PROP_FRAME_COUNT)) bbox = BoundingBox(x1=box_0[0], y1=box_0[1], x2=box_0[0] + box_0[2], y2=box_0[1] + box_0[3]) # Init Goturn objTracker.init(frame_0, bbox, objRegressor) # Init KCF multiTracker = cv2.MultiTracker_create() multiTracker.add(cv2.TrackerKCF_create(), frame_0, (bbox.x1, bbox.y1, (bbox.x2 - bbox.x1), (bbox.y2 - bbox.y1))) num_KCF = 0 TimeSimulation = 0 i = 0 color = (50, 255, 50) while True: i += 1 # Simulate real time by taking the next frame TimeSimuFrame = TimeSimulation * FPS NumframeSimu = int(TimeSimuFrame) print('Tracking on frame:' + str(NumframeSimu) + '/' + str(num_frames)) vid.set(1, NumframeSimu) ok, frame = vid.read() if not ok: break ### Start timer timeA = cv2.getTickCount() ### Update Tracker if num_KCF < Frame_int_Goturn: ok_KCF, box = multiTracker.update(frame) if ok_KCF: bbox = BoundingBox(x1=box[0][0], y1=box[0][1], x2=box[0][0] + box[0][2], y2=box[0][1] + box[0][3]) num_KCF += 1 if num_KCF == Frame_int_Goturn or not ok_KCF: objTracker.update__prev_Jo(frame, bbox) color = (255, 0, 0) if not num_KCF < Frame_int_Goturn or not ok_KCF: num_KCF = 0 bbox = objTracker.track(frame, objRegressor) multiTracker = cv2.MultiTracker_create() multiTracker.add( cv2.TrackerKCF_create(), frame, (int(bbox.x1), int(bbox.y1), int(bbox.x2 - bbox.x1), int(bbox.y2 - bbox.y1))) bbox = objTracker.track(frame, objRegressor) ### Calculate Frames per second (FPS) timeB = cv2.getTickCount() fps = cv2.getTickFrequency() / (timeB - timeA) TimeSimulation += 1 / fps ### Draw bounding box ImageDraw = frame.copy() ImageDraw = cv2.rectangle(ImageDraw, (int(bbox.x1), int(bbox.y1)), (int(bbox.x2), int(bbox.y2)), color, 2) color = (50, 255, 50) # Display FPS on frame cv2.putText(ImageDraw, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 0, 0), 2) if show_tracking: cv2.imshow('Results', ImageDraw) cv2.waitKey(1) if save_tracking: outVideo.write(ImageDraw) timerF = cv2.getTickCount() print('Total processing time =' + str(TimeSimulation)) print('Average FPS =' + str(i / TimeSimulation)) if save_tracking: outVideo.release()
def trackAllJetsonCam(self, inputVideo, save_tracking=False, show_tracking=True, Frame_int_Goturn=0, TimeToRun=30, camera=False): """Track the objects in the video TimeToRun IN SECONDS ! """ objRegressor = self.regressor objTracker = self.tracker if camera: vid = cv2.VideoCapture( "nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720,format=(string)I420, " "framerate=(fraction)20/1 ! nvvidconv flip-method=0 ! video/x-raw, format=(string)BGRx ! videoconvert ! " "video/x-raw, format=(string)BGR ! appsink") num_frames = TimeToRun * 20 else: vid = cv2.VideoCapture(inputVideo) num_frames = int(vid.get(cv2.CAP_PROP_FRAME_COUNT)) ok, frame_0 = vid.read() if not ok: print('Couldnt read first frame. Exit.') exit() FPS = vid.get(cv2.CAP_PROP_FPS) if save_tracking: movie_number = 903 tracker_type = 'GoturnGPU' fourcc = cv2.VideoWriter_fourcc(*'DIVX') width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) output_video_name = 'output_videos/movie_' + str( movie_number) + time.strftime( "_%d%m%Y_%H%M%S") + '_' + tracker_type + '.avi' outVideo = cv2.VideoWriter(output_video_name, fourcc, int(60), (width, height)) box_0 = cv2.selectROI(frame_0) cv2.destroyAllWindows() bbox_0 = BoundingBox(x1=box_0[0], y1=box_0[1], x2=box_0[0] + box_0[2], y2=box_0[1] + box_0[3]) # Init Goturn objTracker.init(frame_0, bbox_0, objRegressor) # Init KCF multiTracker = cv2.MultiTracker_create() multiTracker.add(cv2.TrackerKCF_create(), frame_0, (bbox_0.x1, bbox_0.y1, (bbox_0.x2 - bbox_0.x1), (bbox_0.y2 - bbox_0.y1))) num_KCF = 0 bbox = bbox_0 timerI = cv2.getTickCount() for i in range(2, num_frames): print('Tracking on frame:' + str(i) + '/' + str(num_frames)) ok, frame = vid.read() if not ok: break ### Start timer timer = cv2.getTickCount() ### Update Tracker if num_KCF < Frame_int_Goturn: ok_KCF, box = multiTracker.update(frame) if ok_KCF: bbox = BoundingBox(x1=box[0][0], y1=box[0][1], x2=box[0][0] + box[0][2], y2=box[0][1] + box[0][3]) num_KCF += 1 color = (50, 255, 50) if num_KCF == Frame_int_Goturn or not ok_KCF: objTracker.update__prev_Jo(frame, bbox) if not num_KCF < Frame_int_Goturn or not ok_KCF: num_KCF = 0 bbox = objTracker.track(frame, objRegressor) multiTracker = cv2.MultiTracker_create() multiTracker.add( cv2.TrackerKCF_create(), frame, (int(bbox.x1), int(bbox.y1), int(bbox.x2 - bbox.x1), int(bbox.y2 - bbox.y1))) color = (255, 0, 0) ### Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) ### Draw bounding box ImageDraw = frame.copy() ImageDraw = cv2.rectangle(ImageDraw, (int(bbox.x1), int(bbox.y1)), (int(bbox.x2), int(bbox.y2)), color, 2) # Display FPS on frame cv2.putText(ImageDraw, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 0, 0), 2) if show_tracking: cv2.imshow('Results', ImageDraw) cv2.waitKey(1) if save_tracking: outVideo.write(ImageDraw) timerF = cv2.getTickCount() Time_proc = (timerF - timerI) / cv2.getTickFrequency() print('Total processing time =' + str(int(Time_proc)) + 'sec') print('Average FPS =' + str(i / Time_proc)) cv2.imshow('Results', ImageDraw) cv2.waitKey(5000) vid.release() if save_tracking: outVideo.release()
def trackAllGoturnKcf(self, inputVideo, save_tracking=False, show_tracking=True, Frame_int_Goturn=0): """Track the objects in the video Combining Opencv's KCF tracking with the goturn tracking. """ objRegressor = self.regressor objTracker = self.tracker vid = cv2.VideoCapture(inputVideo) ok, frame_0 = vid.read() if not ok: print('Couldnt read first frame. Exit.') exit() if save_tracking: movie_number = 0 tracker_type = 'GoturnGPU' fourcc = cv2.VideoWriter_fourcc(*'mp4v') width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) FPS = vid.get(cv2.CAP_PROP_FPS) output_video_name = 'output_videos/movie_' + str( movie_number) + time.strftime( "_%d%m%Y_%H%M%S") + '_' + tracker_type + '.mp4' outVideo = cv2.VideoWriter(output_video_name, fourcc, int(FPS), (width, height)) box_0 = cv2.selectROI(frame_0) cv2.destroyAllWindows() num_frames = int(vid.get(cv2.CAP_PROP_FRAME_COUNT)) bbox_0 = BoundingBox(x1=box_0[0], y1=box_0[1], x2=box_0[0] + box_0[2], y2=box_0[1] + box_0[3]) # Init Goturn objTracker.init(frame_0, bbox_0, objRegressor) # Init KCF multiTracker = cv2.MultiTracker_create() multiTracker.add(cv2.TrackerKCF_create(), frame_0, (bbox_0.x1, bbox_0.y1, (bbox_0.x2 - bbox_0.x1), (bbox_0.y2 - bbox_0.y1))) num_KCF = 0 bbox = bbox_0 timerI = cv2.getTickCount() for i in range(2, num_frames): print('Tracking on frame:' + str(i) + '/' + str(num_frames)) ok, frame = vid.read() if not ok: break ### Start timer timer = cv2.getTickCount() ### Update Tracker if num_KCF < Frame_int_Goturn: ok_KCF, box = multiTracker.update(frame) if ok_KCF: bbox = BoundingBox(x1=box[0][0], y1=box[0][1], x2=box[0][0] + box[0][2], y2=box[0][1] + box[0][3]) num_KCF += 1 color = (50, 255, 50) if num_KCF == Frame_int_Goturn or not ok_KCF: objTracker.update__prev_Jo(frame, bbox) if not num_KCF < Frame_int_Goturn or not ok_KCF: num_KCF = 0 bbox = objTracker.track(frame, objRegressor) multiTracker = cv2.MultiTracker_create() multiTracker.add( cv2.TrackerKCF_create(), frame, (int(bbox.x1), int(bbox.y1), int(bbox.x2 - bbox.x1), int(bbox.y2 - bbox.y1))) color = (255, 0, 0) ### Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) ### Draw bounding box ImageDraw = frame.copy() ImageDraw = cv2.rectangle(ImageDraw, (int(bbox.x1), int(bbox.y1)), (int(bbox.x2), int(bbox.y2)), color, 2) # Display FPS on frame cv2.putText(ImageDraw, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 0, 0), 2) if show_tracking: cv2.imshow('Results', ImageDraw) cv2.waitKey(1) if save_tracking: outVideo.write(ImageDraw) timerF = cv2.getTickCount() Time_proc = (timerF - timerI) / cv2.getTickFrequency() print('Total processing time =' + str(int(Time_proc)) + 'sec') print('Average FPS =' + str(i / Time_proc)) cv2.imshow('Results', ImageDraw) cv2.waitKey(5000) vid.release() if save_tracking: outVideo.release()
def __init__(self): """TODO: to be defined1. """ self.bbox = BoundingBox(0, 0, 0, 0) self.image_path = [] self.disp_width = 0 self.disp_height = 0
def __init__(self): """Annotation class stores bounding box, image path""" self.bbox = BoundingBox(0, 0, 0, 0) self.image_path = [] self.disp_width = 0 self.disp_height = 0