File size: 2,212 Bytes
23d8387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# This file is part of OpenCV Zoo project.
# It is subject to the license terms in the LICENSE file found in the same directory.
#
# Copyright (C) 2021, Shenzhen Institute of Artificial Intelligence and Robotics for Society, all rights reserved.
# Third party copyrights are property of their respective owners.

import numpy as np
import cv2 as cv

class DaSiamRPN:
    def __init__(self, model_path, kernel_cls1_path, kernel_r1_path, backend_id=0, target_id=0):
        self._model_path = model_path
        self._kernel_cls1_path = kernel_cls1_path
        self._kernel_r1_path = kernel_r1_path
        self._backend_id = backend_id
        self._target_id = target_id

        self._param = cv.TrackerDaSiamRPN_Params()
        self._param.model = self._model_path
        self._param.kernel_cls1 = self._kernel_cls1_path
        self._param.kernel_r1 = self._kernel_r1_path
        self._param.backend = self._backend_id
        self._param.target = self._target_id
        self._model = cv.TrackerDaSiamRPN.create(self._param)

    @property
    def name(self):
        return self.__class__.__name__

    def setBackend(self, backend_id):
        self._backend_id = backend_id
        self._param = cv.TrackerDaSiamRPN_Params()
        self._param.model = self._model_path
        self._param.kernel_cls1 = self._kernel_cls1_path
        self._param.kernel_r1 = self._kernel_r1_path
        self._param.backend = self._backend_id
        self._param.target = self._target_id
        self._model = cv.TrackerDaSiamRPN.create(self._param)

    def setTarget(self, target_id):
        self._target_id = target_id
        self._param = cv.TrackerDaSiamRPN_Params()
        self._param.model = self._model_path
        self._param.kernel_cls1 = self._kernel_cls1_path
        self._param.kernel_r1 = self._kernel_r1_path
        self._param.backend = self._backend_id
        self._param.target = self._target_id
        self._model = cv.TrackerDaSiamRPN.create(self._param)

    def init(self, image, roi):
        self._model.init(image, roi)

    def infer(self, image):
        isLocated, bbox = self._model.update(image)
        score = self._model.getTrackingScore()
        return isLocated, bbox, score