From 1a4f3e82275c144685402b57ecee85164a9deb36 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Sun, 12 Jul 2020 14:43:32 +0000 Subject: [PATCH] refactored to single area per WatchArea --- stream.go | 59 ++++++++++++++++--------------------------- templates/index.html | 2 +- templates/stream.html | 27 +++++++++++++++----- watch.go | 36 +++++++++++++------------- 4 files changed, 62 insertions(+), 62 deletions(-) diff --git a/stream.go b/stream.go index dbef05e..6a131c4 100755 --- a/stream.go +++ b/stream.go @@ -17,12 +17,12 @@ import ( // 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"` + Name string `json:"name"` + URL string `json:"url"` + Base64 string `json:"base64"` + Interval int `json:"interval"` + WatchAreas []WatchArea `json:"watchAreas"` + FileLock sync.Mutex `json:"lock"` } // NewStream creates a new Stream Object @@ -30,39 +30,29 @@ func NewStream(Name string, URL string) Stream { base64 := URLToBase64(URL) stream := Stream{ - Name: Name, - URL: URL, - Base64: base64, - Interval: 5000, - Watches: make([]Watch, 0), + Name: Name, + URL: URL, + Base64: base64, + Interval: 5000, + WatchAreas: make([]WatchArea, 0), } - stream.Watches = append(stream.Watches, - Watch{ + stream.WatchAreas = append(stream.WatchAreas, + WatchArea{ 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), - }, + Area: image.Rect(30, 30, 400, 200), }, - Watch{ + WatchArea{ Name: "test2", Color: color.RGBA{0, 255, 255, 255}, - Areas: []image.Rectangle{ - image.Rect(800, 30, 1000, 500), - image.Rect(700, 500, 1200, 700), - }, + Area: image.Rect(800, 30, 1000, 500), }, - Watch{ + WatchArea{ Name: "test3", Color: color.RGBA{255, 255, 0, 255}, - Areas: []image.Rectangle{ - image.Rect(50, 400, 450, 700), - }, + Area: image.Rect(50, 400, 450, 700), }, ) - log.Println("TEST") stream.WriteStreamJSON() return stream } @@ -105,15 +95,10 @@ func (s Stream) Update() { 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 - } + for _, watchArea := range s.WatchAreas { + gocv.Rectangle(&debug, watchArea.Area, watchArea.Color, 10) + if s.CheckWatchAreas(watchArea.Area, contours) { + go watchArea.CopyInstant(s.GetCurrentColorInstantPath(), s) } } go gocv.IMWrite(s.GetDebugInstantPath(), debug) diff --git a/templates/index.html b/templates/index.html index 3471ac9..9157a62 100755 --- a/templates/index.html +++ b/templates/index.html @@ -18,7 +18,7 @@ {{ $stream.Name }} {{ $stream.URL }} - {{ len $stream.Watches }} + {{ len $stream.WatchAreas }} {{ $stream.Interval }} Go diff --git a/templates/stream.html b/templates/stream.html index d3bf723..80fa04a 100755 --- a/templates/stream.html +++ b/templates/stream.html @@ -12,15 +12,30 @@ Name - Color - Areas + Color + Area + + + + R + G + B + MinX + MinY + MaxX + MaxY - {{range $watch := .Watches}} + {{range $watchArea := .WatchAreas}} - {{ $watch.Name }} - {{ $watch.Color }} - {{ len $watch.Areas }} + {{ $watchArea.Name }} + {{ $watchArea.Color.R }} + {{ $watchArea.Color.G }} + {{ $watchArea.Color.B }} + {{ $watchArea.Area.Min.X }} + {{ $watchArea.Area.Min.Y }} + {{ $watchArea.Area.Max.X }} + {{ $watchArea.Area.Max.Y }} {{end}} diff --git a/watch.go b/watch.go index 5577a8b..bd61357 100755 --- a/watch.go +++ b/watch.go @@ -12,35 +12,35 @@ import ( const timeLayout = "2006-01-02_15-4-5.jpg" -// Watch defines one or more areas that should be monitored for motion -type Watch struct { - Name string `json:"name"` - Color color.RGBA `json:"color"` - Areas []image.Rectangle `json:"areas"` +// WatchArea defines one or more areas that should be monitored for motion +type WatchArea struct { + Name string `json:"name"` + Color color.RGBA `json:"color"` + Area image.Rectangle `json:"area"` } -// WatchStoreDirExists returns filepath.Join(s.GetStreamStoreDirPath(), w.Name) -func (w Watch) WatchStoreDirExists(s Stream) bool { - return FileExists(w.GetWatchStoreDir(s)) +// WatchAreaStoreDirExists returns filepath.Join(s.GetStreamStoreDirPath(), w.Name) +func (w WatchArea) WatchAreaStoreDirExists(s Stream) bool { + return FileExists(w.GetWatchAreaStoreDir(s)) } -// GetWatchStoreDir returns filepath.Join(s.GetStreamStoreDirPath(), w.Name) -func (w Watch) GetWatchStoreDir(s Stream) string { +// GetWatchAreaStoreDir returns filepath.Join(s.GetStreamStoreDirPath(), w.Name) +func (w WatchArea) GetWatchAreaStoreDir(s Stream) string { return filepath.Join(s.GetStreamStoreDirPath(), w.Name) } -// GetWatchStoreDirInstantPath returns filepath.Join(s.GetWatchStoreDir(), "") -func (w Watch) GetWatchStoreDirInstantPath(s Stream) string { +// GetWatchAreaStoreDirInstantPath returns filepath.Join(s.GetWatchAreaStoreDir(), "") +func (w WatchArea) GetWatchAreaStoreDirInstantPath(s Stream) string { now := time.Now() - return filepath.Join(w.GetWatchStoreDir(s), now.Format(timeLayout)) + return filepath.Join(w.GetWatchAreaStoreDir(s), now.Format(timeLayout)) } -// CopyInstant makes a copy of src to GetWatchStoreDirInstantPath(s) -func (w Watch) CopyInstant(src string, s Stream) { - if !FileExists(w.GetWatchStoreDir(s)) { - os.MkdirAll(w.GetWatchStoreDir(s), os.ModePerm) +// CopyInstant makes a copy of src to GetWatchAreaStoreDirInstantPath(s) +func (w WatchArea) CopyInstant(src string, s Stream) { + if !FileExists(w.GetWatchAreaStoreDir(s)) { + os.MkdirAll(w.GetWatchAreaStoreDir(s), os.ModePerm) } - dest := w.GetWatchStoreDirInstantPath(s) + dest := w.GetWatchAreaStoreDirInstantPath(s) if !FileExists(src) { log.Fatal("Nothing to copy ", src) return