def prepare_model(self, input_type): """ prepares DeepLab model input_type: must be 'image' or 'video' """ assert input_type in ['image', 'video'], "only image or video input possible" super(DeepLabModel, self).prepare_model() self.input_type = input_type # fixed input sizes as model needs resize either way # Input configurations self.category_index = None if self.input_type is 'video': self._input_stream = VideoStream(self.config.VIDEO_INPUT, self.config.WIDTH, self.config.HEIGHT).start() elif self.input_type is 'image': self._input_stream = ImageStream(self.config.IMAGE_PATH, self.config.LIMIT_IMAGES).start() if self.config.WRITE_TIMELINE: self._run_options = tf.RunOptions( trace_level=tf.RunOptions.FULL_TRACE) self._run_metadata = tf.RunMetadata() self._timeliner = TimeLiner() print("> Building Graph") with self.detection_graph.as_default(): with tf.Session(graph=self.detection_graph, config=self._tf_config) as self._sess: return self
def prepare_input_stream(self): """ prepares Input Stream stream types: 'video','image','ros' gets called by prepare model """ if self.input_type is 'video': self._is_videoD = True self._input_stream = VideoStream(self.config.VIDEO_INPUT, self.config.WIDTH, self.config.HEIGHT).start() self.stream_height = self._input_stream.real_height self.stream_width = self._input_stream.real_width elif self.input_type is 'image': self._is_imageD = True self._input_stream = ImageStream( self.config.IMAGE_PATH, self.config.LIMIT_IMAGES, (self.config.WIDTH, self.config.HEIGHT)).start() self.stream_height = self.config.HEIGHT self.stream_width = self.config.WIDTH elif self.input_type is 'ros': self.prepare_ros('detection_node') # Timeliner for image detection if self.config.WRITE_TIMELINE: self.prepare_timeliner()
def prepare_input_stream(self): if self.input_type is 'video': self._is_videoD = True self._input_stream = VideoStream(self.config.VIDEO_INPUT,self.config.WIDTH,self.config.HEIGHT).start() elif self.input_type is 'image': self._is_imageD = True self._input_stream = ImageStream(self.config.IMAGE_PATH,self.config.LIMIT_IMAGES).start() if self.config.WRITE_TIMELINE: self.prepare_timeliner() elif self._input_type is 'ros': self.prepare_ros('deeplab_node')
def prepare_model(self, input_type): """ prepares Object_Detection model input_type: must be 'image' or 'video' """ assert input_type in ['image', 'video' ], "only 'image' or 'video' input possible" super(ObjectDetectionModel, self).prepare_model() self.input_type = input_type # Tracker if self.config.USE_TRACKER: sys.path.append(os.getcwd() + '/rod/kcf') import KCF self._tracker = KCF.kcftracker(False, True, False, False) self._tracker_counter = 0 self._track = False print("> Building Graph") with self.detection_graph.as_default(): with tf.Session(graph=self.detection_graph, config=self._tf_config) as self._sess: # Input Configuration if self.input_type is 'video': self._input_stream = VideoStream( self.config.VIDEO_INPUT, self.config.WIDTH, self.config.HEIGHT).start() height = self._input_stream.real_height width = self._input_stream.real_width elif self.input_type is 'image': self._input_stream = ImageStream( self.config.IMAGE_PATH, self.config.LIMIT_IMAGES, (self.config.WIDTH, self.config.HEIGHT)).start() height = self.config.HEIGHT width = self.config.WIDTH # Timeliner for image detection if self.config.WRITE_TIMELINE: self._run_options = tf.RunOptions( trace_level=tf.RunOptions.FULL_TRACE) self._run_metadata = tf.RunMetadata() self.timeliner = TimeLiner() # Define Input and Ouput tensors self._tensor_dict = self.get_tensordict([ 'num_detections', 'detection_boxes', 'detection_scores', 'detection_classes', 'detection_masks' ]) self._image_tensor = self.detection_graph.get_tensor_by_name( 'image_tensor:0') # Mask Transformations if 'detection_masks' in self._tensor_dict: # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size. detection_boxes = tf.squeeze( self._tensor_dict['detection_boxes'], [0]) detection_masks = tf.squeeze( self._tensor_dict['detection_masks'], [0]) real_num_detection = tf.cast( self._tensor_dict['num_detections'][0], tf.int32) detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1]) detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1]) detection_masks_reframed = reframe_box_masks_to_image_masks( detection_masks, detection_boxes, height, width) detection_masks_reframed = tf.cast( tf.greater(detection_masks_reframed, 0.5), tf.uint8) self._tensor_dict['detection_masks'] = tf.expand_dims( detection_masks_reframed, 0) if self.config.SPLIT_MODEL: self._score_out = self.detection_graph.get_tensor_by_name( '{}:0'.format(self.config.SPLIT_NODES[0])) self._expand_out = self.detection_graph.get_tensor_by_name( '{}:0'.format(self.config.SPLIT_NODES[1])) self._score_in = self.detection_graph.get_tensor_by_name( '{}_1:0'.format(self.config.SPLIT_NODES[0])) self._expand_in = self.detection_graph.get_tensor_by_name( '{}_1:0'.format(self.config.SPLIT_NODES[1])) # Threading self._gpu_worker = SessionWorker("GPU", self.detection_graph, self._tf_config) self._cpu_worker = SessionWorker("CPU", self.detection_graph, self._tf_config) self._gpu_opts = [self._score_out, self._expand_out] self._cpu_opts = [ self._tensor_dict['detection_boxes'], self._tensor_dict['detection_scores'], self._tensor_dict['detection_classes'], self._tensor_dict['num_detections'] ] return self