Передача нескольких изображений для сегментации python? - PullRequest
0 голосов
/ 10 февраля 2020

Итак, я хочу запустить сегментацию по нескольким изображениям / всю папку входных изображений, но все еще пытаюсь выяснить, как это сделать sh, я использую командные строки, и вот мой код (примечание также, что этот код спасает меня от маски после закрытия изображений windows):

        # python maskrcnn_fashion_predict.py --weights mask_rcnn_fashion_0019.h5 --labels 
        # fashion_labels.txt --image images/prv_image.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 glob

        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(file) for file in 
       glob.glob("C:\\Users\\zm\\Desktop\\Project\\Fashion_Keras_Mask_Rcnn\\images\\*.jpg")]
        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

Любое предложение о том, как выполнить sh, было бы здорово, спасибо.

...