fast.ai seach_images_bing throwing PermissionDenied error

在colab上运行fastbook chapter2中运行如下代码时报错

源代码

!pip install -Uqq fastbook
import fastbook
fastbook.setup_book()

from fastbook import *
from fastai.vision.widgets import *

key = os.environ.get('AZURE_SEARCH_KEY', 'XXX') # not showing my key :)
results = search_images_bing(key, 'grizzly bear', min_sz=128)
ims = results.attrgot('content_url')

错误信息:

---------------------------------------------------------------------------
ErrorResponseException                    Traceback (most recent call last)
<ipython-input-107-8c0a1d6b3765> in <module>
----> 1 results = search_images_bing(key, 'grizzly bear', min_sz=128)
      2 ims = results.attrgot('content_url')
      3 
      4 # ims = search_bing_by_term('grizzly bear', 100)
      5 len(ims)

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastbook/__init__.py in search_images_bing(key, term, min_sz)
     50 def search_images_bing(key, term, min_sz=128):
     51     client = api('https://api.cognitive.microsoft.com', auth(key))
---> 52     return L(client.images.search(query=term, count=150, min_height=min_sz, min_width=min_sz).value)
     53 
     54 def plot_function(f, tx=None, ty=None, title=None, min=-2, max=2, figsize=(6,4)):

/opt/conda/envs/fastai/lib/python3.8/site-packages/azure/cognitiveservices/search/imagesearch/operations/_images_operations.py in search(self, query, accept_language, user_agent, client_id, client_ip, location, aspect, color, country_code, count, freshness, height, id, image_content, image_type, license, market, max_file_size, max_height, max_width, min_file_size, min_height, min_width, offset, safe_search, size, set_lang, width, custom_headers, raw, **operation_config)
    489 
    490         if response.status_code not in [200]:
--> 491             raise models.ErrorResponseException(self._deserialize, response)
    492 
    493         deserialized = None

ErrorResponseException: Operation returned an invalid status code 'PermissionDenied'

原因:

查询bing search api,可以看到content_url在新版api中变为contentUrl

更改后代码

!pip install -Uqq fastbook
import fastbook
fastbook.setup_book()

from fastbook import *
from fastai.vision.widgets import *

key = os.environ.get('AZURE_SEARCH_KEY', 'XXX') # not showing my key :)
results = search_images_bing(key, 'grizzly bear', min_sz=128)
ims = results.attrgot('contentUrl') #with changed api

可以正常运行

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值