matterport / Mask_RCNN

Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow
Other
24.53k stars 11.68k forks source link

how to make detection on multiple images ? #1996

Open Mididou opened 4 years ago

Mididou commented 4 years ago

Hello Everyone , so , i want to run a detection on multiple images / an entier folder of input images , but still trying to figure out how to accomplish that , i'm using the command lines , and here's my code :

   ` # USAGE
     # python maskrcnn_fashion_predict.py --weights mask_rcnn_fashion_0019.h5 --labels 
       fashion_labels.txt --image images/prv_image_11.jpg

     # import the necessary packages
     from mrcnn.config import Config   
     from mrcnn import model as modellib
     from mrcnn import visualize
     import numpy as np
     import colorsys
     import argparse
     import imutils
     import random
     import cv2
     import os
     import skimage.io

     import matplotlib.image as mpimg
     import cv2

     import matplotlib.pyplot as plt
     import numpy as np

     # construct the argument parse and parse the arguments
     ap = argparse.ArgumentParser()
     ap.add_argument("-w", "--weights", required=True,
         help="path to Mask R-CNN model weights pre-trained on COCO")
     ap.add_argument("-l", "--labels", required=True,
         help="path to class labels file")
     ap.add_argument("-c", "--confidence", type=float, default=0.5,
         help="minimum probability to filter weak detections")
     ap.add_argument("-i", "--image", required=True,
         help="path to input image to apply Mask R-CNN to")
     args = vars(ap.parse_args())

    # load the class label names from disk, one label per line
    CLASS_NAMES = open(args["labels"]).read().strip().split("\n")

    # generate random (but visually distinct) colors for each class label
    # (thanks to Matterport Mask R-CNN for the method!)
    hsv = [(i / len(CLASS_NAMES), 1, 1.0) for i in range(len(CLASS_NAMES))]
    COLORS = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
    random.seed(42)
    random.shuffle(COLORS)

    class SimpleConfig(Config):
        # give the configuration a recognizable name
        NAME = "fashion"

        # set the number of GPUs to use along with the number of images
        # per GPU
        GPU_COUNT = 1
         IMAGES_PER_GPU = 1

        # number of classes (we would normally add +1 for the background
        # but the background class is *already* included in the class
        # names)

         NUM_CLASSES = 1 + 3

         # Skip detections with < 90% confidence
         DETECTION_MIN_CONFIDENCE = args["confidence"]

             # initialize the inference configuration
             config = SimpleConfig()

             # initialize the Mask R-CNN model for inference and then load the
             # weights
             print("[INFO] loading Mask R-CNN model...")
             model = modellib.MaskRCNN(mode="inference", config=config,
        model_dir=os.getcwd())
            model.load_weights(args["weights"], by_name=True)

            # load the input image, convert it from BGR to RGB channel
           # ordering, and resize the image
           # default value 512 form the width 
           image = cv2.imread(args["image"])
           image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
           image = imutils.resize(image, width=1150)

          # perform a forward pass of the network to obtain the results
          print("[INFO] making predictions with Mask R-CNN...")
          r = model.detect([image], verbose=1)[0]

           image = visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], 
                                           ['BG', 'top', 'boots' , 'bag'], r['scores'],
                                           title="")

            i = 0 
            mask = r["masks"]
            for i in range(mask.shape[2]):
              image = cv2.imread(args["image"])
             # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
              image = imutils.resize(image, width=1150)

              for j in range(image.shape[2]):

                image[:,:,j] = image[:,:,j] * mask[:,:,i]

               # cv2.figure(figsize=(8,8))
               # cv2.imshow("Output", image)
               # cv2.waitKey()
               filename = "Output/segment_%d.jpg"%i
               cv2.imwrite(filename,image)
                i+=1

` Any Suggestion on how to accomplish would be great , thank you.

ro1406 commented 2 years ago

The only way i found to do this is to predict each image separately in a loop. You can use os.listdir() to help you loop through all images in a folder and then read and predict for each image separately.

Do let me know if there is another (perhaps faster) way to do this!