class Client:
    """ Base Client class """

    MULTI_THREADED = False
    # Initialize "Grand total" class variables.
    stats_latency_full_process = RunningStats()
    stats_latency_network_only = RunningStats()
    stats_server_processing_time = RunningStats()

    def __init__(self, host, port):
        # Initialize instance variables.
        self.host = host
        self.port = port
        self.do_server_stats = False
        self.show_responses = False
        self.stats_latency_full_process = RunningStats()
        self.stats_latency_network_only = RunningStats()
        self.stats_server_processing_time = RunningStats()
        self.image_file_name = None
        self.latency_start_time = 0
        self.loop_count = 0
        self.num_repeat = 0
        self.filename_list = []
        self.filename_list_index = 0
        self.json_params = None
        self.base64 = False
        logger.debug("host:port = %s:%d" %(self.host, self.port))

    def start(self):
        logger.debug("image file(s) %s %s" %(self.image_file_name, self.filename_list))

    def next_file_name(self):
        """ If the filename_list array is has more than 1, get the next value. """
        if len(self.filename_list) > 1:
            self.filename_list_index += 1
            if self.filename_list_index >= len(self.filename_list):
                self.filename_list_index = 0
            self.image_file_name = self.filename_list[self.filename_list_index]

    def time_open_socket(self):
        now = time.time()
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
        sock.settimeout(2)
        result = sock.connect_ex((self.host, self.port))
        if result != 0:
            logger.error("Could not connect to %s on port %d" %(self.host, self.port))
            return
        millis = (time.time() - now)*1000
        elapsed = "%.3f" %millis
        if self.show_responses:
            logger.info("%s ms to open socket" %(elapsed))
        self.stats_latency_network_only.push(millis)
        Client.stats_latency_network_only.push(millis)


    def icmp_ping(self):
        args=[PING, '-c', '1', '-W', '1', self.host]
        p_ping = subprocess.Popen(args,
                                  shell=False,
                                  stdout=subprocess.PIPE)
        # save ping stdout
        p_ping_out = str(p_ping.communicate()[0])

        if (p_ping.wait() == 0):
            logger.info(p_ping_out)
            # rtt min/avg/max/mdev = 61.994/61.994/61.994/0.000 ms
            search = re.search(PING_REGEX, p_ping_out, re.M|re.I)
            ping_rtt = search.group(2)
            if self.show_responses:
                logger.info("%s ms ICMP ping" %(ping_rtt))
            self.stats_latency_network_only.push(ping_rtt)
            Client.stats_latency_network_only.push(ping_rtt)
        else:
            logger.error("ICMP ping failed")

    def process_result(self, result):
        global TEST_PASS
        decoded_json = json.loads(result)
        if 'success' in decoded_json:
            if decoded_json['success'] == "true":
                TEST_PASS = True
            else:
                TEST_PASS = False
        if 'latency_start' in decoded_json:
            millis = (time.time() - decoded_json['latency_start'])*1000
            self.stats_latency_network_only.push(millis)
            Client.stats_latency_network_only.push(millis)
        else:
            millis = (time.time() - self.latency_start_time)*1000
            self.stats_latency_full_process.push(millis)
            Client.stats_latency_full_process.push(millis)
            if 'server_processing_time' in decoded_json:
                server_processing_time = decoded_json['server_processing_time']
                self.stats_server_processing_time.push(float(server_processing_time))
                Client.stats_server_processing_time.push(float(server_processing_time))

        if self.show_responses:
            elapsed = "%.3f" %millis
            logger.info("%s ms to send and receive: %s" %(elapsed, result))

    def display_results(self):
        if not self.show_responses or not Client.MULTI_THREADED:
            return

        if self.stats_latency_full_process.n > 0:
            logger.info("====> Average Latency Full Process=%.3f ms (stddev=%.3f)" %(self.stats_latency_full_process.mean(), self.stats_latency_full_process.stddev()))
        if self.stats_latency_network_only.n > 0:
            logger.info("====> Average Latency Network Only=%.3f ms (stddev=%.3f)" %(self.stats_latency_network_only.mean(), self.stats_latency_network_only.stddev()))
        if self.stats_server_processing_time.n > 0:
            logger.info("====> Average Server Processing Time=%.3f ms (stddev=%.3f)" %(self.stats_server_processing_time.mean(), Client.stats_server_processing_time.stddev()))
Example #2
0
            overall_box = np.concatenate((dboxes, tboxes), axis=0).astype(int)
            overall_box[overall_box < 0] = 0
            overall_score = np.concatenate((dscores, tscores), axis=0)

            # Get key frame RE-ID from tracklet
            template_features = tracklet.get_features()
            # print(template_features.shape)
            # print(frame.shape, overall_box)
            after_reid_score, reid_features = reid_rescore(
                reid_module, frame, template_features, overall_box,
                overall_score)
            # print(after_reid_score)
            best_idx = np.argmax(after_reid_score)
            best_bbox = overall_box[best_idx]
            best_score = after_reid_score[best_idx]
            running_stats.push(best_score)

            # Score better than one sigma, treat as key frame
            if best_score >= running_stats.mean(
            ) + running_stats.standard_deviation():
                tracklet.push_frame(
                    frame[best_bbox[1]:best_bbox[3],
                          best_bbox[0]:best_bbox[2], :],
                    np.squeeze(reid_features[best_idx]))

            # print(count, best_score)
            # print(overall_box)
            # if confidence is low than LOST_THRESHOLD, flag_lost = True
            cv2.circle(frame, (int(tracker.tracker.center_pos[0]),
                               int(tracker.tracker.center_pos[1])), 4,
                       (0, 0, 255), -1)
class Client:
    """ Base Client class """

    MULTI_THREADED = False
    # Initialize "Grand total" class variables.
    stats_latency_full_process = RunningStats()
    stats_latency_network_only = RunningStats()
    stats_server_processing_time = RunningStats()

    def __init__(self, host, port):
        # Initialize instance variables.
        self.host = host
        self.port = port
        self.do_server_stats = False
        self.show_responses = False
        self.stats_latency_full_process = RunningStats()
        self.stats_latency_network_only = RunningStats()
        self.stats_server_processing_time = RunningStats()
        self.media_file_name = None
        self.latency_start_time = 0
        self.loop_count = 0
        self.num_repeat = 0
        self.filename_list = []
        self.filename_list_index = 0
        self.json_params = None
        self.base64 = False
        self.video = None
        self.resize = True
        self.resize_long = 240
        self.resize_short = 180
        self.skip_frames = 1
        logger.debug("host:port = %s:%d" % (self.host, self.port))

    def start(self):
        logger.debug("media file(s) %s" % (self.filename_list))
        video_extensions = ('mp4', 'avi', 'mov')
        if self.filename_list[0].endswith(video_extensions):
            logger.debug("It's a video")
            self.media_file_name = self.filename_list[0]
            self.video = cv2.VideoCapture(self.media_file_name)

    def get_next_image(self):
        if self.video is not None:
            for x in range(self.skip_frames):
                ret, image = self.video.read()
                if not ret:
                    logger.debug("End of video")
                    return None
            vw = image.shape[1]
            vh = image.shape[0]
            logger.debug("Video size: %dx%d" % (vw, vh))
            if self.resize:
                if vw > vh:
                    resize_w = self.resize_long
                    resize_h = self.resize_short
                else:
                    resize_w = self.resize_short
                    resize_h = self.resize_long
                image = cv2.resize(image, (resize_w, resize_h))
                logger.debug("Resized image to: %dx%d" % (resize_w, resize_h))
            res, image = cv2.imencode('.JPEG', image)
            image = image.tostring()
        else:
            # If the filename_list array has more than 1, get the next value.
            if len(self.filename_list) > 1:
                self.filename_list_index += 1
                if self.filename_list_index >= len(self.filename_list):
                    self.filename_list_index = 0
            else:
                self.filename_list_index = 0

            if self.stats_latency_full_process.n >= self.num_repeat:
                return None

            self.media_file_name = self.filename_list[self.filename_list_index]
            f = open(self.media_file_name, "rb")
            image = f.read()

        logger.debug("Image data (first 32 bytes logged): %s" % image[:32])
        return image

    def get_server_stats(self):
        url = "http://%s:%d%s" % (self.host, self.port, "/server/usage/")
        if self.tls:
            url = url.replace("http", "https", 1)
        logger.info(requests.get(url).content)

    def time_open_socket(self):
        now = time.time()
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(2)
        result = sock.connect_ex((self.host, self.port))
        if result != 0:
            logger.error("Could not connect to %s on port %d" %
                         (self.host, self.port))
            return
        millis = (time.time() - now) * 1000
        elapsed = "%.3f" % millis
        if self.show_responses:
            logger.info("%s ms to open socket" % (elapsed))
        self.stats_latency_network_only.push(millis)
        Client.stats_latency_network_only.push(millis)

    def icmp_ping(self):
        args = [PING, '-c', '1', '-W', '1', self.host]
        p_ping = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE)
        # save ping stdout
        p_ping_out = str(p_ping.communicate()[0])

        if (p_ping.wait() == 0):
            logger.info(p_ping_out)
            # rtt min/avg/max/mdev = 61.994/61.994/61.994/0.000 ms
            search = re.search(PING_REGEX, p_ping_out, re.M | re.I)
            ping_rtt = float(search.group(2))
            if self.show_responses:
                logger.info("%s ms ICMP ping" % (ping_rtt))
            self.stats_latency_network_only.push(ping_rtt)
            Client.stats_latency_network_only.push(ping_rtt)
        else:
            logger.error("ICMP ping failed")

    def process_result(self, result):
        global TEST_PASS
        try:
            decoded_json = json.loads(result)
        except Exception as e:
            logger.error("Could not decode result. Exception: %s. Result: %s" %
                         (e, result))
            TEST_PASS = False
            return
        if 'success' in decoded_json:
            if decoded_json['success'] == "true":
                TEST_PASS = True
            else:
                TEST_PASS = False
        if 'latency_start' in decoded_json:
            millis = (time.time() - decoded_json['latency_start']) * 1000
            self.stats_latency_network_only.push(millis)
            Client.stats_latency_network_only.push(millis)
        else:
            millis = (time.time() - self.latency_start_time) * 1000
            self.stats_latency_full_process.push(millis)
            Client.stats_latency_full_process.push(millis)
            if 'server_processing_time' in decoded_json:
                server_processing_time = decoded_json['server_processing_time']
                self.stats_server_processing_time.push(
                    float(server_processing_time))
                Client.stats_server_processing_time.push(
                    float(server_processing_time))

        if self.show_responses:
            elapsed = "%.3f" % millis
            logger.info("%s ms to send and receive: %s" % (elapsed, result))

    def display_results(self):
        if not self.show_responses or not Client.MULTI_THREADED:
            return

        if self.stats_latency_full_process.n > 0:
            logger.info(
                "====> Average Latency Full Process=%.3f ms (stddev=%.3f)" %
                (self.stats_latency_full_process.mean(),
                 self.stats_latency_full_process.stddev()))
        if self.stats_latency_network_only.n > 0:
            logger.info(
                "====> Average Latency Network Only=%.3f ms (stddev=%.3f)" %
                (self.stats_latency_network_only.mean(),
                 self.stats_latency_network_only.stddev()))
        if self.stats_server_processing_time.n > 0:
            logger.info(
                "====> Average Server Processing Time=%.3f ms (stddev=%.3f)" %
                (self.stats_server_processing_time.mean(),
                 Client.stats_server_processing_time.stddev()))