more cleanup
This commit is contained in:
parent
93f416403e
commit
79c7e03620
2 changed files with 22 additions and 19 deletions
22
main.go
22
main.go
|
@ -42,28 +42,30 @@ func stream(w http.ResponseWriter, r *http.Request) {
|
||||||
stream := NewStream(URL)
|
stream := NewStream(URL)
|
||||||
|
|
||||||
img := stream.GetStreamInstant(URL)
|
img := stream.GetStreamInstant(URL)
|
||||||
|
|
||||||
go stream.SaveStreamInstant(URL, img)
|
|
||||||
|
|
||||||
if stream.PreviousInstantPathExists() {
|
|
||||||
newMat := gocv.NewMat()
|
|
||||||
|
|
||||||
mat, err := gocv.IMDecode(img, gocv.IMReadGrayScale)
|
mat, err := gocv.IMDecode(img, gocv.IMReadGrayScale)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Could not IMDecode img")
|
log.Fatal("Could not IMDecode img")
|
||||||
}
|
}
|
||||||
|
|
||||||
gocv.GaussianBlur(mat, &newMat, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect)
|
//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()
|
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, newMat, &newMat)
|
gocv.AbsDiff(previous, mat, &mat)
|
||||||
|
|
||||||
gocv.Threshold(newMat, &newMat, 25, 255, gocv.ThresholdBinary)
|
gocv.Threshold(mat, &mat, 25, 255, gocv.ThresholdBinary)
|
||||||
|
|
||||||
img, err = gocv.IMEncode(".jpg", newMat)
|
//img, err = gocv.IMEncode(".jpg", newMat)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Write(img)
|
w.Write(img)
|
||||||
|
|
21
stream.go
21
stream.go
|
@ -40,22 +40,18 @@ func (s Stream) GetStreamInstant(URL string) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath
|
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath
|
||||||
func (s Stream) SaveStreamInstant(URL string, img []byte) {
|
func (s Stream) SaveStreamInstant(URL string, mat gocv.Mat) {
|
||||||
streamDir := URLToBase64(URL)
|
streamStoreDir := s.GetStreamStoreDirPath()
|
||||||
streamStoreDir := filepath.Join(".", "streams", string(streamDir))
|
|
||||||
os.MkdirAll(streamStoreDir, os.ModePerm)
|
os.MkdirAll(streamStoreDir, os.ModePerm)
|
||||||
|
|
||||||
currentStreamInstantPath := filepath.Join(streamStoreDir, "current.jpg")
|
currentStreamInstantPath := s.GetCurrentInstantPath()
|
||||||
swap := FileExists(streamStoreDir)
|
swap := FileExists(s.GetCurrentInstantPath())
|
||||||
|
|
||||||
if swap {
|
if swap {
|
||||||
currentStreamInstantPath = filepath.Join(streamStoreDir, "next.jpg")
|
currentStreamInstantPath = s.GetNextInstantPath()
|
||||||
}
|
}
|
||||||
|
|
||||||
err := ioutil.WriteFile(currentStreamInstantPath, img, os.ModePerm)
|
gocv.IMWrite(currentStreamInstantPath, mat)
|
||||||
if err != nil {
|
|
||||||
log.Fatal("Can't write latest stream instant.")
|
|
||||||
}
|
|
||||||
|
|
||||||
if swap {
|
if swap {
|
||||||
s.SwapInstants(s.GetPreviousInstantPath(), s.GetCurrentInstantPath(), s.GetNextInstantPath())
|
s.SwapInstants(s.GetPreviousInstantPath(), s.GetCurrentInstantPath(), s.GetNextInstantPath())
|
||||||
|
@ -84,6 +80,11 @@ func (s Stream) StreamStorePathExists() bool {
|
||||||
return FileExists(s.GetStreamDirPath())
|
return FileExists(s.GetStreamDirPath())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetStreamStoreDirPath returns filepath.Join(s.GetStreamDirPath(), s.base64)
|
||||||
|
func (s Stream) GetStreamStoreDirPath() string {
|
||||||
|
return filepath.Join(s.GetStreamDirPath(), s.base64)
|
||||||
|
}
|
||||||
|
|
||||||
// GetPreviousInstantPath returns filepath.Join(GetStreamDirPath(), s.base64, "previous.jpg")
|
// GetPreviousInstantPath returns filepath.Join(GetStreamDirPath(), s.base64, "previous.jpg")
|
||||||
func (s Stream) GetPreviousInstantPath() string {
|
func (s Stream) GetPreviousInstantPath() string {
|
||||||
return filepath.Join(s.GetStreamDirPath(), s.base64, "previous.jpg")
|
return filepath.Join(s.GetStreamDirPath(), s.base64, "previous.jpg")
|
||||||
|
|
Loading…
Add table
Reference in a new issue