def process_img(i_sublst): df = pd.DataFrame(columns=[ 'ImageID', 'ImageWidth', 'ImageHeight', 'PredictionString' ]).set_index('ImageID') for i in i_sublst: cnt = 0 # for i in tqdm(range(len(results))): bb_result, segm_result = results[i] bbs = mmcv.concat_list(bb_result) scoring_flag = False if len(segm_result) == 2: ## mask scoring rcnn scoring_flag = True if scoring_flag: segms = mmcv.concat_list(segm_result[0]) probs = mmcv.concat_list(segm_result[1]) else: segms = mmcv.concat_list(segm_result) labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bb_result) ] labels = [CLASSES[i] for i in np.concatenate(labels)] assert len(segms) == len(labels) and len(segms) == len(bbs) if scoring_flag: assert len(segms) == len(probs) cnt += len(segms) if len(segms) == 0: df.loc[sub.index.values[i]] = -1, -1, np.nan else: row = "" h, w = segms[0]['size'] if scoring_flag: for proba, seg, label in zip(probs, segms, labels): prob = "{:.8f}".format( proba) if args.eight_digit else "{:.6f}".format( proba) mask = maskUtils.decode(seg).astype(np.bool) rle = encode_binary_mask(mask).decode("utf-8") row += (label + ' ' + prob + ' ' + rle + ' ') if args.expand: for parent in all_keyed_child[label]: row += (parent + ' ' + prob + ' ' + rle + ' ') # sub.loc[sub.index[i]] = w,h,row.strip(' ') else: for bb, seg, label in zip(bbs, segms, labels): prob = "{:.8f}".format( bb[4]) if args.eight_digit else "{:.6f}".format( bb[4]) mask = maskUtils.decode(seg).astype(np.bool) rle = encode_binary_mask(mask).decode("utf-8") row += (label + ' ' + prob + ' ' + rle + ' ') if args.expand: for parent in all_keyed_child[label]: row += (parent + ' ' + prob + ' ' + rle + ' ') df.loc[sub.index.values[i]] = w, h, row.strip(' ') return df
def show_result(self, data, result, dataset=None, score_thr=0.3): if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None img_tensor = data['img'][0] img_metas = data['img_meta'][0].data[0] imgs = tensor2imgs(img_tensor, **img_metas[0]['img_norm_cfg']) assert len(imgs) == len(img_metas) if dataset is None: class_names = self.CLASSES elif isinstance(dataset, str): class_names = get_classes(dataset) elif isinstance(dataset, (list, tuple)): class_names = dataset else: raise TypeError( 'dataset must be a valid dataset name or a sequence' ' of class names, not {}'.format(type(dataset))) for img, img_meta in zip(imgs, img_metas): h, w, _ = img_meta['img_shape'] img_show = img[:h, :w, :] bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: if len(segm_result) > 1: segms = mmcv.concat_list(segm_result[0]) bboxes[:, -1] = np.concatenate( segm_result[1] ) / 1.3 # rescale the mask scores to the range of [0,1] else: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img_show[mask] = img_show[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) mmcv.imshow_det_bboxes(img_show, bboxes, labels, class_names=class_names, score_thr=score_thr)
def show_result(img, result, class_names, score_thr=0.3, out_file=None): """Visualize the detection results on the image. Args: img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. class_names (list[str] or tuple[str]): A list of class names. score_thr (float): The threshold to visualize the bboxes and masks. out_file (str, optional): If specified, the visualization result will be written to the out file instead of shown in a window. """ assert isinstance(class_names, (tuple, list)) img = mmcv.imread(img) if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: colors = {k: [] for k in 'rgb'} temp = {k: np.random.randint(0, 255) for k in 'rgb'} for k in temp: while 1: c = temp[k] t = set(j for j in range(c - 25, c + 25) if 0 <= j <= 255) if t.intersection(colors[k]): temp[k] = np.random.randint(0, 255) else: break colors[k].append(temp[k]) color_mask = np.array( [colors['r'][0], colors['g'][0], colors['b'][0]]) # color_mask = np.random.randint( # 0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) thickness_t = 2 if img.shape[0] < 3000 else 6 font_scale_t = 0.65 if img.shape[0] < 3000 else 2.5 mmcv.imshow_det_bboxes(img.copy(), bboxes, labels, class_names=class_names, score_thr=score_thr, text_color='yellow', thickness=thickness_t, font_scale=font_scale_t, show=out_file is None, out_file=out_file, win_name='demo')
def recognize_img(self, image_byte=None, image_width=None, image_height=None, image_type=None): image_bgr = np.frombuffer(image_byte, dtype=np.uint8) image_bgr = image_bgr.reshape((image_height, image_width, 3)) result = inference_detector(self.model, image_bgr) assert isinstance(self.class_name, (tuple, list)) if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) res = [] if self.score_thr > 0: assert bboxes.shape[1] == 5 scores = bboxes[:, -1] inds = scores > self.score_thr bboxes = bboxes[inds, :] labels = labels[inds] for bbox, label in zip(bboxes, labels): bbox_int = bbox.astype(np.int32) left_top = (bbox_int[0], bbox_int[1]) right_bottom = (bbox_int[2], bbox_int[3]) label_text = self.class_name[ label] if self.class_name is not None else 'cls {}'.format( label) rs_dict = { 'item_name': str(label_text), 'left_upper_x': int(left_top[0]), 'left_upper_y': int(left_top[1]), 'right_down_x': int(right_bottom[0]), 'right_down_y': int(right_bottom[1]), 'probability': round(bbox[-1], 2) } res.append(rs_dict) print('res type:', type(res)) print(res) return res
def show_mask_result(img, result, score_thr): # 基于掩码,提取轮廓显示 bbox_result, segm_result = result[:2] bboxes = np.vstack(bbox_result) labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) # draw segmentation masks segms = None if segm_result is not None and len(labels) > 0: # non empty segms = mmcv.concat_list(segm_result) if isinstance(segms[0], torch.Tensor): segms = torch.stack(segms, dim=0).detach().cpu().numpy() else: segms = np.stack(segms, axis=0) scores = bboxes[:, -1] inds = scores > score_thr bboxes = bboxes[inds, :] labels = labels[inds] if segms is not None: segms = segms[inds, ...] points = [] for i in range(len(bboxes)): mask = segms[i].astype(np.uint8) contours, _ = cv2.findContours(mask[..., None], cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(img, contours, -1, (0,0,255), 2) for contour in contours: points.append(len(contour)) return img, sum(points) / len(points)
def show_result(img, result, dataset='coco', score_thr=0.3, out_file=None): img = mmcv.imread(img) class_names = get_classes(dataset) if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) mmcv.imshow_det_bboxes(img.copy(), bboxes, labels, class_names=class_names, score_thr=score_thr, show=out_file is None)
def show_result(img, result, class_names, score_thr=0.3, wait_time=0, show=True, out_file=None): """Visualize the detection results on the image. Args: img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. class_names (list[str] or tuple[str]): A list of class names. score_thr (float): The threshold to visualize the bboxes and masks. wait_time (int): Value of waitKey param. show (bool, optional): Whether to show the image with opencv or not. out_file (str, optional): If specified, the visualization result will be written to the out file instead of shown in a window. Returns: np.ndarray or None: If neither `show` nor `out_file` is specified, the visualized image is returned, otherwise None is returned. """ assert isinstance(class_names, (tuple, list)) img = mmcv.imread(img) img = img.copy() if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) mmcv.imshow_det_bboxes(img, bboxes, labels, class_names=class_names, score_thr=score_thr, bbox_color='red', text_color='red', thickness=2, font_scale=0.8, show=show, wait_time=wait_time, out_file=out_file) if not (show or out_file): return img
def toBoxlist(self, result, score_thr=0.3): if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None bboxes = np.vstack(bbox_result) inds = np.where(bboxes[:, -1] > score_thr)[0] bboxes = bboxes[inds] if segm_result is not None: segms = mmcv.concat_list(segm_result) if len(segms) == 0: return None masks = [] for i in inds: mask = maskUtils.decode(segms[i]).astype(np.uint8) masks.append(mask) bbox = torch.tensor(bboxes[:, :4]).reshape(-1, 4) im_sz = segms[0]['size'][::-1] predictions = BoxList(bbox, im_sz, "xyxy") masks = torch.tensor(masks)[:, None] predictions.add_field("mask", masks) scores = torch.tensor(bboxes[:, 4]).reshape(-1) predictions.add_field("scores", scores) assert len(bboxes) == len(masks) == len( scores), "should have the same size" return predictions
def show_result(img_path, result, score_thr): img = cv2.imread(img_path) bbox_result, segm_result = result bboxes = np.vstack(bbox_result) labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) # draw segmentation masks segms = None if segm_result is not None and len(labels) > 0: # non empty segms = mmcv.concat_list(segm_result) if isinstance(segms[0], torch.Tensor): segms = torch.stack(segms, dim=0).detach().cpu().numpy() else: segms = np.stack(segms, axis=0) scores = bboxes[:, -1] inds = scores > score_thr bboxes = bboxes[inds, :] labels = labels[inds] if segms is not None: segms = segms[inds, ...] for i, (bbox, label) in enumerate(zip(bboxes, labels)): mask = segms[i].astype(np.uint8) contours, hierarchy = cv2.findContours(mask[..., None], cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(img, contours, -1, (0, 0, 255), 2) return img
def detect_all(self, imgs, thres, save_num, save_path): result = [] for i, img in enumerate(imgs): detect_res = inference_detector(self.model, img) print(i, imgs[i]) img = mmcv.imread(imgs[i]) img = img.copy() if isinstance(detect_res, tuple): bbox_result, segm_result = detect_res else: bbox_result, segm_result = detect_res, None bboxes = np.vstack(bbox_result) labels = [np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result)] labels = np.concatenate(labels) if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > thres)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 if len(bboxes) > 0: if i < save_num: self.save_res_img(img, labels, bboxes, thres, out_file=os.path.join(save_path + os.path.basename(imgs[i]))) for j, bbox in enumerate(bboxes): if float(bbox[4]) > thres: res_dic = {'name': os.path.basename(imgs[i]), 'category': self.class_names[labels[j]], 'bbox': [round(float(x), 2) for x in bbox[:4]], 'score': float(bbox[4])} print(res_dic) result.append(res_dic) return result
def show_result(self, data, result, img_norm_cfg, frame_index, show=False, dataset=None, score_thr=0.3): if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None img_tensor = data['img'][0] img_metas = data['img_meta'][0].data[0] imgs = tensor2imgs(img_tensor, **img_norm_cfg) assert len(imgs) == len(img_metas) if dataset is None: class_names = self.CLASSES elif isinstance(dataset, str): class_names = get_classes(dataset) elif isinstance(dataset, (list, tuple)): class_names = dataset else: raise TypeError( 'dataset must be a valid dataset name or a sequence' ' of class names, not {}'.format(type(dataset))) for img, img_meta in zip(imgs, img_metas): h, w, _ = img_meta['img_shape'] img_show = img[:h, :w, :] bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img_show[mask] = img_show[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes labels = [ np.full(bbox.shape[0], i, dtype=np.int32) # category_id -1 for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) mmcv.imshow_det_bboxes(img_show, bboxes, labels, show=show, class_names=class_names, wait_time=1, out_file="output/polyp/" + str("{:04d}".format(frame_index)) + ".png", score_thr=score_thr)
def _log_predictions(self, results): table_idxs = self.data_table_ref.get_index() assert len(table_idxs) == len(self.eval_image_indexs) for ndx, eval_image_index in enumerate(self.eval_image_indexs): # Get the result result = results[eval_image_index] if isinstance(result, tuple): bbox_result, segm_result = result if isinstance(segm_result, tuple): segm_result = segm_result[0] # ms rcnn else: bbox_result, segm_result = result, None assert len(bbox_result) == len(self.class_id_to_label) # Get labels bboxes = np.vstack(bbox_result) labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) # Get segmentation mask if available. segms = None if segm_result is not None and len(labels) > 0: segms = mmcv.concat_list(segm_result) segms = mask_util.decode(segms) segms = segms.transpose(2, 0, 1) assert len(segms) == len(labels) # TODO: Panoramic segmentation visualization. # Remove bounding boxes and masks with score lower than threshold. if self.bbox_score_thr > 0: assert bboxes is not None and bboxes.shape[1] == 5 scores = bboxes[:, -1] inds = scores > self.bbox_score_thr bboxes = bboxes[inds, :] labels = labels[inds] if segms is not None: segms = segms[inds, ...] # Get dict of bounding boxes to be logged. wandb_boxes = self._get_wandb_bboxes(bboxes, labels, log_gt=False) # Get dict of masks to be logged. if segms is not None: wandb_masks = self._get_wandb_masks(segms, labels) else: wandb_masks = None # Log a row to the eval table. self.eval_table.add_data( self.data_table_ref.data[ndx][0], self.data_table_ref.data[ndx][1], self.wandb.Image( self.data_table_ref.data[ndx][1], boxes=wandb_boxes, masks=wandb_masks, classes=self.class_set))
def show_rmask(data, result, img_norm_cfg, class_names, score_thr=0.3, file_name='0.png'): bbox_result, segm_result, rbbox_result = result img_tensor = data['img'][0] img_metas = data['img_meta'][0].data[0] imgs = tensor2imgs(img_tensor, **img_norm_cfg) for img, img_meta in zip(imgs, img_metas): h, w, _ = img_meta['img_shape'] img_show = img[:h, :w, :] bboxes = np.vstack(bbox_result) rbboxes = np.vstack(rbbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img_show[mask] = img_show[mask] * 0.5 + color_mask * 0.5 # draw rbbox labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) img = imread(img_show) scores = rbboxes[:, -1] inds = scores > score_thr bboxes = bboxes[inds, :] rbboxes = rbboxes[inds, :8] labels = labels[inds] rbbox_color = (0, 0, 255) text_color = (0, 255, 0) font_scale = 0.5 ''' for rbbox, bbox, label in zip(rbboxes, bboxes, labels): bbox_int = bbox.astype(np.int32) rbbox_int = rbbox.astype(np.int32) rbbox_int = rbbox_int.reshape(4,2) cv2.drawContours(img,[rbbox_int],0,rbbox_color,2) label_text = class_names[ label] if class_names is not None else 'cls {}'.format(label) if len(bbox) > 4: label_text += '|{:.02f}'.format(bbox[-1]) cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2), cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color) ''' cv2.imwrite(file_name, img)
def show_result(self, data, result, dataset=None, score_thr=0.3): if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None img_metas = data['img_meta'][0].data[0] if 'img_raw' in data: img_tensor = data['img_raw'][0] norm_factor = {'mean': [0, 0, 0], 'std': [1, 1, 1], 'to_rgb': True} filename = os.path.join( 'results', data['img_meta'][0].data[0][0]['filename'].split('/')[-1]) else: img_tensor = data['img'][0] norm_factor = img_metas[0]['img_norm_cfg'] filename = None imgs = tensor2imgs(img_tensor, **norm_factor) assert len(imgs) == len(img_metas) if dataset is None: class_names = self.CLASSES elif isinstance(dataset, str): class_names = get_classes(dataset) elif isinstance(dataset, (list, tuple)): class_names = dataset else: raise TypeError( 'dataset must be a valid dataset name or a sequence' ' of class names, not {}'.format(type(dataset))) for img, img_meta in zip(imgs, img_metas): h, w, _ = img_meta['img_shape'] img_show = img[:h, :w, :] bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img_show[mask] = img_show[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) mmcv.imshow_det_bboxes(img_show, bboxes, labels, class_names=class_names, score_thr=score_thr, show=False, out_file=filename)
def detect( self, image_data ): input_image = image_data.asarray().astype( 'uint8' ) from mmdet.apis import inference_detector gpu_string = 'cuda:' + str( self._gpu_index ) detections = inference_detector( self._model, input_image, self._cfg, device=gpu_string ) class_names = [ 'fish' ] * 10000 if isinstance( detections, tuple ): bbox_result, segm_result = detections else: bbox_result, segm_result = detections, None if np.size( bbox_result ) > 0: bboxes = np.vstack( bbox_result ) else: bboxes = [] sys.stdout.write( "Detected " + str( len( bbox_result ) ) + " objects" ) sys.stdout.flush() # convert segmentation masks masks = [] if segm_result is not None: segms = mmcv.concat_list( segm_result ) inds = np.where( bboxes[:, -1] > score_thr )[0] for i in inds: masks.append( maskUtils.decode( segms[i] ).astype( np.bool ) ) # collect labels labels = [ np.full( bbox.shape[0], i, dtype=np.int32 ) for i, bbox in enumerate( bbox_result ) ] if np.size( labels ) > 0: labels = np.concatenate( labels ) else: labels = [] # convert to kwiver format, apply threshold output = [] for entry in []: output.append( DetectedObject( BoundingBox( 1,1,2,2 ) ) ) if np.size( labels ) > 0: mmcv.imshow_det_bboxes( input_image, bboxes, labels, class_names=class_names, score_thr=-100.0, show=True) return DetectedObjectSet( output )
def show_seg_result( img, result, # list masks[cls]->list class_names, score_thr=0.3, wait_time=0, show=True, out_file=None): """Visualize the detection results on the image. Args: img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. class_names (list[str] or tuple[str]): A list of class names. score_thr (float): The threshold to visualize the bboxes and masks. wait_time (int): Value of waitKey param. show (bool, optional): Whether to show the image with opencv or not. out_file (str, optional): If specified, the visualization result will be written to the out file instead of shown in a window. Returns: np.ndarray or None: If neither `show` nor `out_file` is specified, the visualized image is returned, otherwise None is returned. """ assert isinstance(class_names, (tuple, list)) img = mmcv.imread(img) img = img.copy() segm_result = result labels = [ np.full(len(s), i, dtype=np.int32) for i, s in enumerate(segm_result) ] labels = np.concatenate(labels) # 100 if segm_result is not None: segms = mmcv.concat_list(segm_result) segms = np.vstack(segms) inds = np.where(segms[:, -1] > score_thr)[0] np.random.seed(42) color_masks = [ np.random.randint(0, 256, (1, 3), dtype=np.uint8) for _ in range(max(labels) + 1) ] for i in inds: i = int(i) color_mask = color_masks[labels[i]] mask = mask_util.decode(segms[i][0]).astype(np.bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 cur_label = labels[i] label_text = class_names[cur_label] center_y, center_x = ndimage.measurements.center_of_mass(mask) vis_pos = (max(int(center_x) - 10, 0), int(center_y)) cv2.putText(img, label_text, vis_pos, cv2.FONT_HERSHEY_COMPLEX, 0.3, (255, 255, 255)) # green if out_file is not None: imwrite(img, out_file)
def show_result( self, data, result, img_norm_cfg, dataset="coco", score_thr=0.3, ): if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None img_tensor = data["img"][0] img_metas = data["img_meta"][0].data[0] imgs = tensor2imgs(img_tensor, **img_norm_cfg) assert len(imgs) == len(img_metas) if isinstance(dataset, str): class_names = get_classes(dataset) elif isinstance(dataset, (list, tuple)) or dataset is None: class_names = dataset else: raise TypeError( "dataset must be a valid dataset name or a sequence" " of class names, not {}".format(type(dataset)) ) for img, img_meta in zip(imgs, img_metas): h, w, _ = img_meta["img_shape"] img_show = img[:h, :w, :] bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint( 0, 256, (1, 3), dtype=np.uint8 ) mask = maskUtils.decode(segms[i]).astype(np.bool) img_show[mask] = ( img_show[mask] * 0.5 + color_mask * 0.5 ) # draw bounding boxes labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) mmcv.imshow_det_bboxes( img_show, bboxes, labels, class_names=class_names, score_thr=score_thr, )
def show_result(img, result, class_names, score_thr=0.3, wait_time=0, out_file=None): """Visualize the detection results on the image. Args: img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. class_names (list[str] or tuple[str]): A list of class names. score_thr (float): The threshold to visualize the bboxes and masks. wait_time (int): Value of waitKey param. out_file (str, optional): If specified, the visualization result will be written to the out file instead of shown in a window. """ assert isinstance(class_names, (tuple, list)) img = mmcv.imread(img) if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None # print('bbox_result:',bbox_result) bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] # print('labels:',labels[0]==0) labels = np.concatenate(labels) # mmcv.imshow_det_bboxes( (person_bboxes,object_bboxes,image)=imshow_det_bboxes( img.copy(), bboxes, labels, class_names=class_names, score_thr=score_thr, show=out_file is None, wait_time=wait_time, out_file=out_file) # print('len(person_bboxes):',len(person_bboxes)) # print('len(object_bboxes):',len(object_bboxes)) return (person_bboxes, object_bboxes, image)
def show_result(self, data, result, dataset=None, score_thr=0.3): if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None img_tensor = data['img'].data print(img_tensor.shape) img_metas = data['img_meta'].data[0] print(img_metas) imgs = tensor2imgs( img_tensor, **{ 'mean': np.array([0.5, 0.5, 0.5]), 'std': np.array([255., 255., 255.]), 'to_rgb': False }) assert len(imgs) == len(img_metas) if dataset is None: class_names = self.CLASSES elif isinstance(dataset, str): class_names = get_classes(dataset) elif isinstance(dataset, (list, tuple)): class_names = dataset else: raise TypeError( 'dataset must be a valid dataset name or a sequence' ' of class names, not {}'.format(type(dataset))) for img, img_meta in zip(imgs, img_metas): h, w, _ = img_meta['img_shape'] img_show = img[:h, :w, :] bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) print(segms) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) print('amsk shape', mask.shape) img_show[mask] = img_show[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) mmcv.imshow_det_bboxes(img_show, bboxes, labels, class_names=class_names, score_thr=score_thr)
def genereteImgResults(self, results): """结合子图的结果,映射回原图,应用nms, 生成一张整图的结果""" imgResults = defaultdict(list) for image_id, dets in results.items(): img_info = self.coco.imgs[image_id] labels = mmcv.concat_list([[j] * len(det) for j, det in dets.items()]) scores = mmcv.concat_list([det[:, 8] for det in dets.values()]) rbboxes = np.vstack( [det[:, :8] for det in dets.values() if len(det) > 0]) if 'crop' in img_info: rbboxes = self.crop_bbox_map_back(rbboxes, img_info['crop'][:2]) assert len(rbboxes) == len(labels) if len(labels) > 0: result = [rbboxes, labels, scores] imgResults[img_info['file_name']].append(result) return imgResults
def results2txt(self, results, outfile_prefix): """Dump the detection results to a txt file. Args: results (list[list | tuple | ndarray]): Testing results of the dataset. outfile_prefix (str): The filename prefix of the json files. If the prefix is "somepath/xxx", the txt files will be named "somepath/xxx.txt". Returns: list[str: str]: result txt files which contains corresponding instance segmentation images. """ try: import cityscapesscripts.helpers.labels as CSLabels except ImportError: raise ImportError('Please run "pip install citscapesscripts" to ' "install cityscapesscripts first.") result_files = [] os.makedirs(outfile_prefix, exist_ok=True) prog_bar = mmcv.ProgressBar(len(self)) for idx in range(len(self)): result = results[idx] filename = self.img_infos[idx]["filename"] basename = osp.splitext(osp.basename(filename))[0] pred_txt = osp.join(outfile_prefix, basename + "_pred.txt") bbox_result, segm_result = result bboxes = np.vstack(bbox_result) segms = mmcv.concat_list(segm_result) labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) assert len(bboxes) == len(segms) == len(labels) num_instances = len(bboxes) prog_bar.update() with open(pred_txt, "w") as fout: for i in range(num_instances): pred_class = labels[i] classes = self.CLASSES[pred_class] class_id = CSLabels.name2label[classes].id score = bboxes[i, -1] mask = maskUtils.decode(segms[i]).astype(np.uint8) png_filename = osp.join( outfile_prefix, basename + "_{}_{}.png".format(i, classes)) mmcv.imwrite(mask, png_filename) fout.write("{} {} {}\n".format(osp.basename(png_filename), class_id, score)) result_files.append(pred_txt) return result_files
def show_result_in_Chinese(img, result, class_names, score_thr=0.3, out_file=None, thickness=1, bbox_color='green', text_color='green'): assert isinstance(class_names, (tuple, list)) img = mmcv.imread(img) if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) assert bboxes.ndim == 2 assert labels.ndim == 1 assert bboxes.shape[0] == labels.shape[0] assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5 if score_thr > 0: assert bboxes.shape[1] == 5 scores = bboxes[:, -1] inds = scores > score_thr bboxes = bboxes[inds, :] labels = labels[inds] bbox_color = color_val(bbox_color) text_color = color_val(text_color) test_bboxes = nms(bboxes, 0.5) new_bboxes = [bboxes[i] for i in test_bboxes[1]] new_labels = [labels[i] for i in test_bboxes[1]] for bbox, label in zip(new_bboxes, new_labels): bbox_int = bbox.astype(np.int32) left_top = (bbox_int[0], bbox_int[1]) right_bottom = (bbox_int[2], bbox_int[3]) cv2.rectangle( img, left_top, right_bottom, bbox_color, thickness=thickness) label_text = class_names[ label] if class_names is not None else 'cls {}'.format(label) if len(bbox) > 4: label_text += '|{:.02f}'.format(bbox[-1]) img = write_text_to_image(img, label_text, (bbox_int[0], bbox_int[1] - 2), text_color) if out_file is not None: imwrite(img, out_file)
def results2outs(bbox_results=None, mask_results=None, mask_shape=None, **kwargs): """Restore the results (list of results of each category) into the results of the model forward. Args: bbox_results (list[np.ndarray]): Each list denotes bboxes of one category. mask_results (list[list[np.ndarray]]): Each outer list denotes masks of one category. Each inner list denotes one mask belonging to the category. Each mask has shape (h, w). mask_shape (tuple[int]): The shape (h, w) of mask. Returns: tuple: tracking results of each class. It may contain keys as belows: - bboxes (np.ndarray): shape (n, 5) - labels (np.ndarray): shape (n, ) - masks (np.ndarray): shape (n, h, w) - ids (np.ndarray): shape (n, ) """ outputs = dict() if bbox_results is not None: labels = [] for i, bbox in enumerate(bbox_results): labels.extend([i] * bbox.shape[0]) labels = np.array(labels, dtype=np.int64) outputs['labels'] = labels bboxes = np.concatenate(bbox_results, axis=0).astype(np.float32) if bboxes.shape[1] == 5: outputs['bboxes'] = bboxes elif bboxes.shape[1] == 6: ids = bboxes[:, 0].astype(np.int64) bboxes = bboxes[:, 1:] outputs['bboxes'] = bboxes outputs['ids'] = ids else: raise NotImplementedError( f'Not supported bbox shape: (N, {bboxes.shape[1]})') if mask_results is not None: assert mask_shape is not None mask_height, mask_width = mask_shape mask_results = mmcv.concat_list(mask_results) if len(mask_results) == 0: masks = np.zeros((0, mask_height, mask_width)).astype(bool) else: masks = np.stack(mask_results, axis=0) outputs['masks'] = masks return outputs
def generate_label_file(self, imgs, thres): for i, img in enumerate(imgs): detect_res = inference_detector(self.model, img) print(i, imgs[i]) img = mmcv.imread(imgs[i]) img = img.copy() if isinstance(detect_res, tuple): bbox_result, segm_result = detect_res else: bbox_result, segm_result = detect_res, None bboxes = np.vstack(bbox_result) labels = [np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result)] labels = np.concatenate(labels) if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > thres)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 shapes = [] if len(bboxes) > 0: for j, bbox in enumerate(bboxes): shape = { "label": self.class_names[labels[j]], "points": [ [ float(bbox[0]), float(bbox[1]) ], [ float(bbox[2]), float(bbox[3]) ] ], "group_id": None, "shape_type": "rectangle", "flags": {} } if bbox[4] > thres: shapes.append(shape) img = cv2.imread(imgs[i]) img_str = cv2.imencode('.jpg', img)[1].tostring() # 将图片编码成流数据,放到内存缓存中,然后转化成string格式 b64_code = base64.b64encode(img_str) label_file = { "version": "4.2.7", "flags": {}, "shapes": shapes, "imagePath": os.path.basename(imgs[i]), "imageData": b64_code.decode(), "imageHeight": img.shape[0], "imageWidth": img.shape[1] } with open(imgs[i].replace('.jpg', '.json'), 'w') as f: json.dump(label_file, f)
def draw_masks(img, masks, colors, classes): if masks is not None: assert len(colors) == len(masks) mask_colors = [colors[i] for i, mask in enumerate(masks) if mask] mask_classes = [classes[i] for i, mask in enumerate(masks) if mask] masks = mmcv.concat_list(masks) for i, (mask, color, cls) in enumerate(zip(masks, mask_colors, mask_classes)): mask = mutils.decode(mask).astype(np.bool) img[mask] = img[mask] * 0.5 + color * 0.5 img = put_text(img, color[0], cls, i) return img
def show_result(img, result, class_names, score_thr=0.8, out_file=None, out_file_mask=None): """Visualize the detection results on the image. Args: img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. class_names (list[str] or tuple[str]): A list of class names. score_thr (float): The threshold to visualize the bboxes and masks. out_file (str, optional): If specified, the visualization result will be written to the out file instead of shown in a window. """ assert isinstance(class_names, (tuple, list)) img = mmcv.imread(img) if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] flag = False mask = None temp_mask = None for i in inds: # color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) # print(out_file, np.count_nonzero(mask) / np.size(mask) * 100) if (np.count_nonzero(mask) / np.size(mask) * 100 > 2): print(out_file, np.count_nonzero(mask) / np.size(mask) * 100) if flag: temp_mask = np.logical_or(temp_mask, mask) else: temp_mask = mask flag = True if (flag): mask = temp_mask mask_not = np.logical_not(mask) img[mask_not] = img[mask_not] * 0 maskImg = img.copy() maskImg[mask_not] = maskImg[mask_not] * 0 maskImg[mask] = 255 mmcv.imwrite(img, out_file) # Segmentation Image mmcv.imwrite(maskImg, out_file_mask) # Mask Image
def show_result(self, img, result, classes=coco.COCO80_CLASSES, score_thr=0.3, wait_time=0, out_file=None): """Visualize the detection results on the image. Args: img (str or np.ndarray): Image filename or loaded image. result (tuple[list] or list): The detection result, can be either (bbox, segm) or just bbox. class_names (list[str] or tuple[str]): A list of class names. score_thr (float): The threshold to visualize the bboxes and masks. wait_time (int): Value of waitKey param. out_file (str, optional): If specified, the visualization result will be written to the out file instead of shown in a window. """ import mmcv img = mmcv.imread(img) if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None if False: # TODO: Show mask result bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes bboxes = bbox_result[:, :-1].numpy() labels = bbox_result[:, -1].int().numpy() #print(bbox_result.shape, bboxes.shape, labels.shape) mmcv.imshow_det_bboxes( img.copy(), bboxes, labels, class_names=self.CLASSES if classes is None else classes, score_thr=score_thr, show=out_file is None, wait_time=wait_time, out_file=out_file)
def parse_pkldets(input_pkl, img_dir, outdir, score_thr=0.3, dataset='cityscapes'): dets = mmcv.load(input_pkl) num_imgs = len(dets) # num_class = 8 VOC_CLASSES = get_classes(dataset) print(VOC_CLASSES) img_names_list = os.listdir(img_dir) for k in range(num_imgs): print(k) imgHeight, imgWidth = 1024, 2048 imgName = img_names_list[k] # print(dets[k]) bbox_result, segm_result = dets[k] labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) bboxes = np.vstack(bbox_result) segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] # labs, confs, polygons = [], [], [] ftxt = open(outdir + imgName[:-4] + '.txt', 'w') # print(inds) for i in inds: img = np.zeros((imgHeight, imgWidth), np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) # mask predicted. # contour, hier = cv2.findContours(mask, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE) # try: # contour = np.reshape(contour[0], [-1, 2]) # except: # print(k, i, contour, 'Segmentation Error!') # continue # polygon = [] # for j in range(contour.shape[0]): # polygon.append([int(contour[j, 0]), int(contour[j, 1])]) # polygons.append(polygon) # append polygon of a instance. img[mask] = 255 # mask as white-color. cv2.imwrite(outdir + imgName[:-4] + str(i) + '.png', img) # confs.append(round(float(bboxes[i, -1]), 2)) # conf predicted. # labs.append(VOC_CLASSES[labels[i]]) # labels predicted. # print(imgName[:-4]+str(i)+'.png') # print(str(labelDic[VOC_CLASSES[labels[i]]])) ftxt.write(imgName[:-4]+str(i)+'.png'+' '+str(labelDic[VOC_CLASSES[labels[i]]])+' '+str(round(float(bboxes[i, -1]), 2))+'\n')
def main(): args = parse_args() model = init_detector(args.config, args.checkpoint, device=torch.device('cuda', 0)) camera = cv2.VideoCapture(0) CONFIDENCE = 0.6 print('Press "Esc", "q" or "Q" to exit.') while True: ret, img = camera.read() cv2.flip(img, 1, img) result = inference_detector(model, img) ch = cv2.waitKey(1) if ch == 27 or ch == ord('q') or ch == ord('Q'): break dets = [] for i in range(len(result)): dets.append(result[i]) for i in dets[0]: bbox = dets[0][i]['bbox'] obj_class = model.CLASSES[dets[0][i]['label']] + " " + str(bbox[4]) segm_result = dets[1][i] segs_l = [] for s in range(len(dets[1])): segs_l.append(dets[1][s]) x_len = int(abs(bbox[0] - bbox[2])) y_len = int(abs(bbox[1] - bbox[3])) bboxes = np.vstack(dets[0]) if bbox[4] >= CONFIDENCE: # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segs_l) inds = np.where(bboxes[:, -1] > CONFIDENCE)[0] for t in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(dets[1][t]).astype(np.bool) img[mask] = img[mask] * 0.5 + color_mask * 0.5 cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 1) image = cv2.putText(img, obj_class, (bbox[0], bbox[1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA) cv2.imshow('Camera frame', img) '''show_result(
def get_result(data, result, img_norm_cfg, dataset=None, score_thr=0.3): if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None img_tensor = data['img'].data[0] img_metas = data['img_meta'].data[0] mean = img_norm_cfg.mean std = img_norm_cfg.std imgs = tensor2imgs(img_tensor, mean=mean, std=std, to_rgb=False) assert len(imgs) == len(img_metas) if isinstance(dataset, str): class_names = get_classes(dataset) elif isinstance(dataset, (list, tuple)): class_names = dataset else: raise TypeError('dataset must be a valid dataset name or a sequence' ' of class names, not {}'.format(type(dataset))) result_imgs = [] for img, img_meta in zip(imgs, img_metas): h, w, _ = img_meta['img_shape'] img_show = img[:h, :w, :] bboxes = np.vstack(bbox_result) # draw segmentation masks if segm_result is not None: segms = mmcv.concat_list(segm_result) inds = np.where(bboxes[:, -1] > score_thr)[0] for i in inds: color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8) mask = maskUtils.decode(segms[i]).astype(np.bool) img_show[mask] = img_show[mask] * 0.5 + color_mask * 0.5 # draw bounding boxes labels = [ np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(bbox_result) ] labels = np.concatenate(labels) img = imshow_det_bboxes(img_show, bboxes, labels, class_names=class_names, score_thr=score_thr) result_imgs.append(img) return result_imgs