gocv
gocv copied to clipboard
How can I clear my memory? It's growing up!
How can I clear my memory? It's growing up!There is another thing,sometime I can't use camera 0,just can’t,I don't know why?。
func main() { initFyne(500, 500) } func initFyne(w, h int) { W.Resize(fyne.NewSize((w), (h))) A.Settings().SetTheme(theme.LightTheme()) cap, err := gocv.VideoCaptureDevice(1) if err != nil { panic(err) } defer cap.Close() mat := gocv.NewMat() defer mat.Close() go func() { for range time.Tick(time.Second) { cap.Read(&mat) img, err := mat.ToImage() if err != nil { panic(err) } //do something for this img image := canvas.NewImageFromImage(img) image.FillMode = canvas.ImageFillOriginal W.SetContent(container.NewVBox( widget.NewLabel("hahahahahahahahahaha"), image, )) } }() W.ShowAndRun() }
Your Environment
- Operating System and version:win11
- OpenCV version used:4.5.5
- How did you install OpenCV? I forgot, maybe I installed gocv after installing opencv separately
- GoCV version used:0.30.0
- Go version:1.18
- Did you run the
env.sh
orenv.cmd
script before trying togo run
orgo build
? not at all
Hi,
for question : "sometime I can't use camera 0,just can’t..." Sometimes when you plug camera in the USB port. It is assigned as some ID which is used by opencv to access it. When your code crashes or something weird happen or when you remove USB cam to plug it back, the camera ID is not reinstated to its previous value, so it gets assigned to another one.
Q2. Regarding memory build up :
Line : "img, err := mat.ToImage()" For every iteration it's creating new memory buffer in CPU. Solution to it is close those Mat manually. Avoid defer also. Just close it manually