Пример #1
0
    def test_convert_from_storage(self):
        """ Convert from storage example test """

        config = self.imaging_api.api_client.configuration
        imaging_api = ImagingApi(config.api_key['client_secret'],
                                 config.api_key['client_id'], config.host,
                                 config.api_version)

        try:
            # upload local image to storage
            result = imaging_api.upload_file(
                requests.UploadFileRequest(
                    'ExampleFolderPython/inputImage.png',
                    os.path.join(self._local_test_folder, 'test.png'),
                    storage_name=self.test_storage))

            # inspect result.Errors list if there were any
            # inspect result.Uploaded list for uploaded file names

            # convert image from storage to JPEG
            converted_image = imaging_api.convert_image(
                requests.ConvertImageRequest('inputImage.png',
                                             'jpg',
                                             'ExampleFolderPython',
                                             storage=self.test_storage))
            # process resulting image
            # for example, save it to storage

            result = imaging_api.upload_file(
                requests.UploadFileRequest(
                    'ExampleFolderPython/resultImage.jpg',
                    converted_image,
                    storage_name=self.test_storage))

            # inspect result.Errors list if there were any
            # inspect result.Uploaded list for uploaded file names

        finally:
            # remove files from storage
            imaging_api.delete_file(
                requests.DeleteFileRequest(
                    'ExampleFolderPython/inputImage.png',
                    storage_name=self.test_storage))
            imaging_api.delete_file(
                requests.DeleteFileRequest(
                    'ExampleFolderPython/resultImage.jpg',
                    storage_name=self.test_storage))
 def _upload_image_to_cloud(self, image_name, image):
     """Uploads the image to cloud"""
     upload_file_request = requests.UploadFileRequest(os.path.join(ImagingBase.CLOUD_PATH, image_name), image)
     result = self._imaging_api.upload_file(upload_file_request)
     if result.errors:
         print('Uploading errors count: ' + str(len(result.errors)))
     else:
         print('Image ' + image_name + ' is uploaded to cloud storage')
    def search_image_from_web_source(self):
        """Finds the similar images from the URL source"""
        print('Finds similar images from URL:')

        similarity_threshold = 30.0
        max_count = 3
        folder = ImagingAiBase.CLOUD_PATH  # Folder with image to process
        storage = None  # We are using default Cloud Storage

        # Add images from the website to the search context
        image_source_url = urllib.quote_plus(
            'https://www.f1news.ru/interview/hamilton/140909.shtml')
        self._imaging_api.create_web_site_image_features(
            requests.CreateWebSiteImageFeaturesRequest(self._search_context_id,
                                                       image_source_url,
                                                       folder, storage))

        self._wait_idle(self._search_context_id)

        # Download the image from the website
        image_data = req.get(
            'https://cdn.f1ne.ws/userfiles/hamilton/140909.jpg')
        path = os.path.abspath(
            os.path.join(ImagingAiBase.OUTPUT_FOLDER, 'WebSearchSample.jpg'))
        with open(path, "wb") as f:
            f.write(image_data.content)

        # Resize downloaded image to demonstrate search engine capabilities
        resized_image = self._imaging_api.create_resized_image(
            requests.CreateResizedImageRequest(path,
                                               600,
                                               400,
                                               "jpg",
                                               storage=storage))

        # Upload image to cloud
        image_name = 'ReverseSearch.jpg'
        self._imaging_api.upload_file(
            requests.UploadFileRequest(
                ImagingAiBase.CLOUD_PATH + "/" + image_name, resized_image,
                storage))

        # Find similar images in the search context
        find_response = self._imaging_api.find_similar_images(
            requests.FindSimilarImagesRequest(
                self._search_context_id,
                similarity_threshold,
                max_count,
                image_id=ImagingAiBase.CLOUD_PATH + "/" + image_name,
                folder=folder,
                storage=storage))

        print('Similar images found: ' + str(len(find_response.results)))
        print('Similar image id: ' + find_response.results[0].image_id)
        print('Similarity: ' + str(find_response.results[0].similarity))
    def _upload_image_to_cloud(self, image_name, sub_folder=None):
        """Uploads the image to cloud"""
        folder = os.path.join(ImagingAiBase.EXAMPLE_IMAGES_FOLDER, sub_folder) if sub_folder else \
            ImagingAiBase.EXAMPLE_IMAGES_FOLDER

        local_input_image = os.path.join(folder, image_name)
        request = requests.UploadFileRequest(
            os.path.join(ImagingAiBase.CLOUD_PATH, image_name),
            local_input_image)
        self._imaging_api.upload_file(request)

        print("Image {0} was uploaded to cloud storage".format(image_name))
    def test_upload_file(self):
        folder = self.temp_folder + '/Storage'
        file = 'Storage.txt'

        try:
            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)

            original_file = self.imaging_api.download_file(
                requests.DownloadFileRequest(
                    self.original_data_folder + '/' + file,
                    self.test_storage))

            result = self.imaging_api.upload_file(
                requests.UploadFileRequest(
                    folder + '/' + file,
                    original_file,
                    self.test_storage))
            self.assertIsNotNone(result)
            self.assertTrue(not result.errors or len(result) == 0)
            self.assertIsNotNone(result.uploaded)
            self.assertEqual(1, len(result.uploaded))
            self.assertEqual(file, result.uploaded[0])

            exist_response = self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder + '/' + file, self.test_storage))
            self.assertIsNotNone(exist_response)
            self.assertFalse(exist_response.is_folder)
            self.assertTrue(exist_response.exists)

            original_file_info = next(
                x for x in self.imaging_api.get_files_list(
                    requests.GetFilesListRequest(
                        self.original_data_folder,
                        self.test_storage)).value if x.name == file)

            uploaded_file_info = next(
                x for x in self.imaging_api.get_files_list(
                    requests.GetFilesListRequest(
                        folder, self.test_storage)).value if x.name == file)
            self.assertEqual(original_file_info.size, uploaded_file_info.size)

            original_file = self.imaging_api.download_file(
                requests.DownloadFileRequest(
                    self.original_data_folder + '/' + file,
                    self.test_storage))

            uploaded_file = self.imaging_api.download_file(
                requests.DownloadFileRequest(
                    folder + '/' + file, self.test_storage))

            with open(original_file, 'r') as original_reader, open(uploaded_file, 'r') as uploaded_reader:
                original_string = original_reader.read()
                copied_string = uploaded_reader.read()
                self.assertEqual(len(original_string), len(copied_string))
                self.assertEqual(len(original_string), original_file_info.size)
                self.assertEqual(original_string, copied_string)
                self.assertEqual(
                    original_string,
                    original_file_info.path.strip('/'))
                self.assertEqual(
                    original_string.replace(
                        self.original_data_folder,
                        folder),
                    uploaded_file_info.path.strip('/'))

        finally:
            self.imaging_api.delete_file(
                requests.DeleteFileRequest(
                    folder + '/' + file,
                    self.test_storage))
            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder + '/' + file,
                        self.test_storage)).exists)

            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)