Ошибка редактирования изображений Google Cloud DLP API в Python с info_types

Здравствуйте, я пытаюсь использовать образец кода Google для редактирования информации из изображения в python, мне удалось проверить info_types в строке, но когда я пытаюсь использовать образец кода для проверки изображения в дистрибутиве Anaconda, я получаю следующую ошибку:

    ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-44-d8dba8a7c706> in <module>()
----> 1 redact_image('python-pdf-redaction', 'redaction.jpg', 'redaction_out.jpg',info_types, min_likelihood='LIKELY', mime_type=None)

<ipython-input-41-fc41518240a7> in redact_image(project, filename, output_filename, info_types, min_likelihood, mime_type)
     71         parent, inspect_config=inspect_config,
     72         image_redaction_configs=image_redaction_configs,
---> 73         byte_item=byte_item)
     74 
     75     # Write out the results.

~/anaconda3/lib/python3.6/site-packages/google/cloud/dlp_v2/gapic/dlp_service_client.py in redact_image(self, parent, inspect_config, image_redaction_configs, include_findings, byte_item, retry, timeout, metadata)
    431             image_redaction_configs=image_redaction_configs,
    432             include_findings=include_findings,
--> 433             byte_item=byte_item,
    434         )
    435         return self._inner_api_calls["redact_image"](

TypeError: {'name': 'FIRST_NAME'} has type dict, but expected one of: bytes, unicode

Вот мой код:

import mimetypes

    def redact_image(project, filename, output_filename,
                     info_types, min_likelihood=None, mime_type=None):
        """Uses the Data Loss Prevention API to redact protected data       in an image.
        Args:
            project: The Google Cloud project id to use as a parent resource.  
            filename: The path to the file to inspect.
            output_filename: The path to which the redacted image will be written.
            info_types: A list of strings representing info types to look for.
                A full list of info type categories can be fetched from the API.
            min_likelihood: A string representing the minimum likelihood threshold
                that constitutes a match. One of: 'LIKELIHOOD_UNSPECIFIED',
                'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE', 'LIKELY', 'VERY_LIKELY'.
            mime_type: The MIME type of the file. If not specified, the type is
                inferred via the Python standard library's mimetypes module.
        Returns:
            None; the response from the API is printed to the terminal.
        """
        # Import the client library
        import google.cloud.dlp

        # Instantiate a client.
        dlp = google.cloud.dlp.DlpServiceClient()

        # Prepare info_types by converting the list of strings into a list of
        # dictionaries (protos are also accepted).
        info_types = [{'name': info_type} for info_type in info_types]

        # Prepare image_redaction_configs, a list of dictionaries. Each dictionary
        # contains an info_type and optionally the color used for the replacement.
        # The color is omitted in this sample, so the default (black) will be used.
        image_redaction_configs = []

        if info_types is not None:
            for info_type in info_types:
                image_redaction_configs.append({'info_type': info_type})

        # Construct the configuration dictionary. Keys which are None may
        # optionally be omitted entirely.
        inspect_config = {
            'min_likelihood': min_likelihood,
            'info_types': info_types,
        }

        # If mime_type is not specified, guess it from the filename.
        if mime_type is None:
            mime_guess = mimetypes.MimeTypes().guess_type(filename)
            mime_type = mime_guess[0] or 'application/octet-stream'

        # Select the content type index from the list of supported types.
        supported_content_types = {
            None: 1,  # "Unspecified"
            'image/jpeg': 1,
            'image/bmp': 2,
            'image/png': 3,
            'image/svg': 4,
            'text/plain': 5,
        }
        content_type_index = supported_content_types.get(mime_type, 0)

        # Construct the byte_item, containing the file's byte data.
        with open(filename, mode='rb') as f:
            byte_item = {'type': content_type_index, 'data': f.read()}

        # Convert the project id into a full resource id.
        parent = dlp.project_path(project)

        # Call the API.
        response = dlp.redact_image(
            parent, inspect_config=inspect_config,
            image_redaction_configs=image_redaction_configs,
            byte_item=byte_item)

        # Write out the results.
        with open(output_filename, mode='wb') as f:
            f.write(response.redacted_image)
        print("Wrote {byte_count} to {filename}".format(
            byte_count=len(response.redacted_image), filename=output_filename))

redact_image('python-pdf-redaction', 'redaction.jpg', 'redaction_out.jpg',info_types, min_likelihood='LIKELY', mime_type=None)

Я не уверен, нужно ли мне менять тип данных или что-то еще, я не нашел ссылки на эту проблему. Ваш ответ будет очень признателен.

РЕДАКТИРОВАТЬ: я решил свою ошибку, мне пришлось составить список info_types, чтобы указать, какие info_types я хочу отредактировать. т.е. info_types = ['FIRST_NAME', 'LAST_NAME', 'EMAIL_ADDRESS']


person Sarangsinh Gujar    schedule 12.03.2019    source источник


Ответы (1)


Одна вещь, которую необходимо улучшить в примере кода:

byte_item = {'тип': content_type_index, 'данные': f.read ()}

можно изменить на

byte_item = {'тип': 'ИЗОБРАЖЕНИЕ', 'данные': f.read ()}

а затем вы можете избавиться от этого mimetype foo, если знаете, что отправляете только поддерживаемые типы изображений.

person Jordanna Chord    schedule 12.03.2019
comment
мы можем это сделать, но разве не рекомендуется проверять тип файла перед его обработкой? - person Sarangsinh Gujar; 13.03.2019
comment
Зависит от того, контролируете ли вы входящие данные или нет - person Jordanna Chord; 14.03.2019
comment
Правда, это также уменьшило бы сложность. Спасибо - person Sarangsinh Gujar; 14.03.2019