昇腾社区首页
中文
注册

定义sample_detection

sample_detection.py是解析RESTful参数,调用stream,同时定义传出的消息。

编写要点

定义场景类。

如下示例,通过继承BaseInferScene定义了DetectionInferScene类,实现params_validation函数校验并解析RESTful参数,实现infer函数调用stream流程,并且进行后处理。其中包括实现并调用参数校验接口和后处理接口:

class DetectionInferScene(BaseInferScene):
     def __init__(self, *args, **kwargs):
         super(DetectionInferScene, self).__init__(*args, **kwargs)
         self._object_registration = ObjectRegistration()
         self.image_names = None
         self.image_list = None
         self.stream_manager = StreamManager(self.model_config, self.value, self.device_id)
 
    def infer(self):
         data = self.input_queue.get()
         error_dict, args = self.params_validation(data) # 实现并调用参数校验接口
         if error_dict:
             self.output_queue.put((error_dict, None, self.image_names, HTTPStatus.BAD_REQUEST))
         else:
             result_list, time_list = [], []
             for image_dict in self.image_list:
                 success, output_str, time_used, _ = self.stream_manager.process(image_dict.get('image_bytes')) # 调用stream流程
                 res_dict = self.post_process(success, output_str, time_used, args) # 实现并调用后处理接口
                 time_list.append(time_used)
                 result_list.append(res_dict)
             self.output_queue.put((result_list, time_list, self.image_names, HTTPStatus.OK))

调试方法

导入并安装自定义推理服务包后,成功启动推理服务并且成功完成推理任务即可。