Esempio n. 1
0
    def predict(self, imagePaths):

        image_size = self.image_sizes[self.model]
        LABELS = open(self.classesFile).read().strip().split("\n")
        classes = [label.split(',')[0] for label in LABELS]
        num_classes = len(classes)
        weighted_bifpn = False
        colors = [
            np.random.randint(0, 256, 3).tolist() for i in range(num_classes)
        ]
        model, prediction_model = efficientdet(phi=self.model,
                                               weighted_bifpn=weighted_bifpn,
                                               num_classes=num_classes,
                                               score_threshold=self.CONFIDENCE)
        prediction_model.load_weights(self.modelWeights, by_name=True)

        imagePaths = list(paths.list_images(imagePaths))

        for (i, image_path) in enumerate(imagePaths):
            print("[INFO] predicting on image {} of {}".format(
                i + 1, len(imagePaths)))
            image = cv2.imread(image_path)
            src_image = image.copy()
            image = image[:, :, ::-1]
            h, w = image.shape[:2]

            image, scale, offset_h, offset_w = preprocess_image(
                image, image_size=image_size)
            inputs = np.expand_dims(image, axis=0)
            anchors = anchors_for_shape((image_size, image_size))
            # run network
            start = time.time()
            boxes, scores, labels = prediction_model.predict_on_batch([
                np.expand_dims(image, axis=0),
                np.expand_dims(anchors, axis=0)
            ])
            boxes[0, :, [0, 2]] = boxes[0, :, [0, 2]] - offset_w
            boxes[0, :, [1, 3]] = boxes[0, :, [1, 3]] - offset_h
            boxes /= scale
            boxes[0, :, 0] = np.clip(boxes[0, :, 0], 0, w - 1)
            boxes[0, :, 1] = np.clip(boxes[0, :, 1], 0, h - 1)
            boxes[0, :, 2] = np.clip(boxes[0, :, 2], 0, w - 1)
            boxes[0, :, 3] = np.clip(boxes[0, :, 3], 0, h - 1)

            # select indices which have a score above the threshold
            indices = np.where(scores[0, :] > self.CONFIDENCE)[0]

            # select those detections
            boxes = boxes[0, indices]
            scores = scores[0, indices]
            labels = labels[0, indices]

            file = open(image_path[0:image_path.rfind(".")] + ".xml", "w")
            file.write(
                self.generateXML(image_path[0:image_path.rfind(".")],
                                 image_path, h, w, 3, boxes, scores, labels,
                                 classes))
            file.close()
Esempio n. 2
0
def mainDataset(dataset, output, confidence, weights, fichClass, phi=0):
    f = open(fichClass)
    LABELS = f.read().strip().split("\n")
    LABELS = {int(L.split(",")[1]): L.split(",")[0] for L in LABELS}
    f.close()

    weighted_bifpn = False
    model_path = weights
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]

    num_classes = len(LABELS)
    score_threshold = confidence
    model, prediction_model = efficientdet(phi=phi,
                                           weighted_bifpn=weighted_bifpn,
                                           num_classes=num_classes,
                                           score_threshold=score_threshold)
    prediction_model.load_weights(model_path, by_name=True)

    imagePaths = list(paths.list_images(dataset))
    # loop over the input image paths
    for (i, imagePath) in enumerate(imagePaths):
        image = cv2.imread(imagePath)
        image = image[:, :, ::-1]
        h, w = image.shape[:2]

        image, scale, offset_h, offset_w = preprocess_image(
            image, image_size=image_size)
        inputs = np.expand_dims(image, axis=0)
        anchors = anchors_for_shape((image_size, image_size))
        boxes, scores, labels = prediction_model.predict_on_batch(
            [np.expand_dims(image, axis=0),
             np.expand_dims(anchors, axis=0)])
        boxes[0, :, [0, 2]] = boxes[0, :, [0, 2]] - offset_w
        boxes[0, :, [1, 3]] = boxes[0, :, [1, 3]] - offset_h
        boxes /= scale
        boxes[0, :, 0] = np.clip(boxes[0, :, 0], 0, w - 1)
        boxes[0, :, 1] = np.clip(boxes[0, :, 1], 0, h - 1)
        boxes[0, :, 2] = np.clip(boxes[0, :, 2], 0, w - 1)
        boxes[0, :, 3] = np.clip(boxes[0, :, 3], 0, h - 1)
        indices = np.where(scores[0, :] > score_threshold)[0]
        boxes = boxes[0, indices]
        scores = scores[0, indices]
        labels = labels[0, indices]

        # parse the filename from the input image path, construct the
        # path to the output image, and write the image to disk
        filename = imagePath.split(os.path.sep)[-1]
        file = open(imagePath[0:imagePath.rfind(".")] + ".xml", "w")
        file.write(
            generateXML(imagePath[0:imagePath.rfind(".")], imagePath, h, w, 3,
                        boxes, scores, labels, LABELS))
        file.close()
    def preprocess(self, frames):
        inputs, anchors, meta = [], [], []

        for frame in frames:
            image = frame[:, :, ::-1]
            h, w = image.shape[:2]

            image, scale, offset_h, offset_w = preprocess_image(
                image, image_size=self.size)
            inputs.append(image)
            anchors.append(anchors_for_shape((self.size, self.size)))
            meta.append((scale, h, w, offset_h, offset_w))

        return [np.array(inputs), np.array(anchors)], meta
Esempio n. 4
0
num_classes = len(classes)
score_threshold = 0.5
colors = [np.random.randint(0, 256, 3).tolist() for i in range(num_classes)]
model, prediction_model = efficientdet(phi=phi,
                                       weighted_bifpn=weighted_bifpn,
                                       num_classes=num_classes,
                                       score_threshold=score_threshold)
prediction_model.load_weights(model_path, by_name=True)

image_path = 'sample.jpg'
image = cv2.imread(image_path)
src_image = image.copy()
image = image[:, :, ::-1]
h, w = image.shape[:2]

image, scale, offset_h, offset_w = preprocess_image(image, image_size=image_size)
inputs = np.expand_dims(image, axis=0)
anchors = anchors_for_shape((image_size, image_size))
# run network
start = time.time()
boxes, scores, labels = prediction_model.predict_on_batch([np.expand_dims(image, axis=0),
                                                           np.expand_dims(anchors, axis=0)])
print(time.time() - start)
boxes[0, :, [0, 2]] = boxes[0, :, [0, 2]] - offset_w
boxes[0, :, [1, 3]] = boxes[0, :, [1, 3]] - offset_h
boxes /= scale
boxes[0, :, 0] = np.clip(boxes[0, :, 0], 0, w - 1)
boxes[0, :, 1] = np.clip(boxes[0, :, 1], 0, h - 1)
boxes[0, :, 2] = np.clip(boxes[0, :, 2], 0, w - 1)
boxes[0, :, 3] = np.clip(boxes[0, :, 3], 0, h - 1)