gpt4 book ai didi

python-3.x - 使用 OpenCV 图像进行 Azure 识别服务

转载 作者:行者123 更新时间:2023-12-03 06:25:12 24 4
gpt4 key购买 nike

我已经在 Python 3 中打开了 OpenCV 图像,我想将其发送到 Azure 识别服务。

我知道我可以通过 open 以二进制模式读取图像,并使用以下 code 将其发送到 Azure 服务:

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

image_file="........"
subscription_key = "........"
endpoint = "......"

computervision_client = ComputerVisionClient(endpoint,CognitiveServicesCredentials(subscription_key))

with open(image_file, "rb") as image:
read_response = computervision_client.read_in_stream(image, raw=True)

在 OpenCV 中,我会通过以下方式读取图像

import cv2
image_file=cv2.imread(image_file)

如何将OpenCV中已经读取的图像进行转换,以便将其发送到客户端?我不想再次通过 open 阅读我的图片。

最佳答案

How to convert the already read in OpenCV image so that it can be sentto the client? I do not want to read again my picture by open.

您可以利用OpenCV的imencode方法将图像以字节形式传输到内存中,请引用下面的代码:-


import cv2

from azure.cognitiveservices.vision.computervision import ComputerVisionClient

from msrest.authentication import CognitiveServicesCredentials

import io

import time

from azure.core.exceptions import HttpResponseError

from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes

#from azure.core.serialization import De



# replace with your own Azure subscription key and endpoint

subscription_key = "<subscription-key"

endpoint = "<end-point>"



# create a ComputerVisionClient object with Azure authentication

computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))



# read the image with OpenCV

image = cv2.imread("letter-to-santa-claus.png")



# encode the image as a jpeg in memory

retval, buffer = cv2.imencode('.jpg', image)

jpeg_bytes = buffer.tobytes()



# create an IO stream to wrap the bytes data

image_stream = io.BytesIO(jpeg_bytes)




response = computervision_client.read_in_stream(image_stream, raw=True)

read_operation_location = response.headers["Operation-Location"]

# Grab the ID from the URL

operation_id = read_operation_location.split("/")[-1]

# Retrieve the results

while True:

read_result = computervision_client.get_read_result(operation_id)

if read_result.status.lower() not in ['notstarted', 'running']:

break

time.sleep(1)

# Get the detected text

if read_result.status == OperationStatusCodes.succeeded:

for page in read_result.analyze_result.read_results:

for line in page.lines:

# Print line

print(line.text)

输出:-

enter image description here

关于python-3.x - 使用 OpenCV 图像进行 Azure 识别服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75597821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com