From 79c7e03620ebec6f37e100add14409d681301b00 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Tue, 7 Jul 2020 19:04:12 +0000 Subject: [PATCH] more cleanup --- main.go | 20 +++++++++++--------- stream.go | 21 +++++++++++---------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index 787529f..cdd6873 100755 --- a/main.go +++ b/main.go @@ -42,28 +42,30 @@ func stream(w http.ResponseWriter, r *http.Request) { stream := NewStream(URL) img := stream.GetStreamInstant(URL) + mat, err := gocv.IMDecode(img, gocv.IMReadGrayScale) + if err != nil { + log.Fatal("Could not IMDecode img") + } - go stream.SaveStreamInstant(URL, img) + //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) + + go stream.SaveStreamInstant(URL, mat) if stream.PreviousInstantPathExists() { - newMat := gocv.NewMat() - - mat, err := gocv.IMDecode(img, gocv.IMReadGrayScale) if err != nil { log.Fatal("Could not IMDecode img") } - gocv.GaussianBlur(mat, &newMat, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect) - previous := stream.IMReadPrevious() gocv.GaussianBlur(previous, &previous, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect) - gocv.AbsDiff(previous, newMat, &newMat) + gocv.AbsDiff(previous, mat, &mat) - gocv.Threshold(newMat, &newMat, 25, 255, gocv.ThresholdBinary) + gocv.Threshold(mat, &mat, 25, 255, gocv.ThresholdBinary) - img, err = gocv.IMEncode(".jpg", newMat) + //img, err = gocv.IMEncode(".jpg", newMat) } w.Write(img) diff --git a/stream.go b/stream.go index 588d679..148281a 100755 --- a/stream.go +++ b/stream.go @@ -40,22 +40,18 @@ func (s Stream) GetStreamInstant(URL string) []byte { } // SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath -func (s Stream) SaveStreamInstant(URL string, img []byte) { - streamDir := URLToBase64(URL) - streamStoreDir := filepath.Join(".", "streams", string(streamDir)) +func (s Stream) SaveStreamInstant(URL string, mat gocv.Mat) { + streamStoreDir := s.GetStreamStoreDirPath() os.MkdirAll(streamStoreDir, os.ModePerm) - currentStreamInstantPath := filepath.Join(streamStoreDir, "current.jpg") - swap := FileExists(streamStoreDir) + currentStreamInstantPath := s.GetCurrentInstantPath() + swap := FileExists(s.GetCurrentInstantPath()) if swap { - currentStreamInstantPath = filepath.Join(streamStoreDir, "next.jpg") + currentStreamInstantPath = s.GetNextInstantPath() } - err := ioutil.WriteFile(currentStreamInstantPath, img, os.ModePerm) - if err != nil { - log.Fatal("Can't write latest stream instant.") - } + gocv.IMWrite(currentStreamInstantPath, mat) if swap { s.SwapInstants(s.GetPreviousInstantPath(), s.GetCurrentInstantPath(), s.GetNextInstantPath()) @@ -84,6 +80,11 @@ func (s Stream) StreamStorePathExists() bool { return FileExists(s.GetStreamDirPath()) } +// GetStreamStoreDirPath returns filepath.Join(s.GetStreamDirPath(), s.base64) +func (s Stream) GetStreamStoreDirPath() string { + return filepath.Join(s.GetStreamDirPath(), s.base64) +} + // GetPreviousInstantPath returns filepath.Join(GetStreamDirPath(), s.base64, "previous.jpg") func (s Stream) GetPreviousInstantPath() string { return filepath.Join(s.GetStreamDirPath(), s.base64, "previous.jpg")