refactored to single area per WatchArea

This commit is contained in:
BroodjeAap 2020-07-12 14:43:32 +00:00
parent eb11a50d08
commit 1a4f3e8227
4 changed files with 62 additions and 62 deletions

View file

@ -17,12 +17,12 @@ import (
// Stream represents a stream to monitor // Stream represents a stream to monitor
type Stream struct { type Stream struct {
Name string `json:"name"` Name string `json:"name"`
URL string `json:"url"` URL string `json:"url"`
Base64 string `json:"base64"` Base64 string `json:"base64"`
Interval int `json:"interval"` Interval int `json:"interval"`
Watches []Watch `json:"watches"` WatchAreas []WatchArea `json:"watchAreas"`
FileLock sync.Mutex `json:"lock"` FileLock sync.Mutex `json:"lock"`
} }
// NewStream creates a new Stream Object // NewStream creates a new Stream Object
@ -30,39 +30,29 @@ func NewStream(Name string, URL string) Stream {
base64 := URLToBase64(URL) base64 := URLToBase64(URL)
stream := Stream{ stream := Stream{
Name: Name, Name: Name,
URL: URL, URL: URL,
Base64: base64, Base64: base64,
Interval: 5000, Interval: 5000,
Watches: make([]Watch, 0), WatchAreas: make([]WatchArea, 0),
} }
stream.Watches = append(stream.Watches, stream.WatchAreas = append(stream.WatchAreas,
Watch{ WatchArea{
Name: "test", Name: "test",
Color: color.RGBA{255, 0, 255, 255}, Color: color.RGBA{255, 0, 255, 255},
Areas: []image.Rectangle{ Area: image.Rect(30, 30, 400, 200),
image.Rect(30, 30, 400, 200),
image.Rect(400, 30, 500, 200),
image.Rect(400, 200, 500, 500),
},
}, },
Watch{ WatchArea{
Name: "test2", Name: "test2",
Color: color.RGBA{0, 255, 255, 255}, Color: color.RGBA{0, 255, 255, 255},
Areas: []image.Rectangle{ Area: image.Rect(800, 30, 1000, 500),
image.Rect(800, 30, 1000, 500),
image.Rect(700, 500, 1200, 700),
},
}, },
Watch{ WatchArea{
Name: "test3", Name: "test3",
Color: color.RGBA{255, 255, 0, 255}, Color: color.RGBA{255, 255, 0, 255},
Areas: []image.Rectangle{ Area: image.Rect(50, 400, 450, 700),
image.Rect(50, 400, 450, 700),
},
}, },
) )
log.Println("TEST")
stream.WriteStreamJSON() stream.WriteStreamJSON()
return stream return stream
} }
@ -105,15 +95,10 @@ func (s Stream) Update() {
gocv.CvtColor(debug, &debug, gocv.ColorGrayToBGRA) gocv.CvtColor(debug, &debug, gocv.ColorGrayToBGRA)
contours := gocv.FindContours(diff, gocv.RetrievalExternal, gocv.ChainApproxSimple) contours := gocv.FindContours(diff, gocv.RetrievalExternal, gocv.ChainApproxSimple)
for _, watch := range s.Watches { for _, watchArea := range s.WatchAreas {
skipNextAreas := true // Still draw all area rectangles gocv.Rectangle(&debug, watchArea.Area, watchArea.Color, 10)
for _, area := range watch.Areas { if s.CheckWatchAreas(watchArea.Area, contours) {
gocv.Rectangle(&debug, area, watch.Color, 10) go watchArea.CopyInstant(s.GetCurrentColorInstantPath(), s)
if skipNextAreas && s.CheckWatchAreas(area, contours) {
go watch.CopyInstant(s.GetCurrentColorInstantPath(), s)
skipNextAreas = false
break
}
} }
} }
go gocv.IMWrite(s.GetDebugInstantPath(), debug) go gocv.IMWrite(s.GetDebugInstantPath(), debug)

View file

@ -18,7 +18,7 @@
<tr> <tr>
<td>{{ $stream.Name }}</td> <td>{{ $stream.Name }}</td>
<td>{{ $stream.URL }} </td> <td>{{ $stream.URL }} </td>
<td>{{ len $stream.Watches }} </td> <td>{{ len $stream.WatchAreas }} </td>
<td>{{ $stream.Interval }}</td> <td>{{ $stream.Interval }}</td>
<td><a href="/?name={{ $stream.Name }}">Go</a></td> <td><a href="/?name={{ $stream.Name }}">Go</a></td>
</tr> </tr>

View file

@ -12,15 +12,30 @@
<thead> <thead>
<tr> <tr>
<th>Name</th> <th>Name</th>
<th>Color</th> <th colspan="3">Color</th>
<th>Areas</th> <th colspan="4">Area</th>
</tr>
<tr>
<th></th>
<th>R</th>
<th>G</th>
<th>B</th>
<th>MinX</th>
<th>MinY</th>
<th>MaxX</th>
<th>MaxY</th>
</tr> </tr>
</thead> </thead>
{{range $watch := .Watches}} {{range $watchArea := .WatchAreas}}
<tr> <tr>
<td>{{ $watch.Name }}</td> <td>{{ $watchArea.Name }}</td>
<td>{{ $watch.Color }}</td> <td>{{ $watchArea.Color.R }}</td>
<td>{{ len $watch.Areas }}</td> <td>{{ $watchArea.Color.G }}</td>
<td>{{ $watchArea.Color.B }}</td>
<td>{{ $watchArea.Area.Min.X }}</td>
<td>{{ $watchArea.Area.Min.Y }}</td>
<td>{{ $watchArea.Area.Max.X }}</td>
<td>{{ $watchArea.Area.Max.Y }}</td>
</tr> </tr>
{{end}} {{end}}

View file

@ -12,35 +12,35 @@ import (
const timeLayout = "2006-01-02_15-4-5.jpg" const timeLayout = "2006-01-02_15-4-5.jpg"
// Watch defines one or more areas that should be monitored for motion // WatchArea defines one or more areas that should be monitored for motion
type Watch struct { type WatchArea struct {
Name string `json:"name"` Name string `json:"name"`
Color color.RGBA `json:"color"` Color color.RGBA `json:"color"`
Areas []image.Rectangle `json:"areas"` Area image.Rectangle `json:"area"`
} }
// WatchStoreDirExists returns filepath.Join(s.GetStreamStoreDirPath(), w.Name) // WatchAreaStoreDirExists returns filepath.Join(s.GetStreamStoreDirPath(), w.Name)
func (w Watch) WatchStoreDirExists(s Stream) bool { func (w WatchArea) WatchAreaStoreDirExists(s Stream) bool {
return FileExists(w.GetWatchStoreDir(s)) return FileExists(w.GetWatchAreaStoreDir(s))
} }
// GetWatchStoreDir returns filepath.Join(s.GetStreamStoreDirPath(), w.Name) // GetWatchAreaStoreDir returns filepath.Join(s.GetStreamStoreDirPath(), w.Name)
func (w Watch) GetWatchStoreDir(s Stream) string { func (w WatchArea) GetWatchAreaStoreDir(s Stream) string {
return filepath.Join(s.GetStreamStoreDirPath(), w.Name) return filepath.Join(s.GetStreamStoreDirPath(), w.Name)
} }
// GetWatchStoreDirInstantPath returns filepath.Join(s.GetWatchStoreDir(), "") // GetWatchAreaStoreDirInstantPath returns filepath.Join(s.GetWatchAreaStoreDir(), "")
func (w Watch) GetWatchStoreDirInstantPath(s Stream) string { func (w WatchArea) GetWatchAreaStoreDirInstantPath(s Stream) string {
now := time.Now() 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) // CopyInstant makes a copy of src to GetWatchAreaStoreDirInstantPath(s)
func (w Watch) CopyInstant(src string, s Stream) { func (w WatchArea) CopyInstant(src string, s Stream) {
if !FileExists(w.GetWatchStoreDir(s)) { if !FileExists(w.GetWatchAreaStoreDir(s)) {
os.MkdirAll(w.GetWatchStoreDir(s), os.ModePerm) os.MkdirAll(w.GetWatchAreaStoreDir(s), os.ModePerm)
} }
dest := w.GetWatchStoreDirInstantPath(s) dest := w.GetWatchAreaStoreDirInstantPath(s)
if !FileExists(src) { if !FileExists(src) {
log.Fatal("Nothing to copy ", src) log.Fatal("Nothing to copy ", src)
return return