got basic motion detection working
This commit is contained in:
parent
9b93c889eb
commit
7a8a7dc631
1 changed files with 31 additions and 5 deletions
36
main.go
36
main.go
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"image"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -55,14 +56,39 @@ func stream(w http.ResponseWriter, r *http.Request) {
|
|||
// TODO async for this?
|
||||
saveStreamInstant(URL, img)
|
||||
|
||||
mat, err := gocv.IMDecode(img, gocv.IMReadColor)
|
||||
newMat := gocv.NewMat()
|
||||
h := sha256.New()
|
||||
h.Write([]byte(URL))
|
||||
streamDir := base64.URLEncoding.EncodeToString(h.Sum(nil))
|
||||
streamStoreDir := filepath.Join(".", "streams", string(streamDir))
|
||||
previousStreamInstantPath := filepath.Join(streamStoreDir, "previous.jpg")
|
||||
|
||||
gocv.ConvertScaleAbs(mat, &newMat, 2.0, 60)
|
||||
_, err = os.Stat(previousStreamInstantPath)
|
||||
previousExists := !os.IsNotExist(err)
|
||||
|
||||
gocvIMG, err := gocv.IMEncode(".jpg", newMat)
|
||||
if previousExists {
|
||||
newMat := gocv.NewMat()
|
||||
|
||||
w.Write(gocvIMG)
|
||||
mat, err := gocv.IMDecode(img, gocv.IMReadColor)
|
||||
if err != nil {
|
||||
log.Fatal("Could not IMDecode img")
|
||||
}
|
||||
|
||||
gocv.CvtColor(mat, &newMat, gocv.ColorBGRToGray)
|
||||
gocv.GaussianBlur(newMat, &newMat, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect)
|
||||
|
||||
previous := gocv.IMRead(previousStreamInstantPath, gocv.IMReadColor)
|
||||
|
||||
gocv.CvtColor(previous, &previous, gocv.ColorBGRToGray)
|
||||
gocv.GaussianBlur(previous, &previous, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect)
|
||||
|
||||
gocv.AbsDiff(previous, newMat, &newMat)
|
||||
|
||||
gocv.Threshold(newMat, &newMat, 25, 255, gocv.ThresholdBinary)
|
||||
|
||||
img, err = gocv.IMEncode(".jpg", newMat)
|
||||
}
|
||||
|
||||
w.Write(img)
|
||||
}
|
||||
|
||||
func saveStreamInstant(URL string, img []byte) {
|
||||
|
|
Loading…
Add table
Reference in a new issue