streamwatcher/stream.go
2020-10-25 11:45:47 +01:00

310 lines
9.8 KiB
Go

package main
import (
"encoding/json"
"image"
"image/color"
"io/ioutil"
"log"
"math"
"net/http"
"os"
"path/filepath"
"time"
"gocv.io/x/gocv"
)
// Stream represents a stream to monitor
type Stream struct {
Name string `json:"name"`
URL string `json:"url"`
Interval int `json:"interval"`
MotionInterval int `json:"motionInterval"`
WatchAreas []WatchArea `json:"watchAreas"`
Timeouts float64 `json:"-"`
MotionDetected bool `json:"-"`
}
// NewStream creates a new Stream Object
func NewStream(Name string, URL string, Interval int, MotionInterval int) *Stream {
stream := Stream{
Name: Name,
URL: URL,
Interval: Interval,
MotionInterval: MotionInterval,
WatchAreas: make([]WatchArea, 0),
Timeouts: 0,
MotionDetected: false,
}
stream.WriteStreamJSON()
return &stream
}
// StreamFromJSON takes a filepath to a JSON file, and returns the unmarshalled Stream object
func StreamFromJSON(path string) *Stream {
streamJSONFile, _ := ioutil.ReadFile(path)
stream := Stream{
Timeouts: 0,
MotionDetected: false,
}
json.Unmarshal([]byte(streamJSONFile), &stream)
return &stream
}
// Update gets called by UpdateInterval Interval milliseconds to fetch the latest instant
func (s *Stream) Update() {
log.Print("Update: ", s.Name, " - ", len(s.WatchAreas), " - ", s.URL)
resp, err := http.Get(s.URL)
if err != nil {
log.Println(err)
s.Timeouts++ // For increasing the Interval time every timeout
return
}
s.Timeouts = 0
img, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
// Get the image from the response and write it to disk in a goroutine
mat, err := gocv.IMDecode(img, gocv.IMReadColor)
if err != nil {
log.Println("Could not IMDecode img", err)
return
}
go gocv.IMWrite(s.GetCurrentColorInstantPath(), mat)
// Turn the current image into greyscale and also write that to disk in a goroutine
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
}
// If there is a previous image saved to disk, calculate the difference between the two and amplify those differences
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)
// Get the contours and check if they are in any of the WatchAreas
contours := gocv.FindContours(diff, gocv.RetrievalExternal, gocv.ChainApproxSimple)
s.MotionDetected = false
for _, watchArea := range s.WatchAreas {
gocv.Rectangle(&debug, watchArea.Area, watchArea.Color, 10)
if s.CheckWatchAreas(watchArea.Area, contours) {
s.MotionDetected = true
go watchArea.CopyInstant(s.GetCurrentColorInstantPath(), *s)
}
}
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 {
s.Update()
if s.MotionDetected {
time.Sleep(time.Duration(s.MotionInterval) * time.Millisecond)
} else {
expTimeout := math.Pow(2, s.Timeouts)
maxExpTimeout := (int)(math.Min(12, expTimeout))
time.Sleep(time.Duration(s.Interval*maxExpTimeout) * time.Millisecond)
}
}
}
// GetWatchAreaByName finds the WatchArea with the name, if it doesn't exist returns nil, false
func (s *Stream) GetWatchAreaByName(name string) (*WatchArea, bool) {
for _, watchArea := range s.WatchAreas {
if name == watchArea.Name {
return &watchArea, true
}
}
return nil, false
}
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath
func (s *Stream) SaveStreamInstant(mat gocv.Mat) {
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())
}
}
// 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.Println("Couldn't copy current image instant to previous image instant", err)
}
err = os.Rename(next, current)
if err != nil {
log.Println("Couldn't copy next image instant to current image instant", err)
}
}
// AddWatchArea adds a watch area
func (s *Stream) AddWatchArea(name string, area image.Rectangle, color color.RGBA) {
s.WatchAreas = append(s.WatchAreas, WatchArea{
Name: name,
Color: color,
Area: area,
})
s.WriteStreamJSON()
}
// 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.Name)
func (s *Stream) GetStreamStoreDirPath() string {
return filepath.Join(GetStreamDirPath(), s.Name)
}
// GetPreviousInstantPath returns filepath.Join(GetStreamStoreDirPath(), "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())
}
// GetPreviousURL returns the URL towards the static file location of current.jpg
func (s *Stream) GetPreviousURL() string {
return filepath.Join("/streams", s.Name, "previous.jpg")
}
// 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.GetStreamStoreDirPath(), "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())
}
// GetCurrentURL returns the URL towards the static file location of current.jpg
func (s *Stream) GetCurrentURL() string {
return filepath.Join("/streams", s.Name, "current.jpg")
}
// 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.GetStreamStoreDirPath(), "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())
}
// GetCurrentColorURL returns the URL towards the static file location of debug.jpg
func (s *Stream) GetCurrentColorURL() string {
return filepath.Join("/streams", s.Name, "current_color.jpg")
}
// 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.GetStreamStoreDirPath(), "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())
}
// GetNextURL returns the URL towards the static file location of next.jpg
func (s *Stream) GetNextURL() string {
return filepath.Join("/streams", s.Name, "next.jpg")
}
// 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.GetStreamStoreDirPath(), "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())
}
// GetDebugURL returns the URL towards the static file location of debug.jpg
func (s *Stream) GetDebugURL() string {
return filepath.Join("/streams", s.Name, "debug.jpg")
}
// IMReadDebug returns gocv.IMRead(GetDebugInstantPath(), gocv.IMReadGrayScale)
func (s *Stream) IMReadDebug() gocv.Mat {
return gocv.IMRead(s.GetDebugInstantPath(), gocv.IMReadColor)
}