turbidity package integration and improved logging

This commit is contained in:
Russell Stanley 2022-01-13 11:38:30 +10:30 committed by Saxon Nelson-Milton
parent ee9ee08220
commit d2929f7415
2 changed files with 114 additions and 43 deletions

View File

@ -56,20 +56,20 @@ package main
import ( import (
"io" "io"
"io/ioutil"
"log"
"os" "os"
"runtime/pprof" "runtime/pprof"
"strconv" "strconv"
"time" "time"
"gocv.io/x/gocv" "gocv.io/x/gocv"
"gonum.org/v1/gonum/stat"
"gopkg.in/natefinch/lumberjack.v2" "gopkg.in/natefinch/lumberjack.v2"
"bitbucket.org/ausocean/av/container/mts" "bitbucket.org/ausocean/av/container/mts"
"bitbucket.org/ausocean/av/container/mts/meta" "bitbucket.org/ausocean/av/container/mts/meta"
"bitbucket.org/ausocean/av/revid" "bitbucket.org/ausocean/av/revid"
"bitbucket.org/ausocean/av/revid/config" "bitbucket.org/ausocean/av/revid/config"
"bitbucket.org/ausocean/av/turbidity"
"bitbucket.org/ausocean/iot/pi/netlogger" "bitbucket.org/ausocean/iot/pi/netlogger"
"bitbucket.org/ausocean/iot/pi/netsender" "bitbucket.org/ausocean/iot/pi/netsender"
"bitbucket.org/ausocean/iot/pi/sds" "bitbucket.org/ausocean/iot/pi/sds"
@ -107,7 +107,6 @@ const (
profilePath = "rv.prof" profilePath = "rv.prof"
pkg = "rv: " pkg = "rv: "
runPreDelay = 20 * time.Second runPreDelay = 20 * time.Second
turbidityDelay = time.Second // Time delay for evaluating turbidity.
maxImages = 10 // Max number of images read when evaluating turbidity. maxImages = 10 // Max number of images read when evaluating turbidity.
) )
@ -119,53 +118,85 @@ const (
contrastPin = "X39" contrastPin = "X39"
) )
// Turbidity sensor constants.
const (
k1, k2 = 8, 8 // Block size, must be divisible by the size template with no remainder.
filterSize = 3 // Sobel filter size.
scale = 1.0 // Amount of scale applied to sobel filter values.
alpha = 1.0 // Paramater for contrast equation.
)
// This is set to true if the 'profile' build tag is provided on build. // This is set to true if the 'profile' build tag is provided on build.
var canProfile = false var canProfile = false
type turbidityProbe struct { type turbidityProbe struct {
sharpness, contrast float64 sharpness, contrast float64
delay time.Duration
ticker time.Ticker
ts *turbidity.TurbiditySensor
log logger.Logger
} }
// TODO(Russell): complete this implementation of Write. // NewTurbidityProbe returns a new turbidity probe.
func (tp *turbidityProbe) Write(p []byte) (int, error) { func NewTurbidityProbe(log logger.Logger, delay time.Duration) (*turbidityProbe, error) {
ticker := time.NewTicker(turbidityDelay) tp := new(turbidityProbe)
tp.log = log
tp.delay = delay
tp.ticker = *time.NewTicker(delay)
go func() { // Create the turbidity sensor.
for { standard := gocv.IMRead("../../turbidity/images/template.jpg", gocv.IMReadGrayScale)
template := gocv.IMRead("../../turbidity/images/template.jpg", gocv.IMReadGrayScale)
ts, err := turbidity.NewTurbiditySensor(template, standard, k1, k2, filterSize, scale, alpha)
if err != nil {
log.Error("failed create turbidity sensor", "error", err.Error())
}
tp.ts = ts
return tp, nil
}
// Write, reads input h264 frames in the form of a byte stream and writes the the sharpness and contrast
// scores of a video to the the turbidity probe.
func (tp *turbidityProbe) Write(p []byte) (int, error) {
select { select {
case <-ticker.C: case <-tp.ticker.C:
var imgs []gocv.Mat var imgs []gocv.Mat
img := gocv.NewMat() img := gocv.NewMat()
// Write byte array to a temp file. // Write byte array to a temp file.
file, err := ioutil.TempFile("temp", "video*.h264") file, err := os.CreateTemp("temp", "video*.h264")
if err != nil { if err != nil {
log.Fatalf("failed to create temp file: %v", err) tp.log.Error("failed to create temp file", "error", err.Error())
return len(p), err
} }
defer os.Remove(file.Name()) defer os.Remove(file.Name())
_, err = file.Write(p) _, err = file.Write(p)
if err != nil { if err != nil {
log.Fatalf("failed to write to %v: %v", file.Name(), err) tp.log.Error("failed to write to temporary file", "error", err.Error())
return len(p), err
} }
// Read the file and store each frame. // Read the file and store each frame.
vc, err := gocv.VideoCaptureFile(file.Name()) vc, err := gocv.VideoCaptureFile(file.Name())
if err != nil { if err != nil {
log.Fatalf("failed to read read file, %v: %v", file.Name(), err) tp.log.Error("failed to open video file", "error", err.Error())
return len(p), err
} }
for vc.Read(&img) && len(imgs) < maxImages { for vc.Read(&img) && len(imgs) < maxImages {
imgs = append(imgs, img.Clone()) imgs = append(imgs, img.Clone())
} }
// Process video data to get saturation and contrast scores. - TODO // Process video data to get saturation and contrast scores.
tp.contrast = float64(len(imgs)) res, err := tp.ts.Evaluate(imgs)
tp.sharpness = float64(len(imgs)) if err != nil {
return tp.log.Error("evaluate failed", "errror", err.Error())
return len(p), err
} }
tp.contrast = stat.Mean(res.Contrast, nil)
tp.sharpness = stat.Mean(res.Sharpness, nil)
default:
return len(p), nil
} }
}()
time.Sleep(5 * time.Second)
return len(p), nil return len(p), nil
} }
@ -202,6 +233,7 @@ func main() {
rv *revid.Revid rv *revid.Revid
p *turbidityProbe p *turbidityProbe
) )
p, err := NewTurbidityProbe(*log, 60*time.Second)
log.Log(logger.Debug, "initialising netsender client") log.Log(logger.Debug, "initialising netsender client")
ns, err := netsender.New(log, nil, readPin(p, rv), nil, createVarMap()) ns, err := netsender.New(log, nil, readPin(p, rv), nil, createVarMap())

View File

@ -1,14 +1,54 @@
/*
DESCRIPTION
Testing function for turbidity probe.
AUTHORS
Russell Stanley <russell@ausocean.org>
LICENSE
Copyright (C) 2021-2022 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package main package main
import ( import (
"io"
"io/ioutil" "io/ioutil"
"testing" "testing"
"time"
"bitbucket.org/ausocean/utils/logger"
"gopkg.in/natefinch/lumberjack.v2"
) )
// TestProbe reads a given video file and writes the sharpness and contrast scores to a turbidity probe // TestProbe reads a given video file and writes the sharpness and contrast scores to a turbidity probe.
func TestProbe(t *testing.T) { func TestProbe(t *testing.T) {
ts := new(turbidityProbe) // Create lumberjack logger.
fileLog := &lumberjack.Logger{
Filename: logPath,
MaxSize: logMaxSize,
MaxBackups: logMaxBackup,
MaxAge: logMaxAge,
}
log := logger.New(logVerbosity, io.MultiWriter(fileLog), logSuppress)
ts, err := NewTurbidityProbe(*log, time.Microsecond)
if err != nil {
t.Fatalf("failed to create turbidity probe")
}
video, err := ioutil.ReadFile("logo.h264") video, err := ioutil.ReadFile("logo.h264")
if err != nil { if err != nil {
t.Fatalf("failed to read file: %v", err) t.Fatalf("failed to read file: %v", err)
@ -18,6 +58,5 @@ func TestProbe(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("failed to write sharpness and contrast: %v", err) t.Fatalf("failed to write sharpness and contrast: %v", err)
} }
t.Logf("contrast: %v, sharpness: %v\n", ts.contrast, ts.sharpness) t.Logf("contrast: %v, sharpness: %v\n", ts.contrast, ts.sharpness)
} }