/
fetcher.go
265 lines (212 loc) · 5.27 KB
/
fetcher.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
255
256
257
258
259
260
261
262
263
264
265
/* JOB: Fetch Images and Save to File
*/
package main
import (
"fmt"
"image"
"image/color"
"log"
"net/http"
"time"
mjpeg "GoCamCapture/mjpeg"
)
type computeBlock struct {
stamp time.Time
lum uint8
computeImg *image.Gray
diffImg *image.Gray
srcImg image.Image
diffVal int
}
const (
DIFF_VAL_LOWPASS = 20
DIFF_VAL_CUTOFF = 30500
)
func captureFilterCameraPipe(addr string, name string) (shutdown chan int, lastFile chan string) {
shutdown = make(chan int)
lastFile = make(chan string, 5)
everyFrame := make(chan image.Image, 5)
everyBlock := make(chan computeBlock, 5)
filterBlock := make(chan computeBlock, 5)
go fetchMPEGCamLoop(addr, everyFrame, shutdown)
go makeComputeBlock(everyFrame, everyBlock)
go checkNewImage(everyBlock, filterBlock)
go saveLoopToFile(filterBlock, name, lastFile)
return shutdown, lastFile
}
func fetchMPEGCamLoop(addr string, outImg chan image.Image, shutdown chan int) {
var decodeErr error
var img image.Image
for {
resp, errA := http.Get(addr)
if errA != nil {
log.Println(addr, errA)
return
}
log.Println("Fetching... ", addr, decodeErr)
d, err := mjpeg.NewDecoderFromResponse(resp)
if err != nil {
log.Println("Failed to create Decoder:", addr, err)
return
}
for decodeErr = d.Decode(&img); decodeErr == nil; decodeErr = d.Decode(&img) {
select {
case <-shutdown:
close(outImg)
return
default:
outImg <- img
}
}
}
}
func makeComputeBlock(srcImg chan image.Image, outBlock chan computeBlock) {
for {
img, ok := <-srcImg
if !ok {
close(outBlock)
return
}
cb := computeBlock{
stamp: time.Now(),
srcImg: img,
computeImg: ToComputeImageManual(img),
}
cb.lum = lumAvg(cb.computeImg)
outBlock <- cb
}
}
func checkNewImage(inBlock chan computeBlock, outBlock chan computeBlock) {
var prevBlock computeBlock
prevBlock.computeImg = nil
for {
newBlk, ok := <-inBlock
if !ok {
close(outBlock)
return
}
if prevBlock.computeImg == nil {
// First Image
prevBlock = newBlk
outBlock <- prevBlock
} else {
// Compare Difference
newBlk.diffImg = DiffImg(prevBlock.computeImg, newBlk.computeImg)
newBlk.diffVal = totalValFilter(newBlk.diffImg, DIFF_VAL_LOWPASS)
if newBlk.diffVal > DIFF_VAL_CUTOFF {
prevBlock = newBlk
outBlock <- prevBlock
}
}
}
}
func saveLoopToFile(inBlock chan computeBlock, cameraName string, outfilename chan string) {
historyBlocks := []computeBlock{}
for {
newBlk, ok := <-inBlock
if !ok {
saveMovie(cameraName)
saveCompImage(historyBlocks)
close(outfilename)
return
}
// Save To File
newFilename := fmt.Sprintf("%s/_%s_%d.jpg", CaptureFolder, cameraName, newBlk.stamp.UnixNano())
if newBlk.diffImg != nil {
rgbImg := ToRGBAImage(newBlk.srcImg)
DrawClock(rgbImg, &newBlk.stamp)
saveJPEGToFolder(newFilename, rgbImg)
}
// Non Blocking Channel
select {
case outfilename <- newFilename:
default:
}
// Clear out mem
newBlk.srcImg = nil
historyBlocks = append(historyBlocks, newBlk)
// Do Hourly Reports
if newBlk.stamp.Hour() != historyBlocks[0].stamp.Hour() {
// Start Movie Saving
go saveMovie(cameraName)
saveCompImage(historyBlocks)
// Clear Out
historyBlocks = []computeBlock{}
}
}
}
func saveCompImage(historyBlocks []computeBlock) {
imgList := make([]*image.Gray, len(historyBlocks), len(historyBlocks))
for i, v := range historyBlocks {
imgList[i] = v.diffImg
}
c := ExposeCompose(imgList)
if c != nil {
saveJPEGToFolder("_compose.jpg", c)
saveGIFToFolder("_compose.gif", c, 256)
} else {
log.Println("Expose was NIL")
}
}
//------------------------------------------------------------------------------
// Lum
func makeLumHourlyImg(blkList []computeBlock) *image.Paletted {
// Make Colour Pal
numColours := 256
w := 60
h := 60
cg := ColourGrad([]GradStop{
{0.0, color.RGBA{0, 0, 255, 255}},
{0.5, color.RGBA{0, 255, 0, 255}},
{1.0, color.RGBA{255, 0, 0, 255}},
})
tempPal := cg.makePal(0.0, 1.0, numColours)
m := image.NewPaletted(image.Rect(0, 0, w, h), tempPal)
// Start
for _, blk := range blkList {
pOff := blk.stamp.Second() + blk.stamp.Minute()*60
m.Pix[pOff] = blk.lum
}
return m
}
func makeLumTimeline(blkList []computeBlock) *image.Paletted {
// Make Colour Pal
numColours := 256
width := len(blkList)
height := numColours
cg := ColourGrad([]GradStop{
{0.0, color.RGBA{0, 0, 255, 255}},
{0.5, color.RGBA{0, 255, 0, 255}},
{1.0, color.RGBA{255, 0, 0, 255}},
})
tempPal := cg.makePal(0.0, 1.0, numColours)
m := image.NewPaletted(image.Rect(0, 0, width, height), tempPal)
for i, blk := range blkList {
hOff := blk.lum
for off, y := i, 0; y <= int(hOff); off, y = i+y*width, y+1 {
m.Pix[off] = hOff
}
}
return m
}
func makeMotionReport(dVal []int, maxVal int) *image.Paletted {
// Make Colour Pal
width := len(dVal)
height := maxVal / 1000
tempPal := color.Palette([]color.Color{
color.RGBA{0, 0, 0, 0},
color.RGBA{255, 255, 255, 255},
})
m := image.NewPaletted(image.Rect(0, 0, width, height), tempPal)
for i, v := range dVal {
hOff := int(v / 1000)
if hOff >= height {
hOff = height - 1
fmt.Println("Height too large, corrected")
}
for off, y := i, 0; y <= hOff; off, y = off+width, y+1 {
m.Pix[off] = 1
}
}
return m
}