iOS11 Vision.FrameWork 使用小结
开始
iOS苹果发布了iOS11系统的beta1版本,同时开源出来了Vision 框架,本文就学习下怎么使用vision框架。
首先你需要下载安装xcod9 beta版本,下载链接;然后还需要下载Core ML model,苹果现在提供了4个model,(Places205-GoogLeNet、ResNet50、Inception v3、VGG16)这四个model都是进行照片分类的,下面就以GoogLeNet为例来讲诉下使用方法。
首先
新建一个xcode工程,然后直接把下载好的GoogLeNetPlaces.mlmodel直接拖到工程文件夹中,
![](https://img.haomeiwen.com/i3508227/18690094591b15c8.png)
点击工程里面的GoogLeNetPlaces.mlmodel,你会看到xcode直接生成了代码文件
![](https://img.haomeiwen.com/i3508227/9baa36f4fcba0307.png)
点击箭头我们可以看到GoogLeNetPlaces文件,里面有一个输入、输出以及GoogLeNetPlaces类。
![](https://img.haomeiwen.com/i3508227/0ca0de2d624ef2f2.png)
如果拖动mlmodel文件到工程中出现了没有箭头的情况,可以在工程设置中Build phases中的compile sources中添加model文件,xcode就会生成代码。
![](https://img.haomeiwen.com/i3508227/8c83ebca2788d01d.png)
![](https://img.haomeiwen.com/i3508227/2120ad146f74f3b9.png)
使用
在代码中直接添加vision和model的头文件
#import <Vision/Vision.h>
#import "GoogLeNetPlaces.h"
就可以直接写代码了
MLModel*mlModel = [[GoogLeNetPlaces new] model];
VNCoreMLModel*coreModel = [VNCoreMLModel modelForMLModel:mlModel error:nil];
// init request照片分类
VNCoreMLRequest*request = [[VNCoreMLRequest alloc] initWithModel:coreModel completionHandler:_classificationHandler];
// 人脸信息
VNDetectFaceRectanglesRequest* faceRequest = [[VNDetectFaceRectanglesRequest alloc] initWithCompletionHandler:_faceLandMarkHandler];
VNImageRequestHandler*handler = [[VNImageRequestHandler alloc] initWithCGImage:image.CGImage options:@{}]
[handler performRequests:@[request, faceRequest] error:nil];
使用VNCoreMLRequest
第一步:创建mlModel,可以直接初始化GoogLeNetPlaces,然后访问其属性model,也可以使用URL生成一个model,使用接口modelWithContentsOfURL:error:可以通过URL创建一个model
第二步:创建VNCoreMLModel,通过MLModel生成一个VNCoreMLModel
第三步:创建VNCoreMLRequest,VNCoreMLRequest含有一个completionHandler的block,用来执行识别结果和错误处理。VNCoreMLRequest对于不同的model,返回不同VNObservation,GoogLeNetPlaces是一个分类的model,所以会返回一个VNClassificationObservation
第四步:创建VNImageRequestHandler,然后执行request
在classificationHandler我们可以看到
_classificationHandler= ^(VNRequest*_Nonnullrequest,NSError*_Nullableerror) {
__strongtypeof(weakSelf) strongSelf = weakSelf;
VNClassificationObservation*firstObservation = [request.results firstObject];
VNClassificationObservation*secondObservation = [request.results objectAtIndex:1];
VNClassificationObservation*thirdObservation = [request.results objectAtIndex:2];
});
我们可以看到,一张图片可以返回多个VNClassificationObservation,表示这张图片可以有多个结果。VNClassificationObservation中有两个属性我们需要关注,一个是identifier属性,表示识别出来的类别,另一个是confidence,表示当前类别的权重,就是有多大的概率是这个类别。request.results这个是根据confidence的值从大到小排列的。
在这个例子中,可以看到还使用了VNDetectFaceRectanglesRequest这个请求,这个请求是用来判断图片中的人脸位置的,会返回多个VNFaceObservation,VNFaceObservation中有一个boundingBox的属性,代表人脸的位置,这个是相对位置,值是0-1;还需要转化为图片的像素位置。
VNDetectFaceRectanglesRequest *faceRequest = (VNDetectFaceRectanglesRequest*)request;
VNFaceObservation*firstObservation = [faceRequest.results firstObject];
CGRect boundingBox = [firstObservation boundingBox];
CGImageRef imageRef = [strongSelf->_currentImage CGImage];
NSUInteger width =CGImageGetWidth(imageRef);
NSUInteger height =CGImageGetHeight(imageRef);
CGRect rect =VNImageRectForNormalizedRect(boundingBox,width,height);
这里我们使用了VNUtils中的VNImageRectForNormalizedRect接口,可以转换boundingBox为图片的像素位置。
分析
打开Vision框架,可以看到框架中最多的就是各种requset,每种request都有一种对应的VNObservation返回
![](https://img.haomeiwen.com/i3508227/98571e43bb5b0084.png)
从Vision的框架和例子中我们可以总结出使用流程
(1)创建不同的VNImageBasedRequest,实现不同的block处理逻辑
(2)生成VNRequestHandler,然后执行这些request,执行结果会通过实现的block回调
(3)使用VNUtil工具类辅助处理。