Swift AVCaptureVideoPreviewLayer不检测屏幕两个范围内的对象
我下载了苹果公司关于在实时捕获中识别物体的项目。 当我尝试该应用程序时,我发现如果我将要识别的对象放在相机视图的顶部或底部,则该应用程序无法识别该对象: 在第一张图片中,香蕉位于摄像头视图的中心,应用程序能够识别它 在这两幅图像中,香蕉靠近摄影机视图的边界,无法识别对象 以下是会话和预览层的设置方式:Swift AVCaptureVideoPreviewLayer不检测屏幕两个范围内的对象,swift,xcode,avcapturesession,coreml,apple-vision,Swift,Xcode,Avcapturesession,Coreml,Apple Vision,我下载了苹果公司关于在实时捕获中识别物体的项目。 当我尝试该应用程序时,我发现如果我将要识别的对象放在相机视图的顶部或底部,则该应用程序无法识别该对象: 在第一张图片中,香蕉位于摄像头视图的中心,应用程序能够识别它 在这两幅图像中,香蕉靠近摄影机视图的边界,无法识别对象 以下是会话和预览层的设置方式: func setupAVCapture() { var deviceInput: AVCaptureDeviceInput! // Select a video devi
func setupAVCapture() {
var deviceInput: AVCaptureDeviceInput!
// Select a video device, make an input
let videoDevice = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .back).devices.first
do {
deviceInput = try AVCaptureDeviceInput(device: videoDevice!)
} catch {
print("Could not create video device input: \(error)")
return
}
session.beginConfiguration()
session.sessionPreset = .vga640x480 // Model image size is smaller.
// Add a video input
guard session.canAddInput(deviceInput) else {
print("Could not add video device input to the session")
session.commitConfiguration()
return
}
session.addInput(deviceInput)
if session.canAddOutput(videoDataOutput) {
session.addOutput(videoDataOutput)
// Add a video data output
videoDataOutput.alwaysDiscardsLateVideoFrames = true
videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)]
videoDataOutput.setSampleBufferDelegate(self, queue: videoDataOutputQueue)
} else {
print("Could not add video data output to the session")
session.commitConfiguration()
return
}
let captureConnection = videoDataOutput.connection(with: .video)
// Always process the frames
captureConnection?.isEnabled = true
do {
try videoDevice!.lockForConfiguration()
let dimensions = CMVideoFormatDescriptionGetDimensions((videoDevice?.activeFormat.formatDescription)!)
bufferSize.width = CGFloat(dimensions.width)
bufferSize.height = CGFloat(dimensions.height)
videoDevice!.unlockForConfiguration()
} catch {
print(error)
}
session.commitConfiguration()
previewLayer = AVCaptureVideoPreviewLayer(session: session)
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
rootLayer = previewView.layer
previewLayer.frame = rootLayer.bounds
rootLayer.addSublayer(previewLayer)
}
你可以下载这个项目,
我想知道这是否正常
有什么解决办法吗?
是否使用coreml拍摄方形照片,但不包括这两个范围?
有什么提示吗?谢谢这可能是因为
imageCropAndScaleOption
设置为centerCrop
Core ML模型需要方形图像,但视频帧不是方形的。这可以通过在VNCoreMLRequest
上设置imageCropAndScaleOption
选项来解决。但是,结果可能不如中心裁剪的结果好(这取决于模型最初的训练方式)
另请参见Apple文档中的
VNIGECROPANDSCALEOPTION
。这可能是因为imageCropAndScaleOption
设置为centerCrop
Core ML模型需要方形图像,但视频帧不是方形的。这可以通过在VNCoreMLRequest
上设置imageCropAndScaleOption
选项来解决。但是,结果可能不如中心裁剪的结果好(这取决于模型最初的训练方式)
另请参见Apple文档中的vImageCropandScaleOption