使用微软 azure-cognitiveservices 识别实验操作柜内仪表数据(一)


前言

场景:P2实验室某安全区域有一环境检测仪表,具备检测空间内温度、湿度、噪声等环境安全指标。
如何通过无线(不与仪表建立直接物理连接)方式准确获取仪表实时数据,并接入中控系统呢?通过结合高清摄像头与 azure-cognitiveservices 的方案值得考虑。

在这里插入图片描述


提示:以下是本篇文章正文内容,下面案例可供参考

[1个简单Demo]使用计算机视觉客户端,获取该物体的描述

1.安装客户端库

代码如下:

pip install –upgrade azure-cognitiveservices-vision-computervision

pip install pillow

2.创建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 =”PASTE_YOUR_COMPUTER_VISION_SUBSCRIPTION_KEY_HERE”
endpoint = “PASTE_YOUR_COMPUTER_VISION_ENDPOINT_HERE”

3.实例化

代码如下:

computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))

local_image_path =./sensor/sensor.jpg”

local_image = open(local_image_path, “rb”)

4.获取description_result

代码如下:

description_result = computervision_client.describe_image_in_stream(local_image)

print(“Description of local image:)
if (len(description_result.captions) == 0):
print(“No description detected.)
else:
for caption in description_result.captions:
print(“‘{
    
    }’ with confidence {
    
    :.2f}%.format(caption.text, caption.confidence * 100))

=========== result ===============

===== Describe an Image – local =====
Description of local image:
‘a rectangular sign with blue text’ with confidence 31.88%

在这里插入图片描述

5.完整代码

代码如下:

from logging import exception

from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from msrest.authentication import CognitiveServicesCredentials

subscription_key =”PASTE_YOUR_COMPUTER_VISION_SUBSCRIPTION_KEY_HERE”
endpoint = “PASTE_YOUR_COMPUTER_VISION_ENDPOINT_HERE”
local_image_path =./sensor/sensor.jpg”

 

def main():
try:
print(===== Describe an Sensor-Screenshot =====)
computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))
# Open local image file
local_image = open(local_image_path, “rb”)

# Call API
description_result = computervision_client.describe_image_in_stream(local_image)

# Get the captions (descriptions) from the response, with confidence level
print(“Description of local image:)
if (len(description_result.captions) == 0):
print(“No description detected.)
else:
for caption in description_result.captions:
print(“‘{
    
    }’ with confidence {
    
    :.2f}%.format(caption.text, caption.confidence * 100))

except exception as re:
print(re)

 

if __name__ == ‘__main__’:
main()

猜你喜欢

转载自blog.csdn.net/qq_36700574/article/details/115322945