def detect(self): config = ConfigProto() config.gpu_options.allow_growth = True session = InteractiveSession(config=config) self.MODEL_DIR = self.model_dir() self.NETWORK_PATH = self.network_path() self.InferenceConfig = self.inference_config() self.InferenceConfig.display() self.model = modellib.MaskRCNN(mode="inference", model_dir=self.MODEL_DIR, config=self.InferenceConfig) self.model.load_weights(self.NETWORK_PATH, by_name=True) results = self.model.detect([self.image], verbose=1) """r looks like this: r['rois'], r['masks'], r['class_ids'], r['scores']""" r = results[0] class_names = self.class_names() visualize.display_instances(self.image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores']) session.close() ret = self.further_image_manipulation(r) return ret
def detect(self, image_path, background_color): cuda.select_device(0) config = ConfigProto() config.gpu_options.allow_growth = True session = InteractiveSession(config=config) ROOT_DIR = os.path.abspath("/") MODEL_DIR = os.path.join(ROOT_DIR, "logs") sys.path.append(ROOT_DIR) # To find local version of the library sys.path.append(os.path.join(ROOT_DIR, "samples/coco/")) # To find local version COCO_MODEL_PATH = "/home/bernihoh/Bachelor/MaskRCNN/mask_rcnn_coco.h5" if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) config = InferenceConfig() config.display() model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(COCO_MODEL_PATH, by_name=True) COCO_DIR = "/home/bernihoh/Bachelor/MaskRCNN/samples/coco" # geändert: Zeile eingefügt dataset = coco.CocoDataset() dataset.load_coco(COCO_DIR, "train") dataset.prepare() class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'] image = skimage.io.imread(image_path) print(dataset.class_names) plt.imshow(image) # Run detection results = model.detect([image], verbose=1) # Visualize results r = results[0] visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores']) used_class = r["class_ids"] print(used_class) mask = r["masks"] mask = mask.astype(np.ubyte) # maskimg = mask[:, :, 1] ^ mask[:, :, 1] maskimg = np.zeros((image.shape[0], image.shape[1])) maskimg = maskimg.astype(np.ubyte) background_mask = np.full((image.shape[0], image.shape[1]), background_color, dtype=np.uint8) skimage.io.imshow(background_mask) plt.show() for i in range(mask.shape[2]): # skimage.io.imshow(mask[:, :, i]) # plt.show() # maskimg = maskimg | mask[:, :, i] a = used_class[i] - 1 if used_class[i] - 1 < 0: a = 0 background_mask = background_mask - mask[:, :, i] * (a + 127) maskimg = np.maximum(maskimg, mask[:, :, i] * a) skimage.io.imshow(background_mask) plt.show() maskimg = np.maximum(maskimg, background_mask) # maskimg[maskimg == 0] = 124 # maskimg = skimage.exposure.rescale_intensity(maskimg) skimage.io.imshow(maskimg) plt.show() # skimage.io.imsave("/home/bernihoh/Bachelor/MaskRCNN/ownimages/mask138-1.jpg", maskimg) session.close() cuda.close() return maskimg
for i in range(mask.shape[2]): maskimg = np.maximum(maskimg, mask[:, :, i]*(used_class[i])) skimage.io.imshow(maskimg) plt.show() # skimage.io.imsave("/home/bernihoh/Bachelor/MaskRCNN/ownimages/mask138-1.jpg", maskimg) """ image = skimage.io.imread( "/home/bernihoh/Bachelor/SPADE/results/coco_pretrained/test_latest/images/input_label/2020-05-28 13:58:02.png" ) plt.imshow(image) # Run detection results = model.detect([image], verbose=1) # Visualize results r = results[0] visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores']) used_class = r["class_ids"] print(used_class) mask = r["masks"] mask = mask.astype(np.ubyte) # maskimg = mask[:, :, 1] ^ mask[:, :, 1] maskimg = np.zeros((image.shape[0], image.shape[1])) maskimg = maskimg.astype(np.ubyte) for i in range(mask.shape[2]): maskimg = np.maximum(maskimg, mask[:, :, i] * (used_class[i])) skimage.io.imshow(maskimg) plt.show() # skimage.io.imsave("/home/bernihoh/Bachelor/MaskRCNN/ownimages/mask138-1.jpg", maskimg)