def Init_tracker(firstframe, position, session, network): frame = firstframe height, width = frame.shape[:2] if len(frame.shape) == 3: is_color = True # 彩色图每张(frame)为[h,w,3]的形式 else: is_color = False frame = frame[:, :, np.newaxis] # 灰度图每张转化为[h,w,1]的形式 # 目标位置信息posision的格式为(xmin,ymin,w,h) tracker = ECOTracker(is_color, session, network) # 初始化追踪器(类) # 第一帧操作 bbox = position # 目标初始位置(第一帧位置) tracker.init(frame, bbox) bbox = (bbox[0] - 1, bbox[1] - 1, bbox[0] + bbox[2] - 1, bbox[1] + bbox[3] - 1) # bbox改为左上角和右下角的位置 frame = cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 255, 0), 2) frame = frame.squeeze() # 真实位置画框 if len(frame.shape) == 3: frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # 由于cv2的imshow图像格式为BGR,而Image读取的图像格式为RGB,转换 frame = cv2.putText(frame, str('start'), (5, 20), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1) # 在图像上显示帧数 cv2.imshow('track', frame) cv2.waitKey(30) return tracker
def init_data(self): self.src_video_path = None self.video_len = None self.cur_frame_id = 0 self.tmr = QTimer() self.time = QTime() self.proc_time = 40 self.tmr.setInterval(self.proc_time) self.tmr.timeout.connect(self.timeout_handler) self.tmr.start() self.time.start() self.fps_period = None self.last_timestamp = self.time.elapsed() self.video_capturer = None self.frame = None self.prev_frame = None self.frame_draw = None self.frame_crop = None self.proc_qimage = None self.proc_qpixmap = None self.amount_of_frames = None self.feat_extr = Feature_Extractor() self.bay_cl = BayesClassifier() self.proceed_frame = Proceed_Frame() self.tracker = Tracker(4, 15, 30, 31) self.orb = orb() self.dist_thresh = 4 self.max_frames_to_skip = 15 self.size_orb_area = 30 self.left_corner = None self.right_corner = None self.left_corner_real = None self.right_corner_real = None self.kp_select = None self.nkeypoints = 300 self.brief_size = 31 self.begin = QtCore.QPoint() self.end = QtCore.QPoint() self.width = None self.height = None self.enable_playing = False self.fern_det = False self.orb_det = False self.harris_det = False self.enable_clust = False self.enable_tracking_orb = False self.enable_tracking_Hungarian = False self.enable_tracking_hung_orb = False self.enable_eco = False self.camera_id = 0 self.camera_capturer = cv2.VideoCapture(self.camera_id) self.camera_frame = None self.video_frame = None self.video_src = 'video' self.clust_kps = [] self.en_roi = False self.bbox_eco = () self.bbox_init_eco = () self.tracker_eco = ECOTracker(True) pass
def run_ECO(seq, rp, saveimage): x = seq.init_rect[0] y = seq.init_rect[1] w = seq.init_rect[2] h = seq.init_rect[3] frames = [np.array(Image.open(filename)) for filename in seq.s_frames] # frames = [cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB) for filename in seq.s_frames] if len(frames[0].shape) == 3: is_color = True else: is_color = False frames = [frame[:, :, np.newaxis] for frame in frames] tic = time.clock() # starting tracking tracker = ECOTracker(is_color) res = [] for idx, frame in enumerate(frames): if idx == 0: bbox = (x, y, w, h) tracker.init(frame, bbox) bbox = (bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]) elif idx < len(frames) - 1: bbox = tracker.update(frame, True) else: # last frame bbox = tracker.update(frame, False) res.append((bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1])) duration = time.clock() - tic result = {} result['res'] = res result['type'] = 'rect' result['fps'] = round(seq.len / duration, 3) return result
def main(video_dir): # load videos filenames = sorted(glob.glob(os.path.join(video_dir, "*.jpg")), key=lambda x: int(os.path.basename(x).split('.')[0])) # frames = [cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB) for filename in filenames] frames = [np.array(Image.open(filename)) for filename in filenames] height, width = frames[0].shape[:2] if len(frames[0].shape) == 3: is_color = True else: is_color = False frames = [frame[:, :, np.newaxis] for frame in frames] gt_bboxes = pd.read_csv( os.path.join(video_dir, "groundtruth.txt"), sep='\t|,| ', header=None, names=['x1', 'y1', 'x2', 'y2', 'x3', 'y3', 'x4', 'y4'], engine='python') title = video_dir.split('/')[-1] # starting tracking tracker = ECOTracker(is_color) for idx, frame in enumerate(frames): if idx == 0: bbox = gt_bboxes.iloc[0].values xmin = np.min(bbox[::2]) xmax = np.max(bbox[::2]) ymin = np.min(bbox[1::2]) ymax = np.max(bbox[1::2]) bbox = (xmin, ymin, xmax - xmin + 1, ymax - ymin + 1) tracker.init(frame, bbox) bbox = (xmin, ymin, xmax, ymax) elif idx < len(frames) - 1: bbox = tracker.update(frame, True) else: # last frame bbox = tracker.update(frame, False) # bbox xmin ymin xmax ymax frame = cv2.rectangle(frame, (int(bbox[0] - 1), int(bbox[1] - 1)), (int(bbox[2] - 1), int(bbox[3] - 1)), (0, 255, 0), 2) gt_bbox = gt_bboxes.iloc[idx].values xmin = np.min(gt_bbox[::2]) xmax = np.max(gt_bbox[::2]) ymin = np.min(gt_bbox[1::2]) ymax = np.max(gt_bbox[1::2]) gt_bbox = (xmin, ymin, xmax, ymax) frame = frame.squeeze() frame = cv2.rectangle( frame, (int(gt_bbox[0] - 1), int(gt_bbox[1] - 1)), # 0-index (int(gt_bbox[2] - 1), int(gt_bbox[3] - 1)), (255, 0, 0), 1) if len(frame.shape) == 3: frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) frame = cv2.putText(frame, str(idx), (5, 20), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1) cv2.imshow(title, frame) cv2.waitKey(30)
def main(): # load videos filenames = sorted(glob.glob("../../test/*.jpg"), key=lambda x: int(os.path.basename(x).split('.')[0])) frames = [np.array(Image.open(filename)) for filename in filenames] height, width = frames[0].shape[:2] # starting tracking tracker = ECOTracker(True) vis = True videoWriter = cv2.VideoWriter('0011.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 20, (224, 224)) for idx, frame in enumerate(frames): if idx == 0: bbox = [ 245, 512, 587, 587 ] #[dets[0].left()+1,dets[0].top()+1,dets[0].right()-dets[0].left(),dets[0].bottom()-dets[0].top()] tracker.init(frame, bbox) bbox = (bbox[0] - 1, bbox[1] - 1, bbox[0] + bbox[2] - 1, bbox[1] + bbox[3] - 1) elif idx < len(frames) - 1: bbox = tracker.update(frame, True, vis) else: # last frame bbox = tracker.update(frame, False, vis) # bbox xmin ymin xmax ymax frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) if len(bbox) == 4: print("write " + str(idx)) actualbbox = [ max(0, int(bbox[1])), min(int(bbox[3]), frame.shape[0]), max(0, int(bbox[0])), min(int(bbox[2]), frame.shape[1]) ] saveframe = frame[actualbbox[0]:actualbbox[1], actualbbox[2]:actualbbox[3]] longerside = max(actualbbox[1] - actualbbox[0], actualbbox[3] - actualbbox[2]) dstsize = (224 * (actualbbox[1] - actualbbox[0]) / longerside, 224 * (actualbbox[3] - actualbbox[2]) / longerside) dstsize = (int(dstsize[1]), int(dstsize[0])) resizedimg = cv2.resize(saveframe, dstsize, interpolation=cv2.INTER_AREA) canvas = 255 * np.ones((224, 224, 3), np.uint8) try: canvas[max(0, -int(int(bbox[1]) * 224 / longerside)):( max(0, -int(int(bbox[1]) * 224 / longerside)) + dstsize[1]), max(0, -int(int(bbox[0]) * 224 / longerside)):( max(0, -int(int(bbox[0]) * 224 / longerside)) + dstsize[0])] = resizedimg videoWriter.write(canvas) except: continue videoWriter.release()
def main(): frames = [] # load videos # filenames = sorted(glob.glob(os.path.join(video_dir, "img/*.jpg")), # key=lambda x: int(os.path.basename(x).split('.')[0])) # # frames = [cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB) for filename in filenames] # frames = [np.array(Image.open(filename)) for filename in filenames] cap = cv2.VideoCapture('/media/aigul/Tom/Aigul/dim_small_target_tracking/Sword/Corsair_LWIR_20.02.19_15.02.11.avi') i = 0 while cap.isOpened(): ret, frame = cap.read() frames.append(frame) i += 1 if i == 654: break height, width = frames[0].shape[:2] if len(frames[0].shape) == 3: is_color = True else: is_color = False frames = [frame[:, :, np.newaxis] for frame in frames] # gt_bboxes = pd.read_csv(os.path.join(video_dir, "groundtruth_rect.txt"), sep='\t|,| ', # header=None, names=['xmin', 'ymin', 'width', 'height'], # engine='python') title = 'demo' # fourcc = cv2.VideoWriter_fourcc(*'XVID') # img_writer = cv2.VideoWriter(os.path.join('./videos', title+'.avi'), # fourcc, 25, (width, height)) # starting tracking tracker = ECOTracker(is_color) vis = True for idx, frame in enumerate(frames): if idx == 0: # bbox = gt_bboxes.iloc[0].values bbox = np.array([329, 357, 22, 49]) tracker.init(frame, bbox) bbox = (bbox[0]-1, bbox[1]-1, bbox[0]+bbox[2]-1, bbox[1]+bbox[3]-1) elif idx < len(frames) - 1: bbox = tracker.update(frame, True, vis) else: # last frame bbox = tracker.update(frame, False, vis) # bbox xmin ymin xmax ymax frame = frame.squeeze() if len(frame.shape) == 3: frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) else: frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) frame = cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 255, 255), 1) # gt_bbox = gt_bboxes.iloc[idx].values # gt_bbox = (gt_bbox[0], gt_bbox[1], # gt_bbox[0]+gt_bbox[2], gt_bbox[1]+gt_bbox[3]) # frame = frame.squeeze() # frame = cv2.rectangle(frame, # (int(gt_bbox[0]-1), int(gt_bbox[1]-1)), # 0-index # (int(gt_bbox[2]-1), int(gt_bbox[3]-1)), # (0, 255, 0), # 1) # if vis and idx > 0: # score = tracker.score # size = tuple(tracker.crop_size.astype(np.int32)) # score = cv2.resize(score, size) # score -= score.min() # score /= score.max() # score = (score * 255).astype(np.uint8) # # score = 255 - score # score = cv2.applyColorMap(score, cv2.COLORMAP_JET) # pos = tracker._pos # pos = (int(pos[0]), int(pos[1])) # xmin = pos[1] - size[1]//2 # xmax = pos[1] + size[1]//2 + size[1] % 2 # ymin = pos[0] - size[0] // 2 # ymax = pos[0] + size[0] // 2 + size[0] % 2 # left = abs(xmin) if xmin < 0 else 0 # xmin = 0 if xmin < 0 else xmin # right = width - xmax # xmax = width if right < 0 else xmax # right = size[1] + right if right < 0 else size[1] # top = abs(ymin) if ymin < 0 else 0 # ymin = 0 if ymin < 0 else ymin # down = height - ymax # ymax = height if down < 0 else ymax # down = size[0] + down if down < 0 else size[0] # score = score[top:down, left:right] # crop_img = frame[ymin:ymax, xmin:xmax] # # if crop_img.shape != score.shape: # # print(left, right, top, down) # # print(xmin, ymin, xmax, ymax) # score_map = cv2.addWeighted(crop_img, 0.6, score, 0.4, 0) # frame[ymin:ymax, xmin:xmax] = score_map # frame = cv2.putText(frame, str(idx), (5, 20), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1) # # img_writer.write(frame) cv2.imshow(title, frame) cv2.waitKey(1)
def main(video_dir): # load videos filenames = sorted(glob.glob(os.path.join(video_dir, "*.jpg")), key=lambda x: int(os.path.basename(x).split('.')[0])) frames = [np.array(Image.open(filename)) for filename in filenames] height, width = frames[0].shape[:2] # starting tracking tracker = ECOTracker(True) vis = True videoname = video_dir.split("/")[2] print(videoname) videoWriter = cv2.VideoWriter(videoname + '.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 20, (224, 224)) for idx, frame in enumerate(frames): if idx == 0: detector = dlib.get_frontal_face_detector() filename = os.path.join(video_dir, "0001.jpg") img = dlib.load_rgb_image(filename) dets = detector(img, 0) print(len(dets)) bbox = [ dets[0].left() + 1, dets[0].top() + 1, dets[0].right() - dets[0].left(), dets[0].bottom() - dets[0].top() ] tracker.init(frame, bbox) bbox = (bbox[0] - 1, bbox[1] - 1, bbox[0] + bbox[2] - 1, bbox[1] + bbox[3] - 1) elif idx < len(frames) - 1: bbox = tracker.update(frame, True, vis) else: # last frame bbox = tracker.update(frame, False, vis) # bbox xmin ymin xmax ymax frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) if len(bbox) == 4: print("write " + str(idx)) actualbbox = [ max(0, int(bbox[1])), min(int(bbox[3]), frame.shape[0]), max(0, int(bbox[0])), min(int(bbox[2]), frame.shape[1]) ] saveframe = frame[actualbbox[0]:actualbbox[1], actualbbox[2]:actualbbox[3]] longerside = max(actualbbox[1] - actualbbox[0], actualbbox[3] - actualbbox[2]) dstsize = (224 * (actualbbox[1] - actualbbox[0]) / longerside, 224 * (actualbbox[3] - actualbbox[2]) / longerside) dstsize = (int(dstsize[1]), int(dstsize[0])) resizedimg = cv2.resize(saveframe, dstsize, interpolation=cv2.INTER_AREA) canvas = 255 * np.ones((224, 224, 3), np.uint8) canvas[max(0, -int(int(bbox[1]) * 224 / longerside)):( max(0, -int(int(bbox[1]) * 224 / longerside)) + dstsize[1]), max(0, -int(int(bbox[0]) * 224 / longerside)):( max(0, -int(int(bbox[0]) * 224 / longerside)) + dstsize[0])] = resizedimg videoWriter.write(canvas) videoWriter.release()
if w > 0: ix, iy = int(x - w / 2), int(y - h / 2) initializing = True if __name__ == '__main__': cap = cv2.VideoCapture(0) # 获取摄像头的宽高,如果修改的话摄像头参数会被永久修改 width = int(cap.get(3)) # CV_CAP_PROP_FRAME_WIDTH height = int(cap.get(4)) # CV_CAP_PROP_FRAME_HEIGHT cv2.namedWindow('tracking') cv2.setMouseCallback('tracking', draw_bbox) # init tracker tracker = ECOTracker() # visualise score(will run slower) vis = False while cap.isOpened(): ret, frame = cap.read() if not ret: break if selecting: cv2.rectangle(frame, (ix, iy), (lx, ly), (0, 255, 255), 1) elif initializing: cv2.rectangle(frame, (ix, iy), (ix + w, iy + h), (0, 255, 255), 2) bbox = [ix, iy, w, h] tracker.init(frame, bbox)
def main(video_dir): # load videos filenames = sorted(glob.glob(os.path.join(video_dir, "img/*.jpg")), key=lambda x: int(os.path.basename(x).split('.')[0])) # frames = [cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB) for filename in filenames] frames = [np.array(Image.open(filename)) for filename in filenames] height, width = frames[0].shape[:2] if len(frames[0].shape) == 3: is_color = True # 彩色图每张(frame)为[h,w,3]的形式 else: is_color = False frames = [frame[:, :, np.newaxis] for frame in frames] # 灰度图每张转化为[h,w,1]的形式 gt_bboxes = pd.read_csv(os.path.join(video_dir, "groundtruth_rect.txt"), sep='\t|,| ', header=None, names=['xmin', 'ymin', 'width', 'height'], engine='python') # 读取目标位置信息(全部) title = video_dir.split('/')[-1] # starting tracking tracker = ECOTracker(is_color) # 初始化追踪器(类) for idx, frame in enumerate(frames): if idx == 0: # 第一帧 bbox = gt_bboxes.iloc[0].values # 目标初始位置(第一帧位置) tracker.init(frame, bbox) bbox = (bbox[0] - 1, bbox[1] - 1, bbox[0] + bbox[2] - 1, bbox[1] + bbox[3] - 1) # bbox改为左上角和右下角的位置 elif idx < len(frames) - 1: bbox = tracker.update(frame, True) # 追踪每一帧的目标位置 else: # last frame bbox = tracker.update(frame, False) # 追踪最后一帧的目标位置 # bbox xmin ymin xmax ymax frame = cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 255, 0), 2) # 预测位置画框 gt_bbox = gt_bboxes.iloc[idx].values gt_bbox = (gt_bbox[0], gt_bbox[1], gt_bbox[0] + gt_bbox[2], gt_bbox[1] + gt_bbox[3]) frame = frame.squeeze() frame = cv2.rectangle( frame, (int(gt_bbox[0] - 1), int(gt_bbox[1] - 1)), # 0-index (int(gt_bbox[2] - 1), int(gt_bbox[3] - 1)), (255, 0, 0), 1) # 真实位置画框 if len(frame.shape) == 3: frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # 由于cv2的imshow图像格式为BGR,而Image读取的图像格式为RGB,转换 frame = cv2.putText(frame, str(idx), (5, 20), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1) # 在图像上显示帧数 cv2.imshow(title, frame) cv2.waitKey(30)
def main(video_dir): # load videos filenames = sorted(glob.glob(os.path.join(video_dir, "*.jpg")), key=lambda x: int(os.path.basename(x).split('.')[0])) frames = [np.array(Image.open(filename)) for filename in filenames] height, width = frames[0].shape[:2] if len(frames[0].shape) == 3: is_color = True else: is_color = False frames = [frame[:, :, np.newaxis] for frame in frames] # starting tracking tracker = ECOTracker(is_color) vis = True for idx, frame in enumerate(frames): print(idx) if idx == 0: cnn_face_detector = dlib.cnn_face_detection_model_v1( "bin/mmod_human_face_detector.dat") filename = os.path.join(video_dir, "0001.jpg") img = dlib.load_rgb_image(filename) dets = cnn_face_detector(img, 1) bbox = [ dets[0].rect.left() + 1, dets[0].rect.top() + 1, dets[0].rect.right() - dets[0].rect.left(), dets[0].rect.bottom() - dets[0].rect.top() ] tracker.init(frame, bbox) bbox = (bbox[0] - 1, bbox[1] - 1, bbox[0] + bbox[2] - 1, bbox[1] + bbox[3] - 1) elif idx < len(frames) - 1: bbox = tracker.update(frame, True, vis) else: # last frame bbox = tracker.update(frame, False, vis) # bbox xmin ymin xmax ymax frame = frame.squeeze() if len(frame.shape) == 3: frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) else: frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) frame = cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 255, 255), 1) frame = frame.squeeze() if vis and idx > 0: score = tracker.score size = tuple(tracker.crop_size.astype(np.int32)) score = cv2.resize(score, size) score -= score.min() score /= score.max() score = (score * 255).astype(np.uint8) # score = 255 - score score = cv2.applyColorMap(score, cv2.COLORMAP_JET) pos = tracker._pos pos = (int(pos[0]), int(pos[1])) xmin = pos[1] - size[1] // 2 xmax = pos[1] + size[1] // 2 + size[1] % 2 ymin = pos[0] - size[0] // 2 ymax = pos[0] + size[0] // 2 + size[0] % 2 left = abs(xmin) if xmin < 0 else 0 xmin = 0 if xmin < 0 else xmin right = width - xmax xmax = width if right < 0 else xmax right = size[1] + right if right < 0 else size[1] top = abs(ymin) if ymin < 0 else 0 ymin = 0 if ymin < 0 else ymin down = height - ymax ymax = height if down < 0 else ymax down = size[0] + down if down < 0 else size[0] score = score[top:down, left:right] crop_img = frame[ymin:ymax, xmin:xmax] score_map = cv2.addWeighted(crop_img, 0.6, score, 0.4, 0) frame[ymin:ymax, xmin:xmax] = score_map saveFileName = "new%04d" % idx cv2.imwrite(saveFileName + ".jpg", frame)
def main(video_dir): # load videos filenames = sorted(glob.glob(os.path.join(video_dir, "img/*.jpg")), key=lambda x: int(os.path.basename(x).split('.')[0])) # frames = [cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB) for filename in filenames] frames = [np.array(Image.open(filename)) for filename in filenames] height, width = frames[0].shape[:2] if len(frames[0].shape) == 3: is_color = True else: is_color = False frames = [frame[:, :, np.newaxis] for frame in frames] gt_bboxes = pd.read_csv(os.path.join(video_dir, "groundtruth_rect.txt"), sep='\t|,| ', header=None, names=['xmin', 'ymin', 'width', 'height'], engine='python') title = video_dir.split('/')[-1] # fourcc = cv2.VideoWriter_fourcc(*'XVID') # img_writer = cv2.VideoWriter(os.path.join('./videos', title+'.avi'), # fourcc, 25, (width, height)) # starting tracking tracker = ECOTracker(is_color) vis = True for idx, frame in enumerate(frames): if idx == 0: bbox = gt_bboxes.iloc[0].values tracker.init(frame, bbox) bbox = (bbox[0] - 1, bbox[1] - 1, bbox[0] + bbox[2] - 1, bbox[1] + bbox[3] - 1) elif idx < len(frames) - 1: bbox = tracker.update(frame, True, vis) else: # last frame bbox = tracker.update(frame, False, vis) # bbox xmin ymin xmax ymax frame = frame.squeeze() if len(frame.shape) == 3: frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) else: frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) frame = cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 255, 255), 1) gt_bbox = gt_bboxes.iloc[idx].values gt_bbox = (gt_bbox[0], gt_bbox[1], gt_bbox[0] + gt_bbox[2], gt_bbox[1] + gt_bbox[3]) frame = frame.squeeze() frame = cv2.rectangle( frame, (int(gt_bbox[0] - 1), int(gt_bbox[1] - 1)), # 0-index (int(gt_bbox[2] - 1), int(gt_bbox[3] - 1)), (0, 255, 0), 1) if vis and idx > 0: score = tracker.score size = tuple(tracker.crop_size.astype(np.int32)) score = cv2.resize(score, size) score -= score.min() score /= score.max() score = (score * 255).astype(np.uint8) # score = 255 - score score = cv2.applyColorMap(score, cv2.COLORMAP_JET) pos = tracker._pos pos = (int(pos[0]), int(pos[1])) xmin = pos[1] - size[1] // 2 xmax = pos[1] + size[1] // 2 + size[1] % 2 ymin = pos[0] - size[0] // 2 ymax = pos[0] + size[0] // 2 + size[0] % 2 left = abs(xmin) if xmin < 0 else 0 xmin = 0 if xmin < 0 else xmin right = width - xmax xmax = width if right < 0 else xmax right = size[1] + right if right < 0 else size[1] top = abs(ymin) if ymin < 0 else 0 ymin = 0 if ymin < 0 else ymin down = height - ymax ymax = height if down < 0 else ymax down = size[0] + down if down < 0 else size[0] score = score[top:down, left:right] crop_img = frame[ymin:ymax, xmin:xmax] # if crop_img.shape != score.shape: # print(left, right, top, down) # print(xmin, ymin, xmax, ymax) score_map = cv2.addWeighted(crop_img, 0.6, score, 0.4, 0) frame[ymin:ymax, xmin:xmax] = score_map frame = cv2.putText(frame, str(idx), (5, 20), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1) # img_writer.write(frame) cv2.imshow(title, frame) cv2.waitKey(1)
def main(): logger = logger_init() # load videos camUse = False bbox = [] if camUse: video = cv2.VideoCapture(0) else: video = cv2.VideoCapture("/home/wnj/projects/videos/people.mp4") ok, frame = video.read() height, width = frame.shape[:2] center = width if len(frame.shape) == 3: is_color = True else: is_color = False frame = frame[:, :, np.newaxis] # starting tracking tracker = ECOTracker(is_color) # while (True): if bbox: # initialize the tracker with frame[0] and bbox tracker.init(frame, bbox) bbox = (bbox[0] - 1, bbox[1] - 1, bbox[0] + bbox[2] - 1, bbox[1] + bbox[3] - 1) cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), thickness=2) break else: pick = detector.detector(frame, None, 0, logger) # check the first frame, if no object, do the next frame for (x, y, w, h) in pick: if abs(x + w / 2 - width / 2) + abs(y + h / 2 - height / 2) < center: center = abs(x + w / 2 - width / 2) + abs(y + h / 2 - height / 2) bbox = (x, y, w, h) title = "ECO" vis = True idx = 0 while True: # 读新的帧 ok, frame = video.read() if not ok: break idx += 1 # start timer timer = cv2.getTickCount() # 用后续帧更新tracker bbox = tracker.track(frame, True, vis) # bbox xmin ymin xmax ymax frame = frame.squeeze() if len(frame.shape) == 3: frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) else: frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) frame = cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 255, 255), 1) if vis: score = tracker.score size = tuple(tracker.crop_size.astype(np.int32)) score = cv2.resize(score, size) score -= score.min() score /= score.max() score = (score * 255).astype(np.uint8) # score = 255 - score score = cv2.applyColorMap(score, cv2.COLORMAP_JET) pos = tracker._pos pos = (int(pos[0]), int(pos[1])) xmin = pos[1] - size[1] // 2 xmax = pos[1] + size[1] // 2 + size[1] % 2 ymin = pos[0] - size[0] // 2 ymax = pos[0] + size[0] // 2 + size[0] % 2 left = abs(xmin) if xmin < 0 else 0 xmin = 0 if xmin < 0 else xmin right = width - xmax xmax = width if right < 0 else xmax right = size[1] + right if right < 0 else size[1] top = abs(ymin) if ymin < 0 else 0 ymin = 0 if ymin < 0 else ymin down = height - ymax ymax = height if down < 0 else ymax down = size[0] + down if down < 0 else size[0] score = score[top:down, left:right] crop_img = frame[ymin:ymax, xmin:xmax] # if crop_img.shape != score.shape: # print(xmin, ymin, xmax, ymax) score_map = cv2.addWeighted(crop_img, 0.6, score, 0.4, 0) frame[ymin:ymax, xmin:xmax] = score_map # counting FPS fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # print idx frame = cv2.putText(frame, str(idx), (5, 20), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1) # print("FPS:", str(int(fps))) cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # img_writer.write(frame) cv2.imshow(title, frame) k = cv2.waitKey(1)&0xff if k == 27: break if k == ord(' '): cv2.waitKey(0) tracker.quit()
std=[0.229, 0.224, 0.225], inplace=False) ]) t = transforms.Resize(size=(256, 256)) predictor = dlib.shape_predictor( "../shape_predictor_68_face_landmarks.dat") detector = dlib.get_frontal_face_detector() fvs = FileVideoStream(file_path).start() stabilizer = VidStab() prev_anchor = None prev_ROTATION = None MOTION_DIFF = 0 frame_count = 0 refreshed = False # ROTATION_ANGLE = 75 tracker = ECOTracker(True) prevTrackPts = [] nextTrackPts = [] last_object = [] kalman_points = [] predict_points = [] for i in range(68): predict_points.append((0.0, 0.0)) last_object.append((0.0, 0.0)) kalman_points.append((0.0, 0.0)) prevTrackPts.append((0.0, 0.0)) nextTrackPts.append((0.0, 0.0)) scaling = 0.5 flag = -1
def trackeco(video_dir, firstbbox, resultpath): # # 读入连续帧 # # filenames为图片名 filenames = sorted(glob.glob(os.path.join(video_dir, "*.jpg")), key=lambda x: int(os.path.basename(x).split('.')[0].split('_')[-1])) # frames包含连续帧数据 # frames = [cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB) for filename in filenames] frames = [np.array(Image.open(filename)) for filename in filenames] height, width = frames[0].shape[:2] if len(frames[0].shape) == 3: is_color = True else: is_color = False frames = [frame[:, :, np.newaxis] for frame in frames] #读取bbox bbox = firstbbox #输出文件,此处需要修改 title = video_dir.split('/')[-1] txtname = os.path.join(resultpath, 'ecoresult.txt') print(txtname) # fourcc = cv2.VideoWriter_fourcc(*'XVID') # img_writer = cv2.VideoWriter(os.path.join('./videos', title+'.avi'), # fourcc, 25, (width, height)) # 开始跟踪 tracker = ECOTracker(is_color) vis = True for idx, frame in enumerate(frames): if idx == 0: #bbox = gt_bboxes.iloc[0].values tracker.init(frame, bbox) bbox = (bbox[0]-1, bbox[1]-1, bbox[0]+bbox[2]-1, bbox[1]+bbox[3]-1) with open(txtname,'w') as f: content = str(np.round(bbox).astype(np.int16)).strip('[').strip(']') f.write(content) f.write('\n') f.close() start_time = time.time() elif idx < len(frames) - 1: bbox = tracker.update(frame, True, vis) with open(txtname,'a') as f: content = str(np.round(bbox).astype(np.int16)).strip('[').strip(']') f.write(content) f.write('\n') f.close() else: # last frame bbox = tracker.update(frame, False, vis) with open(txtname,'a') as f: content = str(np.round(bbox).astype(np.int16)).strip('[').strip(']') f.write(content) f.write('\n') f.close() end_time = time.time() # bbox xmin ymin xmax ymax frame = frame.squeeze() if len(frame.shape) == 3: frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) else: frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) frame = cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 255, 255), 1) #gt_bbox = gt_bboxes.iloc[idx].values #gt_bbox = (gt_bbox[0], gt_bbox[1], # gt_bbox[0]+gt_bbox[2], gt_bbox[1]+gt_bbox[3]) #frame = frame.squeeze() #frame = cv2.rectangle(frame, # (int(gt_bbox[0]-1), int(gt_bbox[1]-1)), # 0-index # (int(gt_bbox[2]-1), int(gt_bbox[3]-1)), # (0, 255, 0), # 1) if vis and idx > 0: score = tracker.score size = tuple(tracker.crop_size.astype(np.int32)) score = cv2.resize(score, size) score -= score.min() score /= score.max() score = (score * 255).astype(np.uint8) # score = 255 - score score = cv2.applyColorMap(score, cv2.COLORMAP_JET) pos = tracker._pos pos = (int(pos[0]), int(pos[1])) xmin = pos[1] - size[1]//2 xmax = pos[1] + size[1]//2 + size[1] % 2 ymin = pos[0] - size[0] // 2 ymax = pos[0] + size[0] // 2 + size[0] % 2 left = abs(xmin) if xmin < 0 else 0 xmin = 0 if xmin < 0 else xmin right = width - xmax xmax = width if right < 0 else xmax right = size[1] + right if right < 0 else size[1] top = abs(ymin) if ymin < 0 else 0 ymin = 0 if ymin < 0 else ymin down = height - ymax ymax = height if down < 0 else ymax down = size[0] + down if down < 0 else size[0] score = score[top:down, left:right] crop_img = frame[ymin:ymax, xmin:xmax] # if crop_img.shape != score.shape: # print(left, right, top, down) # print(xmin, ymin, xmax, ymax) score_map = cv2.addWeighted(crop_img, 0.6, score, 0.4, 0) frame[ymin:ymax, xmin:xmax] = score_map frame = cv2.putText(frame, str(idx), (5, 20), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1) # img_writer.write(frame) # cv2.imshow(title, frame) #jpgpath = os.path.join(resultpath, 'pic') #if not os.path.exists(jpgpath): # os.makedirs(jpgpath) #cv2.imwrite(jpgpath + str(idx) + '.jpg', frame) #cv2.imwrite(os.path.join(jpgpath, str(idx)+'.jpg'), frame) #cv2.waitKey(1) return (idx, end_time - start_time)
def main(video_dir): # 读入连续帧 # filenames为图片名 filenames = sorted(glob.glob(os.path.join(video_dir, "img/*.jpg")), key=lambda x: int(os.path.basename(x).split('.')[0])) # frames包含连续帧数据 frames = [np.array(Image.open(filename)) for filename in filenames] height, width = frames[0].shape[:2] if len(frames[0].shape) == 3: is_color = True else: is_color = False frames = [frame[:, :, np.newaxis] for frame in frames] gt_bboxes = pd.read_csv(os.path.join(video_dir, "groundtruth_rect.txt"), sep='\t|,| ', header=None, names=['xmin', 'ymin', 'width', 'height'], engine='python') print(video_dir) title = video_dir.split('/')[-1] txtname = './results/' + video_dir[10:] + '.txt' print(video_dir[10:-1]) print(txtname) # 完成读取工作,开始跟踪 tracker = ECOTracker(is_color) vis = True for idx, frame in enumerate(frames): if idx == 0: bbox = gt_bboxes.iloc[0].values tracker.init(frame, bbox) bbox = (bbox[0]-1, bbox[1]-1, bbox[0]+bbox[2]-1, bbox[1]+bbox[3]-1) with open(txtname,'w') as f: content = str(np.round(bbox).astype(np.int16)).strip('[').strip(']') f.write(content) f.write('\n') f.close() start_time = time.time() elif idx < len(frames) - 1: bbox = tracker.update(frame, True, vis) with open(txtname,'a') as f: content = str(np.round(bbox).astype(np.int16)).strip('[').strip(']') f.write(content) f.write('\n') f.close() else: # 最后一帧 bbox = tracker.update(frame, False, vis) with open(txtname,'a') as f: content = str(np.round(bbox).astype(np.int16)).strip('[').strip(']') f.write(content) f.write('\n') f.close() end_time = time.time() # bbox xmin ymin xmax ymax frame = frame.squeeze() if len(frame.shape) == 3: frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) else: frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) frame = cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 255, 255), 1) gt_bbox = gt_bboxes.iloc[idx].values gt_bbox = (gt_bbox[0], gt_bbox[1], gt_bbox[0]+gt_bbox[2], gt_bbox[1]+gt_bbox[3]) frame = frame.squeeze() frame = cv2.rectangle(frame, (int(gt_bbox[0]-1), int(gt_bbox[1]-1)), # 0-index (int(gt_bbox[2]-1), int(gt_bbox[3]-1)), (0, 255, 0), 1) if vis and idx > 0: score = tracker.score size = tuple(tracker.crop_size.astype(np.int32)) score = cv2.resize(score, size) score -= score.min() score /= score.max() score = (score * 255).astype(np.uint8) score = cv2.applyColorMap(score, cv2.COLORMAP_JET) pos = tracker._pos pos = (int(pos[0]), int(pos[1])) xmin = pos[1] - size[1]//2 xmax = pos[1] + size[1]//2 + size[1] % 2 ymin = pos[0] - size[0] // 2 ymax = pos[0] + size[0] // 2 + size[0] % 2 left = abs(xmin) if xmin < 0 else 0 xmin = 0 if xmin < 0 else xmin right = width - xmax xmax = width if right < 0 else xmax right = size[1] + right if right < 0 else size[1] top = abs(ymin) if ymin < 0 else 0 ymin = 0 if ymin < 0 else ymin down = height - ymax ymax = height if down < 0 else ymax down = size[0] + down if down < 0 else size[0] score = score[top:down, left:right] crop_img = frame[ymin:ymax, xmin:xmax] score_map = cv2.addWeighted(crop_img, 0.6, score, 0.4, 0) frame[ymin:ymax, xmin:xmax] = score_map frame = cv2.putText(frame, str(idx), (5, 20), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1) cv2.imshow(title, frame) cv2.waitKey(1) FPS = (idx + 1) / (end_time - start_time)
class guiApp(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) self.init_data() self.init_ui() def init_ui(self): self.setWindowTitle('sword v.1') self.lineEdit_video_file_path.editingFinished.connect( self.lineedit_video_file_path_editing_finished) self.lineEdit_video_file_path.setText( 'Corsair_LWIR_20.02.19_15.02.11.avi') self.lineedit_video_file_path_editing_finished() self.lineEdit_descriptor.editingFinished.connect( self.lineedit_descriptor_editing_finished) self.lineEdit_descriptor.setText('fern_data_28_01_2019.mat') self.lineedit_descriptor_editing_finished() self.lineEdit_descriptor.setFocus() self.pushButton_select_path_video.clicked.connect( self.pushButton_select_path_video_clicked) self.checkBox_camera.toggled.connect(self.checkBox_camera_toggled) self.checkBox_fern.toggled.connect(self.checkBox_fern_toggled) self.checkBox_orb.toggled.connect(self.checkBox_orb_toggled) self.checkBox_clust.toggled.connect(self.checkBox_clust_toggled) self.checkBox_harris_corn.toggled.connect( self.checkBox_harris_corn_toggled) self.checkBox_track_orb.toggled.connect( self.checkBox_track_orb_toggled) self.checkBox_Hungarian.toggled.connect( self.checkBox_Hungarian_toggled) self.checkBox_hung_orb.toggled.connect(self.checkBox_hung_orb_toggled) self.checkBox_orb.setChecked(True) self.checkBox_clust.setChecked(True) self.checkBox_eco.toggled.connect(self.checkBox_eco_toggled) self.lineEdit_max_frames_to_skip.editingFinished.connect( self.lineEdit_max_frames_to_skip_editing_finished) self.lineEdit_dist_tresh.editingFinished.connect( self.lineEdit_dist_tresh_editing_finished) self.lineEdit_nkeypoints.editingFinished.connect( self.lineEdit_nkeypoints_editing_finished) self.lineEdit_orb_area.editingFinished.connect( self.lineEdit_orb_area_editing_finished) self.lineEdit_proc_time.editingFinished.connect( self.lineEdit_proc_time_editing_finished) self.lineEdit_brief_size.editingFinished.connect( self.lineEdit_brief_size_editing_finished) self.lineEdit_max_frames_to_skip.setText(str(self.max_frames_to_skip)) self.lineEdit_dist_tresh.setText(str(self.dist_thresh)) self.lineEdit_orb_area.setText(str(self.size_orb_area)) self.lineEdit_nkeypoints.setText(str(self.nkeypoints)) self.lineEdit_proc_time.setText(str(self.proc_time)) self.lineEdit_brief_size.setText(str(self.brief_size)) self.horizontalSlider_video.sliderMoved.connect( self.horizontalSlider_video_moved) self.label_cur_frame.setText('00:00') pass def init_data(self): self.src_video_path = None self.video_len = None self.cur_frame_id = 0 self.tmr = QTimer() self.time = QTime() self.proc_time = 40 self.tmr.setInterval(self.proc_time) self.tmr.timeout.connect(self.timeout_handler) self.tmr.start() self.time.start() self.fps_period = None self.last_timestamp = self.time.elapsed() self.video_capturer = None self.frame = None self.prev_frame = None self.frame_draw = None self.frame_crop = None self.proc_qimage = None self.proc_qpixmap = None self.amount_of_frames = None self.feat_extr = Feature_Extractor() self.bay_cl = BayesClassifier() self.proceed_frame = Proceed_Frame() self.tracker = Tracker(4, 15, 30, 31) self.orb = orb() self.dist_thresh = 4 self.max_frames_to_skip = 15 self.size_orb_area = 30 self.left_corner = None self.right_corner = None self.left_corner_real = None self.right_corner_real = None self.kp_select = None self.nkeypoints = 300 self.brief_size = 31 self.begin = QtCore.QPoint() self.end = QtCore.QPoint() self.width = None self.height = None self.enable_playing = False self.fern_det = False self.orb_det = False self.harris_det = False self.enable_clust = False self.enable_tracking_orb = False self.enable_tracking_Hungarian = False self.enable_tracking_hung_orb = False self.enable_eco = False self.camera_id = 0 self.camera_capturer = cv2.VideoCapture(self.camera_id) self.camera_frame = None self.video_frame = None self.video_src = 'video' self.clust_kps = [] self.en_roi = False self.bbox_eco = () self.bbox_init_eco = () self.tracker_eco = ECOTracker(True) pass def lineedit_video_file_path_editing_finished(self): if os.path.isfile(self.lineEdit_video_file_path.text()): self.src_video_path = os.path.basename( self.lineEdit_video_file_path.text()) self.video_capturer = cv2.VideoCapture( self.lineEdit_video_file_path.text()) self.video_capturer.set(cv2.CAP_PROP_POS_FRAMES, self.cur_frame_id) self.amount_of_frames = self.video_capturer.get( cv2.CAP_PROP_FRAME_COUNT) self.enable_playing = False self.refresh_image_tracker() else: print('Error: path to video file is invalid!') pass def horizontalSlider_video_moved(self): self.cur_frame_id = int(self.amount_of_frames * self.horizontalSlider_video.sliderPosition() / 100) self.video_capturer.set(cv2.CAP_PROP_POS_FRAMES, self.cur_frame_id) self.refresh_image_tracker() self.horizontalSlider_time(self.cur_frame_id) pass def horizontalSlider_time(self, cur_frame_id): if cur_frame_id > 1500: minutes = int(cur_frame_id // 1500) seconds = int((cur_frame_id % 1500) // 25) else: minutes = 0 seconds = int(cur_frame_id // 25) str_seconds = str(seconds) str_minutes = str(minutes) if seconds < 10: str_seconds = '0' + str(seconds) if minutes < 10: str_minutes = '0' + str(minutes) self.label_cur_frame.setText(str_minutes + ':' + str_seconds) pass def lineEdit_max_frames_to_skip_editing_finished(self): self.max_frames_to_skip = int(self.lineEdit_max_frames_to_skip.text()) self.tracker = Tracker(self.dist_thresh, self.max_frames_to_skip, self.size_orb_area, self.brief_size) pass def lineEdit_dist_tresh_editing_finished(self): self.dist_thresh = int(self.lineEdit_dist_tresh.text()) self.tracker = Tracker(self.dist_thresh, self.max_frames_to_skip, self.size_orb_area, self.brief_size) pass def lineEdit_brief_size_editing_finished(self): self.brief_size = int(self.lineEdit_brief_size.text()) self.tracker = Tracker(self.dist_thresh, self.max_frames_to_skip, self.size_orb_area, self.brief_size) pass def lineEdit_nkeypoints_editing_finished(self): self.nkeypoints = int(self.lineEdit_nkeypoints.text()) pass def lineEdit_orb_area_editing_finished(self): self.size_orb_area = int(self.lineEdit_orb_area.text()) self.tracker = Tracker(self.dist_thresh, self.max_frames_to_skip, self.size_orb_area, self.brief_size) pass def lineEdit_proc_time_editing_finished(self): self.proc_time = int(self.lineEdit_proc_time.text()) self.tracker = Tracker(self.dist_thresh, self.max_frames_to_skip, self.size_orb_area, self.brief_size) pass def pushButton_select_path_video_clicked(self): fname = QFileDialog.getOpenFileName(self, 'Select video file', '')[0] if fname is not None: self.src_video_path = os.path.basename(fname) self.lineEdit_video_file_path.setText(fname) self.lineedit_video_file_path_editing_finished() pass def lineedit_descriptor_editing_finished(self): if os.path.isfile(self.lineEdit_descriptor.text()): self.feature = self.lineEdit_descriptor.text() mat = scipy.io.loadmat(self.feature) self.features = mat['features']['x'][0][0] self.weights = mat['WEIGHTS'] self.feat_extr.config_features(self.features) self.bay_cl.config_weights(self.weights) else: print('Error: path to mat file is invalid!') def refresh_image_tracker(self): if self.video_src == 'camera': self.refresh_camera_frame() self.frame = self.camera_frame if self.video_src == 'video': self.refresh_video_frame() self.frame = self.video_frame if self.frame is not None: a = self.frame.shape[1] / 720 b = self.frame.shape[0] / 400 self.roi_rect = () if self.left_corner is not None: self.en_roi = True self.begin = QtCore.QPoint() self.end = QtCore.QPoint() self.roi_rect = (self.left_corner_real[0], self.left_corner_real[1], self.right_corner_real[0] - self.left_corner_real[0], self.right_corner_real[1] - self.left_corner_real[1]) if self.bbox_init_eco != self.roi_rect: self.bbox_eco = () if self.enable_playing: self.frame_draw = self.frame if self.fern_det: self.frame_draw = self.proceed_frame.scan_window( self.frame_draw, self.features, self.weights) elif self.harris_det or self.orb_det or self.enable_tracking_orb or self.enable_tracking_Hungarian or self.enable_tracking_hung_orb: self.tracker.Update(self.frame_draw, self.nkeypoints, self.harris_det, self.enable_clust, self.enable_tracking_Hungarian, self.enable_tracking_orb, self.enable_tracking_hung_orb, self.en_roi, self.roi_rect) self.frame_draw = self.tracker.draw_tracks( self.frame_draw, self.en_roi, self.roi_rect) elif self.enable_eco and len(self.roi_rect) != 0: if len(self.frame_draw.shape) == 3: is_color = True else: is_color = False self.frame_draw = self.frame_draw[:, :, np.newaxis] vis = True if len(self.bbox_eco) == 0: self.bbox_eco = self.roi_rect self.bbox_init_eco = self.roi_rect self.tracker_eco.init(self.frame_draw, self.bbox_eco) self.bbox_eco = (self.bbox_eco[0] - 1, self.bbox_eco[1] - 1, self.bbox_eco[0] + self.bbox_eco[2] - 1, self.bbox_eco[1] + self.bbox_eco[3] - 1) elif self.cur_frame_id < int(self.amount_of_frames) - 1: self.bbox_eco = self.tracker_eco.update( self.frame_draw, True, vis) else: self.bbox_eco = self.tracker_eco.update( self.frame_draw, False, vis) frame = self.frame_draw.squeeze() self.frame_draw = cv2.rectangle( frame, (int(self.bbox_eco[0]), int(self.bbox_eco[1])), (int(self.bbox_eco[2]), int(self.bbox_eco[3])), (0, 0, 255), 1) self.proc_qpixmap = self.proceed_frame.qpixmap_from_arr( self.frame_draw) self.label_video.setPixmap(self.proc_qpixmap) if not self.enable_playing: self.frame_draw = self.frame if self.harris_det or self.enable_tracking_orb or self.enable_tracking_Hungarian or self.enable_tracking_hung_orb: if self.orb: (self.frame_draw, self.clust_kps) = self.orb.orb_desc( self.frame_draw, self.enable_clust, self.nkeypoints, self.brief_size, self.en_roi, self.roi_rect) elif self.harris_det: self.frame_draw = self.orb.harris_corner_det( self.frame_draw, self.nkeypoints, self.size_orb_area) self.proc_qpixmap = self.proceed_frame.qpixmap_from_arr( self.frame_draw) self.label_video.setPixmap(self.proc_qpixmap) pass def timeout_handler(self): if self.enable_playing: self.cur_frame_id += 1 self.refresh_image_tracker() if self.frame is not None: self.label_frame_id.setText(str(self.cur_frame_id)) self.horizontalSlider_video.setSliderPosition( self.cur_frame_id * 100 // self.amount_of_frames) self.horizontalSlider_time(self.cur_frame_id) cur_time = self.time.elapsed() self.fps_period = cur_time - self.last_timestamp self.last_timestamp = cur_time if self.fps_period != 0: self.label_fps.setText(str(int(1000.0 / self.fps_period))) pass def refresh_camera_frame(self): ret, self.camera_frame = self.camera_capturer.read() pass def refresh_video_frame(self): self.video_capturer.set(cv2.CAP_PROP_POS_FRAMES, self.cur_frame_id) ret, self.video_frame = self.video_capturer.read() pass def checkBox_camera_toggled(self, checked): if checked: self.video_src = 'camera' self.refresh_image_tracker() else: self.video_src = 'video' self.refresh_image_tracker() pass def checkBox_fern_toggled(self, checked): self.fern_det = checked self.checkBox_orb.setChecked(False) pass def checkBox_orb_toggled(self, checked): self.orb_det = checked self.checkBox_fern.setChecked(False) pass def checkBox_eco_toggled(self, checked): self.enable_eco = checked self.checkBox_orb.setChecked(False) self.checkBox_clust.setChecked(False) pass def checkBox_harris_corn_toggled(self, checked): self.harris_det = checked self.checkBox_orb.setChecked(False) self.checkBox_clust.setChecked(False) pass def checkBox_clust_toggled(self, checked): self.enable_clust = checked pass def checkBox_track_orb_toggled(self, checked): self.enable_tracking_orb = checked self.checkBox_Hungarian.setChecked(False) self.checkBox_hung_orb.setChecked(False) self.refresh_image_tracker() self.tracker.del_tracks() pass def checkBox_hung_orb_toggled(self, checked): self.enable_tracking_hung_orb = checked self.checkBox_Hungarian.setChecked(False) self.checkBox_track_orb.setChecked(False) self.refresh_image_tracker() self.tracker.del_tracks() pass def checkBox_Hungarian_toggled(self, checked): self.enable_tracking_Hungarian = checked self.checkBox_track_orb.setChecked(False) self.checkBox_hung_orb.setChecked(False) self.refresh_image_tracker() self.tracker.del_tracks() pass def paintEvent(self, event): qp = QPainter() pen = QPen(QtCore.Qt.red, 1) width = self.end.x() - self.begin.x() height = self.end.y() - self.begin.y() if self.frame is not None: qp.begin(self.label_video.pixmap()) qp.setPen(pen) qp.drawRect(self.begin.x() - 20, self.begin.y() - 20, width, height) qp.end() def mousePressEvent(self, event): self.begin = event.pos() self.end = event.pos() self.update() pass def mouseMoveEvent(self, event): self.end = event.pos() self.update() if self.frame is not None: a = self.frame.shape[1] / 720 b = self.frame.shape[0] / 400 if 20 < self.begin.x() < 740 and 20 < self.end.x() < 740 and 20 < self.begin.y() < 420 and 20 < self.end.y() < \ 420 and self.end.x() != self.begin.x() and self.end.y() != self.begin.y() and self.enable_playing == False: self.left_corner = (self.begin.x() - 20, self.begin.y() - 20) self.right_corner = (self.end.x() - 20, self.end.y() - 20) self.left_corner_real = (int(a * (self.begin.x() - 20)), int(b * (self.begin.y() - 20))) self.right_corner_real = (int(a * (self.end.x() - 20)), int(b * (self.end.y() - 20))) self.en_roi = True self.roi_rect = (self.left_corner_real[0], self.left_corner_real[1], self.right_corner_real[0] - self.left_corner_real[0], self.right_corner_real[1] - self.left_corner_real[1]) self.proc_qpixmap = self.proceed_frame.qpixmap_from_arr_mouse_move( self.frame, self.left_corner, self.right_corner) self.label_video.setPixmap(self.proc_qpixmap) def mouseReleaseEvent(self, event): self.end = event.pos() self.update() if self.frame is not None: a = self.frame.shape[1] / 720 b = self.frame.shape[0] / 400 self.kp_select = (int(a * (self.end.x() - 20)), int(b * (self.end.y() - 20))) if self.kp_select is not None: if self.enable_tracking_Hungarian or self.enable_tracking_orb or self.enable_tracking_hung_orb: (self.frame, self.clust_kps) = self.orb.orb_desc( self.frame, self.enable_clust, self.nkeypoints, self.brief_size, self.en_roi, self.roi_rect) if self.en_roi: self.kp_select = (int(a * (self.end.x() - 20)) - self.roi_rect[0], int(b * (self.end.y() - 20)) - self.roi_rect[1]) if self.kp_select[0] > 0 and self.kp_select[1] > 0: self.tracker.init_track(self.kp_select, self.clust_kps) self.checkBox_orb.setChecked(False) pass def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Return: self.enable_playing = not self.enable_playing if event.key() == QtCore.Qt.Key_F12: self.left_corner_real = (0, 0) self.right_corner_real = (self.video_frame.shape[1], self.video_frame.shape[0]) pass