jupyter-notebook - 在 Watson Studio 的 Jupyter Notebook 中,如何引用上传到 "assets"的文件?

标签 jupyter-notebook watson-studio

在 Watson Studio 中,我正在 Jupyter Notebook 中编写代码以使用 Watson Visual Recognition 自定义模型。 它适用于外部图像。 我还无法引用已上传到项目 Assets 的图像。 Assets 的 url 会显示整页,而不仅仅是图像: https://dataplatform.ibm.com/projects/2f4b89d9-b93a-4c98-a327-9b863a467b7c/data-assets/ed16c385-e09e-4bcb-bfab-67ee864538e4/?context=data

谢谢

最佳答案

在 Watson 中, Assets 文件保存在云对象存储 (COS) 中。您必须将镜像从 COS 下载到笔记本服务器文件系统,然后您可以将笔记本中的文件作为常规本地文件引用。

我使用cos api来获取文件。 https://github.com/IBM/ibm-cos-sdk-python .

https://console.bluemix.net/docs/services/cloud-object-storage/libraries/python.html#using-python

首先,了解您的凭据是什么

  1. 突出显示笔记本单元,
  2. 单击“数据”菜单,
  3. 选择文件,
  4. “插入代码”
  5. 凭据。

然后您可以使用 API 将文件下载到本地磁盘存储。例如,从 COS 下载文件:

# The following code contains the credentials for a file in your IBM Cloud Object Storage. 
# You might want to remove those credentials before you share your notebook.
credentials_1 = {
    'IBM_API_KEY_ID': '**************************************',
    'IAM_SERVICE_ID': 'iam-ServiceId-**************************',
    'ENDPOINT': 'https://s3-api.us-geo.objectstorage.service.networklayer.com',
    'IBM_AUTH_ENDPOINT': 'https://iam.ng.bluemix.net/oidc/token',
    'BUCKET': '********************************',
    'FILE': 'file.xlsx'
}

from ibm_botocore.client import Config
import ibm_boto3
def download_file_cos(credentials, local_file_name, key):  
    cos = ibm_boto3.client(service_name='s3',
    ibm_api_key_id=credentials['IBM_API_KEY_ID'],
    ibm_service_instance_id=credentials['IAM_SERVICE_ID'],
    ibm_auth_endpoint=credentials['IBM_AUTH_ENDPOINT'],
    config=Config(signature_version='oauth'),
    endpoint_url=credentials['ENDPOINT'])
    try:
        res=cos.download_file(Bucket=credentials['BUCKET'], Key=key, Filename=local_file_name)
    except Exception as e:
        print(Exception, e)
    else:
        print("Dowloaded:", key, 'from IBM COS to local:', local_file_name)

在笔记本单元格列表目录内容中:

%%script bash
ls -l


# to list all .png files in COS you can use a function like this:        
def list_objects(credentials):  
    cos = ibm_boto3.client(service_name='s3',
    ibm_api_key_id=credentials['IBM_API_KEY_ID'],
    ibm_service_instance_id=credentials['IAM_SERVICE_ID'],
    ibm_auth_endpoint=credentials['IBM_AUTH_ENDPOINT'],
    config=Config(signature_version='oauth'),
    endpoint_url=credentials['ENDPOINT'])
    return cos.list_objects(Bucket=credentials['BUCKET'])

response = list_objects(credentials_1)
for c in response['Contents']:
    if c['Key'].endswith('.png'):
        print(c['Key'], "last modified:", c['LastModified'])

关于jupyter-notebook - 在 Watson Studio 的 Jupyter Notebook 中,如何引用上传到 "assets"的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49881417/

相关文章:

python - JupyterLab 中的 pdb 未进入交互模式

apache-spark - 如何防止每次运行整个笔记本时运行 'pip install ...'?

r - 无法在 IBM Watson Studio R 环境笔记本上安装 CRAN 软件包 "viridis"、 "Hmisc"

machine-learning - 如何使用 Watson Studio 和机器学习部署 scikit learn python 模型?

python - 使用 python ibm-cos-sdk get 创建对象不起作用

python - 使用 slider 小部件以交互方式更改图像内容

python - 如何编写脚本 bash 以在 jupyter-notebook 中输入多行输入?

python - Google Colab 在处理包含大量文件的云端硬盘文件夹时遇到问题

hyperlink - 跨越多个 Jupyter 笔记本的目录

java - 如何修复 java 中的 http 响应代码 400 错误?是否存在格式错误的请求语法或无效的请求消息框架?