added http endpoints for previous/current/currentColor from disk

This commit is contained in:
BroodjeAap 2020-07-09 19:57:30 +00:00
parent 79273b2c0d
commit 0583189645
3 changed files with 88 additions and 3 deletions

66
main.go
View file

@ -84,6 +84,69 @@ func (server Server) stream(w http.ResponseWriter, r *http.Request) {
w.Write(jpg)
}
func (server Server) previous(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.PreviousInstantPathExists() {
return
}
jpg, err := gocv.IMEncode(".jpg", stream.IMReadPrevious())
if err != nil {
log.Fatal("Could not encode Mat to jpg:", URL)
}
w.Write(jpg)
}
func (server Server) currentColor(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.CurrentColorInstantPathExists() {
return
}
jpg, err := gocv.IMEncode(".jpg", stream.IMReadCurrentColor())
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
}
w.Header().Set("Content-Type", "image/jpeg")
URL := r.FormValue("URL")
stream, exists := server.Streams[URL]
if !exists {
return
}
if !stream.CurrentInstantPathExists() {
return
}
jpg, err := gocv.IMEncode(".jpg", stream.IMReadCurrent())
if err != nil {
log.Fatal("Could not encode Mat to jpg:", URL)
}
w.Write(jpg)
}
func main() {
staticFileServer := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", staticFileServer))
@ -110,5 +173,8 @@ func main() {
http.HandleFunc("/", server.index)
http.HandleFunc("/stream", server.stream)
http.HandleFunc("/previous", server.previous)
http.HandleFunc("/current_color", server.currentColor)
http.HandleFunc("/current", server.current)
log.Fatal(http.ListenAndServe(":8080", nil))
}

View file

@ -45,14 +45,16 @@ func (s Stream) Update() {
img, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
mat, err := gocv.IMDecode(img, gocv.IMReadGrayScale)
mat, err := gocv.IMDecode(img, gocv.IMReadColor)
if err != nil {
log.Fatal("Could not IMDecode img")
}
go gocv.IMWrite(s.GetCurrentColorInstantPath(), mat)
grey := gocv.NewMat()
gocv.CvtColor(mat, &grey, gocv.ColorBGRAToBGR)
gocv.GaussianBlur(mat, &mat, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect)
go s.SaveStreamInstant(mat)
go s.SaveStreamInstant(grey)
}
// UpdateInterval calls Update() every interval
@ -162,6 +164,21 @@ func (s Stream) IMReadCurrent() gocv.Mat {
return gocv.IMRead(s.GetCurrentInstantPath(), gocv.IMReadGrayScale)
}
// GetCurrentColorInstantPath returns filepath.Join(s.GetStreamDirPath(), s.Base64, "current_color.jpg")
func (s Stream) GetCurrentColorInstantPath() string {
return filepath.Join(s.GetStreamStoreDirPath(), "current_color.jpg")
}
// CurrentColorInstantPathExists returns FileExists(GetCurrentColorInstantPath())
func (s Stream) CurrentColorInstantPathExists() bool {
return FileExists(s.GetCurrentColorInstantPath())
}
// IMReadCurrentColor returns gocv.IMRead(GetCurrentColorInstantPath(), gocv.IMReadColor)
func (s Stream) IMReadCurrentColor() gocv.Mat {
return gocv.IMRead(s.GetCurrentColorInstantPath(), gocv.IMReadColor)
}
// GetNextInstantPath returns filepath.Join(s.GetStreamDirPath(), s.Base64, "last.jpg")
func (s Stream) GetNextInstantPath() string {
return filepath.Join(s.GetStreamStoreDirPath(), "last.jpg")

View file

@ -1,4 +1,6 @@
{{template "base" .}}
{{define "content"}}
<img src="/stream?URL={{.URL}}">
<img src="/previous?URL={{.URL}}">
<img src="/current?URL={{.URL}}">
{{end}}