class VideoDataProcessor(object):
    def __init__(self, overwrite=False):
        self.bridge = CvBridge()
        self.overwrite = overwrite
        self.data_image_topic = "/camera/data_image"
        self.fd = FindData(overwrite=overwrite)
        quality_low = "-preset veryfast -crf 28"
        quality_med = "-preset medium -crf 23"
        quality_high = "-preset veryslow -crf 16"
        self.quality = quality_high

    def find_and_process_data(self, directory):
        paths = self.find_data(directory)
        self.process_data(paths)

    def find_data(self, directory):
        return self.fd.find_video_data(directory)

    def process_data(self, bag_paths):
        for bag_path in bag_paths:
            images_paths = file_tools.create_images_paths_from_bag_path(bag_path, self.overwrite)
            if images_paths is None:
                continue
            print("Processing data in {0}".format(bag_path))
            image_count = 0
            bag = rosbag.Bag(bag_path)
            for topic, msg, t in bag.read_messages(topics=[self.data_image_topic]):
                self.process_data_image(msg, images_paths, image_count)
                image_count += 1
            images_paths_bash, video_paths = file_tools.get_video_paths(bag_path, images_paths)
            self.create_videos(images_paths_bash, video_paths)
            bag.close()

    def process_data_image(self, data, paths, count):
        try:
            image_cv = self.bridge.imgmsg_to_cv(data, "bgr8")
            image_np = numpy.asarray(image_cv)
            orig_path = paths["original"].format(count=count)
            cv2.imwrite(orig_path, image_np[:, :, 0], [cv2.IMWRITE_PNG_COMPRESSION, 0])
            bg_path = paths["background"].format(count=count)
            cv2.imwrite(bg_path, image_np[:, :, 1], [cv2.IMWRITE_PNG_COMPRESSION, 0])
            fg_path = paths["foreground"].format(count=count)
            cv2.imwrite(fg_path, image_np[:, :, 2], [cv2.IMWRITE_PNG_COMPRESSION, 0])
        except CvBridgeError, e:
            print(e)
class VideoDataProcessor(object):
    def __init__(self,overwrite=False):
        self.bridge = CvBridge()
        self.overwrite = overwrite
        self.data_image_topic = "/camera/data_image"
        self.fd = FindData(overwrite=overwrite)
        quality_low = "-preset veryfast -crf 28"
        quality_med = "-preset medium -crf 23"
        quality_high = "-preset veryslow -crf 16"
        self.quality = quality_high

    def find_and_process_data(self,directory):
        paths = self.find_data(directory)
        self.process_data(paths)

    def find_data(self,directory):
        return self.fd.find_video_data(directory)

    def process_data(self,bag_paths):
        for bag_path in bag_paths:
            images_paths = file_tools.create_images_paths_from_bag_path(bag_path,self.overwrite)
            if images_paths is None:
                continue
            print("Processing data in {0}".format(bag_path))
            image_count = 0
            bag = rosbag.Bag(bag_path)
            for topic, msg, t in bag.read_messages(topics=[self.data_image_topic]):
                self.process_data_image(msg,images_paths,image_count)
                image_count += 1
            images_paths_bash,video_paths = file_tools.get_video_paths(bag_path,images_paths)
            self.create_videos(images_paths_bash,video_paths)
            bag.close()

    def process_data_image(self,data,paths,count):
        try:
          image_cv = self.bridge.imgmsg_to_cv(data, "bgr8")
          image_np = numpy.asarray(image_cv)
          orig_path = paths['original'].format(count=count)
          cv2.imwrite(orig_path,image_np[:,:,0],[cv2.IMWRITE_PNG_COMPRESSION,0])
          bg_path = paths['background'].format(count=count)
          cv2.imwrite(bg_path,image_np[:,:,1],[cv2.IMWRITE_PNG_COMPRESSION,0])
          fg_path = paths['foreground'].format(count=count)
          cv2.imwrite(fg_path,image_np[:,:,2],[cv2.IMWRITE_PNG_COMPRESSION,0])
        except CvBridgeError, e:
          print(e)