文章目錄
- 前言
- [1個簡單Demo]使用計算機視覺客戶端,獲取該物體的描述
- 1.安裝客戶端庫
- 2.創建Python應用程式
- 3.實體化
- 4.獲取description_result
- 5.完整代碼
前言
場景: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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/271457.html
標籤:其他
