Azure 微软|人脸检测|边界框检测|物体分类检测|WEB_API|PYTHON

开通服务

环境

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我似乎没有完全理解你的问题。你是想了解如何使用 Studio 3T 连接到 Azure Cosmos DB 的 API for MongoDB 吗?如果是的话,可以参考 Microsoft Docs 上的以下步骤: 1. 登录 Azure 门户,打开 Cosmos DB 资源,选择“连接字符串”选项卡,并复制“Primary Connection String”值。 2. 打开 Studio 3T,单击“Connect”按钮,然后选择“New Connection”。 3. 在“Connection Manager”窗口中,选择“MongoDB”作为“Connection Type”。 4. 在“Connection”选项卡中,将“Connection Name”设置为所需的名称,并将“Server Address”设置为 Cosmos DB 的主机名和端口号(格式为“<hostname>:<port>”)。 5. 在“Authentication”选项卡中,选择“Username/Password”作为身份验证方法,并输入 Cosmos DB 资源的用户名和密码。 6. 在“SSL”选项卡中,选择“Use SSL/TLS”选项,并将“SSL/TLS Protocol”设置为“TLSv1.2”。 7. 在“Advanced”选项卡中,将“Database”设置为 Cosmos DB 资源的数据库名称,并将“Authentication Database”设置为“admin”。 8. 最后,在“Connection String”选项卡中,将复制的“Primary Connection String”值粘贴到“Connection String”字段中,并将其中的“{username}”和“{password}”分别替换为 Cosmos DB 资源的用户名和密码。 9. 单击“Test”按钮测试连接,如果一切正常,单击“Save”按钮保存连接设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值