-
Notifications
You must be signed in to change notification settings - Fork 871
/
main.go
103 lines (87 loc) · 2.22 KB
/
main.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// What it does:
//
// This example uses one of the Tracker classes from opencv_contrib to track a region of interest (e.g. a face)
// and draws a rectangle around it, before displaying it within a Window.
//
// in this example, users have to select an initial roi with the mouse, and press enter, to start the tracking
// (but the roi could also come from e.g. a previous cascade based detection)
//
// also see https://docs.opencv.org/master/d2/d0a/tutorial_introduction_to_tracker.html for a tutorial
//
// How to run:
//
// tracking [camera ID]
//
// go run ./cmd/tracking/main.go 0
//
package main
import (
"fmt"
"image/color"
"os"
"gocv.io/x/gocv"
"gocv.io/x/gocv/contrib"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("How to run:\n\ttracking [camera ID]")
return
}
// parse args
deviceID := os.Args[1]
// open webcam
webcam, err := gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Printf("Error opening video capture device: %v\n", deviceID)
return
}
defer webcam.Close()
// open display window
window := gocv.NewWindow("Tracking")
defer window.Close()
// create a tracker instance
// (one of MIL, KCF, TLD, MedianFlow, Boosting, MOSSE or CSRT)
tracker := contrib.NewTrackerKCF()
defer tracker.Close()
// prepare image matrix
img := gocv.NewMat()
defer img.Close()
// read an initial image
if ok := webcam.Read(&img); !ok {
fmt.Printf("cannot read device %v\n", deviceID)
return
}
// let the user mark a ROI to track
rect := gocv.SelectROI("Tracking", img)
if rect.Max.X == 0 {
fmt.Printf("user cancelled roi selection\n")
return
}
// initialize the tracker with the image & the selected roi
init := tracker.Init(img, rect)
if !init {
fmt.Printf("Could not initialize the Tracker")
return
}
// color for the rect to draw
blue := color.RGBA{0, 0, 255, 0}
fmt.Printf("Start reading device: %v\n", deviceID)
for {
if ok := webcam.Read(&img); !ok {
fmt.Printf("Device closed: %v\n", deviceID)
return
}
if img.Empty() {
continue
}
// update the roi
rect, _ := tracker.Update(img)
// draw it.
gocv.Rectangle(&img, rect, blue, 3)
// show the image in the window, and wait 10 millisecond
window.IMShow(img)
if window.WaitKey(10) >= 0 {
break
}
}
}