mirror of https://bitbucket.org/ausocean/av.git
initial turbidity probe integration
This commit is contained in:
parent
3614d5c2cc
commit
357510cfab
|
@ -56,6 +56,8 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -105,14 +107,15 @@ const (
|
||||||
profilePath = "rv.prof"
|
profilePath = "rv.prof"
|
||||||
pkg = "rv: "
|
pkg = "rv: "
|
||||||
runPreDelay = 20 * time.Second
|
runPreDelay = 20 * time.Second
|
||||||
turbidityDelay = 60 * time.Second
|
turbidityDelay = time.Second // Time delay for evaluating turbidity.
|
||||||
|
maxImages = 10 // Max number of images read when evaluating turbidity.
|
||||||
)
|
)
|
||||||
|
|
||||||
// Software define pin values.
|
// Software define pin values.
|
||||||
// See https://netreceiver.appspot.com/help, External Pin Assignments.
|
// See https://netreceiver.appspot.com/help, External Pin Assignments.
|
||||||
const (
|
const (
|
||||||
bitratePin = "X36"
|
bitratePin = "X36"
|
||||||
saturationPin = "X38"
|
sharpnessPin = "X38"
|
||||||
contrastPin = "X39"
|
contrastPin = "X39"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -120,31 +123,49 @@ const (
|
||||||
var canProfile = false
|
var canProfile = false
|
||||||
|
|
||||||
type turbidityProbe struct {
|
type turbidityProbe struct {
|
||||||
saturation, contrast float64
|
sharpness, contrast float64
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(Russell): complete this implementation of Write.
|
// TODO(Russell): complete this implementation of Write.
|
||||||
func (tp *turbidityProbe) Write(p []byte) (int, error) {
|
func (tp *turbidityProbe) Write(p []byte) (int, error) {
|
||||||
|
|
||||||
ticker := time.NewTicker(turbidityDelay)
|
ticker := time.NewTicker(turbidityDelay)
|
||||||
const (
|
|
||||||
rows = 2464
|
|
||||||
cols = 3280
|
|
||||||
)
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
mat, err := gocv.NewMatFromBytes(rows, cols, gocv.MatTypeCV8U, p) // IMDecode may also work
|
var imgs []gocv.Mat
|
||||||
|
img := gocv.NewMat()
|
||||||
|
|
||||||
|
// Write byte array to a temp file.
|
||||||
|
file, err := ioutil.TempFile("temp", "video*.h264")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//TODO
|
log.Fatalf("failed to create temp file: %v", err)
|
||||||
}
|
}
|
||||||
mat.GetUCharAt(0, 0)
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -332,10 +353,10 @@ func readPin(p *turbidityProbe, rv *revid.Revid) func(pin *netsender.Pin) error
|
||||||
}
|
}
|
||||||
case pin.Name[0] == 'X':
|
case pin.Name[0] == 'X':
|
||||||
return sds.ReadSystem(pin)
|
return sds.ReadSystem(pin)
|
||||||
case pin.Name == saturationPin:
|
case pin.Name == sharpnessPin:
|
||||||
pin.Value = -1
|
pin.Value = -1
|
||||||
if p != nil {
|
if p != nil {
|
||||||
pin.Value = int(p.saturation * 1000)
|
pin.Value = int(p.sharpness * 1000)
|
||||||
}
|
}
|
||||||
case pin.Name == contrastPin:
|
case pin.Name == contrastPin:
|
||||||
pin.Value = -1
|
pin.Value = -1
|
||||||
|
|
|
@ -3,32 +3,21 @@ package main
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gocv.io/x/gocv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
// TestProbe reads a given video file and writes the sharpness and contrast scores to a turbidity probe
|
||||||
//ts := new(turbidityProbe)
|
func TestProbe(t *testing.T) {
|
||||||
const (
|
ts := new(turbidityProbe)
|
||||||
rows = 2464
|
|
||||||
cols = 3280
|
|
||||||
)
|
|
||||||
|
|
||||||
// Read frame
|
video, err := ioutil.ReadFile("logo.h264")
|
||||||
frame, err := ioutil.ReadFile("AusOcean_logo_1080p.h264")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatalf("failed to read file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode
|
_, err = ts.Write(video)
|
||||||
//mat, err := gocv.NewMatFromBytes(rows, cols, gocv.MatTypeCV8UC3, frame)
|
|
||||||
mat, err := gocv.IMDecode(frame, gocv.IMReadUnchanged)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatalf("failed to write sharpness and contrast: %v", err)
|
||||||
}
|
|
||||||
if mat.Empty() {
|
|
||||||
t.Fatal("Empty gocv.Mat")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gocv.IMWrite("logo.jpg", mat)
|
t.Logf("contrast: %v, sharpness: %v\n", ts.contrast, ts.sharpness)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue