Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python将本地图像而不是URL发送到Microsoft认知视觉API(分析图像)?_Python_Microsoft Cognitive_Vision Api - Fatal编程技术网

如何使用Python将本地图像而不是URL发送到Microsoft认知视觉API(分析图像)?

如何使用Python将本地图像而不是URL发送到Microsoft认知视觉API(分析图像)?,python,microsoft-cognitive,vision-api,Python,Microsoft Cognitive,Vision Api,我正在尝试使用Microsoft认知服务的Vision API(分析图像)。我想知道如何通过restapi调用向visionapi发送本地图像,并使用Python请求结果。有人能帮我吗 Microsoft在其网站上提供的测试选项仅获取URL,我尝试将本地路径转换为URL并将其作为输入,但没有成功。您可以在此处查看完整代码: 但其要点是: import requests # pip3 install requests region = "YOUR-API-REGION" #For example

我正在尝试使用Microsoft认知服务的Vision API(分析图像)。我想知道如何通过restapi调用向visionapi发送本地图像,并使用Python请求结果。有人能帮我吗


Microsoft在其网站上提供的测试选项仅获取URL,我尝试将本地路径转换为URL并将其作为输入,但没有成功。

您可以在此处查看完整代码:

但其要点是:

import requests # pip3 install requests

region = "YOUR-API-REGION" #For example, "westus"
api_key = "YOUR-API-KEY"
path_to_file = "C:/Users/mparnisari/Desktop/test.jpg"

# Read file
with open(path_to_file, 'rb') as f:
    data = f.read()

# Set request headers
headers = dict()
headers['Ocp-Apim-Subscription-Key'] = api_key
headers['Content-Type'] = 'application/octet-stream'

# Set request querystring parameters
params = {'visualFeatures': 'Color,Categories,Tags,Description,ImageType,Faces,Adult'}

# Make request and process response
response = requests.request('post', "https://{}.api.cognitive.microsoft.com/vision/v1.0/analyze".format(region), data=data, headers=headers, params=params)

if response.status_code == 200 or response.status_code == 201:

    if 'content-length' in response.headers and int(response.headers['content-length']) == 0:
        result = None
    elif 'content-type' in response.headers and isinstance(response.headers['content-type'], str):
        if 'application/json' in response.headers['content-type'].lower():
            result = response.json() if response.content else None
        elif 'image' in response.headers['content-type'].lower():
            result = response.content

        print(result)
else:

    print("Error code: %d" % response.status_code)
    print("Message: %s" % response.json())
这将打印如下内容:

{
    'categories': [{
        'name': 'others_',
        'score': 0.0078125
    }, {
        'name': 'outdoor_',
        'score': 0.0078125
    }, {
        'name': 'people_',
        'score': 0.4140625
    }],
    'adult': {
        'isAdultContent': False,
        'isRacyContent': False,
        'adultScore': 0.022686801850795746,
        'racyScore': 0.016844550147652626
    },
    'tags': [{
        'name': 'outdoor',
        'confidence': 0.9997920393943787
    }, {
        'name': 'sky',
        'confidence': 0.9985970854759216
    }, {
        'name': 'person',
        'confidence': 0.997259259223938
    }, {
        'name': 'woman',
        'confidence': 0.944902777671814
    }, {
        'name': 'posing',
        'confidence': 0.8417303562164307
    }, {
        'name': 'day',
        'confidence': 0.2061375379562378
    }],
    'description': {
        'tags': ['outdoor', 'person', 'woman', 'snow', 'posing', 'standing', 'skiing', 'holding', 'lady', 'photo', 'smiling', 'top', 'wearing', 'girl', 'mountain', 'sitting', 'young', 'people', 'sun', 'slope', 'hill', 'man', 'covered', 'umbrella', 'red', 'white'],
        'captions': [{
            'text': 'a woman posing for a picture',
            'confidence': 0.9654204679303702
        }]
    },
    'metadata': {
        'width': 3264,
        'height': 1836,
        'format': 'Jpeg'
    },
    'faces': [{
        'age': 26,
        'gender': 'Female',
        'faceRectangle': {
            'left': 597,
            'top': 2151,
            'width': 780,
            'height': 780
        }
    }],
    'color': {
        'dominantColorForeground': 'White',
        'dominantColorBackground': 'White',
        'dominantColors': ['White', 'Grey'],
        'accentColor': '486E83',
        'isBWImg': False
    },
    'imageType': {
        'clipArtType': 0,
        'lineDrawingType': 0
    }
}

您的代码显示了以下错误:我尝试使用Python2.7,它运行良好。您知道如何在java中执行相同的操作吗?