Apple在iOS 11中具有新功能,允许您使用视觉框架在没有模型的情况下进行对象检测。我尝试这些新的API,但发现VNDetectRectanglesRequest的结果并不好。我正确使用API吗?
这是一个很好的案例:
还有一些不好的情况:
这是我的代码:
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
// create the request
let request2 = VNDetectRectanglesRequest { (request, error) in
self.VNDetectRectanglesRequestCompletionBlock(request: request, error: error)
}
do {
request2.minimumConfidence = 0.7
try self.visionSequenceHandler.perform([request2], on: pixelBuffer)
} catch {
print("Throws: \(error)")
}
}
func VNDetectRectanglesRequestCompletionBlock(request: VNRequest, error: Error?) {
if let array = request.results {
if array.count > 0 {
let ob = array.first as? VNRectangleObservation
print("count: \(array.count)")
print("fps: \(self.measureFPS())")
DispatchQueue.main.async {
let boxRect = ob!.boundingBox
let transRect = self.transformRect(fromRect: boxRect, toViewRect: self.cameraLayer.frame)
var transformedRect = ob!.boundingBox
//transformedRect.origin.y = 1 - transformedRect.origin.y
let convertedRect = self.cameraLayer.layerRectConverted(fromMetadataOutputRect: transformedRect)
self.highlightView?.frame = convertedRect
}
}
}
}
已经提出了许多误解,期望和黑箱问题。但除此之外,您还错误地使用了API。
矩形检测器在图像中找到看起来代表真实世界矩形形状的区域。在大多数情况下,捕捉图像的相机在透视中看到一个真正的矩形物体 - 因此它在2D图像平面上的3D投影通常不是矩形。例如,您的一张照片中计算机屏幕的2D投影更为梯形,因为顶角距离相机比底角更远。
您可以通过查看检测到的矩形的实际角来获得此形状 - 请参阅VNRectangleObservation对象的属性。如果你在这四个角之间画线,你通常会找到一些能更好地跟踪照片中电脑屏幕,纸张等形状的东西。
相反,boundingBox
属性会为您提供最小的矩形区域 - 即图像空间中的矩形 - 包含这些角点。因此,除非您的相机视角恰到好处,否则它不会遵循真正的矩形物体的形状。
您注释掉的线几乎是正确的,您需要将其放回去,但将其更改为:
transformedRect.origin.y = 1 - (transformedRect.origin.y + transformedRect.width)
您的“坏情况”示例广场实际上来自右侧的软玩具。你的好人看起来正确,因为他们在屏幕的中心。