Пример #1
0
 def numpy_saver(self, data, name):
     if len(data.shape) == 4:
         image.saves(os.path.join(self.folder, name), data)
     elif len(data.shape) == 3:
         image.save(os.path.join(self.folder, name + ".png"), data)
     else:
         raise "Wrong type, make sure your image shape=(n,h,w,c) or (h,w,c)"
Пример #2
0
def SPImageSeg(context):
    args = context.args
    images = args.inputImage

    for idx, img in enumerate(images):
        splitX, splitY = (
            math.ceil(img.shape[1] / args.x),
            math.ceil(img.shape[0] / args.y),
        )
        i = 0
        for m in range(splitX):
            for n in range(splitY):
                filename = (
                    os.path.splitext(
                        storage.delimiter.join(
                            images.images[idx].split(storage.delimiter)[8:]
                        )
                    )[0]
                    + "_"
                    + str(i)
                    + ".png"
                )
                image.save(
                    os.path.join(args.outputImage, filename,),
                    img[
                        n * args.y : (n + 1) * args.y, m * args.x : (m + 1) * args.x, :,
                    ],
                )
                i += 1

    return args.outputImage
Пример #3
0
 def list_saver(self, data, name):
     if isinstance(data[0], np.ndarray) and len(data[0].shape) == 3:
         image.saves(os.path.join(self.folder, name), data)
     elif isinstance(data[0], tuple) and len(data[0]) == 2:
         for path, img in data:
             image.save(os.path.join(self.folder, path), img)
     else:
         raise "Wrong type, make sure your image shape=(n,h,w,c)"
Пример #4
0
 def visualizer(self, imgs, datas, output_dir):
     for img, data in zip(imgs, datas):
         im = image.read(img)
         v = Visualizer(
             im[:, :, ::-1],
             MetadataCatalog.get(self.cfg.DATASETS.TRAIN[0]),
             scale=1.2,
         )
         v = v.draw_instance_predictions(data["instances"].to("cpu"))
         image.save(
             os.path.join(output_dir, os.path.basename(img)),
             v.get_image()[:, :, ::-1],
         )
Пример #5
0
def SPObjectDetectionPrediction(context):
    args = context.args
    predict_image = get_predict_file(args.predictImages)
    bboxes = []
    i = 0
    for inputImage in get_od_image(predict_image):
        predictedBox, imageBox = args.model.predict(inputImage)
        bboxes.append(predictedBox)
        image.save(
            os.path.join(
                args.images,
                storage.delimiter.join(predict_image[i].split(
                    storage.delimiter)[8:]),
            ),
            imageBox[:, :, ::-1],
        )
        i += 1
    return bboxes, args.images
Пример #6
0
def SPImageBox(context):
    args = context.args
    images = args.inputImage

    files = []

    try:
        if args.inputData:
            jsonFile = os.path.join(args.inputData, "project.json")
            with open(jsonFile, "rb") as load_f:
                fileInfo = json.load(load_f)

            for i, j in fileInfo["metadata"].items():
                files.append(os.path.join(images.folder, j["vid"]))
                try:
                    filename = (os.path.splitext(j["vid"])[0] + "_" +
                                j["av"]["1"] + ".png")
                except:
                    filename = (os.path.splitext(j["vid"])[0] + "_" +
                                i.split("_")[-1] + ".png")
                xy = j["xy"][1:]
                img = image.read(os.path.join(images.folder, j["vid"]))
                image.save(
                    os.path.join(
                        args.outputImage,
                        filename,
                    ),
                    img[int(xy[1]):int(xy[1] + xy[3]),
                        int(xy[0]):int(xy[0] + xy[2]), :, ],
                )
        elif args.xy:
            for idx, img in enumerate(images):
                files.append(images.images[idx])
                x = int(args.xy[0] + args.x) if args.x else img.shape[1] + 1
                y = int(args.xy[1] + args.y) if args.y else img.shape[0] + 1
                image.save(
                    os.path.join(
                        args.outputImage,
                        storage.delimiter.join(images.images[idx].split(
                            storage.delimiter)[8:]),
                    ),
                    img[int(args.xy[1]):y,
                        int(args.xy[0]):x, :, ],
                )
    except:
        logger.info("can not find project.json or json format error")

    for idx, img in enumerate(images):
        if images.images[idx] not in files:
            image.save(
                os.path.join(
                    args.outputImage,
                    storage.delimiter.join(images.images[idx].split(
                        storage.delimiter)[8:]),
                ),
                img,
            )

    return args.outputImage
Пример #7
0
def SPROI(context):
    args = context.args
    image_folder = args.inputData1
    detection_dict = args.inputData2
    images = os.listdir(image_folder)
    for i, img in enumerate(images):
        image = Image.open(os.path.join(image_folder, img))
        im_height, im_width = image.size
        boxes = detection_dict[i]["detection_boxes"]
        y_min = boxes[0][0] * im_height
        x_min = boxes[0][1] * im_width
        y_max = boxes[0][2] * im_height
        x_max = boxes[0][3] * im_width

        x_2 = int((x_min + x_max) / 2) + 72
        y_1 = int(y_max + 10)
        x_1 = int(x_2 - 72)
        y_2 = int(y_1 + 72)
        box = (x_1, y_1, x_2, y_2)
        roi = image.crop(box)
        im.save(os.path.join(args.outputData1, img), np.array(roi)[:, :, -1])

    return args.outputData1