streamwatcher/stream.go
2020-07-12 12:04:44 +00:00

284 lines
8.4 KiB
Go
Executable file

package main
import (
"encoding/json"
"image"
"image/color"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"gocv.io/x/gocv"
)
// Stream represents a stream to monitor
type Stream struct {
Name string `json:"name"`
URL string `json:"url"`
Base64 string `json:"base64"`
Interval int `json:"interval"`
Watches []Watch `json:"watches"`
FileLock sync.Mutex `json:"lock"`
}
// NewStream creates a new Stream Object
func NewStream(Name string, URL string) Stream {
base64 := URLToBase64(URL)
stream := Stream{
Name: Name,
URL: URL,
Base64: base64,
Interval: 5000,
Watches: make([]Watch, 0),
}
stream.Watches = append(stream.Watches,
Watch{
Name: "test",
Color: color.RGBA{255, 0, 255, 255},
Areas: []image.Rectangle{
image.Rect(30, 30, 400, 200),
image.Rect(400, 30, 500, 200),
image.Rect(400, 200, 500, 500),
},
},
Watch{
Name: "test2",
Color: color.RGBA{0, 255, 255, 255},
Areas: []image.Rectangle{
image.Rect(800, 30, 1000, 500),
image.Rect(700, 500, 1200, 700),
},
},
Watch{
Name: "test3",
Color: color.RGBA{255, 255, 0, 255},
Areas: []image.Rectangle{
image.Rect(50, 400, 450, 700),
},
},
)
log.Println("TEST")
stream.WriteStreamJSON()
return stream
}
// Update gets called by UpdateInterval Interval milliseconds to fetch the latest instant
func (s Stream) Update() {
log.Print("Update: ", s.Name, " - ", s.URL)
resp, err := http.Get(s.URL)
if err != nil {
log.Fatal(err)
}
img, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
mat, err := gocv.IMDecode(img, gocv.IMReadColor)
if err != nil {
log.Fatal("Could not IMDecode img")
}
go gocv.IMWrite(s.GetCurrentColorInstantPath(), mat)
currentGrey := gocv.NewMat()
gocv.CvtColor(mat, &currentGrey, gocv.ColorBGRAToGray)
gocv.GaussianBlur(currentGrey, &currentGrey, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect)
go s.SaveStreamInstant(currentGrey)
if !s.PreviousInstantPathExists() {
return
}
diff := gocv.NewMat()
previousGrey := s.IMReadPrevious()
gocv.AbsDiff(previousGrey, currentGrey, &diff)
gocv.Threshold(diff, &diff, 100, 255, gocv.ThresholdBinary)
dilateKernel := gocv.GetStructuringElement(gocv.MorphRect, image.Pt(20, 20))
gocv.Dilate(diff, &diff, dilateKernel)
debug := gocv.NewMat()
diff.CopyTo(&debug)
gocv.CvtColor(debug, &debug, gocv.ColorGrayToBGRA)
contours := gocv.FindContours(diff, gocv.RetrievalExternal, gocv.ChainApproxSimple)
for _, watch := range s.Watches {
skipNextAreas := true // Still draw all area rectangles
for _, area := range watch.Areas {
gocv.Rectangle(&debug, area, watch.Color, 10)
if skipNextAreas && s.CheckWatchAreas(area, contours) {
go watch.CopyInstant(s.GetCurrentColorInstantPath(), s)
skipNextAreas = false
break
}
}
}
go gocv.IMWrite(s.GetDebugInstantPath(), debug)
}
// CheckWatchAreas unexported
func (s Stream) CheckWatchAreas(area image.Rectangle, contours [][]image.Point) bool {
for _, contour := range contours {
for _, point := range contour {
if point.In(area) {
return true
}
}
}
return false
}
// UpdateInterval calls Update() every interval
func (s Stream) UpdateInterval() {
for {
go s.Update()
time.Sleep(time.Duration(s.Interval) * time.Millisecond)
}
}
// GetStreamInstant http.Get(URL) and returns the response
func (s Stream) GetStreamInstant() gocv.Mat {
resp, err := http.Get(s.URL)
if err != nil {
log.Fatal(err)
}
img, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
mat, err := gocv.IMDecode(img, gocv.IMReadColor)
if err != nil {
log.Fatal("Could not IMDecode img")
}
return mat
}
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath
func (s Stream) SaveStreamInstant(mat gocv.Mat) {
s.FileLock.Lock()
streamStoreDir := s.GetStreamStoreDirPath()
os.MkdirAll(streamStoreDir, os.ModePerm)
currentStreamInstantPath := s.GetCurrentInstantPath()
swap := FileExists(s.GetCurrentInstantPath())
if swap {
currentStreamInstantPath = s.GetNextInstantPath()
}
gocv.IMWrite(currentStreamInstantPath, mat)
if swap {
s.SwapInstants(s.GetPreviousInstantPath(), s.GetCurrentInstantPath(), s.GetNextInstantPath())
}
s.FileLock.Unlock()
}
// SwapInstants swaps the file location, first current -> previous and then next -> current
func (s Stream) SwapInstants(previous string, current string, next string) {
err := os.Rename(current, previous)
if err != nil {
log.Fatal("Couldn't copy current image instant to previous image instant")
}
err = os.Rename(next, current)
if err != nil {
log.Fatal("Couldn't copy next image instant to current image instant")
}
}
// GetStreamJSONPath returns filepath.Join(s.GetStreamStoreDirPath(), "stream.json")
func (s Stream) GetStreamJSONPath() string {
return filepath.Join(s.GetStreamStoreDirPath(), "stream.json")
}
// WriteStreamJSON writes the Stream struct to GetStreamJSONPath()
func (s Stream) WriteStreamJSON() {
if !FileExists(s.GetStreamStoreDirPath()) {
os.MkdirAll(s.GetStreamStoreDirPath(), os.ModePerm)
}
file, _ := json.MarshalIndent(s, "", "\t")
_ = ioutil.WriteFile(s.GetStreamJSONPath(), file, 0644)
}
// GetStreamStoreDirPath returns filepath.Join(s.GetStreamDirPath(), s.Base64)
func (s Stream) GetStreamStoreDirPath() string {
return filepath.Join(GetStreamDirPath(), s.Base64)
}
// GetPreviousInstantPath returns filepath.Join(GetStreamDirPath(), s.Base64, "previous.jpg")
func (s Stream) GetPreviousInstantPath() string {
return filepath.Join(s.GetStreamStoreDirPath(), "previous.jpg")
}
// PreviousInstantPathExists returns FileExists(GetPreviousInstantPath())
func (s Stream) PreviousInstantPathExists() bool {
return FileExists(s.GetPreviousInstantPath())
}
// IMReadPrevious returns gocv.IMRead(GetPreviousInstantPath(), gocv.IMReadGrayScale)
func (s Stream) IMReadPrevious() gocv.Mat {
return gocv.IMRead(s.GetPreviousInstantPath(), gocv.IMReadGrayScale)
}
// GetCurrentInstantPath returns filepath.Join(s.GetStreamDirPath(), s.Base64, "current.jpg")
func (s Stream) GetCurrentInstantPath() string {
return filepath.Join(s.GetStreamStoreDirPath(), "current.jpg")
}
// CurrentInstantPathExists returns FileExists(GetCurrentInstantPath())
func (s Stream) CurrentInstantPathExists() bool {
return FileExists(s.GetCurrentInstantPath())
}
// IMReadCurrent returns gocv.IMRead(GetCurrentInstantPath(), gocv.IMReadGrayScale)
func (s Stream) IMReadCurrent() gocv.Mat {
return gocv.IMRead(s.GetCurrentInstantPath(), gocv.IMReadGrayScale)
}
// GetCurrentColorInstantPath returns filepath.Join(s.GetStreamDirPath(), s.Base64, "current_color.jpg")
func (s Stream) GetCurrentColorInstantPath() string {
return filepath.Join(s.GetStreamStoreDirPath(), "current_color.jpg")
}
// CurrentColorInstantPathExists returns FileExists(GetCurrentColorInstantPath())
func (s Stream) CurrentColorInstantPathExists() bool {
return FileExists(s.GetCurrentColorInstantPath())
}
// IMReadCurrentColor returns gocv.IMRead(GetCurrentColorInstantPath(), gocv.IMReadColor)
func (s Stream) IMReadCurrentColor() gocv.Mat {
return gocv.IMRead(s.GetCurrentColorInstantPath(), gocv.IMReadColor)
}
// GetNextInstantPath returns filepath.Join(s.GetStreamDirPath(), s.Base64, "last.jpg")
func (s Stream) GetNextInstantPath() string {
return filepath.Join(s.GetStreamStoreDirPath(), "last.jpg")
}
// NextInstantPathExists returns FileExists(GetNextInstantPath())
func (s Stream) NextInstantPathExists() bool {
return FileExists(s.GetNextInstantPath())
}
// IMReadNext returns gocv.IMRead(GetNextInstantPath(), gocv.IMReadGrayScale)
func (s Stream) IMReadNext() gocv.Mat {
return gocv.IMRead(s.GetNextInstantPath(), gocv.IMReadGrayScale)
}
// GetDebugInstantPath returns filepath.Join(s.GetStreamDirPath(), s.Base64, "debug.jpg")
func (s Stream) GetDebugInstantPath() string {
return filepath.Join(s.GetStreamStoreDirPath(), "debug.jpg")
}
// DebugInstantPathExists returns FileExists(GetDebugInstantPath())
func (s Stream) DebugInstantPathExists() bool {
return FileExists(s.GetDebugInstantPath())
}
// IMReadDebug returns gocv.IMRead(GetDebugInstantPath(), gocv.IMReadGrayScale)
func (s Stream) IMReadDebug() gocv.Mat {
return gocv.IMRead(s.GetDebugInstantPath(), gocv.IMReadColor)
}