From 0d7faa4397fdd9e0ce757a1fea08a81a29dc76a0 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Sat, 11 Jul 2020 11:27:53 +0000 Subject: [PATCH] added debug endpoint that gets a debug.jpg --- main.go | 23 +++++++++++++++++++++++ stream.go | 18 +++++++++++++++++- templates/stream.html | 1 + 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 9f26a04..c5579fe 100755 --- a/main.go +++ b/main.go @@ -108,6 +108,28 @@ func (server Server) currentColor(w http.ResponseWriter, r *http.Request) { w.Write(jpg) } +func (server Server) debug(w http.ResponseWriter, r *http.Request) { + if r.FormValue("URL") == "" { + return + } + w.Header().Set("Content-Type", "image/jpeg") + URL := r.FormValue("URL") + + stream, exists := server.Streams[URL] + if !exists { + return + } + if !stream.DebugInstantPathExists() { + log.Println(stream.GetDebugInstantPath()) + return + } + jpg, err := gocv.IMEncode(".jpg", stream.IMReadDebug()) + if err != nil { + log.Fatal("Could not encode Mat to jpg:", URL) + } + w.Write(jpg) +} + func (server Server) current(w http.ResponseWriter, r *http.Request) { if r.FormValue("URL") == "" { return @@ -158,5 +180,6 @@ func main() { http.HandleFunc("/previous", server.previous) http.HandleFunc("/current_color", server.currentColor) http.HandleFunc("/current", server.current) + http.HandleFunc("/debug", server.debug) log.Fatal(http.ListenAndServe(":8080", nil)) } diff --git a/stream.go b/stream.go index 59327a9..aaab6d2 100755 --- a/stream.go +++ b/stream.go @@ -63,7 +63,8 @@ func (s Stream) Update() { diff := gocv.NewMat() previousGrey := s.IMReadPrevious() gocv.AbsDiff(previousGrey, currentGrey, &diff) - gocv.Threshold(diff, &diff, 10, 255, gocv.ThresholdBinary) + gocv.Threshold(diff, &diff, 50, 255, gocv.ThresholdBinary) + go gocv.IMWrite(s.GetDebugInstantPath(), diff) } // UpdateInterval calls Update() every interval @@ -202,3 +203,18 @@ func (s Stream) NextInstantPathExists() bool { func (s Stream) IMReadNext() gocv.Mat { return gocv.IMRead(s.GetNextInstantPath(), gocv.IMReadGrayScale) } + +// GetDebugInstantPath returns filepath.Join(s.GetStreamDirPath(), s.Base64, "debug.jpg") +func (s Stream) GetDebugInstantPath() string { + return filepath.Join(s.GetStreamStoreDirPath(), "debug.jpg") +} + +// DebugInstantPathExists returns FileExists(GetDebugInstantPath()) +func (s Stream) DebugInstantPathExists() bool { + return FileExists(s.GetDebugInstantPath()) +} + +// IMReadDebug returns gocv.IMRead(GetDebugInstantPath(), gocv.IMReadGrayScale) +func (s Stream) IMReadDebug() gocv.Mat { + return gocv.IMRead(s.GetDebugInstantPath(), gocv.IMReadGrayScale) +} diff --git a/templates/stream.html b/templates/stream.html index d34640d..b0f22b4 100755 --- a/templates/stream.html +++ b/templates/stream.html @@ -2,5 +2,6 @@ {{define "content"}} + {{end}} \ No newline at end of file