Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gocv to webrtc #126

Open
entrehuihui opened this issue Aug 17, 2022 · 1 comment
Open

gocv to webrtc #126

entrehuihui opened this issue Aug 17, 2022 · 1 comment

Comments

@entrehuihui
Copy link

Is it possible to use gocv to read the video for detection and then forward it to the web through RTC for display

@learncodingforweb
Copy link

I tried example-webrtc-applications,

go func() {
		// Open a video capture device (e.g., webcam)
		videoCapture, err := gocv.OpenVideoCapture(0)
		if err != nil {
			log.Fatalf("Error opening video capture device: %v", err)
		}
		defer videoCapture.Close()

		// Create a Mat to hold the captured frame
		frame := gocv.NewMat()

		for {
			// Read the next frame from the video capture device
			if ok := videoCapture.Read(&frame); !ok {
				log.Fatalf("Error reading frame from video capture device")
			}

			// Send the frame to the frameChannel
			frameChannel <- frame
		}
	}()

	// Start a new Goroutine to process frames from the frameChannel and send them to the video track
	go func() {
		for frame := range frameChannel {
			// Convert the frame to RGBA
			rgba := gocv.NewMat()
			gocv.CvtColor(frame, &rgba, gocv.ColorBGRToRGBA)

			// Convert the RGBA image to a byte slice
			frameBytes := rgba.ToBytes()
			if err != nil {
				log.Printf("Error converting frame to bytes: %v", err)
				continue
			}

			// Create a webrtc.Sample from the frame bytes
			sample := webrtc.Sample{
				Data:     frameBytes,
				Duration: time.Second / 30, // Assuming a frame rate of 30 fps
			}

			// Write the sample to the video track
			if err := videoTrack.WriteSample(sample); err != nil {
				log.Printf("Error writing video frame: %v", err)
			}

			// Close the Mats to release resources
			frame.Close()
			rgba.Close()
		}
	}()

but i am facing undefined: webrtc.Sample while using following go.mod

module webrtc_server

go 1.20

require (
	github.com/gorilla/websocket v1.5.0
	github.com/pion/mediadevices v0.4.0
	github.com/pion/webrtc/v3 v3.2.8
	github.com/redis/go-redis/v9 v9.0.4
	gocv.io/x/gocv v0.32.1
)

require (
	github.com/blackjack/webcam v0.0.0-20220329180758-ba064708e165 // indirect
	github.com/cespare/xxhash/v2 v2.2.0 // indirect
	github.com/davecgh/go-spew v1.1.1 // indirect
	github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
	github.com/gen2brain/malgo v0.11.10 // indirect
	github.com/google/uuid v1.3.0 // indirect
	github.com/pion/datachannel v1.5.5 // indirect
	github.com/pion/dtls/v2 v2.2.7 // indirect
	github.com/pion/ice/v2 v2.3.6 // indirect
	github.com/pion/interceptor v0.1.17 // indirect
	github.com/pion/logging v0.2.2 // indirect
	github.com/pion/mdns v0.0.7 // indirect
	github.com/pion/randutil v0.1.0 // indirect
	github.com/pion/rtcp v1.2.10 // indirect
	github.com/pion/rtp v1.7.13 // indirect
	github.com/pion/sctp v1.8.7 // indirect
	github.com/pion/sdp/v3 v3.0.6 // indirect
	github.com/pion/srtp/v2 v2.0.15 // indirect
	github.com/pion/stun v0.6.0 // indirect
	github.com/pion/transport/v2 v2.2.1 // indirect
	github.com/pion/turn/v2 v2.1.0 // indirect
	github.com/pmezard/go-difflib v1.0.0 // indirect
	github.com/stretchr/testify v1.8.3 // indirect
	golang.org/x/crypto v0.9.0 // indirect
	golang.org/x/image v0.2.0 // indirect
	golang.org/x/net v0.10.0 // indirect
	golang.org/x/sys v0.8.0 // indirect
	gopkg.in/yaml.v3 v3.0.1 // indirect
)

Are you able to resolve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants