2022-02-08 02:27:05 +03:00
|
|
|
//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 (
|
2022-02-08 04:56:38 +03:00
|
|
|
"bytes"
|
2022-03-04 09:39:50 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-02-08 02:27:05 +03:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gocv.io/x/gocv"
|
|
|
|
"gonum.org/v1/gonum/stat"
|
2022-02-09 05:23:18 +03:00
|
|
|
|
2022-02-23 02:56:47 +03:00
|
|
|
"bitbucket.org/ausocean/av/codec/h264"
|
2022-02-08 02:27:05 +03:00
|
|
|
"bitbucket.org/ausocean/av/turbidity"
|
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
|
|
|
)
|
|
|
|
|
2022-02-08 04:56:38 +03:00
|
|
|
// Misc constants.
|
|
|
|
const (
|
2022-04-01 02:55:58 +03:00
|
|
|
maxImages = 1 // Max number of images read when evaluating turbidity.
|
|
|
|
bufferLimit = 20000 // 20KB
|
|
|
|
trimTolerance = 200 // Number of times trim can be called where no keyframe is found.
|
2022-02-08 04:56:38 +03:00
|
|
|
)
|
|
|
|
|
2022-02-08 02:27:05 +03:00
|
|
|
// Turbidity sensor constants.
|
|
|
|
const (
|
2022-02-08 04:56:38 +03:00
|
|
|
k1, k2 = 4, 4 // Block size, must be divisible by the size template with no remainder.
|
2022-02-08 02:27:05 +03:00
|
|
|
filterSize = 3 // Sobel filter size.
|
|
|
|
scale = 1.0 // Amount of scale applied to sobel filter values.
|
|
|
|
alpha = 1.0 // Paramater for contrast equation.
|
|
|
|
)
|
|
|
|
|
2022-02-25 09:32:46 +03:00
|
|
|
// turbidityProbe will hold the latest video data and calculate the sharpness and contrast scores.
|
|
|
|
// These scores will be sent to netreceiver based on the given delay.
|
2022-02-08 03:50:20 +03:00
|
|
|
type turbidityProbe struct {
|
|
|
|
sharpness, contrast float64
|
|
|
|
delay time.Duration
|
|
|
|
ticker time.Ticker
|
|
|
|
ts *turbidity.TurbiditySensor
|
|
|
|
log logger.Logger
|
2022-02-08 04:56:38 +03:00
|
|
|
buffer *bytes.Buffer
|
2022-04-01 02:55:58 +03:00
|
|
|
trimCounter int
|
2022-02-08 03:50:20 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 02:27:05 +03:00
|
|
|
// 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)
|
2022-02-08 04:56:38 +03:00
|
|
|
tp.buffer = bytes.NewBuffer(*new([]byte))
|
2022-02-08 02:27:05 +03:00
|
|
|
|
|
|
|
// Create the turbidity sensor.
|
2022-02-09 03:21:42 +03:00
|
|
|
standard := gocv.IMRead("../../turbidity/images/default.jpg", gocv.IMReadGrayScale)
|
2022-02-08 02:27:05 +03:00
|
|
|
template := gocv.IMRead("../../turbidity/images/template.jpg", gocv.IMReadGrayScale)
|
2022-02-08 04:56:38 +03:00
|
|
|
ts, err := turbidity.NewTurbiditySensor(template, standard, k1, k2, filterSize, scale, alpha, log)
|
2022-02-08 02:27:05 +03:00
|
|
|
if err != nil {
|
2022-03-04 09:39:50 +03:00
|
|
|
return nil, fmt.Errorf("failed to create turbidity sensor: %w", err)
|
2022-02-08 02:27:05 +03:00
|
|
|
}
|
|
|
|
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) {
|
2022-02-25 09:32:46 +03:00
|
|
|
if tp.buffer.Len() == 0 {
|
|
|
|
// The first entry in the buffer must be a keyframe to speed up decoding.
|
2022-02-23 02:56:47 +03:00
|
|
|
video, err := h264.Trim(p)
|
2022-02-21 06:04:02 +03:00
|
|
|
if err != nil {
|
2022-04-01 02:55:58 +03:00
|
|
|
tp.trimCounter++
|
|
|
|
if tp.trimCounter >= trimTolerance {
|
|
|
|
return 0, fmt.Errorf("could not trim h264 within tolerance: %w", err)
|
|
|
|
}
|
|
|
|
return len(p), nil
|
|
|
|
} else {
|
|
|
|
tp.log.Log(logger.Debug, "trim successful", "keyframe error counter", tp.trimCounter)
|
|
|
|
tp.trimCounter = 0
|
2022-02-21 06:04:02 +03:00
|
|
|
}
|
2022-04-01 02:55:58 +03:00
|
|
|
|
2022-02-25 09:32:46 +03:00
|
|
|
n, err := tp.buffer.Write(video)
|
|
|
|
if err != nil {
|
|
|
|
tp.buffer.Reset()
|
2022-03-04 09:39:50 +03:00
|
|
|
return 0, fmt.Errorf("could not write trimmed video to buffer: %w", err)
|
2022-02-25 09:32:46 +03:00
|
|
|
}
|
|
|
|
tp.log.Log(logger.Debug, "video trimmed, write keyframe complete", "size(bytes)", n)
|
|
|
|
} else if tp.buffer.Len() < bufferLimit {
|
2022-04-01 02:55:58 +03:00
|
|
|
// Buffer size is limited to speed up decoding.
|
|
|
|
_, err := tp.buffer.Write(p)
|
2022-02-25 09:32:46 +03:00
|
|
|
if err != nil {
|
|
|
|
tp.buffer.Reset()
|
2022-03-04 09:39:50 +03:00
|
|
|
return 0, fmt.Errorf("could not write to buffer: %w", err)
|
2022-02-25 09:32:46 +03:00
|
|
|
}
|
2022-04-01 02:55:58 +03:00
|
|
|
} else {
|
|
|
|
// Buffer is large enough to begin turbidity calculation.
|
|
|
|
select {
|
|
|
|
case <-tp.ticker.C:
|
|
|
|
tp.log.Log(logger.Debug, "beginning turbidity calculation")
|
|
|
|
startTime := time.Now()
|
|
|
|
err := tp.turbidityCalculation()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not calculate turbidity: %w", err)
|
|
|
|
}
|
|
|
|
tp.log.Log(logger.Debug, "finished turbidity calculation", "total duration (sec)", time.Since(startTime).Seconds())
|
|
|
|
default:
|
2022-03-04 09:39:50 +03:00
|
|
|
}
|
2022-02-21 06:04:02 +03:00
|
|
|
}
|
2022-03-04 09:39:50 +03:00
|
|
|
return len(p), nil
|
2022-02-08 02:27:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tp *turbidityProbe) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-08 04:56:38 +03:00
|
|
|
|
2022-03-04 09:39:50 +03:00
|
|
|
func (tp *turbidityProbe) turbidityCalculation() error {
|
2022-02-08 04:56:38 +03:00
|
|
|
var imgs []gocv.Mat
|
|
|
|
img := gocv.NewMat()
|
2022-03-18 09:04:55 +03:00
|
|
|
|
2022-02-08 04:56:38 +03:00
|
|
|
// Write byte array to a temp file.
|
|
|
|
file, err := os.CreateTemp("temp", "video*.h264")
|
|
|
|
if err != nil {
|
2022-03-04 09:39:50 +03:00
|
|
|
return fmt.Errorf("failed to create temp file: %w", err)
|
2022-02-08 04:56:38 +03:00
|
|
|
}
|
2022-02-25 09:32:46 +03:00
|
|
|
tp.log.Log(logger.Debug, "writing to file", "buffer size(bytes)", tp.buffer.Len())
|
2022-03-18 09:04:55 +03:00
|
|
|
|
2022-02-25 09:32:46 +03:00
|
|
|
_, err = file.Write(tp.buffer.Bytes())
|
2022-02-08 04:56:38 +03:00
|
|
|
if err != nil {
|
2022-03-04 09:39:50 +03:00
|
|
|
return fmt.Errorf("failed to write to temporary file: %w", err)
|
2022-02-08 04:56:38 +03:00
|
|
|
}
|
2022-02-25 09:32:46 +03:00
|
|
|
tp.log.Log(logger.Debug, "write to file success", "buffer size(bytes)", tp.buffer.Len())
|
2022-02-08 04:56:38 +03:00
|
|
|
tp.buffer.Reset()
|
2022-02-16 04:46:09 +03:00
|
|
|
|
2022-02-23 02:56:47 +03:00
|
|
|
// Open the video file.
|
|
|
|
startTime := time.Now()
|
2022-02-08 04:56:38 +03:00
|
|
|
vc, err := gocv.VideoCaptureFile(file.Name())
|
|
|
|
if err != nil {
|
2022-03-04 09:39:50 +03:00
|
|
|
return fmt.Errorf("failed to open video file: %w", err)
|
2022-02-08 04:56:38 +03:00
|
|
|
}
|
2022-02-23 02:56:47 +03:00
|
|
|
tp.log.Log(logger.Debug, "video capture open", "total duration (sec)", time.Since(startTime).Seconds())
|
|
|
|
|
2022-03-04 09:39:50 +03:00
|
|
|
// Store each frame until maximum amount is reached.
|
2022-02-16 04:46:09 +03:00
|
|
|
startTime = time.Now()
|
2022-02-08 04:56:38 +03:00
|
|
|
for vc.Read(&img) && len(imgs) < maxImages {
|
|
|
|
imgs = append(imgs, img.Clone())
|
|
|
|
}
|
|
|
|
if len(imgs) <= 0 {
|
2022-03-04 09:39:50 +03:00
|
|
|
return errors.New("no frames found")
|
2022-02-08 04:56:38 +03:00
|
|
|
}
|
2022-02-16 04:46:09 +03:00
|
|
|
tp.log.Log(logger.Debug, "read time", "total duration (sec)", time.Since(startTime).Seconds())
|
2022-02-23 02:56:47 +03:00
|
|
|
|
2022-02-08 04:56:38 +03:00
|
|
|
// Process video data to get saturation and contrast scores.
|
|
|
|
res, err := tp.ts.Evaluate(imgs)
|
|
|
|
if err != nil {
|
2022-03-04 09:39:50 +03:00
|
|
|
err_ := cleanUp(file.Name(), vc)
|
|
|
|
if err_ != nil {
|
2022-03-17 01:51:26 +03:00
|
|
|
return fmt.Errorf("could not clean up: %v, after evaluation error: %w", err_, err)
|
2022-03-04 09:39:50 +03:00
|
|
|
}
|
|
|
|
return fmt.Errorf("evaluation error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tp.contrast = stat.Mean(res.Contrast, nil)
|
|
|
|
tp.sharpness = stat.Mean(res.Sharpness, nil)
|
|
|
|
|
|
|
|
err = cleanUp(file.Name(), vc)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not clean up: %w", err)
|
2022-02-08 04:56:38 +03:00
|
|
|
}
|
2022-03-04 09:39:50 +03:00
|
|
|
return nil
|
|
|
|
}
|
2022-02-21 06:04:02 +03:00
|
|
|
|
2022-03-04 09:39:50 +03:00
|
|
|
func cleanUp(file string, vc *gocv.VideoCapture) error {
|
|
|
|
err := os.Remove(file)
|
2022-02-21 06:04:02 +03:00
|
|
|
if err != nil {
|
2022-03-04 09:39:50 +03:00
|
|
|
return fmt.Errorf("could not remove temp file: %w", err)
|
2022-02-21 06:04:02 +03:00
|
|
|
}
|
|
|
|
err = vc.Close()
|
|
|
|
if err != nil {
|
2022-03-04 09:39:50 +03:00
|
|
|
return fmt.Errorf("could not close video capture device: %w", err)
|
2022-02-21 06:04:02 +03:00
|
|
|
}
|
2022-03-04 09:39:50 +03:00
|
|
|
return nil
|
2022-02-21 06:04:02 +03:00
|
|
|
}
|