mirror of https://bitbucket.org/ausocean/av.git
turbidity package integration and improved logging
This commit is contained in:
parent
ee9ee08220
commit
d2929f7415
112
cmd/rv/main.go
112
cmd/rv/main.go
|
@ -56,20 +56,20 @@ package main
|
|||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"runtime/pprof"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gocv.io/x/gocv"
|
||||
"gonum.org/v1/gonum/stat"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
|
||||
"bitbucket.org/ausocean/av/container/mts"
|
||||
"bitbucket.org/ausocean/av/container/mts/meta"
|
||||
"bitbucket.org/ausocean/av/revid"
|
||||
"bitbucket.org/ausocean/av/revid/config"
|
||||
"bitbucket.org/ausocean/av/turbidity"
|
||||
"bitbucket.org/ausocean/iot/pi/netlogger"
|
||||
"bitbucket.org/ausocean/iot/pi/netsender"
|
||||
"bitbucket.org/ausocean/iot/pi/sds"
|
||||
|
@ -107,8 +107,7 @@ const (
|
|||
profilePath = "rv.prof"
|
||||
pkg = "rv: "
|
||||
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.
|
||||
)
|
||||
|
||||
// Software define pin values.
|
||||
|
@ -119,53 +118,85 @@ const (
|
|||
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.
|
||||
var canProfile = false
|
||||
|
||||
type turbidityProbe struct {
|
||||
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 NewTurbidityProbe(log logger.Logger, delay time.Duration) (*turbidityProbe, error) {
|
||||
tp := new(turbidityProbe)
|
||||
tp.log = log
|
||||
tp.delay = delay
|
||||
tp.ticker = *time.NewTicker(delay)
|
||||
|
||||
// Create the turbidity sensor.
|
||||
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) {
|
||||
ticker := time.NewTicker(turbidityDelay)
|
||||
select {
|
||||
case <-tp.ticker.C:
|
||||
var imgs []gocv.Mat
|
||||
img := gocv.NewMat()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
var imgs []gocv.Mat
|
||||
img := gocv.NewMat()
|
||||
|
||||
// Write byte array to a temp file.
|
||||
file, err := ioutil.TempFile("temp", "video*.h264")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create temp file: %v", err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
_, err = file.Write(p)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to write to %v: %v", file.Name(), err)
|
||||
}
|
||||
|
||||
// Read the file and store each frame.
|
||||
vc, err := gocv.VideoCaptureFile(file.Name())
|
||||
if err != nil {
|
||||
log.Fatalf("failed to read read file, %v: %v", file.Name(), err)
|
||||
}
|
||||
for vc.Read(&img) && len(imgs) < maxImages {
|
||||
imgs = append(imgs, img.Clone())
|
||||
}
|
||||
|
||||
// Process video data to get saturation and contrast scores. - TODO
|
||||
tp.contrast = float64(len(imgs))
|
||||
tp.sharpness = float64(len(imgs))
|
||||
return
|
||||
}
|
||||
// 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(p)
|
||||
if err != nil {
|
||||
tp.log.Error("failed to write to temporary file", "error", err.Error())
|
||||
return len(p), err
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
// 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())
|
||||
}
|
||||
|
||||
// Process video data to get saturation and contrast scores.
|
||||
res, err := tp.ts.Evaluate(imgs)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
|
@ -202,6 +233,7 @@ func main() {
|
|||
rv *revid.Revid
|
||||
p *turbidityProbe
|
||||
)
|
||||
p, err := NewTurbidityProbe(*log, 60*time.Second)
|
||||
|
||||
log.Log(logger.Debug, "initialising netsender client")
|
||||
ns, err := netsender.New(log, nil, readPin(p, rv), nil, createVarMap())
|
||||
|
|
|
@ -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
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"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) {
|
||||
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")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read file: %v", err)
|
||||
|
@ -18,6 +58,5 @@ func TestProbe(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("failed to write sharpness and contrast: %v", err)
|
||||
}
|
||||
|
||||
t.Logf("contrast: %v, sharpness: %v\n", ts.contrast, ts.sharpness)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue