簡體   English   中英

使用 AVAssetWriter 和 CoreML 的相機上的 FPS 不一致

[英]FPS not consistent on Camera using AVAssetWriter and CoreML

我正在嘗試創建一個應用程序,該應用程序可以使用 AVAssetWriter 以 100 FPS 的速度錄制視頻,並使用 Create ML 中的 ActionClassifier 檢測一個人是否正在執行操作。 但是當我嘗試將 2 放在一起時,在記錄和檢測動作時 FPS 會下降到 30。

如果我自己進行錄制,那么它會以 100 FPS 的速度錄制。

我可以通過設備配置將相機的 FPS 設置為 100 FPS。

捕獲輸出功能設置

  func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        
        bufferImage = sampleBuffer
        guard let calibrationData = CMGetAttachment(sampleBuffer, key: kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, attachmentModeOut: nil) as? Data else {
            return
        }
        
        cameraCalibrationMatrix = calibrationData.withUnsafeBytes { $0.pointee }
        
        if self.isPredictorActivated == true {
            do  {
                let poses = try predictor.processFrame(sampleBuffer)
                if (predictor.isReadyToMakePrediction) {
                   let prediction =  try predictor.makePrediction()
                    let confidence = prediction.confidence * 100

                    DispatchQueue.main.async {
                        self.predictionLabel.text = prediction.label + " " + String(confidence.rounded(toPlaces: 0))
                        if (prediction.label == "HandsUp" && prediction.confidence > 0.85) {
                            print("Challenging")
                            self.didChallengeVideo()
                        }
                    }

                }
            } catch {
                print(error.localizedDescription)
            }
        }
        
       
        
        
        let presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
        
        if assetWriter == nil {
            createWriterInput(for: presentationTimeStamp)
        } else {
            let chunkDuration = CMTimeGetSeconds(CMTimeSubtract(presentationTimeStamp, chunkStartTime))
            //            print("Challenge\(isChallenging)")
            
            if chunkDuration > 1500 || isChallenging {
                assetWriter.endSession(atSourceTime: presentationTimeStamp)
                
                // make a copy, as finishWriting is asynchronous
                let newChunkURL = chunkOutputURL!
                let chunkAssetWriter = assetWriter!
                
                chunkAssetWriter.finishWriting {
                    print("finishWriting says: \(chunkAssetWriter.status.rawValue) \(String(describing: chunkAssetWriter.error))")
                    print("queuing \(newChunkURL)")
                    print("Chunk Duration: \(chunkDuration)")
                    
                    let asset = AVAsset(url: newChunkURL)
                    
                    print("FPS of CHUNK \(asset.tracks.first?.nominalFrameRate)")
                    
                    if self.isChallenging {
                        self.challengeVideoProcess(video: asset)
                    }
                    
                    self.isChallenging = false
                    
                    
                }
                createWriterInput(for: presentationTimeStamp)
                
            }
        }
        
        if !assetWriterInput.append(sampleBuffer) {
            print("append says NO: \(assetWriter.status.rawValue) \(String(describing: assetWriter.error))")
        }
        
        

    }

如果您想每幀都運行它,那么執行動作分類非常昂貴,因此它可能會影響應用程序的整體性能(包括視頻片段 FPS)。 我不知道您需要多久進行一次預測,但我建議您嘗試每秒最多運行 2-3 次 Action Classifier,看看是否有幫助。

每一幀運行動作分類器不會改變你的分類,因為你只向分類器動作窗口添加了一幀,所以沒有必要經常運行它。

例如,如果您的動作分類器設置為窗口 3s 並在 30fps 視頻上進行訓練,則您的分類基於 3 * 30 = 90 幀。 一幀不會有什么不同。

還要確保您的 100fps 與您用於訓練動作分類器的素材相匹配。 否則,您可能會得到錯誤的預測,因為運行在 30fps 視頻上訓練的動作分類器會將 100fps 素材的 1 秒視為超過 3,333 秒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM