带有 VNRecognizedObjectObservation 的边界框框架不正确
Incorrect frame of boundingBox with VNRecognizedObjectObservation
我在使用 Core ML 和 Vision 显示已识别对象周围的边界框时遇到问题。
水平检测似乎工作正常,但是,垂直方框太高,越过视频的上边缘,没有一直到视频的底部,而且它没有'正确跟随相机的运动。在这里您可以看到问题:https://imgur.com/Sppww8T
这是视频数据输出的初始化方式:
let videoDataOutput = AVCaptureVideoDataOutput()
videoDataOutput.alwaysDiscardsLateVideoFrames = true
videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)]
videoDataOutput.setSampleBufferDelegate(self, queue: dataOutputQueue!)
self.videoDataOutput = videoDataOutput
session.addOutput(videoDataOutput)
let c = videoDataOutput.connection(with: .video)
c?.videoOrientation = .portrait
我也尝试过其他视频方向,但没有太大成功。
执行视觉请求:
let handler = VNImageRequestHandler(cvPixelBuffer: image, options: [:])
try? handler.perform(vnRequests)
最后一次处理请求。 viewRect
设置为视频视图的大小:812x375(我知道,视频层本身有点短,但这不是这里的问题):
let observationRect = VNImageRectForNormalizedRect(observation.boundingBox, Int(viewRect.width), Int(viewRect.height))
我也试过做类似的事情(有更多问题):
var observationRect = observation.boundingBox
observationRect.origin.y = 1.0 - observationRect.origin.y
observationRect = videoPreviewLayer.layerRectConverted(fromMetadataOutputRect: observationRect)
我尽量删除了我认为不相关的代码。
我实际上在使用 Apple 的示例代码时遇到过类似的问题,当时边界框不会按预期垂直围绕对象:https://developer.apple.com/documentation/vision/recognizing_objects_in_live_capture也许这意味着 API?
我用的是这样的:
let width = view.bounds.width
let height = width * 16 / 9
let offsetY = (view.bounds.height - height) / 2
let scale = CGAffineTransform.identity.scaledBy(x: width, y: height)
let transform = CGAffineTransform(scaleX: 1, y: -1).translatedBy(x: 0, y: -height - offsetY)
let rect = prediction.boundingBox.applying(scale).applying(transform)
这假定纵向和 16:9 宽高比。它假定 .imageCropAndScaleOption = .scaleFill
。
致谢:转换代码取自此存储库:https://github.com/Willjay90/AppleFaceDetection
我在使用 Core ML 和 Vision 显示已识别对象周围的边界框时遇到问题。
水平检测似乎工作正常,但是,垂直方框太高,越过视频的上边缘,没有一直到视频的底部,而且它没有'正确跟随相机的运动。在这里您可以看到问题:https://imgur.com/Sppww8T
这是视频数据输出的初始化方式:
let videoDataOutput = AVCaptureVideoDataOutput()
videoDataOutput.alwaysDiscardsLateVideoFrames = true
videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)]
videoDataOutput.setSampleBufferDelegate(self, queue: dataOutputQueue!)
self.videoDataOutput = videoDataOutput
session.addOutput(videoDataOutput)
let c = videoDataOutput.connection(with: .video)
c?.videoOrientation = .portrait
我也尝试过其他视频方向,但没有太大成功。
执行视觉请求:
let handler = VNImageRequestHandler(cvPixelBuffer: image, options: [:])
try? handler.perform(vnRequests)
最后一次处理请求。 viewRect
设置为视频视图的大小:812x375(我知道,视频层本身有点短,但这不是这里的问题):
let observationRect = VNImageRectForNormalizedRect(observation.boundingBox, Int(viewRect.width), Int(viewRect.height))
我也试过做类似的事情(有更多问题):
var observationRect = observation.boundingBox
observationRect.origin.y = 1.0 - observationRect.origin.y
observationRect = videoPreviewLayer.layerRectConverted(fromMetadataOutputRect: observationRect)
我尽量删除了我认为不相关的代码。
我实际上在使用 Apple 的示例代码时遇到过类似的问题,当时边界框不会按预期垂直围绕对象:https://developer.apple.com/documentation/vision/recognizing_objects_in_live_capture也许这意味着 API?
我用的是这样的:
let width = view.bounds.width
let height = width * 16 / 9
let offsetY = (view.bounds.height - height) / 2
let scale = CGAffineTransform.identity.scaledBy(x: width, y: height)
let transform = CGAffineTransform(scaleX: 1, y: -1).translatedBy(x: 0, y: -height - offsetY)
let rect = prediction.boundingBox.applying(scale).applying(transform)
这假定纵向和 16:9 宽高比。它假定 .imageCropAndScaleOption = .scaleFill
。
致谢:转换代码取自此存储库:https://github.com/Willjay90/AppleFaceDetection