added go routine for entire turbidity calculation

This commit is contained in:
Russell Stanley 2022-02-07 11:06:25 +10:30
parent b0f756a586
commit 140311fec9
1 changed files with 49 additions and 43 deletions

View File

@ -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}})