added 'MotionInterval' that is used instead of 'Interval' when motion is detected on a Stream

This commit is contained in:
BroodjeAap 2020-07-25 12:39:19 +00:00
parent 3e2e9a8182
commit f8b13dc3ef

View file

@ -22,8 +22,10 @@ type Stream struct {
URL string `json:"url"`
Base64 string `json:"base64"`
Interval int `json:"interval"`
MotionInterval int `json:"motionInterval"`
WatchAreas []WatchArea `json:"watchAreas"`
Timeouts float64 `json:"-"`
MotionDetected bool `json:"-"`
FileLock sync.Mutex `json:"-"`
}
@ -36,8 +38,10 @@ func NewStream(Name string, URL string) *Stream {
URL: URL,
Base64: base64,
Interval: 5000,
MotionInterval: 500,
WatchAreas: make([]WatchArea, 0),
Timeouts: 0,
MotionDetected: false,
}
stream.WriteStreamJSON()
return &stream
@ -48,6 +52,7 @@ func StreamFromJSON(path string) *Stream {
streamJSONFile, _ := ioutil.ReadFile(path)
stream := Stream{
Timeouts: 0,
MotionDetected: false,
}
json.Unmarshal([]byte(streamJSONFile), &stream)
return &stream
@ -95,13 +100,15 @@ func (s *Stream) Update() {
gocv.CvtColor(debug, &debug, gocv.ColorGrayToBGRA)
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)
}
}
go gocv.IMWrite(s.GetDebugInstantPath(), debug)
gocv.IMWrite(s.GetDebugInstantPath(), debug)
}
// CheckWatchAreas unexported
@ -119,12 +126,16 @@ func (s *Stream) CheckWatchAreas(area image.Rectangle, contours [][]image.Point)
// UpdateInterval calls Update() every interval
func (s *Stream) UpdateInterval() {
for {
go s.Update()
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)
}
}
}
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath
func (s *Stream) SaveStreamInstant(mat gocv.Mat) {