我試圖從我的MacBook相機捕獲視頻幀並在飛行中處理它們(用於以後的人臉檢測)。爲了減少內存使用量,我希望將捕獲分辨率從預設值1200x720降低到640x480。AVFoundation:如何調整sampleBuffer(AVCaptureSessionPreset)中的幀大小並不會影響它
這裏是我的代碼來設置捕獲會話:
_session = [[AVCaptureSession alloc] init];
if ([_session canSetSessionPreset:AVCaptureSessionPreset640x480]){
[_session setSessionPreset:AVCaptureSessionPreset640x480];
NSLog(@"resolution preset changed");
}
// configure input
_camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
_deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:_camera error:nil];
// configure output
_videoOutput = [[AVCaptureVideoDataOutput alloc] init];
NSDictionary* newSettings = @{ (NSString*) kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)};
_videoOutput.videoSettings = newSettings;
//discard if the data output queue is blocked
[_videoOutput setAlwaysDiscardsLateVideoFrames:YES];
// process frames on another queue
dispatch_queue_t videoDataOutputQueue;
videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL);
[_videoOutput setSampleBufferDelegate:videoBufferDelegate queue:videoDataOutputQueue];
[_session addInput:_deviceInput];
[_session addOutput:_videoOutput];
[_session startRunning];
在此之後,該會話相應地設置了,它記錄「的決議預設改變」正確和視頻數據到委託轉發上的另一個隊列處理它。當我檢查session.sessionPreset
時,它表示預設爲AVCaptureSessionPreset640x480
。
現在在委託:
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
//get the image from buffer, transform it to CIImage
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
self.image = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer];
當檢查self.image.extent.size
,它顯示和1200x720的大小不正確,因爲如果我沒有改變之前的預設。即使在檢查方法的論點sampleBuffer
時,它也會顯示尺寸爲1200x720。
我瀏覽了互聯網和蘋果的參考了幾個小時,但無法找到解決方案。我希望你能救我!