はじめに
リアルタイムテキスト検出を行っている、こちらの有難いソースを一部修正すればできます。
元記事はこちらをご参照ください。
ソース(変更する部分のみ)
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
let uiImage = sampleBufferToUIImage(sampleBuffer: sampleBuffer, with: UIApplication.shared.statusBarOrientation)
let orientation = CGImagePropertyOrientation(uiImage.imageOrientation)
let ciImage = CIImage(image: uiImage)!
// UI の変更はメインスレッドで実行
DispatchQueue.main.async { [weak self] in
if let frame = self?.imageFrameOnViewController(uiImage: uiImage) {
self?.imageView.image = uiImage
self?.visualizeRectanglesView.frame = frame
}
}
// orientation を必ず設定すること
// let handler = VNImageRequestHandler(ciImage: ciImage, orientation: Int32(orientation.rawValue))
let handler = VNImageRequestHandler(ciImage: ciImage, orientation: orientation)
// let request = VNDetectTextRectanglesRequest() { request, error in
let request = VNDetectFaceRectanglesRequest() { request, error in
// 顔ブロックの矩形を取得
let rects = request.results?.flatMap { result -> [CGRect] in
// guard let observation = result as? VNTextObservation else { return [] }
guard let observation = result as? VNFaceObservation else { return [] }
return [observation.boundingBox]
} ?? []
// // 文字ごとの矩形を取得
// let characterRects = request.results?.flatMap { result -> [VNRectangleObservation] in
// //guard let observation = result as? VNTextObservation else { return [] }
// guard let observation = result as? VNFaceObservation else { return [] }
// return observation.characterBoxes ?? []
// } ?? []
// UI の変更はメインスレッドで実行
DispatchQueue.main.async { [weak self] in
self?.visualizeRectanglesView.rectangles = rects
// self?.visualizeRectanglesView.characterRectangles = characterRects
}
}
// request.reportCharacterBoxes = true
try! handler.perform([request])
}