Exemplo n.º 1
0
 def __init__(self):
     self.alert_logs = []
     self.is_alert_on = False
     self.section_activity = {}
     self.traffic_monitor = SlidingWindow(
         config['TRAFFIC_ALERT_TIME_WINDOW'])
     self.errors_monitor = SlidingWindow(config['ERRORS_ALERT_TIME_WINDOW'])
 def test_even_number_of_values_in_window(self):
     """Return value of [((n)/2)th item + ((n)/2 + 1)th item ] /2 of
     sorted measurements array
     """
     sliding_window = SlidingWindow(SLIDING_WINDOW_SIZE)
     sliding_window.delays = [100, 102]
     self.assertEqual(sliding_window.get_median(), 101)
 def test_delay_is_added(self):
     """Delay is added to the end of sliding window
     """
     sliding_window = SlidingWindow(SLIDING_WINDOW_SIZE)
     network_delay = 100
     sliding_window.add_delay(network_delay)
     self.assertListEqual(sliding_window.delays, [network_delay])
Exemplo n.º 4
0
def main():
    SL = SlidingWindow()
    SL.init()

    cap = cv2.VideoCapture(0)

    while (True):
        # Capture frame-by-frame
        ret, frame = cap.read()

        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        #frame_list = [x for row in frame for pixel in row for x in pixel]
        a, b, c = SL.detect(gray)
        if a != -1:
            print(a, b, c)

        # Display the resulting frame
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()
 def test_sliding_window_size_is_correct(self):
     """We limit the number of measurements in sliding window
     """
     sliding_window = SlidingWindow(SLIDING_WINDOW_SIZE)
     network_delay = 100
     for _ in range(SLIDING_WINDOW_SIZE + 1):
         sliding_window.add_delay(network_delay)
     self.assertEqual(len(sliding_window.delays), SLIDING_WINDOW_SIZE)
Exemplo n.º 6
0
    def process_image(self, img):
        s = SlidingWindow(x_start_stop=[None, None],
                          y_start_stop=y_start_stop,
                          classifier=self.c,
                          scaler=self.X_scaler,
                          color_space=color_space,
                          spatial_size=spatial_size,
                          hist_bins=hist_bins,
                          orient=orient,
                          pix_per_cell=pix_per_cell,
                          cell_per_block=cell_per_block,
                          hog_channel=hog_channel,
                          spatial_feat=spatial_feat,
                          hist_feat=hist_feat,
                          hog_feat=hog_feat,
                          show_debug=SHOW_IMAGES)

        window_img, box_list = s.run(img)

        h = HeatMap(img, 1, show_debug=SHOW_IMAGES)
        h.set_box_list(box_list)
        heat_img = h.get_heat()
        bound_img = h.get_boxed_image(img)
        box_list2 = h.get_boxes()
        #
        #         for i in range(10):
        tracking.set_box_list(img, box_list)

        tracking_heat_img = tracking.get_heat()
        tracking_img = tracking.get_boxed_image(bound_img)

        if (SHOW_IMAGES):
            plt.close()
            plt.imshow(window_img)
            plt.savefig("output_images/4_windows_image.png")

            plt.close()
            plt.imshow(bound_img)
            plt.savefig("output_images/4_boxed_image.png")

            plt.close()
            plt.imshow(tracking_heat_img)
            plt.savefig("output_images/4_tracking_heat_image.png")

            plt.close()
            plt.imshow(tracking_img)
            plt.savefig("output_images/4_tracking_image.png")


#         imageShow(img, \
#                   #img_calibrated, "Calibrated", \
#                   window_img, "windows", \
#                   heat_img, "Heat",
#                   bound_img, "bounded",
#                   tracking_heat_img, "Tracked heat",
#                   tracking_img, "Tracked")

        return tracking_img
 def test_window_moves_when_new_element_is_added(self):
     """Sliding window moves forward when new element is added
     """
     sliding_window = SlidingWindow(SLIDING_WINDOW_SIZE)
     network_delays = [100, 101, 102, 103]
     for delay in network_delays:
         sliding_window.add_delay(delay)
     expected_window = [101, 102, 103]
     self.assertListEqual(sliding_window.delays, expected_window)
Exemplo n.º 8
0
 def on_activate(self, widget, state):
     if state:
         self.__enable_settings(False)
         self.__sliding_window = SlidingWindow(self.__params)
         self.__sliding_window.attach_on_send_complete(self.on_send_complete)
         self.__sliding_window.attach_on_data_availbale(self.on_data_available)
     else:
         self.__enable_settings(True)
         self.__sliding_window.stop()
         self.__sliding_window = None
Exemplo n.º 9
0
def detections(image_path):
    """
	@brief      This function is to generate detections of faces from the given images.
                
    @param      image_path  String of the folder path of the images to be detected.
	"""
    # load the trained classifier model
    clf = joblib.load("hog_svm_model.m")
    # load images of which faces needed to be detected
    images_test = LoadImagesFromFolder(image_path)
    """
        Use function SlidingWindow() to detect faces on each image_test.
        
        This function takes about 67 minutes.
        
        MinSize is set to [60,40], which means the size of the sliding window 
        is fixed at 60*40 px, and this is also the minimum detection window. 
        
        Faces smaller than this window will not be detected.
        
        Scale is set to 0.8, which means at each step, the image will be reduced by 0.8 times. 
    
    """
    boxs = SlidingWindow(images_test, clf, [60, 40], 0.8)
    # If the IoU of 2 boxes is larger than 0.12, then remove the box with lower score.
    candidates = Deduplication(boxs, 0.12)
    # According to the descending order of the box score of each image,
    # the first 80% of the boxes are saved as the final detection box.
    results = BestCandidates(candidates, 0.8)
    np.savetxt('detection.txt', results, fmt="%d %d %d %d %d %.6f")
Exemplo n.º 10
0
def calculate_and_print_medians(input_file, output_file, sliding_window_size):
    """Calculate median using sliding window on delays from input_file
    and store results to output file

    Args:
        input_file (file object): File with delays
        output_file (file object): Output file to store medians
        sliding_window_size (int): Size of sliding window
    """
    sliding_window = SlidingWindow(sliding_window_size)

    for line in input_file:
        # Fail fast here if value is not int
        line = line.rstrip()
        if not line:
            # Skip empty lines
            continue
        delay = int(line)
        sliding_window.add_delay(delay)
        median = sliding_window.get_median()
        output_file.write('%d\r\n' % median)
Exemplo n.º 11
0
 def extract_candidates(self, examples, n=1, threshold=None, with_distance=False, return_scores=False):
     candidates_dict = {}
     if self.init_sw:
         self.sw = SlidingWindow()
         self.sw.fit(examples)
         self.init_sw = False
     predictions = get_predicts_score(examples, self.sw, with_distance)
     for e in tqdm(examples):
         candidates = list(zip(e.endings, predictions[e.example_id]))
         candidates.sort(key=lambda x: x[1], reverse=True)
         if threshold is not None:
             if return_scores:
                 candidates_dict[e.example_id] = [(e.endings.index(c[0]), c[1]) for c in candidates if c[1]>=threshold/100][:n]
             else:
                 candidates_dict[e.example_id] = [e.endings.index(c[0]) for c in candidates if c[1]>=threshold/100][:n]
         else:
             if return_scores:
                 candidates_dict[e.example_id] = [(e.endings.index(c[0]), c[1]) for c in candidates][:n]
             else:
                 candidates_dict[e.example_id] = [e.endings.index(c[0]) for c in candidates][:n]
     logger.info("Average number of candidates:", np.mean([len(c) for c in candidates_dict.values()]))
     return candidates_dict
Exemplo n.º 12
0
        print("Transcribing \"%s\"." % (file))

        ## load in image
        img = cv2.imread(join(path, file))

        ## preprocess image
        preprocessed_lines = preprocess_image(img)

        ## get root filename for writing the transcribed lines
        outfile = file.split('.')[0]

        ## classify lines
        for line in preprocessed_lines:
            sentence = ''
            ## neural network call here
            sw = SlidingWindow()
            sw.WRITE_WINDOWS = False  # If True, the input images of the cnn will be written to a file
            sw.load_image(line)
            transcribed_lines = sw.get_letters()

            ## apply postprocessing
            postp = Bayesian_processor()
            final_letter = postp.apply_postprocessing(transcribed_lines)
            sentence = sentence + str(final_letter)

            ## write croppings to file
            write_to_file(sentence, path, outfile)

        print("Succesfully transcribed \"%s\" to \"%s\"." % (file, outfile))

    print("Finished transcribing.")
Exemplo n.º 13
0
 def test_item_count(self):
     sliding_window = SlidingWindow(10)
     sliding_window.add(4)
     sliding_window.add(53)
     self.assertEqual(len(sliding_window), 2, "sliding window must have 2 items")
Exemplo n.º 14
0
 def test_empty(self):
     sliding_window = SlidingWindow(10)
     self.assertEqual(len(sliding_window), 0, "len of empty sliding window must be 0")
Exemplo n.º 15
0
 def test_datapoints_avg(self):
     sliding_window = SlidingWindow(20)
     edge_sizes = [1, 5]
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     size_datapoints = point_extractor.extract_points(sliding_window, edge_sizes)
     self.assertTrue(size_datapoints[1]['left'] == 1 and size_datapoints[1]['right'] == 1 and size_datapoints[5]['left'] == 1 and size_datapoints[5]['right'] == 1) 
Exemplo n.º 16
0
 def test_datapoints_number(self):
     sliding_window = SlidingWindow(20)
     edge_sizes = [1, 5]
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     sliding_window.add(1)
     size_datapoints = point_extractor.extract_points(sliding_window, edge_sizes)
     self.assertEqual(len(size_datapoints), 2)
Exemplo n.º 17
0
    format='%(asctime)s - %(module)s - %(levelname)s - %(message)s',
    level=logging.INFO)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(
    logging.Formatter(
        '%(asctime)s - %(module)s - %(levelname)s - %(message)s'))
logging.getLogger().addHandler(consoleHandler)

logging.info("starting trader...")
logging.info(
    "api base url: {api_base_url}, samples per min: {samples_per_min}, symbol: {symbol}, interval points: {interval_points}"
    .format(api_base_url=api_base_url,
            samples_per_min=samples_per_min,
            symbol=symbol,
            interval_points=interval_points))
sliding_window = SlidingWindow(max(interval_points) * 2 + 1)

# load previous order
try:
    with open('previous_order.txt') as file:
        previous_order = json.loads(file.readline())
        logging.info("loaded previous order: {previous_order}".format(
            previous_order=previous_order))
except FileNotFoundError as e:
    previous_order = {'type': 'BUY', 'price': 1.16}
    logging.warning("{exception}, using default: {default}".format(
        exception=e, default=previous_order))


def extract_minute_average(samples_per_min=10):
    number_of_samples = 0
Exemplo n.º 18
0
if __name__ == '__main__':
    args = parse_args()
    if not args.population:
        genome = neat.Genome(args.genome_file)
    else:
        pop = neat.Population(args.genome_file)
        genome = util.get_individuals_list(pop)
    fit_func = FitFunction(args.evaluator)
    data = Data(args.data_file)
    mapping = encoder.Mapping.load(
        args.mapping_file) if args.mapping_file is not None else None
    mapping is not None and data is not None and data.encode_from_mapping(
        mapping)
    slider = SlidingWindow(
        args.width, args.shift, args.test_width,
        file_path=args.data_file) if args.width is not None else None

    if slider is None:
        Evaluator.setup(data)
        evaluation = Evaluator.evaluate(genome,
                                        fit_func,
                                        data,
                                        include_roc=args.plot)
        print(evaluation.fitness)
        if args.plot:
            if fit_func is FitFunction.AUC:
                draw_roc(evaluation)
    else:
        for i, (train_data, test_data) in enumerate(slider):
            Evaluator.setup(test_data)
Exemplo n.º 19
0
    def mf_write(self, data):
        start = 0
        payload_size = 512
        packets = []

        # chunk data into packets
        while start < len(data):
            if start + payload_size <= len(data):
                payload = data[start : start + payload_size]
            else:
                payload = data[start :]

            data_packet = MFPacket(
                self.port_number,
                self.destination[1],
                sequence_number = self.sequence_number,
                payload = payload
            )
            packets.append(data_packet)
            self.sequence_number += 1
            start += payload_size

        terminator_packet = MFPacket(
            self.port_number,
            self.destination[1],
            sequence_number = self.sequence_number
        )
        packets.append(terminator_packet)
        self.terminator_packet_seq_num = self.sequence_number
        self.terminator_packet_num_sent = 0
        self.sequence_number += 1

        # populate window and send all packets
        window = SlidingWindow(packets, self.window_size)

        last_sent = time.time()
        time_remaining = self.retransmit_timer.timeout
        self.logger.debug('sending data packets in window.')
        for data_packet in window.window:
            self.io_loop.send_queue.put((data_packet, self.destination))
            #print data_packet.data_offset

        while not window.is_empty():
            try:
                # wait for incoming packet
                ack_packet, address = self.io_loop.receive_queue.get(True, time_remaining)
            except Queue.Empty:
                # timeout, go back n
                self.logger.debug('timed out waiting for ack during data transmission. retransmitting window.')
                last_sent = time.time()
                time_remaining = self.retransmit_timer.timeout

                for data_packet in window.window:

                    if self.terminator_packet_seq_num == data_packet.sequence_number:
                        self.terminator_packet_num_sent += 1
                        if self.terminator_packet_num_sent > 3:
                            self.logger.debug('Unable to end connection, terminating now')
                            return

                    data_packet.frequency += 1
                    data_packet.recalculate_checksum()
                    self.io_loop.send_queue.put((data_packet, self.destination))



                continue

            # if still getting syn/ack, retransmit ack
            if self.__verify_syn_ack(ack_packet, address, 1):
                self.logger.debug('received syn/ack retransmission. retransmitting ack.')
                ack_packet = MFPacket(
                    self.port_number,
                    self.destination[1],
                    ack_number = ack_packet.sequence_number + 1,
                    sequence_number = 2,
                    ack = True
                )
                self.io_loop.send_queue.put((ack_packet, self.destination))
                time_remaining = 0
            # if first packet in pipeline is acknowledged, slide the window
            elif self.__verify_ack(ack_packet, address, window.window[0].sequence_number):
                self.retransmit_timer.update(ack_packet.frequency, time.time() - last_sent)
                self.logger.debug('updated retransmit timer. timeout is now ' + str(self.retransmit_timer.timeout))
                window.slide()
                if not window.is_emptying():
                    self.io_loop.send_queue.put((window.window[-1], self.destination))
                    self.sequence_number += 1
                    #print "executing"
            # otherwise, update time remaining
            else:
                time_remaining -= time.time() - last_sent/4
                if time_remaining < time.time():
                    time_remaining = .5
                self.logger.debug('bunk packet received. time remaining before timeout: ' + str(time_remaining))
 def test_only_one_delay_in_window(self):
     """Return -1 if window consists only of one delay
     """
     sliding_window = SlidingWindow(SLIDING_WINDOW_SIZE)
     sliding_window.delays = [100]
     self.assertEqual(sliding_window.get_median(), -1)
Exemplo n.º 21
0
class Statistics:
    def __init__(self):
        self.alert_logs = []
        self.is_alert_on = False
        self.section_activity = {}
        self.traffic_monitor = SlidingWindow(
            config['TRAFFIC_ALERT_TIME_WINDOW'])
        self.errors_monitor = SlidingWindow(config['ERRORS_ALERT_TIME_WINDOW'])

    def queue_data(self, data):
        parsed_data = Parser.parse_log_line(data)
        if (parsed_data is not None):
            if parsed_data['status'] >= 400:
                self.errors_monitor.push()
            self.traffic_monitor.push()
            self.update_activity_statistics(parsed_data)

    def update_traffic_alert_status(self):
        self.traffic_monitor.update()
        alert_monitor_items_count = self.traffic_monitor.count_elements()
        average_traffic = alert_monitor_items_count / \
            config.get('TRAFFIC_ALERT_TIME_WINDOW')
        if (average_traffic > config.get('AVERAGE_TRAFFIC_TRESHOLD')):
            self.is_alert_on = True
            self.alert_logs.append(
                "High traffic - {} hits - Triggered at {}".format(
                    alert_monitor_items_count, datetime.now()))
        if (average_traffic < config.get('AVERAGE_TRAFFIC_TRESHOLD')
                and self.is_alert_on):
            self.is_alert_on = False
            self.alert_logs.append(
                "Normal traffic - {} hits - Recovered at {}".format(
                    alert_monitor_items_count, datetime.now()))

    def update_error_alert_status(self):
        self.errors_monitor.update()
        errors_monitor_items_count = self.errors_monitor.count_elements()
        max_error = errors_monitor_items_count / \
            config.get('ERRORS_ALERT_TIME_WINDOW')
        if (max_error > config.get('MAX_ERRORS_TRESHOLD')):
            self.alert_logs.append(
                "Too many errors - Errors: {} - Triggered at {}".format(
                    errors_monitor_items_count, datetime.now()))

    def update_activity_statistics(self, data):
        section = self.section_activity.get(
            data['section'], {
                'errors_count': 0,
                'heaviest_request': 0,
                'hits': 0,
                'section': data['section'],
            })
        updated_section = {
            'errors_count':
            section['errors_count'] +
            1 if data['status'] >= 400 else section['errors_count'],
            'heaviest_request':
            data['bytes'] if data['bytes'] > section['heaviest_request'] else
            section['heaviest_request'],
            'hits':
            section['hits'] + 1,
            'section':
            data['section'],
        }
        self.section_activity[data['section']] = updated_section

    def clean_section_activity(self):
        self.section_activity = {}

    def get_sorted_section_activity(self):
        return sorted(list(self.section_activity.values()),
                      key=itemgetter('hits'),
                      reverse=True)
Exemplo n.º 22
0
 def test_clear(self):
     sliding_window = SlidingWindow(10)
     sliding_window.add(4)
     sliding_window.add(53)
     sliding_window.clear()
     self.assertEqual(len(sliding_window), 0, "sliding window after clear must have 0 items")
Exemplo n.º 23
0
 def test_window_side(self):
     sliding_window = SlidingWindow(2)
     sliding_window.add(4)
     sliding_window.add(53)
     sliding_window.add(5)
     self.assertEqual(len(sliding_window), 2, "sliding window must have less or equal number of items than sliding window size")
Exemplo n.º 24
0
class MainWindow:
    def __init__(self, ui_file):
        builder = Gtk.Builder()
        builder.add_from_file(ui_file)
        builder.connect_signals(self)

        self.__builder = builder
        self.__activate_switch  = builder.get_object('activate_switch')
        self.__base_frequency   = builder.get_object('base_frequency')
        self.__max_payload_size = builder.get_object('max_payload_size')
        self.__max_windows      = builder.get_object('max_windows')
        self.__message_box      = builder.get_object('message_box')
        self.__mode_selector    = builder.get_object('mode_selector')
        self.__num_channels     = builder.get_object('num_channels')
        self.__sample_rate      = builder.get_object('sample_rate')
        self.__send             = builder.get_object('send')
        self.__window           = builder.get_object('window')
        self.__window_length    = builder.get_object('window_length')
        self.__timeout          = builder.get_object('timeout')
        self.__headerbar        = builder.get_object('headerbar')
        self.__message_history  = builder.get_object('message_history')

        self.__window.show_all()
        self.__params = None
        self.__resetting = False

        self.__set_default()

        self.__sliding_window = None
        GLib.timeout_add(100, self.on_stream_tick)

        self.__partial_message = ''


    def __enable_settings(self, state):
        self.__base_frequency.set_sensitive(state)
        self.__max_payload_size.set_sensitive(state)
        self.__max_windows.set_sensitive(state)
        self.__mode_selector.set_sensitive(state)
        self.__num_channels.set_sensitive(state)
        self.__sample_rate.set_sensitive(state)
        self.__window_length.set_sensitive(state)

        self.__message_box.set_sensitive(not state)
        if state:
            self.__message_box.set_text('')
            self.__message_box.set_placeholder_text(MESSAGE_INPUT_DISABLED)
        else:
            self.__message_box.set_placeholder_text(MESSAGE_INPUT_ACTIVE)

        #send is always disabled, if we leave setup mode because the text entry is empty
        self.__send.set_sensitive(False)


    def __update_subtitle(self):
        self.__headerbar.set_subtitle('Biterate: ~{:.1f} bps'.format(self.__params.get_max_bps()))


    def __set_default(self):
        self.__activate_switch.set_active(False)
        self.__enable_settings(True)

        self.__params = TransmissionParameters()

        self.__resetting = True
        self.__base_frequency.set_value(self.__params.get_base_freq())
        self.__max_windows.set_value(self.__params.get_seq_max())
        self.__max_payload_size.set_value(self.__params.get_max_payload_size())
        self.__num_channels.set_value(self.__params.get_num_channels())
        self.__sample_rate.set_value(self.__params.get_sample_rate() / 1000)
        self.__window_length.set_value(self.__params.get_window_length())
        self.__mode_selector.set_active(not self.__params.get_is_master())
        self.__timeout.set_text('{:.1f}'.format(self.__params.get_timeout()))
        self.__update_subtitle()
        self.__resetting = False


    def __add_message(self, msg):
        it = self.__message_history.get_end_iter()
        self.__message_history.insert(it, msg)


    def on_send_complete(self):
        self.__message_box.set_sensitive(True)
        self.__message_box.set_placeholder_text(MESSAGE_INPUT_ACTIVE)


    def on_data_available(self):
        data = self.__sliding_window.recv()
        self.__partial_message += data.decode('ascii')
        if self.__partial_message.endswith('\n'):
            self.__add_message('< ' + self.__partial_message)
            self.__partial_message = ''


    def on_stream_tick(self):
        if self.__sliding_window:
            self.__sliding_window.tick()

        return True


    def on_destroy(self, widget):
        if self.__sliding_window:
            self.__sliding_window.stop()
            self.__sliding_window = None
        Gtk.main_quit()


    def on_update_parameters(self, widget):
        if self.__resetting:
            return
        self.__params.set_base_freq(self.__base_frequency.get_value())
        self.__params.set_seq_max(self.__max_windows.get_value_as_int())
        self.__params.set_max_payload_size(self.__max_payload_size.get_value_as_int())
        self.__params.set_num_channels(self.__num_channels.get_value_as_int())
        self.__params.set_sample_rate(round(self.__sample_rate.get_value() * 1000))
        self.__params.set_window_length(self.__window_length.get_value())
        self.__params.set_is_master(not self.__mode_selector.get_active())

        self.__update_subtitle()
        self.__timeout.set_text('{:.1f}'.format(self.__params.get_timeout()))


    def on_activate(self, widget, state):
        if state:
            self.__enable_settings(False)
            self.__sliding_window = SlidingWindow(self.__params)
            self.__sliding_window.attach_on_send_complete(self.on_send_complete)
            self.__sliding_window.attach_on_data_availbale(self.on_data_available)
        else:
            self.__enable_settings(True)
            self.__sliding_window.stop()
            self.__sliding_window = None


    def on_reset(self, widget):
        self.__set_default()


    def on_mode_switch(self, widget):
        if self.__mode_selector.get_active():
            self.__mode_selector.set_label('Slave')
        else:
            self.__mode_selector.set_label('Master')
        self.on_update_parameters(None)


    def on_text_entry_change(self, widget):
        if self.__message_box.get_text() == '':
            self.__send.set_sensitive(False)
        else:
            self.__send.set_sensitive(True)


    def on_send(self, widget):
        message = self.__message_box.get_text()
        message += '\n'

        self.__add_message('> ' + message)

        self.__message_box.set_text('')
        self.__message_box.set_sensitive(False)
        self.__message_box.set_placeholder_text(MESSAGE_INPUT_SENDING)
        self.__sliding_window.send(message.encode('ascii'))
Exemplo n.º 25
0
 def test_sliding_window_size_not_enough(self):
     sliding_window = SlidingWindow(20)
     edge_sizes = [1, 5]
     with self.assertRaises(Exception):
         point_extractor.extract_points(sliding_window, edge_sizes)
Exemplo n.º 26
0
    def send(self, msg):
        floor = 0
        payload_size = 512
        packets = []

        # chunk data into packets
        while floor < len(msg):
            if floor + payload_size <= len(msg):
                payload = msg[floor: floor + payload_size]
            else:
                payload = msg[floor:]

            data_packet = RxPPacket(
                self.port_number,
                self.destination[1],
                seq_number=self.seq_number,
                payload=payload
            )
            packets.append(data_packet)
            self.seq_number += 1
            floor += payload_size

        kill_packet = RxPPacket(
            self.port_number,
            self.destination[1],
            seq_number=self.seq_number
        )
        kills_sent = 0  # allow to be sent 3 times before dropping client cxn
        kill_seq_number = kill_packet.seq_number
        packets.append(kill_packet)  # put that shit at the end after we've added all the other packets
        self.seq_number += 1

        self.logger.debug('Placing packets in window to be sent now...')
        window = SlidingWindow(packets, self.window_size)
        time_sent = time.time()
        time_remaining = self.retransmit_timer.timeout

        self.cxn_status = RxPConnectionStatus.SEND
        for data_packet in window.window:
            self.io.send_queue.put((data_packet, self.destination))

        while not window.is_empty():
            try:
                ack_packet, address = self.io.recv_queue.get(True, time_remaining)

            # resend entire send window (all packets that weren't yet ACK'd by the receiver)
            except Queue.Empty:
                self.logger.debug(
                    'Timed out waiting for ack during data transmission; retransmitting unacknowledged packets.')
                time_sent = time.time()
                time_remaining = self.retransmit_timer.timeout

                for data_packet in window.window:
                    if kill_seq_number == data_packet.seq_number:
                        kills_sent += 1
                        if kills_sent > 3:  # if retransmitted 3 times already, kill cxn with client
                            self.logger.debug(
                                'Kill packet failed to be acknowledged; unable to end connection, closing now.')
                            return

                    if not window.acknowledged_packets.get(data_packet.seq_number):
                        data_packet.frequency += 1
                        data_packet.update_checksum()
                        self.io.send_queue.put((data_packet, self.destination))
                continue

            # if still getting SYN/ACK, retransmit ACK
            if self.__verify_syn_ack(ack_packet, address, 1):
                self.logger.debug('Received SYN/ACK retransmission; retransmitting ACK.')
                ack_packet = RxPPacket(
                    self.port_number,
                    self.destination[1],
                    ack_number=ack_packet.seq_number + 1,
                    seq_number=2,
                    ack=True
                )
                self.io.send_queue.put((ack_packet, self.destination))
                time_remaining = 0

            # if a packet in window is acknowledged, slide the window past said received packet
            elif self.__verify_is_ack(ack_packet, address) and window.index_of_packet(ack_packet) >= 0:
                self.retransmit_timer.update(ack_packet.frequency, time.time() - time_sent)
                self.logger.debug(
                    'ACK received. Updated retransmit timer; timeout is now ' + str(self.retransmit_timer.timeout))
                window.acknowledge_packet(ack_packet)

                if self.__verify_ack(ack_packet, address, window.window[0].seq_number):
                    additions = window.slide()
                    # send newly added packets if they were added
                    if additions > 0:
                        while additions > 0:
                            self.io.send_queue.put((window.window[-additions], self.destination))
                            self.seq_number += 1
                            additions -= 1

            # otherwise, update time remaining
            else:
                time_remaining -= time.time() - time_sent / 4  # decay
                if time_remaining < time.time():
                    time_remaining = .5
                self.logger.debug('Trash packet receive; time remaining before next timeout: ' + str(time_remaining))

        self.cxn_status = RxPConnectionStatus.IDLE
Exemplo n.º 27
0
        probabilities = self.filter_on_seq_of_same_chars(probabilities)
        probabilities = self.apply_threshold(probabilities)
        probabilities.reverse(
        )  # This way the n-grams will be applied from right to left
        posteriors = self.process_word(probabilities)
        posteriors = self.normalize_posteriors(posteriors)
        posteriors = self.filter_on_seq_of_same_chars(
            posteriors
        )  # Filter on same-char-sequences, these may be produced by the n-grams post processing
        posteriors.reverse(
        )  # This way the n-grams will be applied from right to left
        final_sentence = ""
        for idx, letter_probs in enumerate(posteriors):
            best_letter_val = max(letter_probs)
            best_letter_index = letter_probs.index(best_letter_val)
            final_sentence += hebrew_map[best_letter_index]
        return final_sentence


if __name__ == "__main__":
    # When running this script standalone, use this example:

    # Construct mock prediction softmax (of length (n x 27) )
    processor = Bayesian_processor()
    sw = SlidingWindow()
    image_file = "../data/backup_val_lines/line1.png"
    sw.load_image(image_file)

    predicted_sentence = sw.get_letters()
    sentence = processor.apply_postprocessing(predicted_sentence)