美文网首页
Apple Vision Library Introductio

Apple Vision Library Introductio

作者: 四月白绵羊 | 来源:发表于2017-10-20 01:29 被阅读0次

苹果17年发布会上面提出了Core ML,Vision 和 NLP 几个新库。

Vison 的功能有:

  1. Face Detection
  2. Rectangle Detection
  3. Barcode Detection
  4. Text Detection
  5. Object Tracking

下面一一介绍一下:

  1. Face Detection
    Vision库提供了人脸识别功能,除了能够基本地识别出人脸的方框位置之外,还能识别出脸部特征,也就是Face Landmark。
  • 第一种 识别脸部方框:
// Init request and handler
let request = VNDetectFaceRectanglesRequest.init()

let handler = VNImageRequestHandler.init(cgImage: cgImage, options: [:])
do{
    try handler.perform([request]) // 执行请求
}catch{
    print(error.localizedDescription)
}
// 执行的结果会和request一起返回。
let results = request.results as! [VNFaceObservation]
for observation in results {
    // boundingBox是脸部所在的方框坐标。
    print(observation.boundingBox)
}

这里有一个需要注意的地方就是,在苹果的文档中写到,返回的这个boundingBox是标准化了(Normalized rect),并且在这个坐标系中是以左下角为(0,0)的。所以我们需要做相应的坐标转化。下面给出了转化函数:

// scale是bounding box,size是实际图片的大小。
static func generateRect(withScale scale: CGRect, size: CGSize) -> CGRect{
    let scaleSize = CGSize.init(width: scale.width * size.width, height: scale.height * size.height)
    let point = CGPoint.init(x: scale.origin.x * size.width, y: (1 - scale.origin.y) * size.height - scaleSize.height)
    let result = CGRect.init(origin: point, size: scaleSize)
    return result
}
  • 第二种 脸部特征识别
    进行脸部特征(Land Mark)识别的时候,需要先进行面部方框识别(Face Rectangle Detection)。在识别完脸部矩形之后,传入结果到 VNDetectFaceLandmarksRequest 的 inputFaceObservations。具体代码如下
// 从面部方框识别的步骤中获得FaceObservation 列表。
guard let observations = self.detectFaceRectangles(image: image), let cgImage = image.cgImage else{
    return
}

let request = VNDetectFaceLandmarksRequest.init()

request.inputFaceObservations = observations

let handler = VNImageRequestHandler.init(cgImage: cgImage, options: [:])

do{
    try handler.perform([request])
}
catch{
    print(error.localizedDescription)
}

执行完代码之后,会获得一组新的VNFaceObservation 列表。每一个 VNFaceObservation 中都会存有一个 VNFaceLandmarks2D 变量。Landmark 中保存一张脸的脸部特征:脸廓、左右眼、左右眼睫毛、鼻廓、鼻梁、外嘴唇轮廓、内嘴唇轮廓。 这些脸部数据都是由 VNFaceLandmarkRegion2D 来表示。VNFaceLandmarkRegion2D 包含有多个点来表示脸部轮廓。这里的点和之前提到的Rectangle 的点一样,也是标准化了的(这里是相对于该脸所在的方框大小标准化的)。同时这里的坐标系也是以左下角为零点的。所以为了画出这些脸部轮廓,需要先把点的标准化坐标转化成在方框中的坐标,最后再转化成在整张图片中的坐标。

  1. Barcode Detection
    二维码的识别比较简单,功能是能识别图片中 二维码所在的区域,也能识别出二维码所包含的 字符串信息(比如在网上使用在线二维码生成信息图片,Vision能够识别出来)。代码如下:
// Init request and handler
let request = VNDetectBarcodesRequest.init()

let handler = VNImageRequestHandler.init(cgImage: cgImage, options: [:])

do{
    try handler.perform([request])
}
catch{
    print(error.localizedDescription)
}

guard let results = request.results as? [VNBarcodeObservation] else {
    return
}

for item in results{
    print(item.payloadStringValue) // 可能包含的字符串信息
    print(item.symbology)  // 不同类型的二维码含义
    print(item.barcodeDescriptor)
    print(item.boundingBox) // 二维码所在坐标位置
}
  1. Text Detection
    这个功能能够识别图片中存在文字的区域,有个局限性就是它只能识别水平显示的文字,不能识别偏转的文字。
    代码如下:

// Init request and handler.
let request = VNDetectTextRectanglesRequest.init()

let handler = VNImageRequestHandler.init(cgImage: cgImage, options: [:])

do{
    try handler.perform([request])
}
catch{
    print(error.localizedDescription)
}

guard let results = request.results as? [VNTextObservation] else {
    return
}

var rects = [CGRect]()
for item in results{
    rects.append(item.boundingBox)
    print(item.boundingBox)
}
  1. Rectangle Detection

相关文章

网友评论

      本文标题:Apple Vision Library Introductio

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