GetStreamInstant returns a Mat now

This commit is contained in:
BroodjeAap 2020-07-09 18:42:58 +00:00
parent eb8b0d4d61
commit 4a196a21b2
2 changed files with 21 additions and 19 deletions

30
main.go
View file

@ -54,34 +54,30 @@ func (server Server) stream(w http.ResponseWriter, r *http.Request) {
return return
} }
img := stream.GetStreamInstant() mat := stream.GetStreamInstant()
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) diff := gocv.NewMat()
gocv.GaussianBlur(mat, &mat, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect) 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) go stream.SaveStreamInstant(URL, mat)
if stream.PreviousInstantPathExists() { if stream.PreviousInstantPathExists() {
if err != nil {
log.Fatal("Could not IMDecode img")
}
previous := stream.IMReadPrevious() previous := stream.IMReadPrevious()
gocv.GaussianBlur(previous, &previous, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect) 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) gocv.Threshold(diff, &diff, 25, 255, gocv.ThresholdBinary)
//img, err = gocv.IMEncode(".jpg", newMat)
} }
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() { func main() {
@ -105,7 +101,7 @@ func main() {
stream := Stream{} stream := Stream{}
json.Unmarshal([]byte(streamJSONFile), &stream) json.Unmarshal([]byte(streamJSONFile), &stream)
server.Streams[stream.URL] = stream server.Streams[stream.URL] = stream
go stream.Update() go stream.UpdateInterval()
} }
http.HandleFunc("/", server.index) http.HandleFunc("/", server.index)

View file

@ -46,14 +46,20 @@ func (s Stream) UpdateInterval() {
} }
// GetStreamInstant http.Get(URL) and returns the response // 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) resp, err := http.Get(s.URL)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
img, err := ioutil.ReadAll(resp.Body) img, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close() 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 // SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath