This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
/
goambi.go
254 lines (208 loc) · 5.86 KB
/
goambi.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"flag"
"fmt"
"github.com/RandomByte/colorfinder"
"image"
"image/color"
"image/draw"
_ "image/jpeg"
"log"
"math"
"net"
"os"
"os/exec"
"os/signal"
"runtime"
"strconv"
"syscall"
"time"
)
var targetAddress string
var lastColors [6]color.RGBA
type SubImager interface {
SubImage(r image.Rectangle) image.Image
}
func main() {
ip := flag.String("ip", "", "Target IP")
port := flag.Int("port", 0, "Target port")
flag.Parse()
if *ip == "" || *port == 0 {
log.Println("Missing target IP or port")
return
}
targetAddress = *ip + ":" + strconv.Itoa(*port)
log.Println("Target address:", targetAddress)
log.Println(runtime.GOMAXPROCS(8))
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill, syscall.SIGTERM)
defer signal.Stop(sig)
cam := Cam{}
defer cam.Kill()
cam.Setup()
for {
select {
case <-time.After(1 * time.Millisecond):
loop()
case s := <-sig:
log.Println("Got signal:", s)
log.Println("Quitting...")
return
}
}
}
func loop() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered", r)
}
}()
img := loadImage()
colors := computeDominatorColors(img, lastColors)
log.Println(colors)
lastColors = colors
sendToServer(colors)
}
func loadImage() *image.Image {
file, err := os.Open("pic.jpg")
if err != nil {
log.Panic("No picture found", err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
log.Panic(err)
}
bound := img.Bounds()
rgbaImg := image.NewRGBA(bound)
draw.Draw(rgbaImg, bound, img, bound.Min, draw.Src)
var ret image.Image = rgbaImg
return &ret
}
func computeDominatorColors(img *image.Image, lastColors [6]color.RGBA) [6]color.RGBA {
log.Println("Computing dominant colors...")
screen := *getScreen(img)
/*
Dividing screen into 6 areas
A B A
----- -----------------------------
| | | |
50% | 0 | 2 | 4 |
| | | |
----- -----------------------------
| | | |
50% | 1 | 3 | 5 |
| | | |
----- -----------------------------
| 20% | 40% | 20% |
*/
var (
areas [6]image.Image
colors [6]color.RGBA
)
bounds := screen.Bounds()
x0 := float64(bounds.Dx() / 100)
xA := int(math.Floor(x0 * 20))
y := int(math.Floor(float64(bounds.Dy() / 2)))
areaRect := image.Rect(bounds.Min.X, bounds.Min.Y, bounds.Min.X+xA, bounds.Min.Y+y)
areas[0] = screen.(SubImager).SubImage(areaRect)
areaRect = image.Rect(bounds.Min.X, bounds.Min.Y+y, bounds.Min.X+xA, bounds.Max.Y)
areas[1] = screen.(SubImager).SubImage(areaRect)
areaRect = image.Rect(bounds.Min.X+xA, bounds.Min.Y, bounds.Max.X-xA, bounds.Min.Y+y)
areas[2] = screen.(SubImager).SubImage(areaRect)
// Ignore area #3
// areaRect = image.Rect(bounds.Min.X+xA, bounds.Min.Y+y, bounds.Max.X-xA, bounds.Max.Y)
// areas[3] = screen.(SubImager).SubImage(areaRect)
areaRect = image.Rect(bounds.Max.X-xA, bounds.Min.Y, bounds.Max.X, bounds.Min.Y+y)
areas[4] = screen.(SubImager).SubImage(areaRect)
areaRect = image.Rect(bounds.Max.X-xA, bounds.Min.Y+y, bounds.Max.X, bounds.Max.Y)
areas[5] = screen.(SubImager).SubImage(areaRect)
processed := make(chan color.RGBA, 6)
for i := 0; i < len(areas); i++ {
if i == 3 { // Ignore area #3
continue
}
go processArea(areas[i], lastColors[i], processed)
}
for i := 0; i < len(areas); i++ {
if i == 3 { // Ignore area #3
continue
}
log.Println("Processed area", i)
colors[i] = <-processed
}
return colors
}
func processArea(area image.Image, lastColor color.RGBA, processed chan color.RGBA) {
newColor := colorfinder.Find(area.(*image.RGBA))
if lastColor != (color.RGBA{}) {
// Compare to last color
if getDifferenceOfInts(newColor.R, lastColor.R) < 10 {
// if newColor.R < lastColor.R {
log.Println("Prioritizing last red")
newColor.R = lastColor.R
// }
}
if getDifferenceOfInts(newColor.G, lastColor.G) < 10 {
// if newColor.G < lastColor.G {
log.Println("Prioritizing last green")
newColor.G = lastColor.G
// }
}
if getDifferenceOfInts(newColor.B, lastColor.B) < 10 {
// if newColor.B < lastColor.B {
log.Println("Prioritizing last blue")
newColor.B = lastColor.B
// }
}
}
processed <- newColor
}
func getDifferenceOfInts(x, y uint8) int {
return int(math.Abs(float64(x) - float64(y)))
}
func getScreen(img *image.Image) *image.Image {
return img
}
func sendToServer(colors [6]color.RGBA) {
var payload string
// Put together our payload. Will look something like this:
// /0:R070G045B028/1:R068G046B031/2:R066G044B029/3:R064G039B030/4:R064G040B028/5:R070G048B031
// For six areas, it'll always have a length of 90 chars.
for i := 0; i < len(colors); i++ {
if i == 3 { // Ignore area #3
payload += "/3:R000G000B000"
continue
}
payload += fmt.Sprintf("/%v:R%03dG%03dB%03d", i, colors[i].R, colors[i].G, colors[i].B)
}
conn, err := net.Dial("udp", targetAddress)
if err != nil {
fmt.Println(err)
return
}
fmt.Fprintf(conn, payload)
conn.Close()
}
type Cam struct {
Cmd *exec.Cmd
}
func (c *Cam) Setup() {
// Start raspistill in timelapse + burst mode
log.Println("Initializing raspistill process...")
c.Cmd = exec.Command("raspistill", "-v", "-n", "-t", "0", "--thumb", "none", "-o", "pic.jpg", "-roi", "0.51,0.35,0.33,0.24", "-w", "648", "-h", "486", "-tl", "0", "-bm", "-mm", "spot", "-ex", "spotlight", "-awb", "shade", "-ISO", "100")
err := c.Cmd.Start()
if err != nil {
log.Panic(err)
}
// Should wait for something in the stdout - but was unable to get it working :(
// So we just assume that nothing bad will happen when proceeding too soon
// Also, just in case, we wait a sec
time.Sleep(1000 * time.Millisecond)
}
func (c *Cam) Kill() {
err := c.Cmd.Process.Signal(os.Kill)
if err != nil {
log.Panic(err)
}
}