timing for 25 frames testing

This commit is contained in:
Ella Pietraroia 2020-01-20 14:09:42 +10:30
parent 61bb0b68f6
commit 44aac3f9fd
2 changed files with 30 additions and 2 deletions

View File

@ -27,7 +27,9 @@ LICENSE
package filter package filter
import ( import (
"fmt"
"io" "io"
"time"
) )
// Interface for all filters. // Interface for all filters.
@ -44,6 +46,16 @@ type NoOp struct {
func NewNoOp(dst io.Writer) *NoOp { return &NoOp{dst: dst} } func NewNoOp(dst io.Writer) *NoOp { return &NoOp{dst: dst} }
func (n *NoOp) Write(p []byte) (int, error) { return n.dst.Write(p) } func (n *NoOp) Write(p []byte) (int, error) {
if frames == 0 {
t1 = time.Now()
}
frames++
if frames >= 24 {
fmt.Printf("25 frames takes: %dms\n\n", time.Now().Sub(t1).Milliseconds())
frames = 0
}
return n.dst.Write(p)
}
func (n *NoOp) Close() error { return nil } func (n *NoOp) Close() error { return nil }

View File

@ -33,6 +33,7 @@ import (
"image" "image"
"image/color" "image/color"
"io" "io"
"time"
"gocv.io/x/gocv" "gocv.io/x/gocv"
) )
@ -51,6 +52,9 @@ type MOGFilter struct {
hfCount int hfCount int
} }
var frames int = 0
var t1 = time.Now()
// NewMOGFilter returns a pointer to a new MOGFilter struct. // NewMOGFilter returns a pointer to a new MOGFilter struct.
func NewMOGFilter(dst io.WriteCloser, area, threshold float64, history int, debug bool, hf int) *MOGFilter { func NewMOGFilter(dst io.WriteCloser, area, threshold float64, history int, debug bool, hf int) *MOGFilter {
bs := gocv.NewBackgroundSubtractorMOG2WithParams(history, threshold, false) bs := gocv.NewBackgroundSubtractorMOG2WithParams(history, threshold, false)
@ -79,9 +83,17 @@ func (m *MOGFilter) Close() error {
// Write applies the motion filter to the video stream. Only frames with motion // Write applies the motion filter to the video stream. Only frames with motion
// are written to the destination encoder, frames without are discarded. // are written to the destination encoder, frames without are discarded.
func (m *MOGFilter) Write(f []byte) (int, error) { func (m *MOGFilter) Write(f []byte) (int, error) {
if frames == 0 {
t1 = time.Now()
}
if m.hfCount < (m.hf - 1) { if m.hfCount < (m.hf - 1) {
m.hold[m.hfCount] = f m.hold[m.hfCount] = f
m.hfCount++ m.hfCount++
frames++
if frames >= 24 {
fmt.Printf("25 frames takes: %dms\n\n", time.Now().Sub(t1).Milliseconds())
frames = 0
}
return 0, nil return 0, nil
} }
@ -138,7 +150,11 @@ func (m *MOGFilter) Write(f []byte) (int, error) {
if len(contours) == 0 { if len(contours) == 0 {
return 0, nil return 0, nil
} }
frames++
if frames >= 24 {
fmt.Printf("25 frames takes: %dms\n\n", time.Now().Sub(t1).Milliseconds())
frames = 0
}
// Write to destination, past 4 frames then current frame. // Write to destination, past 4 frames then current frame.
for i, h := range m.hold { for i, h := range m.hold {
_, err := m.dst.Write(h) _, err := m.dst.Write(h)