当前位置: 首页>>代码示例>>Python>>正文


Python Preprocessor.preprocess方法代码示例

本文整理汇总了Python中preprocessor.Preprocessor.preprocess方法的典型用法代码示例。如果您正苦于以下问题:Python Preprocessor.preprocess方法的具体用法?Python Preprocessor.preprocess怎么用?Python Preprocessor.preprocess使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在preprocessor.Preprocessor的用法示例。


在下文中一共展示了Preprocessor.preprocess方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestPreprocessor

# 需要导入模块: from preprocessor import Preprocessor [as 别名]
# 或者: from preprocessor.Preprocessor import preprocess [as 别名]
class TestPreprocessor(unittest.TestCase):
    def setUp(self):
        self.mock_metadata_helper = MagicMock(spec=MetadataHelper)

        self.mock_image_open_patcher = patch('preprocessor.Image.open')
        self.mock_image_open = self.mock_image_open_patcher.start()

        self.mock_image = MagicMock()
        self.mock_exif_data = 'a bunch of exif data'
        self.mock_image.info = {
            'exif': self.mock_exif_data
        }
        self.mock_image_open.return_value = self.mock_image

        self.mock_first_transposed_image = MagicMock()
        self.mock_image.transpose.return_value = self.mock_first_transposed_image

        self.mock_second_transposed_image = MagicMock()
        self.mock_first_transposed_image.transpose.return_value = self.mock_second_transposed_image

        self.test_model = Preprocessor(self.mock_metadata_helper)

    def tearDown(self):
        self.mock_image_open_patcher.stop()

    def test_it_should_ignore_video_files(self):
        self.test_model.preprocess('/root/staging/USB/video.mp4')
        self.mock_image_open.assert_not_called()

    def test_it_should_not_touch_images_with_rotation_of_0(self):
        self.mock_metadata_helper.get_rotation.return_value = 0
        self.test_model.preprocess('/root/staging/USB/image.jpg')
        self.mock_image.transpose.assert_not_called()
        self.mock_image.save.assert_called_once_with('/root/staging/USB/image.jpg', exif=self.mock_exif_data)

    def test_it_should_not_touch_images_with_rotation_of_1(self):
        self.mock_metadata_helper.get_rotation.return_value = 1
        self.test_model.preprocess('/root/staging/USB/image.jpg')
        self.mock_image.transpose.assert_not_called()
        self.mock_image.save.assert_called_once_with('/root/staging/USB/image.jpg', exif=self.mock_exif_data)

    def test_it_should_flip_horizontally_an_image_with_a_rotation_of_2(self):
        self.mock_metadata_helper.get_rotation.return_value = 2
        self.test_model.preprocess('/root/staging/USB/image.jpg')
        self.mock_image.transpose.assert_called_once_with(Image.FLIP_LEFT_RIGHT)
        self.mock_first_transposed_image.save.assert_called_once_with('/root/staging/USB/image.jpg',
                                                                      exif=self.mock_exif_data)

    def test_it_should_rotate_by_180_an_image_with_a_rotation_of_3(self):
        self.mock_metadata_helper.get_rotation.return_value = 3
        self.test_model.preprocess('/root/staging/USB/image.jpg')
        self.mock_image.transpose.assert_called_once_with(Image.ROTATE_180)
        self.mock_first_transposed_image.save.assert_called_once_with('/root/staging/USB/image.jpg',
                                                                      exif=self.mock_exif_data)

    def test_it_should_flip_vertically_an_image_with_a_rotation_of_4(self):
        self.mock_metadata_helper.get_rotation.return_value = 4
        self.test_model.preprocess('/root/staging/USB/image.jpg')
        self.mock_image.transpose.assert_called_once_with(Image.FLIP_TOP_BOTTOM)
        self.mock_first_transposed_image.save.assert_called_once_with('/root/staging/USB/image.jpg',
                                                                      exif=self.mock_exif_data)

    def test_it_should_rotate_by_270_and_flip_horizontally_an_image_with_a_rotation_of_5(self):
        self.mock_metadata_helper.get_rotation.return_value = 5
        self.test_model.preprocess('/root/staging/USB/image.jpg')
        self.mock_image.transpose.assert_called_once_with(Image.FLIP_LEFT_RIGHT)
        self.mock_first_transposed_image.transpose.assert_called_once_with(Image.ROTATE_90)
        self.mock_second_transposed_image.save.assert_called_once_with('/root/staging/USB/image.jpg',
                                                                       exif=self.mock_exif_data)

    def test_it_should_rotate_by_270_an_image_with_a_rotation_of_6(self):
        self.mock_metadata_helper.get_rotation.return_value = 6
        self.test_model.preprocess('/root/staging/USB/image.jpg')
        self.mock_image.transpose.assert_called_once_with(Image.ROTATE_270)
        self.mock_first_transposed_image.save.assert_called_once_with('/root/staging/USB/image.jpg',
                                                                      exif=self.mock_exif_data)

    def test_it_should_rotate_by_90_and_flip_horizontally_an_image_with_a_rotation_of_7(self):
        self.mock_metadata_helper.get_rotation.return_value = 7
        self.test_model.preprocess('/root/staging/USB/image.jpg')
        self.mock_image.transpose.assert_called_once_with(Image.ROTATE_90)
        self.mock_first_transposed_image.transpose.assert_called_once_with(Image.FLIP_LEFT_RIGHT)
        self.mock_second_transposed_image.save.assert_called_once_with('/root/staging/USB/image.jpg',
                                                                       exif=self.mock_exif_data)

    def test_it_should_rotate_by_90_an_image_with_a_rotation_of_8(self):
        self.mock_metadata_helper.get_rotation.return_value = 8
        self.test_model.preprocess('/root/staging/USB/image.jpg')
        self.mock_image.transpose.assert_called_once_with(Image.ROTATE_90)
        self.mock_first_transposed_image.save.assert_called_once_with('/root/staging/USB/image.jpg',
                                                                      exif=self.mock_exif_data)

    def test_it_should_set_the_orientation_to_1_when_done(self):
        self.test_model.preprocess('/root/staging/USB/image.jpg')
        self.mock_metadata_helper.set_rotation.assert_called_once_with('/root/staging/USB/image.jpg', 1)

    def test_it_should_not_do_anything_if_the_image_is_missing_exif_data(self):
        self.mock_image.info = {}
        self.test_model.preprocess('/root/staging/USB/image.jpg')
        self.mock_metadata_helper.get_rotation.assert_not_called()
开发者ID:drautb,项目名称:haystack,代码行数:102,代码来源:test_preprocessor.py

示例2: Bird

# 需要导入模块: from preprocessor import Preprocessor [as 别名]
# 或者: from preprocessor.Preprocessor import preprocess [as 别名]
class Bird(object):

    # label_path: path to label file which holds the path of all samples and their class id
    def __init__(self, label_path,label_bg_path, meta_path, training_description):
        self.label_path = label_path
        self.label_bg_path = label_bg_path
        self.meta_path = meta_path
        self.training_description = training_description
        
        if not os.path.isdir(self.training_description):
            os.mkdir(self.training_description)

        self.batch_size = 64
        self.queue_size = 2048

        self.nr_epoch = 10
        self.preprocessor = Preprocessor(10)
        self.augmenter = AugmentTransform(10, 10)
        self.inverse_labels = {}
        self.inverse_labels_bg = {}

        self.train_val_ratio = 0.1
        
    # loads all sample paths and their id into memory. 
    # builds up an inverse lookup structure to augment
    # samples with the same class. 
    def load_labels(self, label_path, label_bg_path):
        f = open(label_path, "r")
        f_bg = open(label_bg_path, "r")
        paths = []
        labels = []

        self.inverse_labels = {}
        self.inverse_labels_bg = {}
        
        for line in f:
            line = line.strip()
            (path, label) = line.split(" ")
            paths.append(path)
            labels.append(label)
            self.inverse_labels.setdefault(label, []).append(path)

        self.nb_species = len(self.inverse_labels)
        print(self.nb_species)
        
        label_bg_paths = []
        for line in f_bg:
            line = line.strip()
            (path, label) = line.split(" ")
 
            self.inverse_labels_bg.setdefault(label, []).append(path)
            label_bg_paths.append(path)

        self.augmenter.configure_same_class_augmentation(self.inverse_labels,label_bg_paths,self.preprocessor,samples_to_add=[1, 2])
        
        return (paths, labels)

    def load_meta_data(self, meta_path):
        with  open(meta_path, "r") as meta:
            first_line=meta.readline()
            first_line = first_line.strip()
            first_line_split = first_line.split(" ")
            nb_f_steps = first_line_split[2]
            nb_t_steps = first_line_split[3]
        self.nb_f_steps = 128 #  int(nb_f_steps)
        self.nb_t_steps = 256 #  int(nb_t_steps)
        
        
    # loads and randomizes data
    def load_data(self):
        (paths, labels) = self.load_labels(self.label_path,self.label_bg_path)
        self.load_meta_data(self.meta_path)
        
        nr_files = len(paths)

        mask = np.arange(nr_files)

        np.random.shuffle(mask)

        train_size = int(nr_files * (1 - self.train_val_ratio))

        paths = np.array(paths)[mask]
        labels = np.array(labels)[mask]

        self.class_weights = {}
        for i in range(self.nb_species):
            weight_mask = labels == str(i)  #  np.equal(labels, i*np.ones(labels.shape))
            nb_class = np.sum(weight_mask)
            if nb_class == 0:
                print("No data for class", str(i))
                continue
            self.class_weights[i] = nr_files/np.sum(weight_mask)

        self.paths = paths[:train_size]
        self.labels = labels[:train_size]
        self.nr_files = train_size

        self.val_paths = paths[train_size:]
        self.val_labels = labels[train_size:]
        self.nr_val_files = (nr_files - train_size) // self.batch_size * self.batch_size
#.........这里部分代码省略.........
开发者ID:FluxB,项目名称:BirdSongClassifier,代码行数:103,代码来源:train.py


注:本文中的preprocessor.Preprocessor.preprocess方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。