deepomatic rpc python客户端
deepomatic-rpc的Python项目详细描述
目录
Deepomatic远程过程调用
Deepomatic远程过程调用。
此远程过程调用旨在帮助您与我们的本地推理服务进行交互。 您可能还想使用我们的命令行接口deepomatic-cli。
安装
联机
pip install deepomatic-rpc
脱机
在具有Internet访问权限的计算机上,您需要使用以下命令下载包及其依赖项:
mkdir deepomatic-rpc # --platform force to get the packages compatibles with all OS pip download --platform any --only-binary=:all: -d ./deepomatic-rpc deepomatic-rpc
然后将deepomatic-rpc
目录保存到您选择的存储设备上。
现在在脱机计算机上检索此目录并安装包:
pip install --no-index --find-links ./deepomatic-rpc ./deepomatic-rpc/deepomatic_rpc-*-py2.py3-none-any.whl
用法
开始
实例化客户端和队列
fromdeepomatic.rpc.clientimportClient# Replace placeholder variables with yourscommand_queue_name='my_command_queue'recognition_version_id=123amqp_url='amqp://myuser:mypassword@localhost:5672/myvhost'# Instanciate clientclient=Client(amqp_url)# Do the following for each stream# Declare lasting command queuecommand_queue=client.new_queue(command_queue_name)# Declare response queue and consumer to get responses# consumer is linked to the response_queue# If queue_name parameter is provided, will declare a durable queue# Otherwise it is an uniq temporary queue.response_queue,consumer=client.new_consuming_queue()# Don't forget to cleanup when you are done sending requests !
发送识别请求
fromdeepomatic.rpcimportv07_ImageInputfromdeepomatic.rpc.responseimportwaitfromdeepomatic.rpc.helpers.v07_protoimportcreate_images_input_mix,create_recognition_command_mix# Create a recognition command mixcommand_mix=create_recognition_command_mix(recognition_version_id,max_predictions=100,show_discarded=False)# Create one image inputimage_input=v07_ImageInput(source='https://static.wamiz.fr/images/animaux/chats/large/bengal.jpg'.encode())# Wrap it inside a generic input mixinput_mix=create_images_input_mix([image_input])# Send the requestcorrelation_id=client.command(command_queue_name,response_queue.name,command_mix,input_mix)# Wait for response, `timeout=float('inf')` or `timeout=-1` for infinite wait, `timeout=None` for non blockingresponse=consumer.get(correlation_id,timeout=5)# get_labelled_output() is a shortcut that give you the corresponding predictions depending on the command mix you used# and raise a ServerError in case of error on the worker side. It should cover most cases but if it doesn't fit your needs, see the Response class. You might want to handle result and errors by yourself using `response.to_result_buffer()`.labels=response.get_labelled_output()predicted=labels.predicted[0]# Predicted is ordered by scoreprint("Predicted label {} with score {}".format(predicted.label_name,predicted.score))# if show_discarded was True, you might want to read `labels.discarded` to see which labels have a low confidence.
流动和清理
当你处理完一个流时,你应该清理你的消费队列。
- 如果程序在之后立即停止,则使用者将被取消,并且队列将在2小时不活动后自动删除(仅当队列是uniq临时队列时)。
- 如果您的程序是一个长时间运行的作业,则在2小时不活动后,代理可能会删除队列并取消使用者,但客户端可能会考虑在发生代理错误时重新声明这两个作业。
因此,调用client.remove_consuming_queue()
删除队列,并确保消费者被取消,以后不会重新声明:
client.remove_consuming_queue(response_queue,consumer)
您可能还希望在不使用以下命令的情况下删除队列:
client.remove_queue(queue)
另外,不要使用没有queue-name参数的new_consuming_queue()
和remove_consuming_queue()
,您可能需要使用contextmanager版本:
withclient.tmp_consuming_queue()as(response_queue,consumer):# this creates a temporary queue alive for the rest of this scope# do your inference requests
如果您不想关心响应队列和使用者,我们将提供一个高级类RPCStream
。
默认情况下,它会保存所有相关ID,以便您可以调用get_next_response()
,以获得与您推送请求相同的响应顺序:
fromdeepomatic.rpc.helpers.protoimportcreate_v07_images_commandserialized_buffer=create_v07_images_command([image_input],command_mix)withclient.new_stream(command_queue_name):# it internally saves the correlation_id so that it can retrieve responses in order# You need to call as many time get_next_response() as send_binary(), or the internal correlation_ids list will keep growing upstream.send_binary(serialized_buffer)response=stream.get_next_response(timeout=1)
另外,您可能需要自己处理响应顺序,在这种情况下,您可以按以下方式创建流:
# with keep_response_order=False, the stream will not buffer correlation_idswithclient.new_stream(command_queue_name,keep_response_order=False):correlation_id=stream.send_binary(serialized_buffer)# directly access the stream's consumer to retrieve a specific responseresponse=stream.consumer.get(correlation_id,timeout=1)
important:如果不使用WITH语句,则必须在结束时调用stream.close()
来清除使用者和响应队列。
高级
快捷方式
- 您可以避免调用
create_images_input_mix
,并通过方法client.v07_images_command
直接发送图像输入列表,该方法将在内部调用create_images_input_mix
:
correlation_id=client.v07_images_command(command_queue_name,response_queue.name,[image_input],command_mix)
- 创建工作流命令组合。将推导识别版本ID,但命令队列名称必须与workflows.json中的识别匹配。
注意,它不允许指定
show_discarded
或max_predictions
:
fromdeepomatic.rpc.helpers.v07_protoimportcreate_workflow_command_mixcommand_mix=create_workflow_command_mix()
- 创建一个推理命令mix;响应将是一个原始张量:
fromdeepomatic.rpc.helpers.v07_protoimportcreate_inference_command_mixoutput_tensors=['prod']command_mix=create_inference_command_mix(output_tensors)
- 一次等待多个相关标识:
fromdeepomatic.rpc.responseimportwait_responses# Wait for responses, `timeout=float('inf')` or `timeout=-1` for infinite waitresponses,pending=wait_responses(consumer,correlation_ids,timeout=10)print(responses)# will print [(0, response), (1, response), (2, response)]# 0, 1, 2 are the position in correlation_ids list in case you want to retrieve their original correlation_id# the list is sorted by positions to keep the same order as the correlation_ids list# if no timeout reached len(response) == len(correlation_ids)print(pending)# should be empty if timeout has not been reached# otherwise should print a list of correlation_id position that didn't get a response (the list is sorted)# If print [3, 5], then correlations_ids[3] and correlation_id[5] didn't get a response on time
图像输入示例
- 使用边框创建图像输入:
fromdeepomatic.rpcimportv07_ImageInputfromdeepomatic.rpcimportBBox# Coordinates between 0 and 1bbox=BBox(xmin=0.3,xmax=0.8,ymin=0.1,ymax=0.9)image_input=v07_ImageInput(source='https://static.wamiz.fr/images/animaux/chats/large/bengal.jpg'.encode(),bbox=bbox)
- 使用多边形选择创建图像输入:
fromdeepomatic.rpcimportv07_ImageInputfromdeepomatic.rpcimportPoint# Coordinates between 0 and 1, minimum 3 points neededpolygon=[Point(x=0.1,y=0.1),Point(x=0.9,y=0.1),Point(x=0.5,y=0.9)]image_input=v07_ImageInput(source='https://static.wamiz.fr/images/animaux/chats/large/bengal.jpg'.encode(),polygon=polygon)
- 从磁盘上的文件创建图像输入:
fromdeepomatic.rpcimportv07_ImageInputfromdeepomatic.rpc.helpers.protoimportbinary_source_from_img_filebinary_content=binary_source_from_img_file(filename)# Also works if you give a fileobjimage_input=v07_ImageInput(source=binary_content)
错误
请将错误报告发送到support@deepomatic.com