Google Cloud DLP API映像在python错误中使用info_types中的错误



您好,我正在尝试从Python中的图像中编辑信息的示例代码在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']

代码样本需要改进的一件事:

byte_item = {'type':content_type_index,'data':f.read()}

可以更改为

byte_item = {'type':'image','data':f.read()}

,然后假设您知道自己只发送支持的图像类型,则可以摆脱模拟型FOO。

相关内容

  • 没有找到相关文章

最新更新