先说:最近准备做基于openCV的移动端人脸对比和识别,所以记录一下在过程中遇到的问题和解决办法,还有一些感想,文章随时更新,有问题希望大家可以多沟通交流
一写markdown上的不兼容,看着不习惯可以去 原文链接
1 选择&安装配置
1.1 首先Go的OpenVC两个版本的库
1.2 安装OpenCV(OpenCV3+)
依赖很多,安装时间很长,请准备好瓜子和板凳,直待GoCV安装完成
brew install hybridgroup/tools/opencv
运行以下命令获取GoCV库
go get -u -d gocv.io/x/gocv
切换到gocv文件夹下
cd $GOPATH/src/gocv.io/x/gocv
运行go run XX.go 选其中一个demo文件运行即可
1.2.1 But!! 你可能会遇到一个报错
go run: cannot run *_test.go files (video_test.go)
或
pkg-config --cflags -- opencv4
Package opencv4 was not found in the pkg-config search path. Perhaps you should add the directory containing `opencv4.pc' to the PKG_CONFIG_PATH environment variable No package 'opencv4' found pkg-config: exit status 1
你可以通过brew install pkgconfig
解决
1.2.2 你还可能遇到Library not loaded
dyld: Library not loaded:glog啊或者gflags啊之类的
总之缺什么库啊引用缺失啊 ,你就导入什么就好了brew install glog
1.2.3 运行成功
运行
cd $GOPATH/src/gocv.io/x/gocv
go run ./cmd/version/main.go
成功->输出
gocv version: 0.18.0
opencv lib version: 4.0.0
2 运行Demo
2.1 面部检测Demo 直接用goCV的Demo就好了,搞起来
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="go" contenteditable="false" cid="n166" mdtype="fences" style="font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; padding: 10px 30px; border: 1px solid; width: inherit; caret-color: rgb(184, 191, 198); color: rgb(184, 191, 198); position: relative !important;">import(
"fmt"
"image/color"
"gocv.io/x/gocv"
)
funcmain() {
// set to use a video capture device 0
deviceID:=0
// open webcam
webcam, err:=gocv.OpenVideoCapture(deviceID)
iferr!=nil{
fmt.Println(err)
return
}
deferwebcam.Close()
// open display window
window:=gocv.NewWindow("Face Detect")
deferwindow.Close()
// prepare image matrix
img:=gocv.NewMat()
deferimg.Close()
// color for the rect when faces detected
blue:=color.RGBA{0, 0, 255, 0}
// load classifier to recognize faces
classifier:=gocv.NewCascadeClassifier()
deferclassifier.Close()
if!classifier.Load("data/haarcascade_frontalface_default.xml") {
fmt.Println("Error reading cascade file: data/haarcascade_frontalface_default.xml")
return
}
fmt.Printf("start reading camera device: %v\n", deviceID)
for{
ifok:=webcam.Read(&img); !ok{
fmt.Printf("cannot read device %v\n", deviceID)
return
}
ifimg.Empty() {
continue
}
// detect faces
rects:=classifier.DetectMultiScale(img)
fmt.Printf("found %d faces\n", len(rects))
// draw a rectangle around each face on the original image
for_, r:=rangerects{
gocv.Rectangle(&img, r, blue, 3)
}
// show the image in the window, and wait 1 millisecond
window.IMShow(img)
window.WaitKey(1)
}
}</pre>
2.1.1 But!!!! 你可能会遇到报错
Error reading cascade file: data/haarcascade_frontalface_default.xml
路径不对,问题代码行数if !classifier.Load("data/haarcascade_frontalface_default.xml") {
替换load路径为你的opencv的路径$GOPATH/src/gocv.io/x/gocv/data/haarcascade_frontalface_default.xml
3.打包go文件为静态.a包
安装gomobilego get golang.org/x/mobile/cmd/gomobile
需要翻墙
然后安装gomobile init
绑定go get -d golang.org/x/mobile/example/bind/...
最后生成gomobile的生成.a的测试包helpDemo,以iOS为例子
cd $GOPATH/src/golang.org/x/mobile/example/bind
gomobile bind -target=ios golang.org/x/mobile/example/bind/hello
因为gomobile在生成.framework时,调用了CommandLineTools,所以你需要在MacOS上安装CommandLineTools,或者Xcode
But!!!!!即使你安装了CommandLineTools或者Xcode也还是会有可能会报出gomobile: -target=ios requires XCode
的错误,我理解[图片上传失败...(image-81b24c-1544679956585)]
是因为gomobile实现的xcrun找不到你的CommandLineTools,路径错误,解决:
sudo xcode-select -r
xcode-select --help
Options:
-h, --help print this help message and exit
-p, --print-path print the path of the active developer directory
-s <path>, --switch <path> set the path for the active developer directory
--install open a dialog for installation of the command line developer tools
-v, --version print the xcode-select version
-r, --reset reset to the default command line tools path
有疑问加站长微信联系(非本文作者)