tidying up and adding any other comments

This commit is contained in:
Ella Pietraroia 2020-01-31 10:53:25 +10:30
parent 513eee1260
commit 6470c98a08
1 changed files with 10 additions and 35 deletions

View File

@ -96,18 +96,14 @@ func (b *BasicFilter) Close() error {
func (b *BasicFilter) Process(j int, wg *sync.WaitGroup) {
defer wg.Done()
for i := 0; i < b.w; i++ {
// ti0 := time.Now()
n := b.img.At(i, j)
// ti1 := time.Now()
R, G, B, _ := n.RGBA()
// ti2 := time.Now()
// Compare the difference of the RGB values of each pixel to the background image.
diffB := absDiff(B, b.bg[j][i][2])
diffR := absDiff(R, b.bg[j][i][0])
diffG := absDiff(G, b.bg[j][i][1])
// ti3 := time.Now()
diff := diffR + diffG + diffB
if diff > 45000 {
@ -121,17 +117,9 @@ func (b *BasicFilter) Process(j int, wg *sync.WaitGroup) {
b.bwImg.SetRGBA(i, j, color.RGBA{0x00, 0x00, 0x00, 0xff})
}
}
// ti4 := time.Now()
// Update backgound image.
copy(b.bg[j][i][:], []uint32{R, G, B})
// ti5 := time.Now()
// fmt.Print("At: ", ti1.Sub(ti0).Nanoseconds(), "\n")
// fmt.Print("RGBA: ", ti2.Sub(ti1).Nanoseconds(), "\n")
// fmt.Print("3x absDiff: ", ti3.Sub(ti2).Nanoseconds(), "\n")
// fmt.Print("total diff + threshold + debug: ", ti4.Sub(ti3).Nanoseconds(), "\n")
// fmt.Print("update bg: ", ti5.Sub(ti4).Nanoseconds(), "\n")
// fmt.Print("Total: ", time.Now().Sub(ti0).Nanoseconds(), "\n\n")
}
}
@ -140,7 +128,7 @@ func (b *BasicFilter) Process(j int, wg *sync.WaitGroup) {
// are written to the destination encoder, frames without are discarded.
func (b *BasicFilter) Write(f []byte) (int, error) {
t0 := time.Now()
//decode MJPEG
// Decode MJPEG.
r := bytes.NewReader(f)
var err error
b.img, err = jpeg.Decode(r)
@ -150,8 +138,8 @@ func (b *BasicFilter) Write(f []byte) (int, error) {
t1 := time.Now()
//get background image and save a new background image if needed
//first frame must always be sent
// Get background image and save a new background image if needed
// first frame must always be sent.
if b.bg == nil {
bounds := b.img.Bounds()
b.w = bounds.Max.X
@ -174,13 +162,9 @@ func (b *BasicFilter) Write(f []byte) (int, error) {
return len(f), nil
}
//for all pixels get the difference from the background image
// c := make(chan int)
// fmt.Print("channel made \n")
var m sync.Mutex
// Use 4x goroutines to each process one row of pixels
j := 0
var m sync.Mutex
m.Lock()
var wg sync.WaitGroup
@ -198,9 +182,7 @@ func (b *BasicFilter) Write(f []byte) (int, error) {
m.Unlock()
}
t2 := time.Now()
//visualise
// Will save a video of where motion is detected in motion.mjpeg (in the current folder).
if b.debug {
col := color.RGBA{200, 100, 0, 255}
d := &font.Drawer{
@ -221,19 +203,12 @@ func (b *BasicFilter) Write(f []byte) (int, error) {
return len(f), err
}
}
t3 := time.Now()
fmt.Print("Encode: ", t1.Sub(t0).Milliseconds(), "\n")
fmt.Print("Calc loop: ", t2.Sub(t1).Milliseconds(), "\n")
fmt.Print("VLC: ", t3.Sub(t2).Milliseconds(), "\n")
fmt.Print("Total: ", time.Now().Sub(t0).Milliseconds(), "\n\n")
//choose a threshold that motion is detected for if greater
// If there are not enough motion pixels then discard the frame.
if b.motion < 1000 {
return len(f), nil
}
//discard non motion frames
//write motion frames
// Write all motion frames.
return b.dst.Write(f)
}