在 Vision 识别事件之前记录相机输出

问题描述

我的应用程序使用 Vision 识别事件并使用 CMSampleBuffer 来执行此操作。 活动结束后,我正在成功使用 AVWriter 录制视频。

现在我想记录完整的动作,从而记录事件发生前 1-2 秒。

我尝试将 CMSampleBuffer 推入环形缓冲区,但这会使缓冲区的相机饿死。

func captureOutput(_ output: AVCaptureOutput,didOutput sampleBuffer: CMSampleBuffer,from connection: AVCaptureConnection) {
// sends that to detectBall
  /// Gets called by the camera every time there is a new buffer available
 func detectBall(inBuffer buffer: CMSampleBuffer,ballDetectionRequest: VNCoreMLRequest,orientation: CGImagePropertyOrientation,frame: normalizedPoint,updatingRingBuffer: PassthroughSubject<AppEnvironment.Avstate.RingBufferItem,Never>
  ) throws {
    // I tried to convert it into a CVPixelBuffer but its a shallow copy as well so it also starves the camera
    let imageBuffer: CVPixelBuffer = CMSampleBufferGetimageBuffer(buffer)!
    /// rotated 90 because of the cameras native landscape orientation
   
    
    let visionHandler = VNImageRequestHandler(ciImage: croppedImage,options: [:])
    try visionHandler.perform([ballDetectionRequest])
    if let results = ballDetectionRequest as? [VNClassificationObservation] {
      // Filter out classification results with low confidence
      let filteredResults = results.filter { $0.confidence > 0.9 }
      guard let topResult = results.first,topResult.confidence > 0.9 else { return }
      // print("       its a: \(topResult.identifier)")
      // print("copy buffer")
      
      updatingRingBuffer.send(AppEnvironment.Avstate.RingBufferItem(
    /// HERE IS THE PROBLEM: AS SOON AS I SEND IT SOMEWHERE ELSE THE CAMERA IS STARVED
                                buffer: imageBuffer,ball: topResult.identifier == "ball")

如何实现连续存储这 1-2 秒的视频而不将其写入磁盘然后将其添加到视频文件中?

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)