Beispiel #1
0
 def start(self):
     """
     Collect and preprocess all the data
     :return:
     """
     for (labeled_file, channel) in self.files:
         logging.info("Incorporating file %s" % (labeled_file))
         local_labeled_file = self.storage.get(labeled_file)
         if local_labeled_file is not None:
             self.fc = FrameCollection()
             self.fc.load_from_file(local_labeled_file)
             self.nframes += len(self.fc)
             for i in xrange(len(self.fc)):
                 img1 = self.fc.Frame(i).get_image(channel=channel)
                 img = cv2.resize(img1, dsize=self.frame_resolution, interpolation=cv2.INTER_CUBIC)
                 label = self.fc.Frame(i).get_label(channel=channel)
                 if label is not None:
                     label.set_image_shape(shape=img1.shape)
                     mask = self.fc.Frame(i).get_label(channel=channel).get_mask()
                     mask = cv2.resize(mask, dsize=self.heatmap_resolution, interpolation=cv2.INTER_CUBIC)
                 else:
                     mask = np.zeros(self.heatmap_resolution, dtype=np.uint8)
                 self.masks.append(mask)
                 self.frames.append(img)
             if self.remove_files:
                 os.remove(local_labeled_file)
class ImagesToLabeledMovie(object):

    def __init__(self, label_file=None, image_dir=None, output_file=None, skip=None):
        if label_file is None or image_dir is None or output_file is None:
            raise Exception("Nescessary arguments are missing")
        self.label_file=open(label_file, "r")
        self.image_dir = image_dir
        self.output_file = output_file
        self.frame_collection = FrameCollection()
        self.file_index = 1
        if skip is not None:
            self.skip = int(skip)
        else:
            self.skip = 0

    def get_next_image_file(self):
        p = self.image_dir
        if not p.endswith("/"):
            p += "/"
        p += ("%04d.jpg" % self.file_index)
        self.file_index += 1
        if os.path.isfile(p):
            return p
        else:
            return None

    def run(self):
        i = 0
        while True:
            p = self.get_next_image_file()
            if p is None:
                break
            img = cv2.imread(p)
            if i >= self.skip:
                rect = self.label_file.readline()
                try:
                    box = map(lambda x: int(x), rect.split("\t"))
                    B = BoundingRegion(image_shape=img.shape, box=np.array(box))
                except:
                    try:
                        box = map(lambda x: int(x), rect.split(","))
                        B = BoundingRegion(image_shape=img.shape, box=np.array(box))
                    except:
                        print "No more bounding boxes!"
                        B = BoundingRegion()
            else:
                B = BoundingRegion()
            F = LabeledMovieFrame(internal_storage_method='jpg', compression_level=90)
            F.set_image(img)
            F.set_label(B)
            B.draw_box(img)
            cv2.imshow("image", img)
            cv2.moveWindow("image", 50, 50)
            cv2.waitKey(1)
            i += 1
            self.frame_collection.append(F)
        self.frame_collection.write_to_file(self.output_file)
 def __init__(self, label_file=None, image_dir=None, output_file=None, skip=None):
     if label_file is None or image_dir is None or output_file is None:
         raise Exception("Nescessary arguments are missing")
     self.label_file=open(label_file, "r")
     self.image_dir = image_dir
     self.output_file = output_file
     self.frame_collection = FrameCollection()
     self.file_index = 1
     if skip is not None:
         self.skip = int(skip)
     else:
         self.skip = 0
    def __init__(self,
                 image_dir=None,
                 output_file=None,
                 skip=None,
                 channel="default",
                 file_prefix="000000",
                 dsize="1.0",
                 flip_vert=None,
                 flip_hor=None,
                 rotate=None,
                 shape=(100, 100),
                 format="jpg",
                 quality=90):

        if image_dir is None or output_file is None:
            raise Exception("Nescessary arguments are missing")
        self.image_dir = image_dir
        self.output_file = output_file
        self.frame_collection = FrameCollection()
        self.channel = channel
        self.file_index = 1
        if skip is not None:
            self.skip = int(skip)
        else:
            self.skip = 0
        self.files = sorted([f for f in os.listdir(image_dir) if os.path.isfile(os.path.join(image_dir, f)) and f.startswith(file_prefix)])
        self.shape = tuple(map(lambda x: int(x), shape.split("x")))
        self.rotate = rotate
        self.flip_vert = flip_vert
        self.flip_hor = flip_hor
        self.dsize = dsize
        self.format = format
        self.quality = quality
Beispiel #5
0
 def buffer_files(self, idx, channel, files):
     for (labeled_file, channel) in files:
         logging.info("Incorporating file %s" % (labeled_file))
         local_labeled_file = self.storage.get(labeled_file)
         if local_labeled_file is not None:
             self.fc = FrameCollection()
             self.fc.load_from_file(local_labeled_file)
             self.nframes += len(self.fc)
             for i in xrange(len(self.fc)):
                 img1 = self.fc.Frame(i).get_image(channel=channel)
                 img = cv2.resize(img1, dsize=self.frame_resolution, interpolation=cv2.INTER_CUBIC)
                 label = self.fc.Frame(i).get_label(channel=channel)
                 label.set_image_shape(shape=img1.shape)
                 mask = self.fc.Frame(i).get_label(channel=channel).get_mask()
                 mask = cv2.resize(mask, dsize=self.heatmap_resolution, interpolation=cv2.INTER_CUBIC)
                 mask3d = np.zeros((mask.shape[0], mask.shape[1], 3), dtype=mask.dtype)
                 mask3d[:, :, idx] = mask
                 self.masks.append(mask3d)
                 self.frames.append(img)
             if self.remove_files:
                 os.remove(local_labeled_file)
Beispiel #6
0
    def __init__(self,
                 image_dir=None,
                 output_file=None,
                 skip=None,
                 channel="default",
                 file_prefix="000000",
                 dsize="1.0",
                 flip_vert=None,
                 flip_hor=None,
                 rotate=None,
                 shape=(100, 100),
                 format="jpg",
                 quality=90):

        if image_dir is None or output_file is None:
            raise Exception("Nescessary arguments are missing")
        self.image_dir = image_dir
        self.output_file = output_file
        self.frame_collection = FrameCollection()
        self.channel = channel
        self.file_index = 1
        if skip is not None:
            self.skip = int(skip)
        else:
            self.skip = 0
        self.files = sorted([
            f for f in os.listdir(image_dir)
            if os.path.isfile(os.path.join(image_dir, f))
            and f.startswith(file_prefix)
        ])
        self.shape = tuple(map(lambda x: int(x), shape.split("x")))
        self.rotate = rotate
        self.flip_vert = flip_vert
        self.flip_hor = flip_hor
        self.dsize = dsize
        self.format = format
        self.quality = quality
Beispiel #7
0
 def reset(self, reload=False):
     cv2.destroyAllWindows()
     fc = FrameCollection()
     if reload:
         fc.load_from_file(filename=self.output_file)
     else:
         fc.load_from_file(filename=self.input_filename)
     self.movie = fc
     self.tracker = None
     self.current_frame_index = 0
     self.right_button_pressed = False
     self.left_button_pressed = False
     self._anchor = None
     self.x_size = 30
     self.y_size = 30
     self._tracked_bounds = None
     self._stored_bounds = None
     self._current_bounds = BoundingRegion()
     # Make sure self.timer is set
     self.schedule_callback()
     # Initialization of the Graphic User Interface
     self.ready_to_refresh = threading.Lock()
     self.timer_lock = threading.Lock()
     self.win_name = 'Labeling video GUI'
     self.image = self.movie.Frame(self.current_frame_index).get_image(channel=self.channel)
     self.display_image = self.movie.Frame(self.current_frame_index).get_image(channel=self.channel)
     self.image_shape = self.image.shape
     self.create_windows()
     self.set_target_absent()
     self.image_buffer = {}
     self._last_update = time.time()
     self.refresh_timer = threading.Timer(0.05, self.refresh)
     self.refresh_timer.start()
     self.needs_refresh = True
     self.trim_end = len(self.movie)-1
     self.trim_start = 0
 def reset(self, reload=False):
     cv2.destroyAllWindows()
     fc = FrameCollection()
     if reload:
         fc.load_from_file(filename=self.output_file)
     else:
         fc.load_from_file(filename=self.input_filename)
     self.movie = fc
     self.tracker = None
     self.current_frame_index = 0
     self.right_button_pressed = False
     self.left_button_pressed = False
     self._anchor = None
     self.x_size = 30
     self.y_size = 30
     self._tracked_bounds = None
     self._stored_bounds = None
     self._current_bounds = BoundingRegion()
     # Make sure self.timer is set
     self.schedule_callback()
     # Initialization of the Graphic User Interface
     self.ready_to_refresh = threading.Lock()
     self.timer_lock = threading.Lock()
     self.win_name = 'Labeling video GUI'
     self.image = self.movie.Frame(
         self.current_frame_index).get_image(channel=self.channel)
     self.display_image = self.movie.Frame(
         self.current_frame_index).get_image(channel=self.channel)
     self.image_shape = self.image.shape
     self.create_windows()
     self.set_target_absent()
     self.image_buffer = {}
     self._last_update = time.time()
     self.refresh_timer = threading.Timer(0.05, self.refresh)
     self.refresh_timer.start()
     self.needs_refresh = True
     self.trim_end = len(self.movie) - 1
     self.trim_start = 0
    parser.add_argument(
        "-t",
        "--target",
        type=str,
        default="default",
        help="Crop to bounding box, black frame is target absent")

    args = parser.parse_args()
    if not args.input_file:
        parser.print_help()
    else:
        cv2.namedWindow("Player")
        cv2.moveWindow("Player", 50, 50)
        box_shape = tuple(map(lambda x: int(x), args.size.split("x")))
        _video = None
        fc = FrameCollection()
        for i in range(len(args.input_file)):
            fc.load_from_file(filename=args.input_file[i])
            fc.set_active_channel(args.channel)
            for i in xrange(len(fc)):
                img = fc.Frame(i).get_image(channel=args.channel)
                if args.box:
                    for target in fc.Frame(i).get_targets():
                        br = fc.Frame(i).get_label(channel=args.channel,
                                                   target=target)
                        br.draw_box(img, thickness=1, annotation=target)
                if args.crop:
                    br = fc.Frame(i).get_label(channel=args.channel,
                                               target=args.target)
                    if not br.empty:
                        box = br.get_box_pixels()
Beispiel #10
0
class ImagesToLabeledMovie(object):

    def __init__(self,
                 image_dir=None,
                 output_file=None,
                 skip=None,
                 channel="default",
                 file_prefix="000000",
                 dsize="1.0",
                 flip_vert=None,
                 flip_hor=None,
                 rotate=None,
                 shape=(100, 100),
                 format="jpg",
                 quality=90):

        if image_dir is None or output_file is None:
            raise Exception("Nescessary arguments are missing")
        self.image_dir = image_dir
        self.output_file = output_file
        self.frame_collection = FrameCollection()
        self.channel = channel
        self.file_index = 1
        if skip is not None:
            self.skip = int(skip)
        else:
            self.skip = 0
        self.files = sorted([f for f in os.listdir(image_dir) if os.path.isfile(os.path.join(image_dir, f)) and f.startswith(file_prefix)])
        self.shape = tuple(map(lambda x: int(x), shape.split("x")))
        self.rotate = rotate
        self.flip_vert = flip_vert
        self.flip_hor = flip_hor
        self.dsize = dsize
        self.format = format
        self.quality = quality

    def get_next_image_file(self):
        if self.file_index < len(self.files):
            self.file_index += 1
            return os.path.join(self.image_dir, self.files[self.file_index-2])
        else:
            return None

    def run(self):
        i = 0
        cv2.namedWindow("image")
        cv2.moveWindow("image", 50, 50)
        while True:
            p = self.get_next_image_file()
            if p is None:
                break
            img = np.memmap(p, dtype=np.uint8, mode="r", shape=self.shape)
            F = LabeledMovieFrame(internal_storage_method=self.format, compression_level=int(self.quality))
            if self.rotate is not None and self.rotate in ["90", "180", "270"]:
                img = np.rot90(img)
                if self.rotate in ["180", "270"]:
                    img = np.rot90(img)
                if self.rotate in ["270"]:
                    img = np.rot90(img)
            if self.flip_vert:
                img = cv2.flip(img, 0)
            if self.flip_hor:
                img = cv2.flip(img, 1)
            if self.dsize.count("x") == 1:
                osize = tuple(map(lambda x: int(x), self.dsize.split("x")))
            else:
                factor = float(self.dsize)
                osize = int(img.shape[1]*factor), int(img.shape[0]*factor)
            img = cv2.resize(img, dsize=osize, interpolation=cv2.INTER_CUBIC)
            F.set_image(img)
            cv2.imshow("image", img)
            cv2.waitKey(40)
            i += 1
            self.frame_collection.append(F)
        self.frame_collection.write_to_file(self.output_file)
Beispiel #11
0
class SimpleSignalProvider(AbstractExecutionManager.AbstractSignalProvider):
    def __init__(self, frame_resolution, heatmap_resolution, files, storage, channel="default", remove_files=True, reverse=False):
        """
        Handy object to provide input frames and supervising heatmaps efficiently

        :param frame_resolution:
        :param heatmap_resolution:
        :param files:
        :param channel:
        """
        self.files = files
        self.frame_resolution = frame_resolution
        self.heatmap_resolution = heatmap_resolution
        self.channel = channel
        self.frames = []
        self.nframes = 0
        self.masks = []
        self.index = 0
        self.remove_files = remove_files
        self.reverse = reverse
        self.storage=storage

    def start(self):
        """
        Collect and preprocess all the data
        :return:
        """
        for (labeled_file, channel) in self.files:
            logging.info("Incorporating file %s" % (labeled_file))
            local_labeled_file = self.storage.get(labeled_file)
            if local_labeled_file is not None:
                self.fc = FrameCollection()
                self.fc.load_from_file(local_labeled_file)
                self.nframes += len(self.fc)
                for i in xrange(len(self.fc)):
                    img1 = self.fc.Frame(i).get_image(channel=channel)
                    img = cv2.resize(img1, dsize=self.frame_resolution, interpolation=cv2.INTER_CUBIC)
                    label = self.fc.Frame(i).get_label(channel=channel)
                    if label is not None:
                        label.set_image_shape(shape=img1.shape)
                        mask = self.fc.Frame(i).get_label(channel=channel).get_mask()
                        mask = cv2.resize(mask, dsize=self.heatmap_resolution, interpolation=cv2.INTER_CUBIC)
                    else:
                        mask = np.zeros(self.heatmap_resolution, dtype=np.uint8)
                    self.masks.append(mask)
                    self.frames.append(img)
                if self.remove_files:
                    os.remove(local_labeled_file)

    def get_signal(self, name, time):
        """
        Return the requested signal. If time is zero, return the current signal, otherwise
        return past of future signals as indicated by the time parameter

        :param name:
        :param time:
        :return:
        """
        if name == "frame":
            return self.frames[(self.index+time) % self.nframes]
        elif name == "mask":
            return self.masks[(self.index+time) % self.nframes]
        else:
            raise Exception("Unknown signal type")

    def get_length(self):
        return self.nframes

    def advance(self):
        """
        Move current index by one step forward
        :return:
        """
        if self.reverse:
            self.index = (self.index - 1) % self.nframes
        else:
            self.index = (self.index + 1) % self.nframes

    def finish(self):
        """
        Cleanup
        :return:
        """
        pass

    def reset(self):
        self.index = 0

    def get_index(self):
        """
        Return the index of the current frame in the sequence
        :return:
        """
        return self.index
Beispiel #12
0
class ImagesToLabeledMovie(object):
    def __init__(self,
                 image_dir=None,
                 output_file=None,
                 skip=None,
                 channel="default",
                 file_prefix="000000",
                 dsize="1.0",
                 flip_vert=None,
                 flip_hor=None,
                 rotate=None,
                 shape=(100, 100),
                 format="jpg",
                 quality=90):

        if image_dir is None or output_file is None:
            raise Exception("Nescessary arguments are missing")
        self.image_dir = image_dir
        self.output_file = output_file
        self.frame_collection = FrameCollection()
        self.channel = channel
        self.file_index = 1
        if skip is not None:
            self.skip = int(skip)
        else:
            self.skip = 0
        self.files = sorted([
            f for f in os.listdir(image_dir)
            if os.path.isfile(os.path.join(image_dir, f))
            and f.startswith(file_prefix)
        ])
        self.shape = tuple(map(lambda x: int(x), shape.split("x")))
        self.rotate = rotate
        self.flip_vert = flip_vert
        self.flip_hor = flip_hor
        self.dsize = dsize
        self.format = format
        self.quality = quality

    def get_next_image_file(self):
        if self.file_index < len(self.files):
            self.file_index += 1
            return os.path.join(self.image_dir,
                                self.files[self.file_index - 2])
        else:
            return None

    def run(self):
        i = 0
        cv2.namedWindow("image")
        cv2.moveWindow("image", 50, 50)
        while True:
            p = self.get_next_image_file()
            if p is None:
                break
            img = np.memmap(p, dtype=np.uint8, mode="r", shape=self.shape)
            F = LabeledMovieFrame(internal_storage_method=self.format,
                                  compression_level=int(self.quality))
            if self.rotate is not None and self.rotate in ["90", "180", "270"]:
                img = np.rot90(img)
                if self.rotate in ["180", "270"]:
                    img = np.rot90(img)
                if self.rotate in ["270"]:
                    img = np.rot90(img)
            if self.flip_vert:
                img = cv2.flip(img, 0)
            if self.flip_hor:
                img = cv2.flip(img, 1)
            if self.dsize.count("x") == 1:
                osize = tuple(map(lambda x: int(x), self.dsize.split("x")))
            else:
                factor = float(self.dsize)
                osize = int(img.shape[1] * factor), int(img.shape[0] * factor)
            img = cv2.resize(img, dsize=osize, interpolation=cv2.INTER_CUBIC)
            F.set_image(img)
            cv2.imshow("image", img)
            cv2.waitKey(40)
            i += 1
            self.frame_collection.append(F)
        self.frame_collection.write_to_file(self.output_file)
Beispiel #13
0
    parser.add_argument("-b", "--box", help="Draw the bounding box", action="store_true")
    parser.add_argument("-c", "--channel", type=str, default="default", help="Channel ")
    parser.add_argument("-o", "--output", type=str, default="", help="Output file")
    parser.add_argument("-k", "--crop", action="store_true", help="Crop to bounding box, black frame is target absent")
    parser.add_argument("-s", "--size", type=str, default="200x200", help="Size of the crop, default 200x200")
    parser.add_argument("-t", "--target", type=str, default="default", help="Crop to bounding box, black frame is target absent")

    args = parser.parse_args()
    if not args.input_file:
        parser.print_help()
    else:
        cv2.namedWindow("Player")
        cv2.moveWindow("Player", 50, 50)
        box_shape = tuple(map(lambda x: int(x), args.size.split("x")))
        _video = None
        fc = FrameCollection()
        for i in range(len(args.input_file)):
            fc.load_from_file(filename=args.input_file[i])
            fc.set_active_channel(args.channel)
            for i in xrange(len(fc)):
                img = fc.Frame(i).get_image(channel=args.channel)
                if args.box:
                    for target in fc.Frame(i).get_targets():
                        br = fc.Frame(i).get_label(channel=args.channel, target=target)
                        br.draw_box(img, thickness=1, annotation=target)
                if args.crop:
                    br = fc.Frame(i).get_label(channel=args.channel, target=args.target)
                    if not br.empty:
                        box = br.get_box_pixels()
                        crop = img[box[1]:box[1]+box[3], box[0]:box[0]+box[2]]
                        crop_res = cv2.resize(crop, dsize=box_shape, interpolation=cv2.INTER_CUBIC)