From 140311fec960c1fc77c90ff8b059c69d24fbeaca Mon Sep 17 00:00:00 2001 From: Russell Stanley Date: Mon, 7 Feb 2022 11:06:25 +1030 Subject: [PATCH] added go routine for entire turbidity calculation --- cmd/rv/main.go | 92 +++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 43 deletions(-) diff --git a/cmd/rv/main.go b/cmd/rv/main.go index bb5666fc..da6d2415 100644 --- a/cmd/rv/main.go +++ b/cmd/rv/main.go @@ -163,49 +163,7 @@ func (tp *turbidityProbe) Write(p []byte) (int, error) { tp.buffer.Write(p) select { case <-tp.ticker.C: - var imgs []gocv.Mat - img := gocv.NewMat() - - // Write byte array to a temp file. - file, err := os.CreateTemp("temp", "video*.h264") - if err != nil { - tp.log.Error("failed to create temp file", "error", err.Error()) - return len(p), err - } - defer os.Remove(file.Name()) - _, err = file.Write(tp.buffer.Bytes()) - if err != nil { - tp.log.Error("failed to write to temporary file", "error", err.Error()) - return len(p), err - } - tp.buffer.Reset() - // Read the file and store each frame. - vc, err := gocv.VideoCaptureFile(file.Name()) - if err != nil { - tp.log.Error("failed to open video file", "error", err.Error()) - return len(p), err - } - for vc.Read(&img) && len(imgs) < maxImages { - imgs = append(imgs, img.Clone()) - } - if len(imgs) <= 0 { - tp.log.Log(logger.Warning, "no frames found", "error", err.Error()) - return len(p), nil - } - tp.log.Log(logger.Debug, "found frames", "frames", len(imgs)) - - // Process video data to get saturation and contrast scores. - go func() { - startTime := time.Now() - res, err := tp.ts.Evaluate(imgs) - if err != nil { - tp.log.Error("evaluate failed", "error", err.Error()) - } else { - tp.log.Log(logger.Debug, "finished evaluation", "total duration (sec)", time.Since(startTime).Seconds()) - tp.contrast = stat.Mean(res.Contrast, nil) - tp.sharpness = stat.Mean(res.Sharpness, nil) - } - }() + go tp.turbidityCalculation(p) default: return len(p), nil } @@ -216,6 +174,54 @@ func (tp *turbidityProbe) Close() error { return nil } +func (tp *turbidityProbe) turbidityCalculation(p []byte) { + var imgs []gocv.Mat + img := gocv.NewMat() + // Write byte array to a temp file. + file, err := os.CreateTemp("temp", "video*.h264") + if err != nil { + tp.log.Error("failed to create temp file", "error", err.Error()) + // TODO: Error handling. + return + } + defer os.Remove(file.Name()) + _, err = file.Write(tp.buffer.Bytes()) + if err != nil { + tp.log.Error("failed to write to temporary file", "error", err.Error()) + // TODO: Error handling. + return + } + tp.buffer.Reset() + // Read the file and store each frame. + vc, err := gocv.VideoCaptureFile(file.Name()) + if err != nil { + tp.log.Error("failed to open video file", "error", err.Error()) + // TODO: Error handling. + return + } + for vc.Read(&img) && len(imgs) < maxImages { + imgs = append(imgs, img.Clone()) + } + if len(imgs) <= 0 { + tp.log.Log(logger.Warning, "no frames found", "error", err.Error()) + return + } + tp.log.Log(logger.Debug, "found frames", "frames", len(imgs)) + + // Process video data to get saturation and contrast scores. + startTime := time.Now() + res, err := tp.ts.Evaluate(imgs) + if err != nil { + tp.log.Error("evaluate failed", "error", err.Error()) + // TODO: Error handling. + } else { + tp.log.Log(logger.Debug, "finished evaluation", "total duration (sec)", time.Since(startTime).Seconds()) + tp.contrast = stat.Mean(res.Contrast, nil) + tp.sharpness = stat.Mean(res.Sharpness, nil) + } + return +} + func main() { mts.Meta = meta.NewWith([][2]string{{metaPreambleKey, metaPreambleData}})