Skip to content

Commit 7918fc1

Browse files
authored
Add files via upload
1 parent fd1da1d commit 7918fc1

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

python/KCF.pyx

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from cvt cimport *
2+
from libcpp cimport bool
3+
4+
cdef extern from "../src/kcftracker.hpp":
5+
cdef cppclass KCFTracker:
6+
KCFTracker(bool, bool, bool, bool)
7+
void init(Rect, Mat)
8+
Rect update(Mat)
9+
10+
cdef class kcftracker:
11+
cdef KCFTracker *classptr
12+
13+
def __cinit__(self, hog, fixed_window, multiscale, lab):
14+
self.classptr = new KCFTracker(hog, fixed_window, multiscale, lab)
15+
16+
def __dealloc(self):
17+
del self.classptr
18+
19+
def init(self, rectlist, ary):
20+
self.classptr.init(pylist2cvrect(rectlist), nparray2cvmat(ary))
21+
22+
def update(self, ary):
23+
rect = self.classptr.update(nparray2cvmat(ary))
24+
return cvrect2pylist(rect)

python/cvt.pyx

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import numpy as np
2+
from libc.string cimport memcpy
3+
4+
def nptype2cvtype(npty, nch):
5+
if npty == np.uint8:
6+
r = CV_8U
7+
elif npty == np.int8:
8+
r = CV_8S
9+
elif npty == np.uint16:
10+
r = CV_16U
11+
elif npty == np.int16:
12+
r = CV_16S
13+
elif npty == np.int32:
14+
r = CV_32S
15+
elif npty == np.float32:
16+
r = CV_32F
17+
elif npty == np.float64:
18+
r = CV_64F
19+
20+
return CV_MAKETYPE(r, nch)
21+
22+
def cvtype2nptype(cvty):
23+
d = CV_MAT_DEPTH(cvty)
24+
nch = CV_MAT_CN(cvty)
25+
26+
if d == CV_8U:
27+
r = np.uint8
28+
elif d == CV_8S:
29+
r = np.int8
30+
elif d == CV_16U:
31+
r = np.uint16
32+
elif d == CV_16S:
33+
r = np.int16
34+
elif d == CV_32S:
35+
r = np.int32
36+
elif d == CV_32F:
37+
r = np.float32
38+
elif d == CV_64F:
39+
r = np.float64
40+
return [r, nch]
41+
42+
cdef Mat nparray2cvmat(np.ndarray ary):
43+
if ary.ndim==3 and ary.shape[2]==1:
44+
ary = ary.reshape(ary.shape[0], ary.shape[1])
45+
46+
cdef int r = ary.shape[0]
47+
cdef int c = ary.shape[1]
48+
49+
if ary.ndim == 2:
50+
nch = 1
51+
else:
52+
nch = ary.shape[2]
53+
54+
if not ary.flags['C_CONTIGUOUS']:
55+
ary = np.ascontiguousarray(ary, dtype=ary.dtype)
56+
#cdef Mat outmat = Mat(r, c, nptype2cvtype(ary.dtype,nch))
57+
#memcpy(outmat.data, ary.data, ary.nbytes)
58+
cdef Mat outmat = Mat(r, c, nptype2cvtype(ary.dtype,nch), ary.data, 0) #memoryview
59+
return outmat
60+
61+
cdef np.ndarray cvmat2nparray(Mat &inmat):
62+
[ty, nch] = cvtype2nptype(inmat.type())
63+
64+
if nch == 1:
65+
dim = (inmat.rows, inmat.cols)
66+
else:
67+
dim = (inmat.rows, inmat.cols, nch)
68+
69+
cdef np.ndarray ary = np.empty(dim, dtype=ty)
70+
memcpy(ary.data, inmat.data, ary.nbytes)
71+
return ary
72+
73+
cdef inline Rect pylist2cvrect(list rectlist):
74+
cdef Rect rect = Rect(rectlist[0], rectlist[1], rectlist[2], rectlist[3])
75+
return rect
76+
77+
cdef inline list cvrect2pylist(Rect &rect):
78+
return [rect.x, rect.y, rect.width, rect.height]

python/cvtype.pxd

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
cdef extern from "opencv2/core/cvdef.h":
2+
cdef int CV_8U
3+
cdef int CV_8S
4+
cdef int CV_16U
5+
cdef int CV_16S
6+
cdef int CV_32S
7+
cdef int CV_32F
8+
cdef int CV_64F
9+
cdef int CV_MAKETYPE(int, int)
10+
cdef int CV_MAT_DEPTH(int)
11+
cdef int CV_MAT_CN(int)
12+
13+
cdef extern from "opencv2/core/mat.hpp" namespace "cv":
14+
cdef cppclass Mat:
15+
Mat()
16+
Mat(int, int, int)
17+
Mat(int, int, int, void*, int)
18+
int type()
19+
void* data
20+
int cols
21+
int rows
22+
23+
cdef extern from "opencv2/core/types.hpp" namespace "cv":
24+
cdef cppclass Point:
25+
Point()
26+
Point(int, int)
27+
int x, y
28+
cdef cppclass Size:
29+
Size()
30+
Size(int, int)
31+
int width, height
32+
cdef cppclass Rect:
33+
Rect()
34+
Rect(int, int, int, int)
35+
int x, y, width, height

0 commit comments

Comments
 (0)