#swift #tensorflow #avkit #cvpixelbuffer
Вопрос:
Я пытаюсь создать приложение искусственного интеллекта, которое может обнаруживать объекты с помощью tensorFlowLite. Но я не могу снять CVPixelBuffer с камеры с помощью AVKit. Я использовал эту строку кодов :
let imageWidth = CVPixelBufferGetWidth(pixelBuffer)
let imageHeight = CVPixelBufferGetHeight(pixelBuffer)
let sourcePixelFormat = CVPixelBufferGetPixelFormatType(pixelBuffer)
assert(sourcePixelFormat == kCVPixelFormatType_32ARGB ||
sourcePixelFormat == kCVPixelFormatType_32BGRA ||
sourcePixelFormat == kCVPixelFormatType_32RGBA)
моя функция вывода AVKit:
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
//print("Camera was able to capture a frame:", Date())
let pixelBuffer : CVPixelBuffer? = CMSampleBufferGetImageBuffer(sampleBuffer)
guard let imagePixelBuffer = pixelBuffer else { return }
guard let ai = modelDataHandler else { return }
var results = ai.runModel(onFrame: imagePixelBuffer)
print(results)
}
Мои свойства AVKit:
private let session: AVCaptureSession = AVCaptureSession()
private lazy var videoDataOutput = AVCaptureVideoDataOutput()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
session.sessionPreset = .high
guard modelDataHandler != nil else {
fatalError("Failed to load model")
}
let captureSession = AVCaptureSession()
captureSession.sessionPreset = .high
guard let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else { return }
guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return }
captureSession.addInput(input)
captureSession.startRunning()
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
view.layer.addSublayer(previewLayer)
previewLayer.frame = view.frame
let dataOutput = AVCaptureVideoDataOutput()
dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
captureSession.addOutput(dataOutput)
}
Но я не могу передать блок assert, мой результат CVPixelBuffer:
CVPixelFormatresult