pion / example-webrtc-applications

Examples of WebRTC applications that are large, or use 3rd party libraries
https://pion.ly/
MIT License
1.02k stars 241 forks source link

gocv to webrtc #126

Open entrehuihui opened 2 years ago

entrehuihui commented 2 years ago

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

learncodingforweb commented 1 year ago

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.