Example #1
0
def DeepKCF(seq, res_path, bSaveImage):

    parameters = Params()

    #img_files, pos, target_sz, ground_truth, video_path = load_video_info(video_path)  # Load video info
    img_files, pos, target_sz, video_path = load_video_gt(seq)

    parameters.init_pos = np.floor(pos) + np.floor(
        target_sz / 2)  # Initial position
    parameters.pos = parameters.init_pos  # Current position
    parameters.target_size = np.floor(target_sz)  # Size of target
    parameters.img_files = img_files  # List of image files
    parameters.video_path = video_path  # Path to the sequence

    num_frames = len(img_files)

    results = np.zeros((num_frames, 4))

    start = start_timer()

    # For each frame
    for frame in xrange(num_frames):

        # Read the image
        #im = cv2.imread(video_path + img_files[frame], 1)
        im = scpmi.imread(video_path + img_files[frame])

        # Initialize the tracker using the first frame
        if frame == 0:
            tracker1 = Tracker(im, parameters)
            tracker1.train(im, True)
            results[frame, :] = np.array((pos[0] + np.floor(target_sz[0] / 2),
                                          pos[1] + np.floor(target_sz[1] / 2),
                                          target_sz[0], target_sz[1]))
        else:
            results[frame, :], lost, xtf = tracker1.detect(
                im)  # Detect the target in the next frame
            if not lost:
                tracker1.train(im, False,
                               xtf)  # Update the model with the new infomation

    tracker1.close()
    duration = end_timer(start, "to complete tracking")
    fps = round(num_frames / duration, 2)

    return results, fps
Example #2
0
class TorrentClient:
    def __init__(self, torrent):
        self.tracker = Tracker(torrent)
        self.available_peers = Queue()
        self.peers = []
        self.piece_manager = PieceManager(torrent)
        self.abort = False

    async def start(self):
        self.peers = [
            PeerConnection(self.available_peers,
                           self.tracker.torrent.info_hash,
                           self.tracker.peer_id, self.piece_manager,
                           self._on_block_retrieved)
            for _ in range(MAX_PEER_CONNECTIONS)
        ]

        previous = None
        interval = 30 * 60

        while True:
            if self.piece_manager.complete:
                logging.info('Torrent fully downloaded!')
                break
            if self.abort:
                logging.info('Aborting download...')
                break

            current = time.time()
            if (not previous) or (previous + interval <
                                  current):  # 该联系tracker啦
                response = await self.tracker.connect(
                    first=previous if previous else False,
                    uploaded=self.piece_manager.bytes_uploaded,
                    downloaded=self.piece_manager.bytes_downloaded)

                if response:
                    previous = current
                    interval = response.interval
                    self._empty_queue()  # 清空available_peers队列
                    for peer in response.peers:  # 更新available_peers队列
                        self.available_peers.put_nowait(peer)
            else:
                await asyncio.sleep(5)
        self.stop()

    def _empty_queue(self):
        while not self.available_peers.empty():
            self.available_peers.get_nowait()

    def stop(self):

        self.abort = True
        for peer in self.peers:
            peer.stop()
        self.piece_manager.close()
        self.tracker.close()

    def _on_block_retrieved(self, peer_id, piece_index, block_offset, data):
        self.piece_manager.block_received(peer_id=peer_id,
                                          piece_index=piece_index,
                                          block_offset=block_offset,
                                          data=data)
Example #3
0
class TorrentClient:
    """
    TorrentClient is a local peer which is responsible for managing connections
    to other peers to download and upload the data for the torrent.

    It makes periodic calls to the tracker registered to the torrent meta-info to get peer
    information for the torrent.

    Each received peer is stored in the queue from which the PeerConnection objects consume
    (maximum peer connections is defined by global MAX_PEER_CONNECTIONS variable).
    """

    def __init__(self, torrent):
        # Tracker object that defines methods to connect to the tracker
        self.tracker = Tracker(torrent)
        self.piece_manager = PieceManager(torrent)
        # Queue of potential peers which the PeerConnection objects will consume
        self.available_peers = asyncio.Queue()
        # List of PeerConnection objects which might be connected to the peer.
        # Else it is waiting to consume a peer from the available_peers queue
        self.peers = []
        self.abort = False

    async def start(self):
        """
        Start downloading the torrent data by connecting to the tracker and starting
        the workers
        """
        self.peers = [
            PeerConnection(
                self.available_peers,
                self.tracker.torrent.info_hash,
                self.tracker.peer_id,
                self.piece_manager,
                self._on_block_retrieved
            )
            for _ in range(MAX_PEER_CONNECTIONS)
        ]

        # Timestamp of the last announce call to the tracker
        previous_request = None
        # Default request interval (in sec) to tracker
        request_interval = 300  # sec

        while True:
            if self.abort:
                logging.warning("Aborting download")
                break

            if self.piece_manager.complete:
                logging.info("Download completed")
                break

            current_time = time.time()

            # Check if request_interval time has passed so make announce call to tracker
            if (not previous_request) or (previous_request + request_interval) < current_time:
                tracker_response = await self.tracker.connect(
                    first=previous_request if previous_request else False,
                    uploaded=self.piece_manager.bytes_uploaded,
                    downloaded=self.piece_manager.bytes_downloaded
                )

                if tracker_response:
                    logging.debug("Got tracker response")
                    previous_request = current_time
                    request_interval = tracker_response.interval
                    self._empty_queue()
                    for peer in tracker_response.peers:
                        if peer:
                            self.available_peers.put_nowait(peer)
            else:
                logging.warning("Waiting for next tracker announce call, interval is {request_interval}".format(
                    request_interval=request_interval))
                for peer in self.peers:
                    logging.debug("State of peer {id} is {status}".format(
                        id=peer.remote_id,
                        status=peer.my_state))
                await asyncio.sleep(5)
        self.stop()

    def stop(self):
        """
        Stop download and send stop signal to all peer objects
        """
        self.abort = True
        for peer in self.peers:
            peer.stop()
        self.piece_manager.close()
        self.tracker.close()

    def _empty_queue(self):
        """
        Remove all peers from the queue
        """
        while not self.available_peers.empty():
            self.available_peers.get_nowait()

    def _on_block_retrieved(self, peer_id, piece_index, block_offset, data):
        """
        Callback function passed to PeerConnection object called when block is
        retrieved from peer
        :param peer_id: The id of peer the block was retrieved from
        :param piece_index: The piece index of the block
        :param block_offset: Block offset inside the piece
        :param data: Binary block data
        :return:
        """
        self.piece_manager.block_received(
            peer_id=peer_id,
            piece_index=piece_index,
            block_offset=block_offset,
            data=data
        )