美文网首页
ubuntu安装gocv

ubuntu安装gocv

作者: 夜空最亮的9星 | 来源:发表于2024-01-18 13:49 被阅读0次

安装gocv

cd $HOME/folder/with/your/src/

git clone https://github.com/hybridgroup/gocv.git

cd gocv
sudo make install

由于国内访问git比较慢,所有,可以打开Makefile把里面的74行,和76行

    OPENCV_VERSION?=4.8.1
    curl -Lo opencv.zip https://github.com/opencv/opencv/archive/refs/tags/$(OPENCV_VERSION).zip
    curl -Lo opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/$(OPENCV_VERSION).zip

需要下载的文件提前放在本地

mkdir /tmp/opencv

mv opencv-4.8.1.zip opencv.zip

mv opencv_contrib-4.8.1.zip opencv_contrib.zip


验证

在 gocv-release 目录下执行:

go run ./cmd/version/main.go

root@mini-001:/app/gocv-release$ go run ./cmd/version/main.go
gocv version: 0.35.0
opencv lib version: 4.8.1

报错1

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.8.1) /tmp/opencv/opencv-4.8.1/modules/highgui/src/window_gtk.cpp:638: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'

解决:

sudo apt install libgtk2.0*

测试

pkg-config --cflags --libs gtk+-2.0

root@mini-001:/app/gocv-release$ pkg-config --cflags --libs gtk+-2.0
-pthread -I/usr/include/gtk-2.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/x86_64-linux-gnu -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/fribidi -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/uuid -I/usr/include/freetype2 -I/usr/include/libpng16 -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0 -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lharfbuzz -lfontconfig -lfreetype
root@mini-001:/app/gocv-release$ 

参考:https://zhuanlan.zhihu.com/p/672827901

报错2

OpenVideoCapture
Failed to load module "canberra-gtk-module"

这个错误表明无法加载 "canberra-gtk-module" 模块。"canberra-gtk-module" 是 GTK 库的一个模块,用于提供音频支持。

出现这个错误可能有几个原因:

缺少 "canberra-gtk-module":你的系统上可能没有安装 "canberra-gtk-module" 或者安装的版本不兼容。请尝试安装该模块并确保安装的版本与你的 GTK 版本兼容。
在 Ubuntu 上,你可以使用以下命令安装 "canberra-gtk-module":

sudo apt-get install libcanberra-gtk-module

    
package main

import (
    "gocv.io/x/gocv"
)

func main() {

    // 打开视频文件(若是网络摄像头支持RTSP推流,可以使用gocv.OpenVideoCapture("rtsp://<username>:<pwd>@host:port"))
    rtsp_url := "rtsp://admin:Ab123456@123@192.168.0.64:554/Streaming/Channels/102?transportmode=unicast"

    // webcam, err = gocv.VideoCaptureFile("test.mp4")
    
    webcam, _ := gocv.OpenVideoCapture(rtsp_url)
    window := gocv.NewWindow("Hello")
    img := gocv.NewMat()

    for {
        webcam.Read(&img)
        window.IMShow(img)
        window.WaitKey(1)
    }
}

相关文章

网友评论

      本文标题:ubuntu安装gocv

      本文链接:https://www.haomeiwen.com/subject/zpzrodtx.html