Computer Vision api not working

sumeet kotgire 1 Reputation point
2020-10-02T12:26:28.307+00:00

import os
import sys
import requests

If you are using a Jupyter notebook, uncomment the following line.

%matplotlib inline

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from PIL import Image
from io import BytesIO

Add your Computer Vision subscription key and endpoint to your environment variables.

if '-' in os.environ:
subscription_key = os.environ['-']
else:
print("\nSet the COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.\nRestart your shell or IDE for changes to take effect.")
sys.exit()

if 'https://visionapic.cognitiveservices.azure.com/' in os.environ:
endpoint = os.environ['https://visionapic.cognitiveservices.azure.com/']

ocr_url = endpoint + "vision/v3.0/ocr"

Set image_url to the URL of an image that you want to analyze.

image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Atomist_quote_from_Democritus.png/338px-Atomist_quote_from_Democritus.png"

headers = {'Ocp-Apim-Subscription-Key': subscription_key}
params = {'language': 'unk', 'detectOrientation': 'true'}
data = {'url': image_url}
response = requests.post(ocr_url, headers=headers, params=params, json=data)
response.raise_for_status()

analysis = response.json()

Extract the word bounding boxes and text.

line_infos = [region["lines"] for region in analysis["regions"]]
word_infos = []
for line in line_infos:
for word_metadata in line:
for word_info in word_metadata["words"]:
word_infos.append(word_info)
word_infos

Display the image and overlay it with the extracted text.

plt.figure(figsize=(5, 5))
image = Image.open(BytesIO(requests.get(image_url).content))
ax = plt.imshow(image, alpha=0.5)
for word in word_infos:
bbox = [int(num) for num in word["boundingBox"].split(",")]
text = word["text"]
origin = (bbox[0], bbox[1])
patch = Rectangle(origin, bbox[2], bbox[3],
fill=False, linewidth=2, color='y')
ax.axes.add_patch(patch)
plt.text(origin[0], origin[1], text, fontsize=20, weight="bold", va="top")
plt.show()
plt.axis("off")

i put key still i get error like
Set the COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.
Restart your shell or IDE for changes to take effect.

Azure Computer Vision
Azure Computer Vision
An Azure artificial intelligence service that analyzes content in images and video.
338 questions
{count} votes

1 answer

Sort by: Most helpful
  1. YutongTie-MSFT 48,001 Reputation points
    2020-11-03T22:51:38.677+00:00

    Hello Sumeet,

    Hope you have solved your issue. The code depends on your system as following:

    For Mac OS, you should edit your .bash_profile, and add the environment variable:

    export COGNITIVE_SERVICE_KEY=your-key
    

    After you add the environment variable, run source ~/.bash_profile from your console window to make the changes effective.

    For Linux,

    export COGNITIVE_SERVICE_KEY=your-key
    

    After you add the environment variable, run source ~/.bashrc from your console window to make the changes effective.

    For Windows, you should run the code I mentioned:

    setx COGNITIVE_SERVICE_KEY "your-key"
    

    After you add the environment variable, you may need to restart any running programs that will need to read the environment variable, including the console window. For example, if you are using Visual Studio as your editor, restart Visual Studio before running the example.

    Thanks a lot,
    Yutong

    0 comments No comments