def show_double_bbox(img, pred_bbox, pred_score, gt_bbox, idx, lost_number,): """ :param img: (h,w,c) :param pred_bbox: cx,cy,w,h :param gt_bbox: :param idx: the frame idx :param lost_number: the lost number in the video :return: """ pred_bbox = center2corner(pred_bbox) gt_bbox = center2corner(gt_bbox) pred_bbox = list(map(lambda x: int(x), pred_bbox)) gt_bbox = list(map(lambda x: int(x), gt_bbox)) cv2.rectangle(img, (pred_bbox[0], pred_bbox[1]), (pred_bbox[2], pred_bbox[3]), (0, 0, 255), 2) cv2.rectangle(img, (gt_bbox[0], gt_bbox[1]), (gt_bbox[2], gt_bbox[3]), (255, 255, 0), 2) cv2.putText(img, str(idx), (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) cv2.putText(img, str(lost_number), (40, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.putText(img, '{:.2f}'.format(pred_score), (40, 120), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) # cv2.namedWindow('double bbox', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # cv2.resizeWindow('double bbox', 600, 400) # cv2.imshow('double bbox', img) # cv2.waitKey(200) visual_dir='visual_imgs' if not os.path.isdir(visual_dir): os.makedirs(visual_dir) cv2.imwrite(os.path.join(visual_dir,'{}.png'.format(idx)),img)
def show_single_bbox(img, bbox): pred_bbox = center2corner(bbox) pred_bbox = list(map(lambda x: int(x), pred_bbox)) cv2.rectangle(img, (pred_bbox[0], pred_bbox[1]), (pred_bbox[2], pred_bbox[3]), (0, 0, 255), 2) cv2.namedWindow('single bbox', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) cv2.resizeWindow('single bbox', 600, 400) cv2.imshow('single bbox', img) cv2.waitKey(1)
def get_bbox(self, image, ori_bbox): img_h, img_w = image.shape[:2] w, h = ori_bbox[2] - ori_bbox[0], ori_bbox[3] - ori_bbox[1] context_amount = 0.5 wc_z = w + context_amount * (w + h) hc_z = h + context_amount * (w + h) s_z = np.sqrt(wc_z * hc_z) scale_z = cfg.TRAIN.EXAMPLER_SIZE / s_z w = w * scale_z h = h * scale_z cx, cy = img_w // 2, img_h // 2 bbox = center2corner(Center(cx, cy, w, h)) return bbox
def _get_bbox(self, image, shape): imh, imw = image.shape[:2] if len(shape) == 4: w, h = shape[2] - shape[0], shape[3] - shape[1] else: w, h = shape context_amount = 0.5 exemplar_size = cfg.TRAIN.EXEMPLAR_SIZE wc_z = w + context_amount * (w + h) hc_z = h + context_amount * (w + h) s_z = np.sqrt(wc_z * hc_z) scale_z = exemplar_size / s_z w = w * scale_z h = h * scale_z cx, cy = imw // 2, imh // 2 bbox = center2corner(Center(cx, cy, w, h)) return bbox
def get_bbox(image, ori_bbox): """ :param image: :param ori_bbox: cx,cy,w,h :return: """ img_h, img_w = image.shape[:2] w, h = ori_bbox[2], ori_bbox[3] context_amount = 0.5 wc_z = w + context_amount * (w + h) hc_z = h + context_amount * (w + h) s_z = np.sqrt(wc_z * hc_z) scale_z = cfg.TRAIN.EXAMPLER_SIZE / s_z w = w * scale_z h = h * scale_z cx, cy = img_w // 2, img_h // 2 bbox = center2corner([cx, cy, w, h]) return Corner(*bbox)
def _shift_scale_aug(self, image, bbox, crop_bbox, size): im_h, im_w = image.shape[:2] # adjust crop bounding box crop_bbox_center = corner2center(crop_bbox) if self.scale: scale_x = (1.0 + Augmentation.random() * self.scale) scale_y = (1.0 + Augmentation.random() * self.scale) h, w = crop_bbox_center.h, crop_bbox_center.w scale_x = min(scale_x, float(im_w) / w) scale_y = min(scale_y, float(im_h) / h) crop_bbox_center = Center(crop_bbox_center.x, crop_bbox_center.y, crop_bbox_center.w * scale_x, crop_bbox_center.h * scale_y) crop_bbox = center2corner(crop_bbox_center) if self.shift: sx = Augmentation.random() * self.shift sy = Augmentation.random() * self.shift x1, y1, x2, y2 = crop_bbox sx = max(-x1, min(im_w - 1 - x2, sx)) sy = max(-y1, min(im_h - 1 - y2, sy)) crop_bbox = Corner(x1 + sx, y1 + sy, x2 + sx, y2 + sy) # adjust target bounding box x1, y1 = crop_bbox.x1, crop_bbox.y1 bbox = Corner(bbox.x1 - x1, bbox.y1 - y1, bbox.x2 - x1, bbox.y2 - y1) if self.scale: bbox = Corner(bbox.x1 / scale_x, bbox.y1 / scale_y, bbox.x2 / scale_x, bbox.y2 / scale_y) image = self._crop_roi(image, crop_bbox, size) return image, bbox
def __call__(self, image, bbox, size, gray=False): shape = image.shape crop_bbox = center2corner( Center(shape[0] // 2, shape[1] // 2, size - 1, size - 1)) # gray augmentation if gray: image = self._gray_aug(image) # shift scale augmentation image, bbox = self._shift_scale_aug(image, bbox, crop_bbox, size) # color augmentation if self.color > np.random.random(): image = self._color_aug(image) # blur augmentation if self.blur > np.random.random(): image = self._blur_aug(image) # flip augmentation if self.flip and self.flip > np.random.random(): image, bbox = self._flip_aug(image, bbox) return image, bbox
def generate_all_anchors(self, im_c, size): """ im_c: image center size: image size """ if self.image_center == im_c and self.size == size: return False self.image_center = im_c self.size = size a0x = im_c - size // 2 * self.stride ori = np.array([a0x] * 4, dtype=np.float32) zero_anchors = self.anchors + ori x1 = zero_anchors[:, 0] y1 = zero_anchors[:, 1] x2 = zero_anchors[:, 2] y2 = zero_anchors[:, 3] x1, y1, x2, y2 = map(lambda x: x.reshape(self.anchor_num, 1, 1), [x1, y1, x2, y2]) cx, cy, w, h = corner2center([x1, y1, x2, y2]) disp_x = np.arange(0, size).reshape(1, 1, -1) * self.stride disp_y = np.arange(0, size).reshape(1, -1, 1) * self.stride cx = cx + disp_x cy = cy + disp_y # broadcast zero = np.zeros((self.anchor_num, size, size), dtype=np.float32) cx, cy, w, h = map(lambda x: x + zero, [cx, cy, w, h]) x1, y1, x2, y2 = center2corner([cx, cy, w, h]) self.all_anchors = (np.stack([x1, y1, x2, y2]).astype(np.float32), np.stack([cx, cy, w, h]).astype(np.float32)) return True