Yue

Yue.Piao


  • Home

  • Archives

Untitled

Posted on 2018-12-12

先说:最近准备做基于openCV的移动端人脸对比和识别,所以记录一下在过程中遇到的问题和解决办法,还有一些感想,文章随时更新,有问题希望大家可以多沟通交流

一写markdown上的不兼容,看着不习惯可以去 原文链接

1 选择&安装配置

1.1 首先Go的OpenVC两个版本的库

OpenCV2+

OpenCV3+

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就好了,搞起来
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import (
"fmt"
"image/color"

"gocv.io/x/gocv"
)

func main() {
// set to use a video capture device 0
deviceID := 0

// open webcam
webcam, err := gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Println(err)
return
}
defer webcam.Close()

// open display window
window := gocv.NewWindow("Face Detect")
defer window.Close()

// prepare image matrix
img := gocv.NewMat()
defer img.Close()

// color for the rect when faces detected
blue := color.RGBA{0, 0, 255, 0}

// load classifier to recognize faces
classifier := gocv.NewCascadeClassifier()
defer classifier.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 {
if ok := webcam.Read(&img); !ok {
fmt.Printf("cannot read device %v\n", deviceID)
return
}
if img.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 := range rects {
gocv.Rectangle(&img, r, blue, 3)
}

// show the image in the window, and wait 1 millisecond
window.IMShow(img)
window.WaitKey(1)
}
}
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-20181213133617633

是因为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 , –switch 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

Yue

1 posts
© 2018 Yue
Powered by Hexo
|
Theme — NexT.Mist v5.1.4