我正在尝试使用Vision框架检测本地录制视频中的人脸。提供的大多数样本都是在实时摄像头视频中检测面部。
答案 0 :(得分:4)
VNSequenceRequestHandler
而不是VNImageRequestHandler
。
import UIKit
import AVFoundation
import CoreML
import Vision
class ViewController: UIViewController {
var player: AVPlayer!
var videoOutput: AVPlayerItemVideoOutput?
override func viewDidLoad() {
super.viewDidLoad()
let player = AVPlayer(url: localURL)
player.play()
player.currentItem?.addObserver(
self,
forKeyPath: #keyPath(AVPlayerItem.status),
options: [.initial, .old, .new],
context: nil)
player.addPeriodicTimeObserver(
forInterval: CMTime(value: 1, timescale: 30),
queue: DispatchQueue(label: "videoProcessing", qos: .background),
using: { time in
self.doThingsWithFaces()
})
self.player = player
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard let keyPath = keyPath, let item = object as? AVPlayerItem
else { return }
switch keyPath {
case #keyPath(AVPlayerItem.status):
if item.status == .readyToPlay {
self.setUpOutput()
}
break
default: break
}
}
func setUpOutput() {
guard self.videoOutput == nil else { return }
let videoItem = player.currentItem!
if videoItem.status != AVPlayerItemStatus.readyToPlay {
// see https://forums.developer.apple.com/thread/27589#128476
return
}
let pixelBuffAttributes = [
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,
] as [String: Any]
let videoOutput = AVPlayerItemVideoOutput(pixelBufferAttributes: pixelBuffAttributes)
videoItem.add(videoOutput)
self.videoOutput = videoOutput
}
func getNewFrame() -> CVPixelBuffer? {
guard let videoOutput = videoOutput, let currentItem = player.currentItem else { return nil }
let time = currentItem.currentTime()
if !videoOutput.hasNewPixelBuffer(forItemTime: time) { return nil }
guard let buffer = videoOutput.copyPixelBuffer(forItemTime: time, itemTimeForDisplay: nil)
else { return nil }
return buffer
}
func doThingsWithFaces() {
guard let buffer = getNewFrame() else { return }
// some CoreML / Vision things on that.
// There are numerous examples with this
}
}