龙芯派安装go-opencv
- 其实龙芯派2代已经集成了opencv2 但其实没有安装完全,我们无法正常使用,所以在使用前我们要将其安装完整
yum install numpy opencv* #在龙芯派联网的情况下执行这条命令即可,这样opencv2 就可以安装完成了,就可以正常使用opencv2了
- 要对go的一些环境变量进行修改
export CGO_CFLAGS="-I/usr/include/opencv"
export CGO_LDFLAGS="-L/usr/lib64 -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videosta -lpthread"
-
如果还是不行按照接下来的配置来配置go的环境变量
CGO_ENABLED=1\ GOOS=linux\ GOARCH=mips64le\ CC=mips-linux-gnu-gcc\ CXX=mips-linux-gnu-g++\ CGO_CFLAGS="-I/usr/include/opencv"\ CGO_LDFLAGS="-L/usr/lib64 -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videosta"\ CGO_LDFLAGS="-static"\
在这里提一句我们使用的go的opencv库是go-opencv而不是gocv我们这个库使用的是opencv2
库地址 :https://github.com/go-opencv/go-opencv
-
在这里我给出一个使用opencv2在龙芯派上使用go视频推流的小例子
package main import "C" import ( "fmt" "github.com/go-opencv/go-opencv/opencv" "log" "net/http" _ "net/http/pprof" "unsafe" //"github.com/gwuhaolin/livego" "github.com/hybridgroup/mjpeg" ) var ( deviceID int err error stream *mjpeg.Stream Cap *opencv.Capture ) func main() { // parse args //deviceID := 0 host := ":8554" // open webcam Cap = opencv.NewCameraCapture(0) //Cap = opencv.NewFileCapture("/home/zjh/test.flv") fmt.Println("1") if Cap == nil { panic("can not open camera") return } defer Cap.Release() // 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() { for { img := Cap.RetrieveFrame(1) if img != nil { buf := opencv.EncodeImage(".jpg", unsafe.Pointer(img.GetMat()), []int{64}) bufss := buf.GetData() fmt.Println("9") stream.UpdateJPEG(bufss) fmt.Println("10") } else if img == nil { fmt.Println("Image is nil") break } } }
- 要想跑起来这个程序还是不行,因为swig库支持的版本太低了,我们要去swig的官网,下载源码后在龙芯派上进行交叉编译
-
然后执行
export CGO_LDFLAGS="-g -O2 -lm" go run main.go
即可编译成功