龍芯go之路(二)-安裝opencv-go

hjavn 發表於 2021-09-13
Go OpenCV

工具與資源中心

幫助開發者更加高效的工作,提供圍繞開發者全生命週期的工具與資源

developer.aliyun.com/tool?spm=a1z3...

龍芯派安裝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

    即可編譯成功

本文轉自:developer.aliyun.com/article/79028...

本作品採用《CC 協議》,轉載必須註明作者和本文連結