示例#1
0
    def classify(self, img, pixel_rect):
        img_h, img_w = img.shape[:2]
        img_dims = (img_w, img_h)
        w, h = self.window_dims
        window_shape = (h, w)
        window_aspect = w / float(h)
        # print window_aspect

        # Enlarge to window_dims:
        enlarged_rect = pixel_rect.enlarge_to_aspect(window_aspect)
        # Ensure rectangle is located within its image:
        object_rect = enlarged_rect.translated([0,0], img_dims)
        # if not pixel_rect.lies_within_frame(img_dims):

        sample = utils.crop_rectangle(img, object_rect)
        sample = utils.resize_sample(sample, shape=window_shape)
        sample = self.prepare_sample(sample)

        flat_sample = sample.reshape(sample.shape[0] * sample.shape[1] * sample.shape[2])
        feed = {self.x: [flat_sample], self.keep_prob: 1.0}
        label_probs = self.sess.run(tf.nn.softmax(self.logits), feed_dict=feed)

        pos_prob, neg_prob = label_probs[0]
        is_object = pos_prob > neg_prob

        return is_object, object_rect, pos_prob
示例#2
0
    def classify(self, img, pixel_rect):
        img_h, img_w = img.shape[:2]
        img_dims = (img_w, img_h)
        w, h = self.window_dims
        window_shape = (h, w)
        window_aspect = w / float(h)
        # print window_aspect

        # Enlarge to window_dims:
        enlarged_rect = pixel_rect.enlarge_to_aspect(window_aspect)
        # Ensure rectangle is located within its image:
        object_rect = enlarged_rect.translated([0, 0], img_dims)
        # if not pixel_rect.lies_within_frame(img_dims):

        sample = utils.crop_rectangle(img, object_rect)
        sample = utils.resize_sample(sample, shape=window_shape)
        sample = self.prepare_sample(sample)

        flat_sample = sample.reshape(sample.shape[0] * sample.shape[1] *
                                     sample.shape[2])
        feed = {self.x: [flat_sample], self.keep_prob: 1.0}
        label_probs = self.sess.run(tf.nn.softmax(self.logits), feed_dict=feed)

        pos_prob, neg_prob = label_probs[0]
        is_object = pos_prob > neg_prob

        return is_object, object_rect, pos_prob
示例#3
0
def sliding_window_generator(img, window_dims=(32, 32), scale_factor=1.1, strides=(8, 8), only_rects=False):
    """ Creates an image pyramid, and generates views into each of its levels.
    Returns the pixel rectangle corresponding to each image
    """

    h, w = img.shape[:2]
    orig_dims = (w, h)

    sx, sy = strides
    ww, wh = window_dims
    for img in image_pyramid(img, scale_factor, window_dims):
        h, w = img.shape[:2]
        img_dims = (w, h)
        mx = w - ww
        my = h - wh
        for x in xrange(0, mx, sx):
            for y in xrange(0, my, sy):
                # Create the window in the current pyramid level:
                local_rect = gm.PixelRectangle.from_opencv_bbox([x, y, ww, wh])

                # Crop the window:
                window = None
                if not only_rects:
                    window = utils.crop_rectangle(img, local_rect)

                # Find the window location in the original image:
                orig_rect = local_rect.scale_image(img_dims, orig_dims)

                if (not window is None) and window.shape[0] != wh:
                    print 'ERROR:', (w, h), (x, y), (mx, my), local_rect

                yield (window, orig_rect)
示例#4
0
    def update_display(self):
        h, w = self.current_img.shape[:2]
        max_dim = float(self.display_width)
        if w > max_dim + 50 or h > max_dim + 50:
            sx = max_dim / w
            sy = max_dim / h
            self.current_scale = min(sx, sy)
            self.current_img = cv2.resize(self.current_img,
                                          dsize=None,
                                          fx=self.current_scale,
                                          fy=self.current_scale)

        clone_img = self.current_img.copy()

        # Draw bounding boxes for current image:
        for rect in self.get_image_rectangles(self.current_path):
            oh, ow = self.original_shape[:2]
            rect = rect.scale_image((ow, oh), (w, h))
            if self.flipped:
                rect = rect.rotated_180((w, h))

            tl = tuple(rect.tl)
            br = tuple(rect.br)
            cv2.rectangle(clone_img, tl, br, (255, 255, 255), 3)
            cv2.rectangle(clone_img, tl, br, (255, 0, 127), 2)

        # Draw bounding box being edited:
        if self.rect_tl and self.rect_br:
            cv2.rectangle(clone_img, self.rect_tl, self.rect_br,
                          (255, 255, 255), 3)
            cv2.rectangle(clone_img, self.rect_tl, self.rect_br, (0, 255, 0),
                          2)

            rect = gm.PixelRectangle.fromCorners(self.rect_tl, self.rect_br)
            rect = self.convert_to_rect_in_original(rect)

            cropped = utils.crop_rectangle(self.original_img, rect)
            cw = self.current_img.shape[1]
            ch = self.current_img.shape[0]
            if rect.w > 5 and rect.h > 5:
                if rect.w > rect.h:
                    preview_width = cw / 5.0
                    preview_height = preview_width / rect.aspect
                else:
                    preview_height = ch / 4.0
                    preview_width = preview_height * rect.aspect
                new_shape = (max(2, int(round(preview_height))),
                             max(2, int(round(preview_width))))
                preview = utils.resize_sample(cropped,
                                              new_shape,
                                              use_interp=False)
                clone_img[0:new_shape[0], 0:new_shape[1], :] = preview

        cv2.imshow(self.winName, clone_img)
示例#5
0
    def update_display(self):
        h, w = self.current_img.shape[:2]
        max_dim = float(self.display_width)
        if w > max_dim + 50 or h > max_dim + 50:
            sx = max_dim / w
            sy = max_dim / h
            self.current_scale = min(sx, sy)
            self.current_img = cv2.resize(self.current_img, dsize=None, fx=self.current_scale, fy=self.current_scale)

        clone_img = self.current_img.copy()

        # Draw bounding boxes for current image:
        for rect in self.get_image_rectangles(self.current_path):
            oh, ow = self.original_shape[:2]
            rect = rect.scale_image((ow, oh), (w,h))
            if self.flipped:
                rect = rect.rotated_180((w,h))

            tl = tuple(rect.tl)
            br = tuple(rect.br)
            cv2.rectangle(clone_img, tl, br, (255, 255, 255), 3)
            cv2.rectangle(clone_img, tl, br, (255, 0, 127), 2)

        # Draw bounding box being edited:
        if self.rect_tl and self.rect_br:
            cv2.rectangle(clone_img, self.rect_tl, self.rect_br, (255, 255, 255), 3)
            cv2.rectangle(clone_img, self.rect_tl, self.rect_br, (0, 255, 0), 2)

            rect = gm.PixelRectangle.fromCorners(self.rect_tl, self.rect_br)
            rect = self.convert_to_rect_in_original(rect)

            cropped = utils.crop_rectangle(self.original_img, rect)
            cw = self.current_img.shape[1]
            ch = self.current_img.shape[0]
            if rect.w > 5 and rect.h > 5:
                if rect.w > rect.h:
                    preview_width = cw / 5.0
                    preview_height = preview_width / rect.aspect
                else:
                    preview_height = ch / 4.0
                    preview_width = preview_height * rect.aspect
                new_shape = (max(2, int(round(preview_height))), max(2, int(round(preview_width))))
                preview = utils.resize_sample(cropped, new_shape, use_interp=False)
                clone_img[0:new_shape[0],0:new_shape[1],:] = preview

        cv2.imshow(self.winName, clone_img)
示例#6
0
def sliding_window_generator(img,
                             window_dims=(32, 32),
                             scale_factor=1.1,
                             strides=(8, 8),
                             only_rects=False):
    """ Creates an image pyramid, and generates views into each of its levels.
    Returns the pixel rectangle corresponding to each image
    """

    h, w = img.shape[:2]
    orig_dims = (w, h)

    sx, sy = strides
    ww, wh = window_dims
    for img in image_pyramid(img, scale_factor, window_dims):
        h, w = img.shape[:2]
        img_dims = (w, h)
        mx = w - ww
        my = h - wh
        for x in xrange(0, mx, sx):
            for y in xrange(0, my, sy):
                # Create the window in the current pyramid level:
                local_rect = gm.PixelRectangle.from_opencv_bbox([x, y, ww, wh])

                # Crop the window:
                window = None
                if not only_rects:
                    window = utils.crop_rectangle(img, local_rect)

                # Find the window location in the original image:
                orig_rect = local_rect.scale_image(img_dims, orig_dims)

                if (not window is None) and window.shape[0] != wh:
                    print 'ERROR:', (w, h), (x, y), (mx, my), local_rect

                yield (window, orig_rect)