您的位置:首页 > 移动开发 > IOS开发

ios开发知识框架_iOS视觉框架:基础知识

2020-08-23 22:47 3987 查看

ios开发知识框架

Let’s talk about computer vision for iOS?

我们来谈谈iOS的计算机视觉吗?

This post is a continuation of two other previous posts that i give an introduction to image processing and computer vision for iOS and explain how to use image filters with Core Image. Now, the purpose of this article is to present Vision, Apple’s framework for developing computer vision iOS applications.

这篇文章是其他两篇文章的延续,我对iOS的图像处理和计算机视觉进行介绍,并说明了如何在Core Image中使用图像滤镜 。 现在,本文的目的是介绍Vision,这是Apple用于开发计算机视觉iOS应用程序的框架。

Vision Framework offers a number of image analysis and computer vision capabilities. With it, you can perform:

视觉框架提供了许多图像分析和计算机视觉功能。 有了它,您可以执行:

  • Detection of face to face markers;

    检测面对面的标记;
  • Text detection;

    文字检测;
  • Barcode recognition;

    条码识别
  • Image registration;

    图像配准;
  • Feature tracking;

    功能跟踪;
  • Classification and detection of objects using CoreML models.

    使用CoreML模型对对象进行分类和检测。

In Vision, there are three roles:

在Vision中,有三个角色:

Request — used when you ask the framework to detect something and it describes your request for analysis. There are different types of request:

请求 -在您要求框架检测到某些东西时使用,它描述了您的分析请求。 有不同类型的请求:

  • VNDetectFaceRectanglesRequest — to detect faces in an image.

    VNDetectFaceRectanglesRequest —检测图像中的脸部。
  • VNDetectBarcodesRequest — for barcode detection.

    VNDetectBarcodesRequest —用于条形码检测。
  • VNDetectTextRectanglesRequest — for visible text region detection in an image.

    VNDetectTextRectanglesRequest —用于图像中的可见文本区域检测。
  • VNCoreMLRequest — for image analysis that use Core ML models to process images.

    VNCoreMLRequest —用于使用Core ML模型处理图像的图像分析。
  • VNClassifyImageRequest — request to classify an image.

    VNClassifyImageRequest —请求对图像进行分类。
  • VNDetectFaceLandmarksRequest — request to find facial features in an image, such as eyes and mouth.

    VNDetectFaceLandmarksRequest-请求查找图像中的面部特征,例如眼睛和嘴巴。
  • VNTrackObjectRequest — request that tracks the movement of an object in several images or video.

    VNTrackObjectRequest —请求在多个图像或视频中跟踪对象的运动。

Request Handler — will execute your request, being used when you want the framework to do something, processing one or more request analyzes. There are two types:

请求处理程序 —将执行您的请求,在您希望框架做某事,处理一个或多个请求分析时使用。 有两种类型:

  • VNImageRequestHandler — to analyze an image.

    VNImageRequestHandler —分析图像。
  • VNSequenceRequestHandler — to analyze a sequence of images, used mainly for object tracking.

    VNSequenceRequestHandler —分析图像序列,主要用于对象跟踪。

Observations — the results of requests requested to Vision are wrapped in observations. The information in the observations is a result bounding box that can be of types:

观察结果-向Vision提出的请求结果包含在观察结果中。 观察结果中的信息是结果边界框,其类型可以是:

  • VNClassificationObservation — classification information resulting from image analysis with a Core ML model

    VNClassificationObservation-使用Core ML模型进行图像分析得出的分类信息
  • VNFaceObservation — for face detection.

    VNFaceObservation —用于面部检测。
  • VNDetectedObjectObservation — for object detection.

    VNDetectedObjectObservation —用于对象检测。
  • VNCoreMLFeatureValueObservation — a collection of key-value information resulting from prediction of image analysis with a Core ML model.

    VNCoreMLFeatureValueObservation —由使用Core ML模型预测图像分析得到的键值信息的集合。
  • VNHorizonObservation — determine the horizon angle in an image.

    VNHorizo​​nObservation —确定图像中的水平角。
  • VNImageAlignmentObservation — detect transforms needed to align the content of two images.

    VNImageAlignmentObservation —检测对齐两个图像的内容所需的变换。
  • VNPixelBufferObservation — an output image resulting from image-to-image processing analysis with a Core ML model.

    VNPixelBufferObservation —由Core ML模型进行的图像到图像处理分析得出的输出图像。

I will show the use of Vision Framework through two examples! Let’s code!

我将通过两个示例展示Vision Framework的用法! 让我们编码吧!

# 1 — Sorting images using Core ML models

#1 —使用Core ML模型对图像进行排序

Core ML offers several pre-trained models that can be used for classification, but it is also possible to train other models using Apple’s Create ML framework. You can learn more about Core ML and Create ML with Apple's Documentation.

Core ML提供了几种可用于分类的预训练模型,但是也可以使用Apple的Create ML框架训练其他模型。 您可以通过Apple的文档了解有关Core ML和Create ML 的更多信息

In this case, we will use a pre-trained model. What happens is that we have a static image as input that is processed and analyzed according to the specified model and, as an output, we have a vector classification information with data such as Identifier (image classification) and Confidence (percentage of classification correctness) for each identifier.

在这种情况下,我们将使用预先训练的模型。 发生的事情是,我们有一个静态图像作为输入,并根据指定的模型进行处理和分析,作为输出,我们有一个向量分类信息,其中包含诸如标识符(图像分类)和置信度(分类正确性百分比)之类的数据每个标识符。

The first thing we need to do is import CoreML and Vision.

我们需要做的第一件事是导入CoreML和Vision。

import CoreMLimport Vision

In our example, we will use the MobileNetV2.mlmodel provided by Apple:

在我们的示例中,我们将使用Apple提供的MobileNetV2.ml模型

Pre trained model available by Apple. Apple提供了预训练的模型。

As stated earlier, the handler will need to access an input image to allow classification. The Vision Request Handler works with CIImage-type images. An instance of CIImage is an immutable object that represents an image and is an object that does not directly represent the image’s bitmap data.

如前所述,处理程序将需要访问输入图像以进行分类。 视觉请求处理程序可用于CIImage类型的图像。 CIImage的实例是代表图像的不可变对象,并且是不直接代表图像的位图数据的对象。

To work with a Core ML model, we need to instantiate it in order to access it.

要使用Core ML模型,我们需要实例化它才能访问它。

guard let model = try? VNCoreMLModel(for: MobileNetV2().model) else{     fatalError("Erro acessando modelo")}

We now need to instantiate the request and, to perform image classification using the Core ML model above, we must treat the result as VNClassificationObservation.

现在,我们需要实例化请求,并且要使用上面的Core ML模型执行图像分类,我们必须将结果视为VNClassificationObservation。

let request = VNCoreMLRequest(model: model) { (request, error) in     guard let results = request.results as?                       
[VNClassificationObservation]
else{
fatalError()
}
}

As stated earlier, we need to create a request and send it to the handler by perform(:

如前所述,我们需要创建一个请求,然后通过perform(:

let handler = VNImageRequestHandler(ciImage: image)do{    try handler.perform([request])
}catch{
print(error)
}

In summary, assuming we have a detect method responsible for analysis and classification, it should look like this:

总而言之,假设我们有一个负责分析和分类的检测方法,它应该看起来像这样:

func detect(image: CIImage){    guard let model = try? VNCoreMLModel(for: MobileNetV2().model)     
else{
fatalError("Erro acessando modelo")
} let request = VNCoreMLRequest(model: model) {(request, error) in

guard
let results = request.results as?
[VNClassificationObservation]
else{
fatalError()
}
} let handler = VNImageRequestHandler(ciImage: image) do{ try handler.perform([request])
}catch{
print(error)
}
}

You may now create a label to show the result of the classification. The observation would be an array of information and the first element should be the best match (confidence) 😉.

您现在可以创建一个标签来显示分类的结果。 观察将是一系列信息,第一个元素应是最佳匹配(置信度)😉。

Pretty easy right? Now let’s see another example!

很容易吧? 现在让我们来看另一个例子!

# 2 — Detecting Faces within an image

#2-在图像中检测人脸

As the example above, we will implement the detect method but, in this case, the function is to detect faces within an image. Remember that Vision works with CIImage.

如上面的示例,我们将实现检测方法,但是在这种情况下,该功能是检测图像中的人脸。 请记住,Vision与CIImage一起使用。

First, we need to instantiate the request as NDetectFaceRectanglesRequest and treat the results as VNFaceObservation. In this case, if you are using the Xcode simulator, it will return the number of faces in the image that is the number of elements in the observation array. If you are using your mobile device, it will highlight the faces drawing a yellow rectangle around it.

首先,我们需要将请求实例化为NDetectFaceRectanglesRequest,并将结果视为VNFaceObservation。 在这种情况下,如果您使用的是Xcode模拟器,它将返回图像中人脸的数量,即观察数组中元素的数量。 如果您使用的是移动设备,它将突出显示在其周围绘制黄色矩形的面Kong。

let request = VNDetectFaceRectanglesRequest(){(request, error) in    guard let observation = request.results as? [VNFaceObservation]                      

else{
return
}
}

As the example #1 above, the handler of type VNImageRequestHandler should be performed and the method detect should be like this:

如上面的示例1所示,应执行VNImageRequestHandler类型的处理程序,并且detect方法应如下所示:

func detect(image: CIImage){   let request = VNDetectFaceRectanglesRequest(){(request, error) in     guard let observation = request.results as? [VNFaceObservation]    

else{
return
}
} let handler = VNImageRequestHandler(ciImage: image) do{
try handler.perform([request])
}catch{
print(error)
}
}

That's it! Now you can start using Vision Framework in your iOS applications!

而已! 现在,您可以开始在iOS应用程序中使用Vision Framework了!

If you want to learn more about Text Recognition, read this article about Natural Language Processing in iOS.

如果您想了解有关文本识别的更多信息,请阅读有关iOS中自然语言处理的文章。

Enjoy! 😘

请享用! 😘

翻译自: https://medium.com/academy-eldoradocps/vision-framework-for-ios-an-introduction-78d02c3e1ef

ios开发知识框架

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: