From 97bbceea8afc78098f2f61de03223d65e06f4219 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Sat, 11 Jul 2020 20:07:33 +0000 Subject: [PATCH] got watch areas working with current frames being copied to directories --- stream.go | 64 ++++++++++++++++++++++++++++++++++++------------------- watch.go | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 22 deletions(-) diff --git a/stream.go b/stream.go index 02f5df3..ab2c8e2 100755 --- a/stream.go +++ b/stream.go @@ -34,18 +34,32 @@ func NewStream(URL string) Stream { Interval: 5000, Watches: make([]Watch, 0), } - watch := 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), + 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), + }, }, - } - log.Println(watch) - stream.Watches = append(stream.Watches, watch) - log.Println(stream) + 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), + }, + }, + ) stream.WriteStreamJSON() return stream } @@ -88,26 +102,32 @@ 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) - for contourIndex, contour := range contours { - for _, point := range contour { - - if point.In(area) { - gocv.DrawContours(&debug, contours, contourIndex, color.RGBA{50, 250, 50, 150}, 5) - break - } else { - //log.Println("Outside area") - } - } + 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 { diff --git a/watch.go b/watch.go index 29a382b..5577a8b 100755 --- a/watch.go +++ b/watch.go @@ -3,11 +3,57 @@ package main import ( "image" "image/color" + "io/ioutil" + "log" + "os" + "path/filepath" + "time" ) +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"` } + +// WatchStoreDirExists returns filepath.Join(s.GetStreamStoreDirPath(), w.Name) +func (w Watch) WatchStoreDirExists(s Stream) bool { + return FileExists(w.GetWatchStoreDir(s)) +} + +// GetWatchStoreDir returns filepath.Join(s.GetStreamStoreDirPath(), w.Name) +func (w Watch) GetWatchStoreDir(s Stream) string { + return filepath.Join(s.GetStreamStoreDirPath(), w.Name) +} + +// GetWatchStoreDirInstantPath returns filepath.Join(s.GetWatchStoreDir(), "") +func (w Watch) GetWatchStoreDirInstantPath(s Stream) string { + now := time.Now() + return filepath.Join(w.GetWatchStoreDir(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) + } + dest := w.GetWatchStoreDirInstantPath(s) + if !FileExists(src) { + log.Fatal("Nothing to copy ", src) + return + } + if FileExists(dest) { + os.Remove(dest) + } + srcData, err := ioutil.ReadFile(src) + if err != nil { + log.Println(err) + } + err = ioutil.WriteFile(dest, srcData, 0644) + if err != nil { + log.Println(err) + } +}