patch build issues from latest turbidity probe PR

This commit is contained in:
Russell Stanley 2022-02-08 09:57:05 +10:30
parent 18b9a3dbab
commit 4f33f13358
6 changed files with 174 additions and 86 deletions

View File

@ -61,8 +61,6 @@ import (
"strconv" "strconv"
"time" "time"
"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"
@ -107,7 +105,6 @@ const (
profilePath = "rv.prof" profilePath = "rv.prof"
pkg = "rv: " pkg = "rv: "
runPreDelay = 20 * time.Second runPreDelay = 20 * time.Second
maxImages = 10 // Max number of images read when evaluating turbidity.
) )
// Software define pin values. // Software define pin values.
@ -118,14 +115,6 @@ 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
@ -137,73 +126,6 @@ type turbidityProbe struct {
log logger.Logger log logger.Logger
} }
// 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) {
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(p)
if err != nil {
tp.log.Error("failed to write to temporary file", "error", err.Error())
return len(p), err
}
// 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
}
func (tp *turbidityProbe) Close() error {
return nil
}
func main() { func main() {
mts.Meta = meta.NewWith([][2]string{{metaPreambleKey, metaPreambleData}}) mts.Meta = meta.NewWith([][2]string{{metaPreambleKey, metaPreambleData}})

120
cmd/rv/probe.go Normal file
View File

@ -0,0 +1,120 @@
//go:build !nocv
// +build !nocv
/*
DESCRIPTION
Provides the methods for the turbidity probe using GoCV. Turbidity probe
will collect the most recent frames in a buffer and write the latest sharpness
and contrast scores to the 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 (
"os"
"time"
"bitbucket.org/ausocean/av/turbidity"
"bitbucket.org/ausocean/utils/logger"
"gocv.io/x/gocv"
"gonum.org/v1/gonum/stat"
)
// 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.
)
// Misc constants.
const (
maxImages = 10 // Max number of images read when evaluating turbidity.
)
// 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) {
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(p)
if err != nil {
tp.log.Error("failed to write to temporary file", "error", err.Error())
return len(p), err
}
// 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
}
func (tp *turbidityProbe) Close() error {
return nil
}

52
cmd/rv/probe_circleci.go Normal file
View File

@ -0,0 +1,52 @@
//go:build nocv
// +build nocv
/*
DESCRIPTION
Replaces turbidity probe implementation that uses the gocv package.
When Circle-CI builds revid this is needed because Circle-CI does not
have a copy of Open CV installed.
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 (
"time"
"bitbucket.org/ausocean/utils/logger"
)
// NewTurbidityProbe returns a new turbidity probe.
func NewTurbidityProbe(log logger.Logger, delay time.Duration) (*turbidityProbe, error) {
tp := new(turbidityProbe)
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) {
return len(p), nil
}
func (tp *turbidityProbe) Close() error {
return nil
}

View File

@ -1,6 +1,6 @@
/* /*
NAME NAME
audio_OSX.go audio_darwin.go
AUTHORS AUTHORS
Russell Stanley <russell@ausocean.org> Russell Stanley <russell@ausocean.org>
@ -29,5 +29,5 @@ import (
) )
func (r *Revid) setupAudio() error { func (r *Revid) setupAudio() error {
return errors.New("audio not implemented on OSX") return errors.New("audio not implemented on darwin(macOS)")
} }

View File

@ -1,6 +1,3 @@
//go:build !nocv
// +build !nocv
/* /*
DESCRIPTION DESCRIPTION
Results struct used to store results from the turbidity sensor. Results struct used to store results from the turbidity sensor.

View File

@ -1,6 +1,3 @@
//go:build !nocv
// +build !nocv
/* /*
DESCRIPTION DESCRIPTION
Holds the turbidity sensor struct. Can evaluate 4x4 chessboard markers in an Holds the turbidity sensor struct. Can evaluate 4x4 chessboard markers in an