streamwatcher/main.go
2020-07-07 19:04:12 +00:00

93 lines
2 KiB
Go
Executable file

package main
import (
"image"
"log"
"net/http"
"path/filepath"
"text/template"
"time"
"gocv.io/x/gocv"
)
func index(w http.ResponseWriter, r *http.Request) {
if r.FormValue("URL") == "" {
indexTemplate, err := template.ParseFiles(filepath.Join("templates", "index.html"))
if err != nil {
log.Fatal(err)
}
indexTemplate.Execute(w, nil)
return
}
streamURL := Stream{
URL: r.FormValue("URL"),
}
streamTemplate, err := template.ParseFiles(filepath.Join("templates", "stream.html"))
if err != nil {
log.Fatal(err)
}
streamTemplate.Execute(w, streamURL)
}
func stream(w http.ResponseWriter, r *http.Request) {
if r.FormValue("URL") == "" {
return
}
w.Header().Set("Content-Type", "image/jpeg")
URL := r.FormValue("URL")
stream := NewStream(URL)
img := stream.GetStreamInstant(URL)
mat, err := gocv.IMDecode(img, gocv.IMReadGrayScale)
if err != nil {
log.Fatal("Could not IMDecode 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() {
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.Threshold(mat, &mat, 25, 255, gocv.ThresholdBinary)
//img, err = gocv.IMEncode(".jpg", newMat)
}
w.Write(img)
}
func listenAndServe() {
staticFileServer := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", staticFileServer))
http.HandleFunc("/", index)
http.HandleFunc("/stream", stream)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func backend() {
for {
time.Sleep(time.Second)
}
}
func main() {
go listenAndServe()
backend()
}