Esempio n. 1
0
    def __call__(self, image, box, state, display_tracking_speed=False):
        """Track the object in an image using the pre-calculated internal state
        and a new image. (init must be run before this function.)
        
        Args:
            image(A(H,W,3), uint8): Input image
            box : Unused; only here for compatibility.
            display_tracking_speed (bool, optional): Option to print trakcing fps
        
        Returns:
            box(A(4)): Returns a relative bounding box of type [center_x, center_y, width, height] 
            state(?): Returns meta-information required for maintaining tracking state.

        Note:
            A(shape[,dtype=float]) in the above documentation is a shorthand for numpy.array.
        """
        # tracking and visualization
        toc = 0

        tic = cv2.getTickCount()
        state = SiamRPN_track(state, image, use_gpu=self.use_gpu_)  # track
        toc += cv2.getTickCount() - tic

        box = (
            state["target_pos"][0],
            state["target_pos"][1],
            state["target_sz"][0],
            state["target_sz"][1],
        )

        img_h, img_w = np.shape(image)[:2]
        box = utils.convert_to_relative([img_w, img_h], box)

        return box, state
Esempio n. 2
0
    def track(self, img):
        self.count -= 1

        # get siamrpn tracker's result
        state = SiamRPN_track(self.state, img)
        res = cxy_wh_2_rect(state['target_pos'], state['target_sz'])
        res = [int(l) for l in res]

        if self.count <= 0:
            # use detector to examine
            human = self.yolo.detect_human(img)
            if len(human) == 0:
                self.human_detected = False
                # if no detection, return sot result
                return res
            self.count = self.max_count

            # compare iou between detect result and tracking result
            bbox = [
                human[0], human[1], human[2] - human[0], human[3] - human[1]
            ]
            bbox = [int(l) for l in bbox]
            if iou(bbox, res) < self.iou_threshold:
                self.init_SOT(bbox, img)
                return bbox
            else:
                return res
        return res
Esempio n. 3
0
    def update(self, image):
        self.state = SiamRPN_track(self.state, image)
        target_pos = self.state['target_pos']
        target_size = self.state['target_sz']
        bbox = [target_pos[0], target_pos[1], target_size[0], target_size[1]]

        net = self.state['net']

        return bbox
Esempio n. 4
0
    def update(self, image):
        image = np.asarray(image)

        # track
        self.state = SiamRPN_track(self.state, image)

        # get box
        box = cxy_wh_2_rect(self.state['target_pos'], self.state['target_sz'])
        box = np.array([i for i in box])

        return box
Esempio n. 5
0
def main():
    cap = cv2.VideoCapture(0)
    net = SiamRPNvot()
    net.load_state_dict(torch.load('./DaSiamRPN/SiamRPNVOT.model'))
    net.eval().cuda()
    
    _, im = cap.read()
    init_box = cv2.selectROI('selectROI', im)
    print(init_box)
    target_pos, target_sz = rect_2_cxy_wh(init_box)
    state = SiamRPN_init(im, target_pos, target_sz, net)

    while True:
        _, im = cap.read()
        state = SiamRPN_track(state, im)  # track
        res = cxy_wh_2_rect(state['target_pos'], state['target_sz'])
        res = [int(l) for l in res]
        cv2.rectangle(im, (res[0], res[1]), (res[0] + res[2], res[1] + res[3]), (0, 255, 255), 3)
        cv2.imshow('SiamRPN', im)
        cv2.waitKey(1)
Esempio n. 6
0
def main():
    # load model
    net = SiamRPNvot()
    net.load_state_dict(torch.load('./DaSiamRPN/SiamRPNVOT.model'))
    net.eval().cuda()

    # image and init box
    dataset_folder = './test_video/Panda'
    ground_truth = np.loadtxt(join(dataset_folder, 'groundtruth_rect.txt'),
                              delimiter=',')
    init_box = ground_truth[0, :]
    image_list = [
        join(dataset_folder, 'img', x)
        for x in list(listdir(join(dataset_folder, 'img')))
    ]

    # tracker init
    target_pos, target_sz = rect_2_cxy_wh(init_box)
    im = cv2.imread(image_list[0])
    state = SiamRPN_init(im, target_pos, target_sz, net)

    toc = 0
    # tracking and visualization
    for image_name in image_list[1:]:
        im = cv2.imread(image_name)
        tic = cv2.getTickCount()
        state = SiamRPN_track(state, im)  # track
        toc += cv2.getTickCount() - tic
        res = cxy_wh_2_rect(state['target_pos'], state['target_sz'])
        res = [int(l) for l in res]
        cv2.rectangle(im, (res[0], res[1]), (res[0] + res[2], res[1] + res[3]),
                      (0, 255, 255), 3)
        cv2.imshow('SiamRPN', im)
        cv2.waitKey(1)

    print('Tracking Speed {:.1f}fps'.format(
        (len(image_list) - 1) / (toc / cv2.getTickFrequency())))
net.load_state_dict(torch.load('./DaSiamRPN/SiamRPNVOT.model'))
net.eval().cuda()

init_box = []

while not detect_human:
    _, img = cap.read()
    human = yolo.detect_human(img)
    print(human)
    if len(human) == 0:
        continue
    else:
        detect_human = True
        init_box = [
            human[0], human[1], human[2] - human[0], human[3] - human[1]
        ]
print('human detected ! start initialize SiamRPN')

target_pos, target_sz = rect_2_cxy_wh(init_box)
state = SiamRPN_init(img, target_pos, target_sz, net)

while True:
    _, img = cap.read()
    state = SiamRPN_track(state, img)  # track
    res = cxy_wh_2_rect(state['target_pos'], state['target_sz'])
    res = [int(l) for l in res]
    cv2.rectangle(img, (res[0], res[1]), (res[0] + res[2], res[1] + res[3]),
                  (0, 255, 255), 3)
    cv2.imshow('SiamRPN', img)
    cv2.waitKey(1)