Isai Perez
Isai Perez

Reputation: 11

IBM watson image recognition

Im new to Watson and as I'm taking a moog course. I was going through the examples i ran into issues and i cant figure it out. i keep getting error 403. any help would be greatly appreciated. thank you

import cv2
import urllib.request
from matplotlib import pyplot as plt
from matplotlib import rcParams
from ibm_watson import VisualRecognitionV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

my_apikey = 'mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm'

# Paste your API key for IBM Watson Visual Recognition below:
authenticator = IAMAuthenticator(my_apikey)
visrec = VisualRecognitionV3("2020-07-30", authenticator=authenticator)

def plt_image(image_url, size=(10, 8)):
# Downloads an image from a URL, and displays it in the notebook
    urllib.request.urlretrieve(image_url, "image.jpg")  # downloads file as "image.jpg"
    image = cv2.imread("image.jpg")

# If image is in color, then correct color coding from BGR to RGB
    if len(image.shape) == 3:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

rcParams['figure.figsize'] = size[0], size[1]  # set image display size

plt.axis("off")
plt.imshow(image, cmap="Greys_r")
plt.show()


from pandas.io.json import json_normalize


def getdf_visrec(url, apikey=my_apikey):
    json_result = visrec.classify(url=url, threshold='0.6', classifier_ids= 'E36').get_result()

    json_classes = json_result['images'][0]['classifiers'][0]['classes']

    df = json_normalize(json_classes).sort_values('score', ascending=False).reset_index(drop=True)

    return df






url = 'http://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/CV0101/Images/76011_MAIN._AC_SS190_V1446845310_.jpg'
plt_image(url)
getdf_visrec(url,my_apikey)

this is the error code that i get when i run this

ERROR:root:Forbidden
Traceback (most recent call last):
File "C:\Users\isaip\OneDrive\College\python pp\venv\lib\site-packages\ibm_cloud_sdk_core            \   base_service.py", line 229, in send
response.status_code, error_message, http_response=response)
ibm_cloud_sdk_core.api_exception.ApiException: Error: Forbidden, Code: 403
Traceback (most recent call last):
File "C:/Users/isaip/OneDrive/Desktop/.idea/pythonpy/watson.py", line 49, in <module>
getdf_visrec(url,my_apikey)
File "C:/Users/isaip/OneDrive/Desktop/.idea/pythonpy/watson.py", line 34, in getdf_visrec
json_result = visrec.classify(url=url, threshold='0.6', classifier_ids= 'E36').get_result()
File "C:\Users\isaip\OneDrive\College\python pp\venv\lib\site-packages\ibm_watson\  visual_recognition_v3.py", line 183, in classify
response = self.send(request)
File "C:\Users\isaip\OneDrive\College\python pp\venv\lib\site-packages\ibm_cloud_sdk_core\  base_service.py", line 229, in send
response.status_code, error_message, http_response=response)
ibm_cloud_sdk_core.api_exception.ApiException: Error: Forbidden, Code: 403

Process finished with exit code 1

Upvotes: 1

Views: 213

Answers (1)

chughts
chughts

Reputation: 4735

Getting a 403 on a Watson services is quite common. It occurs when the key does not match the endpoint, basically when you are using an incorrect key, incorrect endpoint or even both are incorrect.

Check the API documentation on how to set the url endpoint.

from ibm_watson import VisualRecognitionV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

authenticator = IAMAuthenticator('{apikey}')
visual_recognition = VisualRecognitionV3(
    version='{version}',
    authenticator=authenticator
)

visual_recognition.set_service_url('{url}')

Upvotes: 2

Related Questions