Beispiel #1
0
 def __init__(self, bootstrap_servers, source_topic, target_topic):
     self.consumer = ImageConsumer(bootstrap_servers, source_topic)
     self.producer = ImageProducer(bootstrap_servers, target_topic)
     self.current_mean = np.array([])
     self.current_variance = np.array([])
     self.current_std = np.array([])
     self.gaussian = Gaussian()
     self.max_cost = None
     self.min_cost = None
Beispiel #2
0
 def __init__(self,
              bootstrap_servers,
              source_topic,
              target_topic,
              sequence_size,
              output_frame_size,
              to_grayscale=True,
              debug=False):
     self.consumer = ImageConsumer(bootstrap_servers, source_topic)
     self.producer = ImageProducer(bootstrap_servers,
                                   target_topic,
                                   debug=debug)
     self.sequence_size = sequence_size
     self.output_frame_size = output_frame_size
     self.to_grayscale = to_grayscale
     self.frame_counter = 0
Beispiel #3
0
 def __init__(self, topic, enable_auto_commit=True, group_id_suffix=None):
     self.consumer = ImageConsumer(KAFKA_BROKER_LIST,
                                   topic,
                                   enable_auto_commit=enable_auto_commit,
                                   group_id_suffix=group_id_suffix)
 def __init__(self, bootstrap_servers, source_topic, target_topic, cube_depth=5, tile_size=10):
     self.consumer = ImageConsumer(bootstrap_servers, source_topic)
     self.producer = ImageProducer(bootstrap_servers, target_topic)
     self.cube_depth = cube_depth
     self.tile_size = tile_size
     self.frame_counter = 0
class SpatioTemporalFeaturesProducer:
    def __init__(self, bootstrap_servers, source_topic, target_topic, cube_depth=5, tile_size=10):
        self.consumer = ImageConsumer(bootstrap_servers, source_topic)
        self.producer = ImageProducer(bootstrap_servers, target_topic)
        self.cube_depth = cube_depth
        self.tile_size = tile_size
        self.frame_counter = 0
          
    def send_frames(self):       
        counter = 0
        frame_sequence = []
        original_frame_sequence = []
      
        for msg in self.consumer.get_consumer():
            
            frame = frame_from_bytes_str(msg.value['data'])
            original_frame_sequence.append(frame)
            
              
            if counter%self.cube_depth == 0 and counter!=0:
                # To grayscale
                frame = frame_to_gray(frame)
                # Divide pixels by 255
                reduced_frame = reduce_frame(frame)
                frame_sequence.append(reduced_frame)
                
                
                features = generate_features(frame_sequence, self.cube_depth, self.tile_size)
                result = features.reshape(1, features.shape[0]*features.shape[1])
                
                
                ##Keep original frames
                #jpeg encode
                jpeg_frames = np.array([cv2.imencode('.jpg', x)[1] for x in original_frame_sequence])
                origin_frames = frame_to_bytes_str(jpeg_frames)
                
                
                end_frame_number = self.frame_counter
                source_end_timestamp = msg.value['timestamp']
                
                extra_fields = {'origin_frames':origin_frames, 
                                'start_frame_number': start_frame_number,
                                'end_frame_number': end_frame_number,
                                'source_end_timestamp': source_end_timestamp, 
                                'source_start_timestamp': source_start_timestamp}
                
                self.producer.send_frame(result, extra_fields=extra_fields) 
                
                start_frame_number = end_frame_number
                source_start_timestamp = source_end_timestamp
                # Reset frame sequences
                original_frame_sequence = []
                frame_sequence = []
                counter+=1
                self.frame_counter+=1
            else:
                if counter == 0:
                    start_frame_number = self.frame_counter
                    source_start_timestamp = msg.value['timestamp']
                    counter+=1
                    self.frame_counter+=1
                    continue
            
                # To grayscale
                frame = frame_to_gray(frame)
                # Divide pixels by 255
                reduced_frame = reduce_frame(frame)
                frame_sequence.append(reduced_frame)
                counter+=1
                self.frame_counter+=1
Beispiel #6
0
class ConceptDriftMovingAvg:
    def __init__(self, bootstrap_servers, source_topic, target_topic):
        self.consumer = ImageConsumer(bootstrap_servers, source_topic)
        self.producer = ImageProducer(bootstrap_servers, target_topic)
        self.current_mean = np.array([])
        self.current_variance = np.array([])
        self.current_std = np.array([])
        self.gaussian = Gaussian()
        self.max_cost = None
        self.min_cost = None

    def update_moving_measures(self, x, beta=0.998):
        if self.current_mean.size == 0:
            self.current_mean = np.zeros(x.shape)
            self.current_variance = np.zeros(x.shape)
            self.current_std = np.zeros(x.shape)

        mean = (beta * self.current_mean) + ((1 - beta) * x)
        self.current_mean = mean
        variance = (1 - beta) * (self.current_variance +
                                 np.power(beta * (x - mean), 2))
        self.current_variance = variance
        self.current_std = np.sqrt(variance)

    def get_measures(self):
        return self.current_mean, self.current_variance, self.current_std

    def evaluate_and_update(self, x, beta=0.998):
        result = self.gaussian.evaluate(x, self.current_mean, self.current_std)
        self.update_moving_measures(x, beta)

        return result

    def consume_features_and_predict(self, treshold=0):
        for msg in self.consumer.get_consumer():
            load_bytes = BytesIO(msg.value)
            feature = np.load(load_bytes, allow_pickle=True)

            result = self.evaluate_and_update(feature)

            if result > treshold:
                pass
                #Anomaly, if more than 2 minutes, then concept has drifted
                #producer.send(source_image)

    def update_max_min(self, value):
        if self.max_cost == None:
            self.max_cost = value
            self.min_cost = value
        else:
            if value < self.min_cost:
                self.min_cost = value

            if value > self.max_cost:
                self.max_cost = value

    def get_min_cost(self):
        return self.min_cost

    def get_max_cost(self):
        return self.max_cost

    def get_current_reg_score(self, value):
        sa = (value - self.min_cost) / (self.max_cost - self.min_cost)
        regularity_score = 1.0 - sa
        return regularity_score

    def reset_cost(self):
        self.max_cost = None
        self.min_cost = None
Beispiel #7
0
class SequenceFramesFeaturesProducer:
    def __init__(self,
                 bootstrap_servers,
                 source_topic,
                 target_topic,
                 sequence_size,
                 output_frame_size,
                 to_grayscale=True,
                 debug=False):
        self.consumer = ImageConsumer(bootstrap_servers, source_topic)
        self.producer = ImageProducer(bootstrap_servers,
                                      target_topic,
                                      debug=debug)
        self.sequence_size = sequence_size
        self.output_frame_size = output_frame_size
        self.to_grayscale = to_grayscale
        self.frame_counter = 0

    def send_frames(self):
        counter = 0
        frame_sequence = []
        original_frame_sequence = []
        frame_height, frame_width = self.output_frame_size

        for msg in self.consumer.get_consumer():
            frame = frame_from_bytes_str(msg.value['data'])
            original_frame_sequence.append(frame)

            if counter % self.sequence_size == 0 and counter != 0:
                # Resize
                frame = cv2.resize(frame, self.output_frame_size)
                # To grayscale
                if self.to_grayscale:
                    frame = frame_to_gray(frame)
                # Divide pixels by 255
                reduced_frame = reduce_frame(frame)

                frame_sequence.append(reduced_frame)

                #Todo: Think when image is RGB
                #Todo: Maybe possible with resize, reshape?
                clip = np.zeros(shape=(self.sequence_size, frame_height,
                                       frame_width, 1))
                clip[:, :, :, 0] = frame_sequence

                #Keep original frames
                #jpeg encode
                jpeg_frames = np.array([
                    cv2.imencode('.jpg', x)[1] for x in original_frame_sequence
                ])
                origin_frames = frame_to_bytes_str(jpeg_frames)

                end_frame_number = self.frame_counter
                source_end_timestamp = msg.value['timestamp']

                extra_fields = {
                    'origin_frames': origin_frames,
                    'start_frame_number': start_frame_number,
                    'end_frame_number': end_frame_number,
                    'source_end_timestamp': source_end_timestamp,
                    'source_start_timestamp': source_start_timestamp
                }

                #send to producer
                self.producer.send_frame(clip, extra_fields=extra_fields)

                start_frame_number = end_frame_number
                source_start_timestamp = source_end_timestamp
                # Reset frame sequences
                original_frame_sequence = []
                frame_sequence = []
                counter += 1
                self.frame_counter += 1

            else:
                if counter == 0:
                    start_frame_number = self.frame_counter
                    source_start_timestamp = msg.value['timestamp']
                    counter += 1
                    self.frame_counter += 1
                    continue

                # Resize
                frame = cv2.resize(frame, self.output_frame_size)
                # To grayscale
                if self.to_grayscale:
                    frame = frame_to_gray(frame)
                # Divide pixels by 255
                reduced_frame = reduce_frame(frame)

                frame_sequence.append(reduced_frame)
                counter += 1
                self.frame_counter += 1