def read_flow(flow_or_path, quantize=False, *args, **kwargs): """Read an optical flow map Args: flow_or_path(ndarray or str): either a flow map or path of a flow quantize(bool): whether to read quantized pair, if set to True, remaining args will be passed to :func:`dequantize_flow` Returns: ndarray: optical flow """ if isinstance(flow_or_path, np.ndarray): if (flow_or_path.ndim != 3) or (flow_or_path.shape[-1] != 2): raise ValueError( 'Invalid flow with shape {}'.format(flow_or_path.shape)) return flow_or_path elif not isinstance(flow_or_path, str): raise TypeError( '"flow_or_path" must be a filename or numpy array, not {}'.format( type(flow_or_path))) if not quantize: with open(flow_or_path, 'rb') as f: try: header = f.read(4).decode('utf-8') except: raise IOError('Invalid flow file: {}'.format(flow_or_path)) else: if header != 'PIEH': raise IOError( 'Invalid flow file: {}, header does not contain PIEH'. format(flow_or_path)) w = np.fromfile(f, np.int32, 1).squeeze() h = np.fromfile(f, np.int32, 1).squeeze() flow = np.fromfile(f, np.float32, w * h * 2).reshape((h, w, 2)) else: from cvbase.image import read_img from cvbase.opencv import IMREAD_UNCHANGED dx_filename, dy_filename = _pair_name(flow_or_path) dx = read_img(dx_filename, flag=IMREAD_UNCHANGED) dy = read_img(dy_filename, flag=IMREAD_UNCHANGED) flow = dequantize_flow(dx, dy, *args, **kwargs) return flow.astype(np.float32)
def show_img(img, win_name='', wait_time=0): """Show an image Args: img(str or ndarray): the image to be shown win_name(str): the window name wait_time(int): value of waitKey param """ cv2.imshow(win_name, read_img(img)) cv2.waitKey(wait_time)
def draw_bboxes_with_label(img, bboxes, labels, top_k=0, bbox_color=Color.green, text_color=Color.green, thickness=1, font_scale=0.5, show=True, win_name='', wait_time=0, out_file=None): # yapf: disable """Draw bboxes with label text in image Args: img(str or ndarray): the image to be shown bboxes(list or ndarray): a list of ndarray of shape (k, 4) labels(str or list): label name file or list of label names top_k(int): draw top_k bboxes only if positive bbox_color(Color or tuple): color to draw bboxes text_color(Color or tuple): color to draw label texts thickness(int): thickness of bbox lines font_scale(float): font scales show(bool): whether to show the image win_name(str): the window name wait_time(int): value of waitKey param out_file(str or None): the filename to write the image """ img = read_img(img) label_names = read_labels(labels) if isinstance(bbox_color, Color): bbox_color = bbox_color.value if isinstance(text_color, Color): text_color = text_color.value assert len(bboxes) == len(label_names) for i, _bboxes in enumerate(bboxes): bboxes_int = _bboxes[:, :4].astype(np.int32) if top_k <= 0: _top_k = _bboxes.shape[0] else: _top_k = min(top_k, _bboxes.shape[0]) for j in range(_top_k): left_top = (bboxes_int[j, 0], bboxes_int[j, 1]) right_bottom = (bboxes_int[j, 2], bboxes_int[j, 3]) cv2.rectangle(img, left_top, right_bottom, bbox_color, thickness=thickness) if _bboxes.shape[1] > 4: label_text = '{}|{:.02f}'.format(label_names[i], _bboxes[j, 4]) else: label_text = label_names[i] cv2.putText(img, label_text, (bboxes_int[j, 0], bboxes_int[j, 1] - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color) if show: show_img(img, win_name, wait_time) if out_file is not None: write_img(img, out_file)
def test_resize_keep_ar(self): # resize (400, 300) to (max_1000, max_600) resized_img = resize_keep_ar(self.img_path, 1000, 600) assert resized_img.shape == (600, 800, 3) resized_img, scale = resize_keep_ar(self.img_path, 1000, 600, True) assert resized_img.shape == (600, 800, 3) and scale == 2.0 # resize (400, 300) to (max_200, max_180) img = read_img(self.img_path) resized_img = resize_keep_ar(img, 200, 180) assert resized_img.shape == (150, 200, 3) resized_img, scale = resize_keep_ar(self.img_path, 200, 180, True) assert resized_img.shape == (150, 200, 3) and scale == 0.5 # max_long_edge cannot be less than max_short_edge with pytest.raises(ValueError): resize_keep_ar(self.img_path, 500, 600)
def test_limit_size(self): # limit to 800 resized_img = limit_size(self.img_path, 800) assert resized_img.shape == (300, 400, 3) resized_img, scale = limit_size(self.img_path, 800, True) assert resized_img.shape == (300, 400, 3) and scale == 1 # limit to 200 resized_img = limit_size(self.img_path, 200) assert resized_img.shape == (150, 200, 3) resized_img, scale = limit_size(self.img_path, 200, True) assert resized_img.shape == (150, 200, 3) and scale == 0.5 # test with img rather than img path img = read_img(self.img_path) resized_img = limit_size(img, 200) assert resized_img.shape == (150, 200, 3) resized_img, scale = limit_size(img, 200, True) assert resized_img.shape == (150, 200, 3) and scale == 0.5
def draw_bboxes(img, bboxes, colors=Color.green, top_k=0, thickness=1, show=True, win_name='', wait_time=0, out_file=None): # yapf: disable """Draw bboxes on an image Args: img(str or ndarray): the image to be shown bboxes(list or ndarray): a list of ndarray of shape (k, 4) colors(list or Color or tuple): a list of colors, corresponding to bboxes top_k(int): draw top_k bboxes only if positive thickness(int): thickness of lines show(bool): whether to show the image win_name(str): the window name wait_time(int): value of waitKey param out_file(str or None): the filename to write the image """ img = read_img(img) if isinstance(bboxes, np.ndarray): bboxes = [bboxes] if isinstance(colors, (tuple, Color)): colors = [colors for _ in range(len(bboxes))] for i in range(len(colors)): if isinstance(colors[i], Color): colors[i] = colors[i].value assert len(bboxes) == len(colors) for i, _bboxes in enumerate(bboxes): _bboxes = _bboxes.astype(np.int32) if top_k <= 0: _top_k = _bboxes.shape[0] else: _top_k = min(top_k, _bboxes.shape[0]) for j in range(_top_k): left_top = (_bboxes[j, 0], _bboxes[j, 1]) right_bottom = (_bboxes[j, 2], _bboxes[j, 3]) cv2.rectangle(img, left_top, right_bottom, colors[i], thickness=thickness) if show: show_img(img, win_name, wait_time) if out_file is not None: write_img(img, out_file)
def test_read_img(self): img = read_img(self.img_path) assert img.shape == (300, 400, 3)