got watch areas working with current frames being copied to directories
This commit is contained in:
parent
f4099dc4f5
commit
97bbceea8a
2 changed files with 88 additions and 22 deletions
64
stream.go
64
stream.go
|
@ -34,18 +34,32 @@ func NewStream(URL string) Stream {
|
||||||
Interval: 5000,
|
Interval: 5000,
|
||||||
Watches: make([]Watch, 0),
|
Watches: make([]Watch, 0),
|
||||||
}
|
}
|
||||||
watch := Watch{
|
stream.Watches = append(stream.Watches,
|
||||||
Name: "test",
|
Watch{
|
||||||
Color: color.RGBA{255, 0, 255, 255},
|
Name: "test",
|
||||||
Areas: []image.Rectangle{
|
Color: color.RGBA{255, 0, 255, 255},
|
||||||
image.Rect(30, 30, 400, 200),
|
Areas: []image.Rectangle{
|
||||||
image.Rect(400, 30, 500, 200),
|
image.Rect(30, 30, 400, 200),
|
||||||
image.Rect(400, 200, 500, 500),
|
image.Rect(400, 30, 500, 200),
|
||||||
|
image.Rect(400, 200, 500, 500),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
Watch{
|
||||||
log.Println(watch)
|
Name: "test2",
|
||||||
stream.Watches = append(stream.Watches, watch)
|
Color: color.RGBA{0, 255, 255, 255},
|
||||||
log.Println(stream)
|
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()
|
stream.WriteStreamJSON()
|
||||||
return stream
|
return stream
|
||||||
}
|
}
|
||||||
|
@ -88,26 +102,32 @@ 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 _, watch := range s.Watches {
|
||||||
|
skipNextAreas := true // Still draw all area rectangles
|
||||||
for _, area := range watch.Areas {
|
for _, area := range watch.Areas {
|
||||||
gocv.Rectangle(&debug, area, watch.Color, 10)
|
gocv.Rectangle(&debug, area, watch.Color, 10)
|
||||||
for contourIndex, contour := range contours {
|
if skipNextAreas && s.CheckWatchAreas(area, contours) {
|
||||||
for _, point := range contour {
|
go watch.CopyInstant(s.GetCurrentColorInstantPath(), s)
|
||||||
|
skipNextAreas = false
|
||||||
if point.In(area) {
|
break
|
||||||
gocv.DrawContours(&debug, contours, contourIndex, color.RGBA{50, 250, 50, 150}, 5)
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
//log.Println("Outside area")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
go gocv.IMWrite(s.GetDebugInstantPath(), debug)
|
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
|
// UpdateInterval calls Update() every interval
|
||||||
func (s Stream) UpdateInterval() {
|
func (s Stream) UpdateInterval() {
|
||||||
for {
|
for {
|
||||||
|
|
46
watch.go
46
watch.go
|
@ -3,11 +3,57 @@ package main
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"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
|
// Watch defines one or more areas that should be monitored for motion
|
||||||
type Watch struct {
|
type Watch struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Color color.RGBA `json:"color"`
|
Color color.RGBA `json:"color"`
|
||||||
Areas []image.Rectangle `json:"areas"`
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue