core icon indicating copy to clipboard operation
core copied to clipboard

Which widget should be used?

Open zsy-cn opened this issue 1 month ago • 2 comments

Describe the feature

I want to use Gocv to capture video from a USB camera. Which widget should I use to display this?

  • Gocv https://github.com/hybridgroup/gocv

Relevant code


zsy-cn avatar Oct 31 '25 10:10 zsy-cn

Thanks for asking about this. You could try to use the video widget, or you could just use gocv's builtin interface. I would have to take a closer look to see which of those approaches would work the best.

kkoreilly avatar Oct 31 '25 19:10 kkoreilly

Thank you for answering my question. I am try to use the video widget,but failed. so i am using the image widget. However, there are two problems: the window cannot automatically adjust to the camera's resolution, and it cannot fill the entire image widget and cpu usage is very high.

package main

import (
	"fmt"

	"cogentcore.org/core/base/errors"
	"cogentcore.org/core/core"
	"cogentcore.org/core/styles"
	"gocv.io/x/gocv"
)

func main() {
	var webcam *gocv.VideoCapture
	var err error
	b := core.NewBody()
	img := core.NewImage(b)
	img.Styler(func(s *styles.Style) {
		s.Grow.Set(1, 1)
	})
	webcam, err = gocv.VideoCaptureDevice(0)
	if err != nil {
		fmt.Printf("err: %#v", err)
	}
	fmt.Printf("gocv version: %s\n", gocv.Version())
	fmt.Printf("opencv lib version: %s\n", gocv.OpenCVVersion())
	imgcamer := gocv.NewMat()
	go func() {
		for {
			webcam.Read(&imgcamer)
			// rotated_180 = gocv.rotate(img, cv2.ROTATE_180)
			success := gocv.IMWrite("temp.png", imgcamer)
			if !success {
				fmt.Printf("gocv.IMWrite error")
			}
			errors.Log(img.Open("temp.png"))
			img.Update()
		}
	}()
	b.RunMainWindow()
}

zsy-cn avatar Nov 01 '25 04:11 zsy-cn