[562]微软的Computer Vision API获得图片描述-Python

注册账号:https://www.microsoft.com/cognitive-services/en-us/sign-up,获得一个免费key,免费key限制每月5,000请求, 每分钟20请求。

使用微软的Computer Vision API获得图片描述

除了Computer Vision API之外,微软还提供了文本分析,人脸分析等等其它api。

Python代码:

import urllib
import httplib
import json
import ast
 
#Microsoft计算机视觉API需要一张图片url和key
image_url = 'http://blog.topspeedsnail.com/wp-content/uploads/2016/11/obm.jpg'
api_key = '866c33************629ac3ef'
 
headers = {
	# Request headers
	'Content-Type': 'application/json',
	'Ocp-Apim-Subscription-Key': api_key,
}
params = urllib.urlencode({
	# Request parameters
	'maxCandidates': '1',
})
 
data = json.dumps({"Url": image_url}, separators=(',',':'))
 
try:
	conn = httplib.HTTPSConnection('api.projectoxford.ai')
	conn.request("POST", "/vision/v1.0/describe?%s" % params, data, headers)
	response = conn.getresponse()
	data = ast.literal_eval(response.read())
	# 返回json格式
    print(data)
	captioned_data = data['description']['captions'][0]["text"]
	print(captioned_data)
	conn.close()
except Exception as e:
	print(e)

测试:

使用微软的Computer Vision API获得图片描述-Python

{'metadata': {'width': 500, 'format': 'Jpeg', 'height': 332}, 'description': {'captions': [{'text': 'Barack Obama in a suit and tie', 'confidence': 0.6395414545684182}], 'tags': ['person', 'man', 'indoor', 'suit', 'holding', 'smiling', 'camera', 'looking', 'standing', 'table', 'wearing', 'black', 'talking', 'food', 'phone', 'white', 'room']}, 'requestId': 'c312a4b9-61b0-43bd-8ed5-ac1950edeaaa'}
Barack Obama in a suit and tie

使用微软的Computer Vision API获得图片描述-Python

{'metadata': {'width': 700, 'format': 'Jpeg', 'height': 700}, 'description': {'captions': [{'text': 'a close up of a cat', 'confidence': 0.7131832090765806}], 'tags': ['mammal', 'animal', 'indoor', 'cat', 'sitting', 'white', 'laying', 'front', 'small', 'top', 'close', 'lying', 'large', 'black', 'dog', 'orange', 'table', 'pink', 'blue', 'sink', 'room']}, 'requestId': '8627f941-ff03-444c-9929-5b5746121092'}
a close up of a cat

来源:http://blog.topspeedsnail.com/archives/10422

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值