MicrosoftDocs / azure-docs

Open source documentation of Microsoft Azure
https://docs.microsoft.com/azure
Creative Commons Attribution 4.0 International
10.26k stars 21.43k forks source link

Quickstart: Image Analysis 4.0 python on Linux #109605

Closed MaurusGubser closed 1 year ago

MaurusGubser commented 1 year ago

I tried out the Quickstart: Image Analysis 4.0 on an Ubuntu 22.04 machine. The script quickstart.py

import os
import azure.ai.vision as sdk

service_options = sdk.VisionServiceOptions(os.environ["VISION_ENDPOINT"],
                                           os.environ["VISION_KEY"])

vision_source = sdk.VisionSource(
    url="https://learn.microsoft.com/azure/cognitive-services/computer-vision/media/quickstarts/presentation.png")

analysis_options = sdk.ImageAnalysisOptions()

analysis_options.features = (
    sdk.ImageAnalysisFeature.CAPTION |
    sdk.ImageAnalysisFeature.TEXT
)

analysis_options.language = "en"

analysis_options.gender_neutral_caption = True

image_analyzer = sdk.ImageAnalyzer(service_options, vision_source, analysis_options)

result = image_analyzer.analyze()

if result.reason == sdk.ImageAnalysisResultReason.ANALYZED:

    if result.caption is not None:
        print(" Caption:")
        print("   '{}', Confidence {:.4f}".format(result.caption.content, result.caption.confidence))

    if result.text is not None:
        print(" Text:")
        for line in result.text.lines:
            points_string = "{" + ", ".join([str(int(point)) for point in line.bounding_polygon]) + "}"
            print("   Line: '{}', Bounding polygon {}".format(line.content, points_string))
            for word in line.words:
                points_string = "{" + ", ".join([str(int(point)) for point in word.bounding_polygon]) + "}"
                print("     Word: '{}', Bounding polygon {}, Confidence {:.4f}"
                      .format(word.content, points_string, word.confidence))

else:

    error_details = sdk.ImageAnalysisErrorDetails.from_result(result)
    print(" Analysis failed.")
    print("   Error reason: {}".format(error_details.reason))
    print("   Error code: {}".format(error_details.error_code))
    print("   Error message: {}".format(error_details.message))

fails with the error message:

 Analysis failed.
   Error reason: ImageAnalysisErrorReason.CONNECTION_FAILURE
   Error code: -1
   Error message: Unknown error in sending http request

I did set the endpoint and key variable correctly; the same script, with corresponding endpoint and key variable works on a windows 10 machine. Also, when working from the command line and using curl

curl -H "Ocp-Apim-Subscription-Key: <subscriptionKey>" -H "Content-Type: application/json" "https://<endpoint>/computervision/imageanalysis:analyze?features=caption,read&model-version=latest&language=en&api-version=2023-02-01-preview" -d "{'url':'https://learn.microsoft.com/azure/cognitive-services/computer-vision/media/quickstarts/presentation.png'}"

the microsoft cognitive service works on my Linux machine.

My setting:


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

YashikaTyagii commented 1 year ago

@MaurusGubser Thanks for your feedback! We will investigate and update as appropriate.

RamanathanChinnappan-MSFT commented 1 year ago

@MaurusGubser

It seems like there is a connection issue with the Azure service. You can try the following steps to troubleshoot the issue:

  1. Check if the endpoint and key variables are set correctly.
  2. Check if the machine has internet connectivity.
  3. Check if there are any firewalls or proxies blocking the connection.
  4. Try running the script on a different machine to see if the issue persists.
  5. If the issue persists, you can try using a different endpoint or key to see if that resolves the issue.

For an issue like this, I'd recommend you create a support ticket since the support team will be able to respond much more quickly and have a conversation with you to figure out what could be going on.

Naveenommi-MSFT commented 1 year ago

@MaurusGubser We are going to close this thread, if there are any further questions regarding the documentation, please tag me in your reply and we will be happy to continue the conversation.

MaurusGubser commented 1 year ago

@RamanathanChinnappan-MSFT Thanks for your answer and suggestions. So far, I still could not figure out what the exact problem is. However, I managed to run the quickstart.py script in a Debian 11 VM on my machine. I suspect your point 3 might be the problem, but so far, I did not manage to run the quickstart.py script on my machine. I will update this issue, when I find out more.

fsoft72 commented 1 year ago

@Naveenommi-MSFT

I have the same problem here. I was able to run the quickstart.py script by installing a complete Anaconda environment, so I don't think it is firewall or keys related. It is very strange...

YashikaTyagii commented 1 year ago

@MaurusGubser Thanks for your feedback. I suspect this could be a doc-bug. I am going to assign this case to content author who will update the document accordingly.

fsoft72 commented 1 year ago

@YashikaTyagii I start to suspect it has something to do with the azure-ai-vision library. I have removed the lib and wrote this (quick and dirty) code, and everything works:

def azure_image_desc(image_url):
    import requests

    url = f"{azure_endpoint}/computervision/imageanalysis:analyze?features=caption,read&model-version=latest&language=en&api-version=2023-02-01-preview"

    headers = {
        "Ocp-Apim-Subscription-Key": azure_subscription_key,
        "Content-Type": "application/json"
    }

    data = {
        "url": image_url
    }

    response = requests.post(url, headers=headers, json=data)

    if response.status_code == 200:
        data = response.json()
        return data['captionResult']['text']
    else:
        return response.text
fsoft72 commented 1 year ago

My Settings:

MaurusGubser commented 1 year ago

I second that. I wrote a similar script on my Ubuntu 22.04 machine using the request library, which works:

import json
import os
import requests
import numpy as np

from dotenv import load_dotenv

load_dotenv()

endpoint = os.getenv("VISION_ENDPOINT")
key = os.getenv("VISION_KEY")

headers = {
    # Request headers
    "Ocp-Apim-Subscription-Key": key,
    "Content-Type": "application/octet-stream",
}

features = "read"
model_version = "latest"
language = "en"
api_version = "2023-02-01-preview"

img_path = "/path/to/my/file.jpg"

with open(img_path, "rb") as img_stream:
    res = requests.post(
        url=f"{endpoint}computervision/imageanalysis:analyze?features={features}&model-version={model_version}&language={language}&api-version={api_version}",
        headers=headers,
        data=img_stream)

print(f"Status code {res.status_code}, reason: {res.reason}")

Also, as already written above, when I use curl from the command line, it also works.

MaurusGubser commented 1 year ago

@YashikaTyagii I did some other tests. On a HP Elite Desk (Intel Core i5-4590T CPU@2.00GHz) I installed a dual boot with Windows10 Pro and Ubuntu 22.04 LTS. On the Windows 10, the quickstart.py was running, on Ubuntu 22.04, I had the same troubles as on my Ubuntu machine.

deoxykev commented 1 year ago

Also have this issue (running on Amazon linux)

PatrickFarley commented 1 year ago

Thank you, we have an SDK installation doc coming soon that will provide more detail on which varieties of Linux can work with which SDKs

please-close

fengjihua commented 1 year ago

Also have this issue running on CentOS 7.9.2009 x86_64(Py3.8)

mPyKen commented 1 year ago

for those landing here, this should be the issue https://github.com/Azure-Samples/azure-ai-vision-sdk/issues/40#issuecomment-1638669858