Skip to content

Commit

Permalink
Add taking short recordings at start and end of recording window.
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronRP committed Dec 6, 2022
1 parent c896b2d commit cb96aec
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/thermal-recorder/cptv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func BenchmarkMotionDetection(b *testing.B) {

recorder := new(recorder.NoWriteRecorder)

processor := motion.NewMotionProcessor(lepton3.ParseRawFrame, &config.Motion, &config.Recorder, &config.Location, nil, recorder, new(TestCamera), nil)
processor := motion.NewMotionProcessor(lepton3.ParseRawFrame, &config.Motion, &config.Recorder, &config.Location, nil, recorder, new(TestCamera), nil, nil)
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down
2 changes: 1 addition & 1 deletion cmd/thermal-recorder/cptvplaybacktester.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (cpt *CPTVPlaybackTester) Detect(filename string) *EventLoggingRecordingLis
listener.config = cpt.config
listener.verbose = verbose
listener.framesHz = camera.FPS()
processor := motion.NewMotionProcessor(lepton3.ParseRawFrame, &cpt.config.Motion, &cpt.config.Recorder, &cpt.config.Location, listener, recorder, camera, nil)
processor := motion.NewMotionProcessor(lepton3.ParseRawFrame, &cpt.config.Motion, &cpt.config.Recorder, &cpt.config.Location, listener, recorder, camera, nil, nil)

if err != nil {
log.Printf("Could not open file %v", err)
Expand Down
32 changes: 32 additions & 0 deletions cmd/thermal-recorder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/TheCacophonyProject/event-reporter/eventclient"
"github.com/TheCacophonyProject/go-cptv/cptvframe"
"github.com/TheCacophonyProject/lepton3"
"github.com/TheCacophonyProject/window"
arg "github.com/alexflint/go-arg"
"periph.io/x/periph/host"

Expand Down Expand Up @@ -184,9 +185,13 @@ func handleConn(conn net.Conn, conf *Config) error {
recorder,
headerInfo,
constantRecorder,
NewCPTVFileRecorder(conf, headerInfo, headerInfo.Brand(), headerInfo.Model(), headerInfo.CameraSerial(), headerInfo.Firmware()),
)

log.Print("reading frames")

go snapshotTriggers(processor, conf.Recorder.Window)

frameLogIntervalFirstMin *= headerInfo.FPS()
frameLogInterval *= headerInfo.FPS()
rawFrame := make([]byte, headerInfo.FrameSize())
Expand Down Expand Up @@ -254,3 +259,30 @@ func logConfig(conf *Config) {
log.Printf("location longitude: %v", conf.Location.Longitude)
log.Printf("recording window: %s", conf.Recorder.Window)
}

func snapshotTriggers(processor *motion.MotionProcessor, window window.Window) {
if window.NoWindow {
log.Println("no recording window so will make snapshot every 12 hours")
triggerTime := time.Now().Add(time.Minute)
for {
time.Sleep(time.Until(triggerTime))
log.Println("making snapshot")
processor.StartSnapshot = true
triggerTime = triggerTime.Add(time.Hour * 12)
}
}

// Make snapshot at start of window.
sleepTime := time.Minute
if !window.Active() {
sleepTime = time.Until(window.NextStart()) + time.Minute
}
time.Sleep(sleepTime)
log.Println("making start of window snapshot")
processor.StartSnapshot = true

// Make snapshot at end of window.
time.Sleep(time.Until(window.NextEnd()) - 2*time.Minute)
log.Println("making end of window snapshot")
processor.StartSnapshot = true
}
31 changes: 31 additions & 0 deletions motion/motionprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewMotionProcessor(
recorder recorder.Recorder,
c cptvframe.CameraSpec,
constantRecorder recorder.Recorder,
snapshotRecorder recorder.Recorder,
) *MotionProcessor {
return &MotionProcessor{
parseFrame: parseFrame,
Expand All @@ -60,6 +61,7 @@ func NewMotionProcessor(
constantRecorder: constantRecorder,
constantRecording: !isNullOrNullPointer(constantRecorder),
CurrentFrame: 0,
snapshotRecorder: snapshotRecorder,
}
}

Expand Down Expand Up @@ -91,6 +93,10 @@ type MotionProcessor struct {
constantRecorder recorder.Recorder
crFrames int
CurrentFrame uint32
snapshotRecorder recorder.Recorder
StartSnapshot bool
SnapshotRecording bool
snapshotFrames int
}

type RecordingListener interface {
Expand All @@ -114,9 +120,34 @@ func (mp *MotionProcessor) Process(rawFrame []byte) error {
mp.CurrentFrame += 1
mp.process(frame)
mp.processConstantRecorder(frame)
mp.processSnapshot(frame)
return nil
}

func (mp *MotionProcessor) processSnapshot(frame *cptvframe.Frame) {
if mp.StartSnapshot {
mp.StartSnapshot = false
if err := mp.snapshotRecorder.StartRecording(mp.motionDetector.background, 0); err != nil {
mp.log.Printf("error with starting constant recorder: %v", err)
return
}
mp.SnapshotRecording = true
}
if !mp.SnapshotRecording {
return
}
mp.snapshotRecorder.WriteFrame(frame)
mp.snapshotFrames++
if mp.snapshotFrames > 20 {
mp.SnapshotRecording = false
if err := mp.snapshotRecorder.StopRecording(); err != nil {
mp.log.Printf("error with stoping constant recorder: %v", err)
return
}
mp.snapshotFrames = 0
}
}

func (mp *MotionProcessor) stopConstantRecorder() {
if !mp.constantRecording {
return
Expand Down
2 changes: 1 addition & 1 deletion motion/motionprocessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func FramesFrom(start, end int) []int {
func SetupTest(mConf *config.ThermalMotion, rConf *recorder.RecorderConfig, lConf *config.Location) (*TestRecorder, *TestFrameMaker) {
recorder := new(TestRecorder)
camera := new(TestCamera)
processor := NewMotionProcessor(lepton3.ParseRawFrame, mConf, rConf, lConf, nil, recorder, camera, nil)
processor := NewMotionProcessor(lepton3.ParseRawFrame, mConf, rConf, lConf, nil, recorder, camera, nil, nil)

scenarioMaker := MakeTestFrameMaker(processor, camera)
return recorder, scenarioMaker
Expand Down

0 comments on commit cb96aec

Please sign in to comment.