gocv icon indicating copy to clipboard operation
gocv copied to clipboard

Low framerate at higher resolution

Open syllith opened this issue 9 months ago • 0 comments

My webcam is capable of doing 30fps at 1080p. I get decent frame rate when at lower resolutions, but it tanks quickly as you increase it. At 1080p, I'm only getting around 2-3 FPS. I've used other mjpeg streamers that do 30 FPS, so I'm not sure what's going on.

  • Operating System and version: Windows 11
  • OpenCV version used: 4.8
  • How did you install OpenCV? Through the provided script for gocv
  • GoCV version used: 0.34.0
  • Go version: 1.21
package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/hybridgroup/mjpeg"
	"gocv.io/x/gocv"
)

var (
	webcam *gocv.VideoCapture
	stream *mjpeg.Stream
)

func main() {
	if len(os.Args) < 3 {
		fmt.Println("Usage:\n\tmjpeg-streamer [camera ID] [host:port]")
		return
	}

	deviceID := os.Args[1]
	host := os.Args[2]

	webcam, _ = gocv.OpenVideoCapture(deviceID)
	defer webcam.Close()

	webcam.Set(gocv.VideoCaptureFrameWidth, 1920)
	webcam.Set(gocv.VideoCaptureFrameHeight, 1080)

	stream = mjpeg.NewStream()
	go mjpegCapture()

	fmt.Println("Streaming at " + host)
	http.Handle("/", stream)
	log.Fatal(http.ListenAndServe(host, nil))
}

func mjpegCapture() {
	img := gocv.NewMat()
	defer img.Close()

	params := []int{gocv.IMWriteJpegQuality, 50}
	for {
		if ok := webcam.Read(&img); !ok || img.Empty() {
			continue
		}

		buf, _ := gocv.IMEncodeWithParams(".jpg", img, params)
		stream.UpdateJPEG(buf.GetBytes())
		buf.Close()
	}
}

syllith avatar Sep 08 '23 00:09 syllith