From 4a196a21b2c91fe3bdbf12641ef1e65feed4da97 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Thu, 9 Jul 2020 18:42:58 +0000 Subject: [PATCH] GetStreamInstant returns a Mat now --- main.go | 30 +++++++++++++----------------- stream.go | 10 ++++++++-- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index eba6230..c870dec 100755 --- a/main.go +++ b/main.go @@ -54,34 +54,30 @@ func (server Server) stream(w http.ResponseWriter, r *http.Request) { return } - img := stream.GetStreamInstant() - mat, err := gocv.IMDecode(img, gocv.IMReadGrayScale) - if err != nil { - log.Fatal("Could not IMDecode img") - } + mat := stream.GetStreamInstant() - //gocv.Resize(mat, &mat, image.Point{X: 0, Y: 0}, 0, 0, gocv.InterpolationCubic) - gocv.GaussianBlur(mat, &mat, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect) + diff := gocv.NewMat() + mat.CopyTo(&diff) + gocv.CvtColor(diff, &diff, gocv.ColorBGRAToGray) + gocv.GaussianBlur(diff, &diff, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect) go stream.SaveStreamInstant(URL, mat) if stream.PreviousInstantPathExists() { - if err != nil { - log.Fatal("Could not IMDecode img") - } - previous := stream.IMReadPrevious() gocv.GaussianBlur(previous, &previous, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect) - gocv.AbsDiff(previous, mat, &mat) + gocv.AbsDiff(previous, diff, &diff) - gocv.Threshold(mat, &mat, 25, 255, gocv.ThresholdBinary) - - //img, err = gocv.IMEncode(".jpg", newMat) + gocv.Threshold(diff, &diff, 25, 255, gocv.ThresholdBinary) } - w.Write(img) + jpg, err := gocv.IMEncode(".jpg", mat) + if err != nil { + log.Fatal("Could not encode Mat to jpg:", URL) + } + w.Write(jpg) } func main() { @@ -105,7 +101,7 @@ func main() { stream := Stream{} json.Unmarshal([]byte(streamJSONFile), &stream) server.Streams[stream.URL] = stream - go stream.Update() + go stream.UpdateInterval() } http.HandleFunc("/", server.index) diff --git a/stream.go b/stream.go index 96b464c..a034229 100755 --- a/stream.go +++ b/stream.go @@ -46,14 +46,20 @@ func (s Stream) UpdateInterval() { } // GetStreamInstant http.Get(URL) and returns the response -func (s Stream) GetStreamInstant() []byte { +func (s Stream) GetStreamInstant() gocv.Mat { resp, err := http.Get(s.URL) if err != nil { log.Fatal(err) } img, err := ioutil.ReadAll(resp.Body) defer resp.Body.Close() - return img + + mat, err := gocv.IMDecode(img, gocv.IMReadColor) + if err != nil { + log.Fatal("Could not IMDecode img") + } + + return mat } // SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath