func (c *Motion) Detect(caster *broker.Broker) {
	c.Init()
	conn := listen(c.Protocol, c.ListenPort)

	//f, _ := os.Create("motion.vec")
	numMacroblocks := ((c.Width + 16) / 16) * (c.Height / 16) // the right-most column is padding?
	numUsableMacroblocks := (c.Width / 16) * (c.Height / 16)

	currMacroBlocks := make([]motionVector, 0, numMacroblocks)
	currVectorBlocks := make([]motionVector, numUsableMacroblocks/(c.BlockWidth*c.BlockWidth))
	vectorAvgBlocks := make([]motionVector, numUsableMacroblocks/(c.BlockWidth*c.BlockWidth))
	_ = vectorAvgBlocks
	c.output = make([]byte, numUsableMacroblocks/(c.BlockWidth*c.BlockWidth))
	vectorHistory := make([][]motionVector, 0, c.NumInspectFrames)
	ignoredFrames := 0

	buf := make([]byte, 1024)
	s := bufio.NewReader(conn)
	blocksRead := 0
	for {
		_, err := s.Read(buf)

		if err != nil {
			log.Println("Motion detection stopped: " + err.Error())
			return
		}

		bufIdx := 0
		for bufIdx < len(buf) {
			// Manually convert since binary.Read runs really slow on a Pi Zero (~20% CPU)
			temp := motionVector{}
			temp.X = int8(buf[0+bufIdx])
			temp.Y = int8(buf[1+bufIdx])
			//temp.SAD = int16(buf[2+bufIdx]) << 4
			//temp.SAD |= int16(buf[3+bufIdx])
			currMacroBlocks = append(currMacroBlocks, temp)
			bufIdx += sizeofMotionVector
			blocksRead++

			if blocksRead == numMacroblocks {
				blocksRead = 0
				if ignoredFrames < ignoreFirstFrames {
					ignoredFrames++
					continue
				}
				c.condenseBlocksAvg(&currVectorBlocks, &currMacroBlocks)
				//c.findTemporalAverage(&vectorAvgBlocks, &vectorHistory, &currVectorBlocks)
				c.logVectorHistory(&vectorHistory, &currVectorBlocks)
				//if c.publishAvg(caster, &vectorAvgBlocks, &currVectorBlocks) > 0 {
				if c.publishHist(caster, &vectorHistory) > 0 {
					c.recorder.StopTime = time.Now().Add(time.Second * 2)
				}

				//binary.Write(f, binary.LittleEndian, &currMacroBlocks) // write to file
				currMacroBlocks = currMacroBlocks[:0]
			}
		}
	}
}
// Detect motion by comparing the most recent frame with an average of the past numFrames.
// Lower senseThreshold value increases the sensitivity to motion.
Detect motion by comparing the most recent frame with an average of the past numFrames.
Lower senseThreshold value increases the sensitivity to motion.
// Detect motion by comparing the most recent frame with an average of the past numFrames . // Lower senseThreshold value increases the sensitivity to motion .
func (c *Motion) Detect(caster *broker.Broker) {
	c.Init()
	conn := listen(c.Protocol, c.ListenPort)
	numMacroblocks := ((c.Width + 16) / 16) * (c.Height / 16) 
	numUsableMacroblocks := (c.Width / 16) * (c.Height / 16)
	currMacroBlocks := make([]motionVector, 0, numMacroblocks)
	currVectorBlocks := make([]motionVector, numUsableMacroblocks/(c.BlockWidth*c.BlockWidth))
	vectorAvgBlocks := make([]motionVector, numUsableMacroblocks/(c.BlockWidth*c.BlockWidth))
	_ = vectorAvgBlocks
	c.output = make([]byte, numUsableMacroblocks/(c.BlockWidth*c.BlockWidth))
	vectorHistory := make([][]motionVector, 0, c.NumInspectFrames)
	ignoredFrames := 0
	buf := make([]byte, 1024)
	s := bufio.NewReader(conn)
	blocksRead := 0
	for {
		_, err := s.Read(buf)
		if err != nil {
			log.Println("Motion detection stopped: " + err.Error())
			return
		}
		bufIdx := 0
		for bufIdx < len(buf) {
			temp := motionVector{}
			temp.X = int8(buf[0+bufIdx])
			temp.Y = int8(buf[1+bufIdx])
			currMacroBlocks = append(currMacroBlocks, temp)
			bufIdx += sizeofMotionVector
			blocksRead++
			if blocksRead == numMacroblocks {
				blocksRead = 0
				if ignoredFrames < ignoreFirstFrames {
					ignoredFrames++
					continue
				}
				c.condenseBlocksAvg(&currVectorBlocks, &currMacroBlocks)
				c.logVectorHistory(&vectorHistory, &currVectorBlocks)
				if c.publishHist(caster, &vectorHistory) > 0 {
					c.recorder.StopTime = time.Now().Add(time.Second * 2)
				}
				currMacroBlocks = currMacroBlocks[:0]
			}
		}
	}
}
func ( c * Motion ) Detect ( caster * broker . Broker ) { c . Init ( ) conn := listen ( c . Protocol , c . ListenPort ) numMacroblocks := ( ( c . Width + 16 ) / 16 ) * ( c . Height / 16 ) numUsableMacroblocks := ( c . Width / 16 ) * ( c . Height / 16 ) currMacroBlocks := make ( [ ] motionVector , 0 , numMacroblocks ) currVectorBlocks := make ( [ ] motionVector , numUsableMacroblocks / ( c . BlockWidth * c . BlockWidth ) ) vectorAvgBlocks := make ( [ ] motionVector , numUsableMacroblocks / ( c . BlockWidth * c . BlockWidth ) ) _ = vectorAvgBlocks c . output = make ( [ ] byte , numUsableMacroblocks / ( c . BlockWidth * c . BlockWidth ) ) vectorHistory := make ( [ ] [ ] motionVector , 0 , c . NumInspectFrames ) ignoredFrames := 0 buf := make ( [ ] byte , 1024 ) s := bufio . NewReader ( conn ) blocksRead := 0 for { _ , err := s . Read ( buf ) if err != nil { log . Println ( " " + err . Error ( ) ) return } bufIdx := 0 for bufIdx < len ( buf ) { temp := motionVector { } temp . X = int8 ( buf [ 0 + bufIdx ] ) temp . Y = int8 ( buf [ 1 + bufIdx ] ) currMacroBlocks = append ( currMacroBlocks , temp ) bufIdx += sizeofMotionVector blocksRead ++ if blocksRead == numMacroblocks { blocksRead = 0 if ignoredFrames < ignoreFirstFrames { ignoredFrames ++ continue } c . condenseBlocksAvg ( & currVectorBlocks , & currMacroBlocks ) c . logVectorHistory ( & vectorHistory , & currVectorBlocks ) if c . publishHist ( caster , & vectorHistory ) > 0 { c . recorder . StopTime = time . Now ( ) . Add ( time . Second * 2 ) } currMacroBlocks = currMacroBlocks [ : 0 ] } } } }
Detect motion by comparing the most recent frame with an average of the past numFrames.
Detect motion by comparing the most recent frame with an average of the past numFrames .
//f, _ := os.Create("motion.vec") // the right-most column is padding? // Manually convert since binary.Read runs really slow on a Pi Zero (~20% CPU) //temp.SAD = int16(buf[2+bufIdx]) << 4 //temp.SAD |= int16(buf[3+bufIdx]) //c.findTemporalAverage(&vectorAvgBlocks, &vectorHistory, &currVectorBlocks) //if c.publishAvg(caster, &vectorAvgBlocks, &currVectorBlocks) > 0 { //binary.Write(f, binary.LittleEndian, &currMacroBlocks) // write to file