iOSのVisionの機能を一通り試す

水平線の検出

VNDetectHorizonRequestを使う。VNImageBasedRequestを継承しており、追加メソッドやプロパティはない。ので、使い方は画像分類とかと同じ。

結果としてはVNHorizonObservationの配列が得られる。VNHorizonObservationはtransformとangleをプロパティに持つ。

open class VNHorizonObservation : VNObservation {

    
    open var transform: CGAffineTransform { get }

    open var angle: CGFloat { get }
}

transformをどう使って画像を補正するか、また複数の結果が得られた場合はどのように扱うのがいいのか、GitHubでコード検索してサンプルを探してみる。

矩形検出

画像内から矩形領域を検出する。VNDetectRectanglesRequestを使う。VNImageBasedRequestを継承。追加プロパティは結構ある。

open class VNDetectRectanglesRequest : VNImageBasedRequest {
    
    /*!
     @brief Specifies the minimum aspect ratio of the rectangle(s) to look for, range [0.0, 1.0], default 0.5
     */
    open var minimumAspectRatio: VNAspectRatio

    
    /*!
     @brief Specifies the maximum aspect ratio of the rectangle(s) to look for, range [0.0, 1.0], default 1.0
     */
    open var maximumAspectRatio: VNAspectRatio

    
    /*!
     @brief Specifies the maximum number of degrees a rectangle corner angle can deviate from 90 degrees, range [0,45], default 30
     */
    open var quadratureTolerance: VNDegrees

    
    /*!
     @brief Specifies the minimum size of the rectangle to be detected, as a proportion of the smallest dimension, range [0.0, 1.0], default .2. Any smaller rectangles that may have been detected will not be returned. 
     */
    open var minimumSize: Float

    
    /*!
     @brief Specifies a minimum confidence score, range [0.0, 1.0], default 0.0. Any rectangles with a lower confidence score will not be returned.
     */
    open var minimumConfidence: VNConfidence

    
    /*!
     @brief Specifies the maximum number of rectangles to be returned.  The default is 1.  Setting this property to 0 will allow an unlimited number of observations to be returned.
     */
    open var maximumObservations: Int
}

検出すべき「矩形」を条件(サイズ、アスペクト比、角の角度、信頼度)で絞り込むためのプロパティ群のようだ。ほとんどは名前だけで内容がわかるが、(英語的に)パッと意味がわかりにくいquadratureToleranceは、角の角度が90°からの逸脱をどの程度まで許容するか、を0〜45で指定する。デフォルトは30。

結果はVNRectangleObservationの配列が返ってくる。検出した矩形の4つの頂点の座標を持っている。

open class VNRectangleObservation : VNDetectedObjectObservation {
    
    open var topLeft: CGPoint { get }

    open var topRight: CGPoint { get }

    open var bottomLeft: CGPoint { get }

    open var bottomRight: CGPoint { get }
}

テキスト検出

VNDetectTextRectanglesRequestを使う。VNImageBasedRequestを継承。追加プロパティがひとつだけあり、個々の文字のバウンディングボックスも返すかどうかを指定する。

/*!
    @brief Specify whether or not the bounding boxes of individual characters should also be returned in the resultant VNTextObservations. Default is NO.
*/
open var reportCharacterBoxes: Bool

結果としてはVNTextObservationの配列が得られる。バウンディングボックスをCGRect型でプロパティに保持。

open var boundingBox: CGRect { get }

物体追跡(オブジェクトトラッキング)

VNTrackObjectRequestを使う。VNTrackingRequestを継承。そのVNTrackingRequestはVNImageBasedRequestを継承している。

open class VNTrackingRequest : VNImageBasedRequest {
    
    /*!
     @property property inputObservation
     @abstract The observation object that defines a region to track. Providing an observation not returned from a tracker (e.g. user-defined, or from a detector) begins a new tracker for the sequence. Providing an observation that was returned from a tracker continues the use of that tracker, to track the region to the next frame. In general, unless documented in the request's documentation, the rectangle must be defined in normalized coordinates (both dimensions normalized to [0,1] with the origin at the lower-left corner).
    */
    open var inputObservation: VNDetectedObjectObservation

    
    /*!
     @property property trackingLevel
     @abstract Tracking level allows tuning tracking algorithm to prefer speed (VNRequestOptionTrackingLevelFast) vs. tracking object location accuracy (VNRequestOptionTrackingLevelAccurate)
     */
    open var trackingLevel: VNRequestTrackingLevel

    
    /*!
     @property property lastFrame
     @abstract This property allows marking the last frame for tracking using current tracker. If set to YES, the results for this frame will be processed and returned and the current tracker will be released to the pool of available trackers
     */
    open var isLastFrame: Bool
}

VNTrackObjectRequestはVNTrackingRequestに対してイニシャライザが追加されているだけ。

open class VNTrackObjectRequest : VNTrackingRequest {
    
    /*!
     @brief Create a new request with detected object observation.
     
     @param    observation          Detected object observation with bounding box info.
     */
    public init(detectedObjectObservation observation: VNDetectedObjectObservation)

    
    /*!
     @brief Create a new request with detected object observation.
     
     @param    observation          Detected object observation with bounding box info.
     @param    completionHandler    The block that is invoked when the request has been performed.
     */
    public init(detectedObjectObservation observation: VNDetectedObjectObservation, completionHandler: VNRequestCompletionHandler? = nil)
}

初期化の際にVNDetectedObjectObservationオブジェクトを渡すようになっている。VNDetectedObjectObservationは検出対象オブジェクトのバウンディングボックスを持っている。

let req = VNTrackObjectRequest(detectedObjectObservation: VNDetectedObjectObservation(boundingBox: rect))

結果として受け取るのもVNDetectedObjectObservationの配列。その時点でのトラッキング対象オブジェクトの位置を示すバウンディングボックスが得られる。

矩形追跡

ここから先は

4,448字

¥ 300

最後まで読んでいただきありがとうございます!もし参考になる部分があれば、スキを押していただけると励みになります。 Twitterもフォローしていただけたら嬉しいです。 https://twitter.com/shu223/