【问题标题】:Incorrect frame of boundingBox with VNRecognizedObjectObservation带有 VNRecognizedObjectObservation 的 boundingBox 框架不正确
【发布时间】:2019-08-03 13:50:45
【问题描述】:

我在使用 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 存在问题?

【问题讨论】:

    标签: ios coreml


    【解决方案1】:

    我使用这样的东西:

    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

    致谢:转换代码取自此 repo:https://github.com/Willjay90/AppleFaceDetection

    【讨论】:

    • 啊,是的,这是完美的。我最终使用了一个稍微改变的代码:let scale = CGAffineTransform.identity.scaledBy(x: viewRect.width, y: viewRect.height); let transform = CGAffineTransform(scaleX: 1, y: -1).translatedBy(x: 0, y: -viewRect.height); let observationRect = observation.boundingBox.applying(scale).applying(transform) 关键似乎是vnRequest.imageCropAndScaleOption = .scaleFill,因为没有它根本不起作用。现在我的百吉饼追踪器工作完美?
    猜你喜欢
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多