#1、gocv简介
gocv是golang中对opencv库封装。
#2、gocv安装
##2、1下载
go get -u -d gocv.io/x/gocv
##2、2编译
略
#3、demo
package main
import (
"fmt"
"log"
"net/http"
_ "net/http/pprof"
//"github.com/gwuhaolin/livego"
"github.com/hybridgroup/mjpeg"
"gocv.io/x/gocv"
)
var (
deviceID int
err error
webcam *gocv.VideoCapture
stream *mjpeg.Stream
)
func main() {
// parse args
deviceID := 0
host := ":8554"
// open webcam
webcam, err = gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Printf("Error opening capture device: %v\n", deviceID)
return
}
defer webcam.Close()
// create the mjpeg stream
stream = mjpeg.NewStream()
// start capturing
go mjpegCapture()
fmt.Println("Capturing. Point your browser to " + host)
// start http server
http.Handle("/", stream)
log.Fatal(http.ListenAndServe(host, nil))
}
func mjpegCapture() {
img := gocv.NewMat()
defer img.Close()
for {
if ok := webcam.Read(&img); !ok {
fmt.Printf("Device closed: %v\n", deviceID)
return
}
if img.Empty() {
continue
}
buf, _ := gocv.IMEncode(".jpg", img)
stream.UpdateJPEG(buf)
}
}
然后在网页中输入127.0.0.1:8554/
即可访问摄像头