Defining sample_detection

sample_detection.py parses RESTful parameters, invokes streams, and defines output messages.

Key Points

Define a scenario class.

In the following example, DetectionInferScene is defined by inheriting BaseInferScene, so that the params_validation function can verify and parse RESTful parameters, and the infer function can invoke and post-process the stream process. Interfaces for parameter verification and postprocessing are also implemented.

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) # Implement and call the parameter verification interface.
         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')) # Invoke the stream process.
                 res_dict = self.post_process(success, output_str, time_used, args) # Implement and call the postprocessing interface.
                 time_list.append(time_used)
                 result_list.append(res_dict)
             self.output_queue.put((result_list, time_list, self.image_names, HTTPStatus.OK))

Debugging Methods

After the custom inference service package is installed and imported, start the inference service and complete the inference job.