开通服务
略
环境
pip install --upgrade azure-cognitiveservices-vision-computervision
pip install pillow
subscription_key = “”
endpoint = “”
这两个是你需要的,在注册中获得.另外这个服务是免费的,每月限定次数,每次限定并发
python 代码
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials
from array import array
import os
from PIL import Image
import sys
import time
subscription_key = "xxxxx"
endpoint = "xxxxx"
computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))
#============================== 描述一副图(带置信度) ===================================
local_image = open("fun.jpeg", "rb")
description_result = computervision_client.describe_image_in_stream(local_image)
if (len(description_result.captions) == 0):
print("No description detected.")
else:
for caption in description_result.captions:
print(caption)
print("'{}' with confidence {:.2f}%".format(caption.text, caption.confidence * 100))
结果: a woman holding a book’ with confidence 51.00%
#============================== 位置检测 ===================================
local_image_objects = open('catjpeg.jpeg', "rb")
detect_objects_results_local = computervision_client.detect_objects_in_stream(local_image_objects)
if len(detect_objects_results_local.objects) == 0:
print("No objects detected.")
else:
for object in detect_objects_results_local.objects:
print(object)
print("object at location {}, {}, {}, {}".format( \
object.rectangle.x, object.rectangle.x + object.rectangle.w, \
object.rectangle.y, object.rectangle.y + object.rectangle.h))
结果: object at location 456, 748, 22, 576
object at location 720, 997, 2, 572
#============================== 分类 ===================================
local_image = open('catjpeg.jpeg', "rb")
# Select visual feature type(s)
local_image_features = ["categories"]
# Call API
categorize_results_local = computervision_client.analyze_image_in_stream(local_image, local_image_features)
# Print category results with confidence score
print("Categories from local image: ")
if (len(categorize_results_local.categories) == 0):
print("No categories detected.")
else:
for category in categorize_results_local.categories:
print("'{}' with confidence {:.2f}%".format(category.name, category.score * 100))
结果: animal_cat’ with confidence 96.88%
#============================== 图中所有可能存在的东西 ===================================
local_image = open('catjpeg.jpeg', "rb")
# Call API local image
tags_result_local = computervision_client.tag_image_in_stream(local_image)
# Print results with confidence score
print("Tags in the local image: ")
if (len(tags_result_local.tags) == 0):
print("No tags detected.")
else:
for tag in tags_result_local.tags:
print("'{}' with confidence {:.2f}%".format(tag.name, tag.confidence * 100))
结果:
‘person’ with confidence 99.83%
‘tree’ with confidence 99.27%
‘clothing’ with confidence 99.14%
‘human face’ with confidence 97.90%
‘woman’ with confidence 93.78%
‘outdoor’ with confidence 92.40%
‘lady’ with confidence 92.21%
‘long hair’ with confidence 91.87%
‘photo shoot’ with confidence 90.29%
‘fashion accessory’ with confidence 89.75%
‘female person’ with confidence 89.10%
‘waist’ with confidence 87.91%
‘day dress’ with confidence 85.61%
‘holding’ with confidence 73.37%
‘girl’ with confidence 69.34%
‘young’ with confidence 63.56%
‘wearing’ with confidence 55.63%
‘dress’ with confidence 55.21%
#============================== 人脸检测 ===================================
local_image = open('test.jpeg', "rb")
local_image_features = ["faces"]
detect_faces_results_local = computervision_client.analyze_image_in_stream(local_image, local_image_features)
print("Faces in the local image: ")
if (len(detect_faces_results_local.faces) == 0):
print("No faces detected.")
else:
for face in detect_faces_results_local.faces:
print("'{}' of age {} at location {}, {}, {}, {}".format(face.gender, face.age, \
face.face_rectangle.left, face.face_rectangle.top, \
face.face_rectangle.left + face.face_rectangle.width, \
face.face_rectangle.top + face.face_rectangle.height))
结果: Female of age 23 at location 163, 97, 263, 197
#==================== 羞羞 检测 =================================
local_image = open('fun.jpeg', "rb")
local_image_features = ["adult"]
detect_adult_results_local = computervision_client.analyze_image_in_stream(local_image, local_image_features)
print("Analyzing local image for adult or racy content ... ")
print("Is adult content: {} with confidence {:.2f}".format(detect_adult_results_local .adult.is_adult_content, detect_adult_results_local .adult.adult_score * 100))
print("Has racy content: {} with confidence {:.2f}".format(detect_adult_results_local .adult.is_racy_content, detect_adult_results_local .adult.racy_score * 100))
结果:
Is adult content: False with confidence 0.29
Has racy content: False with confidence 0.47
#==================== 不同类型边界框检测 =================================
local_image_objects = open('catjpeg.jpeg', "rb")
# Call API with local image
detect_objects_results_local = computervision_client.detect_objects_in_stream(local_image_objects)
# Print results of detection with bounding boxes
print("Detecting objects in local image:")
if len(detect_objects_results_local.objects) == 0:
print("No objects detected.")
else:
for object in detect_objects_results_local.objects:
print("object at location {}, {}, {}, {}".format( \
object.rectangle.x, object.rectangle.x + object.rectangle.w, \
object.rectangle.y, object.rectangle.y + object.rectangle.h))
object at location 456, 748, 22, 576
object at location 720, 997, 2, 572