Skip to content

Commit df64d5b

Browse files
committed
extend interface
1 parent fd9c30c commit df64d5b

File tree

6 files changed

+111
-20
lines changed

6 files changed

+111
-20
lines changed

v2/blob/array_tracker_test.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,48 @@ func TestSimpleArrayTracker(t *testing.T) {
1515
rectHalfWidth := 75
1616

1717
correctOverallBlobies := 3
18+
commonOptions := BlobOptions{
19+
ClassID: classID,
20+
ClassName: className,
21+
MaxPointsInTrack: maxPointsInTrack,
22+
}
23+
for i := range objectOne {
24+
centerOne := objectOne[i]
25+
centerTwo := objectTwo[i]
26+
centerThree := objectThree[i]
27+
28+
rectOne := image.Rect(centerOne[0]-rectHalfWidth, centerOne[1]-rectHalfHeight, centerOne[0]+rectHalfWidth, centerOne[1]+rectHalfHeight)
29+
rectTwo := image.Rect(centerTwo[0]-rectHalfWidth, centerTwo[1]-rectHalfHeight, centerTwo[0]+rectHalfWidth, centerTwo[1]+rectHalfHeight)
30+
rectThree := image.Rect(centerThree[0]-rectHalfWidth, centerThree[1]-rectHalfHeight, centerThree[0]+rectHalfWidth, centerThree[1]+rectHalfHeight)
31+
32+
blobOne := NewSimpleBlobie(rectOne, &commonOptions)
33+
blobTwo := NewSimpleBlobie(rectTwo, &commonOptions)
34+
blobThree := NewSimpleBlobie(rectThree, &commonOptions)
35+
currentFrameBlobies := []Blobie{blobOne, blobTwo, blobThree}
36+
allblobies.MatchToExisting(currentFrameBlobies)
37+
38+
if correctOverallBlobies != len(allblobies.Objects) {
39+
t.Errorf("[Simple] Total number of blobs on frame %d should be %d, bot got %d", i, correctOverallBlobies, len(allblobies.Objects))
40+
}
41+
}
42+
}
43+
44+
func TestKalmanArrayTracker(t *testing.T) {
45+
maxPointsInTrack := 150
46+
classID := 1
47+
className := "just_an_object"
48+
allblobies := NewBlobiesDefaults()
49+
50+
rectHalfHeight := 30
51+
rectHalfWidth := 75
52+
53+
correctOverallBlobies := 3
54+
commonOptions := BlobOptions{
55+
ClassID: classID,
56+
ClassName: className,
57+
MaxPointsInTrack: maxPointsInTrack,
58+
TimeDeltaSeconds: 1.0,
59+
}
1860
for i := range objectOne {
1961
centerOne := objectOne[i]
2062
centerTwo := objectTwo[i]
@@ -24,14 +66,14 @@ func TestSimpleArrayTracker(t *testing.T) {
2466
rectTwo := image.Rect(centerTwo[0]-rectHalfWidth, centerTwo[1]-rectHalfHeight, centerTwo[0]+rectHalfWidth, centerTwo[1]+rectHalfHeight)
2567
rectThree := image.Rect(centerThree[0]-rectHalfWidth, centerThree[1]-rectHalfHeight, centerThree[0]+rectHalfWidth, centerThree[1]+rectHalfHeight)
2668

27-
blobOne := NewSimpleBlobie(rectOne, maxPointsInTrack, classID, className)
28-
blobTwo := NewSimpleBlobie(rectTwo, maxPointsInTrack, classID, className)
29-
blobThree := NewSimpleBlobie(rectThree, maxPointsInTrack, classID, className)
69+
blobOne := NewKalmanBlobie(rectOne, &commonOptions)
70+
blobTwo := NewKalmanBlobie(rectTwo, &commonOptions)
71+
blobThree := NewKalmanBlobie(rectThree, &commonOptions)
3072
currentFrameBlobies := []Blobie{blobOne, blobTwo, blobThree}
3173
allblobies.MatchToExisting(currentFrameBlobies)
3274

3375
if correctOverallBlobies != len(allblobies.Objects) {
34-
t.Errorf("Total number of blobs on frame %d should be %d, bot got %d", i, correctOverallBlobies, len(allblobies.Objects))
76+
t.Errorf("[Kalman] Total number of blobs on frame %d should be %d, bot got %d", i, correctOverallBlobies, len(allblobies.Objects))
3577
}
3678
}
3779
}

v2/blob/blob.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
)
99

1010
type Blobie interface {
11+
GetID() uuid.UUID
1112
GetCenter() image.Point
1213
GetCurrentRect() image.Rectangle
1314
GetPredictedNextPosition() image.Point
15+
GetTrack() []image.Point
1416
GetDiagonal() float64
1517
GetClassID() int
1618
GetClassName() string

v2/blob/kalman_blob.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type KalmanBlobie struct {
4646
}
4747

4848
// NewKalmanBlobie - Constructor for KalmanBlobie (default values)
49-
func NewKalmanBlobie(rect image.Rectangle, maxPointsInTrack int, dT float64, classID int, className string) Blobie {
49+
func NewKalmanBlobie(rect image.Rectangle, options *BlobOptions) Blobie {
5050
center := image.Pt((rect.Min.X*2+rect.Dx())/2, (rect.Min.Y*2+rect.Dy())/2)
5151
width := float64(rect.Dx())
5252
height := float64(rect.Dy())
@@ -58,22 +58,30 @@ func NewKalmanBlobie(rect image.Rectangle, maxPointsInTrack int, dT float64, cla
5858
Diagonal: math.Sqrt(math.Pow(width, 2) + math.Pow(height, 2)),
5959
AspectRatio: width / height,
6060
Track: []image.Point{center},
61-
TrackTime: []time.Time{time.Now()},
62-
maxPointsInTrack: maxPointsInTrack,
6361
isExists: true,
6462
isStillBeingTracked: true,
6563
noMatchTimes: 0,
6664
pointTracker: kf.NewPointTracker(),
6765
yMatrix: mat.NewDense(2, 1, []float64{centerX, centerY}),
6866
uMatrix: mat.NewDense(4, 1, []float64{0.0, 0.0, 0.0, 0.0}),
69-
dt: dT,
70-
classID: classID,
71-
className: className,
7267
crossedLine: false,
7368
}
7469
kalmanBlobie.pointTracker.SetStateValue(centerX, centerY, 0, 0)
75-
kalmanBlobie.pointTracker.SetTime(dT)
76-
70+
if options != nil {
71+
kalmanBlobie.TrackTime = []time.Time{options.Time}
72+
kalmanBlobie.maxPointsInTrack = options.MaxPointsInTrack
73+
kalmanBlobie.classID = options.ClassID
74+
kalmanBlobie.className = options.ClassName
75+
kalmanBlobie.dt = options.TimeDeltaSeconds
76+
kalmanBlobie.pointTracker.SetTime(options.TimeDeltaSeconds)
77+
} else {
78+
kalmanBlobie.TrackTime = []time.Time{time.Now()}
79+
kalmanBlobie.maxPointsInTrack = 10
80+
kalmanBlobie.classID = -1
81+
kalmanBlobie.className = "No class"
82+
kalmanBlobie.dt = 1.0
83+
kalmanBlobie.pointTracker.SetTime(1.0)
84+
}
7785
return &kalmanBlobie
7886
}
7987

@@ -143,6 +151,10 @@ func (b *KalmanBlobie) Update(newb Blobie) error {
143151
return nil
144152
}
145153

154+
func (sb *KalmanBlobie) GetID() uuid.UUID {
155+
return sb.ID
156+
}
157+
146158
func (sb *KalmanBlobie) GetCenter() image.Point {
147159
return sb.Center
148160
}
@@ -151,6 +163,10 @@ func (sb *KalmanBlobie) GetCurrentRect() image.Rectangle {
151163
return sb.CurrentRect
152164
}
153165

166+
func (sb *KalmanBlobie) GetTrack() []image.Point {
167+
return sb.Track
168+
}
169+
154170
func (sb *KalmanBlobie) GetDiagonal() float64 {
155171
return sb.Diagonal
156172
}

v2/blob/options.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package blob
2+
3+
import (
4+
"time"
5+
)
6+
7+
// BlobOptions Options for blob
8+
type BlobOptions struct {
9+
ClassID int
10+
ClassName string
11+
MaxPointsInTrack int
12+
Time time.Time
13+
TimeDeltaSeconds float64
14+
}

v2/blob/simple_blob.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ type SimpleBlobie struct {
3434
crossedLine bool
3535
}
3636

37+
func (sb *SimpleBlobie) GetID() uuid.UUID {
38+
return sb.ID
39+
}
40+
3741
func (sb *SimpleBlobie) GetCenter() image.Point {
3842
return sb.Center
3943
}
@@ -42,6 +46,10 @@ func (sb *SimpleBlobie) GetCurrentRect() image.Rectangle {
4246
return sb.CurrentRect
4347
}
4448

49+
func (sb *SimpleBlobie) GetTrack() []image.Point {
50+
return sb.Track
51+
}
52+
4553
func (sb *SimpleBlobie) GetDiagonal() float64 {
4654
return sb.Diagonal
4755
}
@@ -75,27 +83,34 @@ func (sb *SimpleBlobie) SetExists(isExists bool) {
7583
}
7684

7785
// NewSimpleBlobie - Constructor for SimpleBlobie (default values)
78-
func NewSimpleBlobie(rect image.Rectangle, maxPointsInTrack, classID int, className string) Blobie {
86+
func NewSimpleBlobie(rect image.Rectangle, options *BlobOptions) Blobie {
7987
center := image.Pt((rect.Min.X*2+rect.Dx())/2, (rect.Min.Y*2+rect.Dy())/2)
8088
width := float64(rect.Dx())
8189
height := float64(rect.Dy())
82-
return &SimpleBlobie{
90+
blobie := SimpleBlobie{
8391
CurrentRect: rect,
8492
Center: center,
8593
Area: width * height,
8694
Diagonal: math.Sqrt(math.Pow(width, 2) + math.Pow(height, 2)),
8795
AspectRatio: width / height,
8896
Track: []image.Point{center},
89-
TrackTime: []time.Time{time.Now()},
90-
maxPointsInTrack: maxPointsInTrack,
9197
isExists: true,
9298
isStillBeingTracked: true,
9399
noMatchTimes: 0,
94-
95-
classID: classID,
96-
className: className,
97-
crossedLine: false,
100+
crossedLine: false,
101+
}
102+
if options != nil {
103+
blobie.TrackTime = []time.Time{options.Time}
104+
blobie.maxPointsInTrack = options.MaxPointsInTrack
105+
blobie.classID = options.ClassID
106+
blobie.className = options.ClassName
107+
} else {
108+
blobie.TrackTime = []time.Time{time.Now()}
109+
blobie.maxPointsInTrack = 10
110+
blobie.classID = -1
111+
blobie.className = "No class"
98112
}
113+
return &blobie
99114
}
100115

101116
// NewBlobieDefaults - Constructor for SimpleBlobie (default values)

v2/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfF
3434
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
3535
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
3636
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
37+
gocv.io/x/gocv v0.23.0 h1:3Fgbt06/uR8Zf9emWndhjbUjdrw+nto69R/b4noFydY=
38+
gocv.io/x/gocv v0.23.0/go.mod h1:Rar2PS6DV+T4FL+PM535EImD/h13hGVaHhnCu1xarBs=
3739
gocv.io/x/gocv v0.26.0 h1:1azNvYEM245YN1bdw/WdX5YJzLg3Sr4STX0MqdWBIXM=
3840
gocv.io/x/gocv v0.26.0/go.mod h1:7Ju5KbPo+R85evmlhhKPVMwXtgDRNX/PtfVfbToSrLU=
3941
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

0 commit comments

Comments
 (0)