-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfinder.go
147 lines (116 loc) · 3.31 KB
/
finder.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package helios
import (
"fmt"
"github.com/go-vgo/robotgo"
"github.com/vcaesar/gcv"
"os"
"time"
)
type Finder struct {
screen *Screen
pollInterval *PollInterval
}
func NewFinder(screen *Screen, pollInterval *PollInterval) *Finder {
return &Finder{screen: screen, pollInterval: pollInterval}
}
func (f *Finder) Find(i *Image, r *Region) *Match {
// Currently only works for the main monitor.
backgroundImg := robotgo.CaptureImg()
templateImagePath := "./template.png"
// Because of bug: https://github.com/vcaesar/gcv/issues/3, we have to save the screenshot first
// and then use it.
robotgo.SavePng(backgroundImg, templateImagePath)
templateImg, _, err := robotgo.DecodeImg(templateImagePath)
if err != nil {
// @todo return error too?
fmt.Println(err)
return nil
}
if err := os.Remove(templateImagePath); err != nil {
fmt.Println(err)
return nil
}
subImg, _, err := robotgo.DecodeImg(i.GetPath())
if err != nil {
return nil
}
// This just calls OpenCV MinMaxLoc()
// These two give different results, we need both....
_, maxConfidence, _, maxLoc := gcv.FindImg(subImg, templateImg)
if maxConfidence < float32(i.confidenceThreshold) {
return nil
}
scaleSize := robotgo.ScaleF()
region := &Region{
topLeft: &Point{
x: float64(maxLoc.X) / scaleSize,
y: float64(maxLoc.Y) / scaleSize,
},
width: i.img.Bounds().Size().X / int(scaleSize),
height: i.img.Bounds().Size().Y / int(scaleSize),
screen: f.screen,
}
return NewMatch(i, maxConfidence, f.screen, f.screen.highlighter, region)
}
func (f *Finder) FindAll(i *Image, r *Region) []*Match {
// Currently only works for the main monitor.
backgroundImg := robotgo.CaptureImg()
templateImagePath := "./template.png"
// Because of bug: https://github.com/vcaesar/gcv/issues/3, we have to save the screenshot first
// and then use it.
robotgo.SavePng(backgroundImg, templateImagePath)
templateImg, _, err := robotgo.DecodeImg(templateImagePath)
if err != nil {
// @todo return error too?
fmt.Println(err)
return nil
}
if err := os.Remove(templateImagePath); err != nil {
fmt.Println(err)
return nil
}
subImg, _, err := robotgo.DecodeImg(i.GetPath())
if err != nil {
return nil
}
results := gcv.FindAllImg(subImg, templateImg)
scaleSize := robotgo.ScaleF()
var matches []*Match
for _, result := range results {
maxConfidence := float32(result.MaxVal[0])
if result.MaxVal[0] < maxConfidence {
continue
}
region := &Region{
topLeft: &Point{
x: float64(result.TopLeft.X) / scaleSize,
y: float64(result.TopLeft.Y) / scaleSize,
},
width: i.img.Bounds().Size().X / int(scaleSize),
height: i.img.Bounds().Size().Y / int(scaleSize),
screen: f.screen,
}
matches = append(matches, NewMatch(i, maxConfidence, f.screen, f.screen.highlighter, region))
}
return matches
}
func (f *Finder) Wait(i *Image, r *Region, t time.Duration) *Match {
// Default to 0.25 seconds if config not provided for PollInterval.
pollInterval := f.pollInterval
if f.pollInterval == nil {
pollInterval = &PollInterval{100 * time.Millisecond}
}
for {
if time.Now().Unix() > time.Now().Add(t).Unix() {
break
}
if match := f.Find(i, r); match != nil {
return match
}
time.Sleep(pollInterval.Duration)
}
return nil
}
func inBetween(i, min, max float64) bool {
return (i >= min) && (i <= max)
}