def detect(self, image): cuda.select_device(0) config = ConfigProto() config.gpu_options.allow_growth = True session = InteractiveSession(config=config) ROOT_DIR = "/home/bernihoh/Bachelor/SMS/MaskRCNN/samples/SMSNetworks/face_feature_detection/" MODEL_DIR = os.path.join(ROOT_DIR, "logsFaceFeatureDetection") COCO_MODEL_PATH = "/home/bernihoh/Bachelor/SMS/MaskRCNN/samples/SMSNetworks/face_feature_detection/mask_rcnn_face_feature_detection_0029.h5" config = InferenceConfig() config.display() # Create model object in inference mode. model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) # Load weights trained on MS-COCO model.load_weights(COCO_MODEL_PATH, by_name=True) class_names = ["bg", "iris_l", "inner_eye_l", "outer_eye_l", "eye_brow_l", "cheek_l", "iris_r", "inner_eye_r", "outer_eye_r", "eye_brow_r", "cheek_r", "nose_tip", "nose", "mouth", "chin", "face", "head", "distortion"] results = model.detect([image], verbose=1) r = results[0] session.close() cuda.close() return r
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
# Directory of images to run detection on #IMAGE_DIR = os.path.join(ROOT_DIR, "pic_train") class InferenceConfig(head_detection.CocoHeadDetectionConfig): # Set batch size to 1 since we'll be running inference on # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU GPU_COUNT = 1 IMAGES_PER_GPU = 1 config = InferenceConfig() config.display() # Create model object in inference mode. model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) # Load weights trained on MS-COCO model.load_weights(COCO_MODEL_PATH, by_name=True) # Load COCO dataset # COCO_DIR = "/home/bernihoh/Bachelor/MaskRCNN/samples/coco" # geändert: Zeile eingefügt #dataset = head_detection.CocoHeadDetectionDataset() #dataset.load_head_detection("/home/bernihoh/Bachelor/SMS/MaskRCNN/samples/SMSNetworks/head_place_detection/", "train") #dataset.prepare() class_names = ["bg", "head"] # Load a random image from the images folder """ for i in range(8): image = skimage.io.imread("/home/bernihoh/Bachelor/SMS/MaskRCNN/samples/SMSNetworks/head_place_detection/pic_val/"+str(i)+".png")
else: class InferenceConfig(CocoConfig): # Set batch size to 1 since we'll be running inference on # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU GPU_COUNT = 1 IMAGES_PER_GPU = 1 DETECTION_MIN_CONFIDENCE = 0 config = InferenceConfig() config.display() # Create model if args.command == "train": model = modellib.MaskRCNN(mode="training", config=config, model_dir=args.logs) else: model = modellib.MaskRCNN(mode="inference", config=config, model_dir=args.logs) # Select weights file to load if args.model.lower() == "coco": model_path = COCO_MODEL_PATH elif args.model.lower() == "last": # Find last trained weights model_path = model.find_last() elif args.model.lower() == "imagenet": # Start from ImageNet trained weights model_path = model.get_imagenet_weights()