我们使用以下代码根据0个参考角检测到了-10度到10度之间的图像水平角,用于流式视频。
此外,当检测到的图像角度为零或超出该范围时,将不会返回结果。因此,无法检测到它是否为零或超出该范围。
而且,该范围似乎非常狭窄。没有代码还是有问题吗?
还有另一个问题。这里找到的角度是来自图像分析还是与物理设备角度有关?
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection){
pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
DispatchQueue.main.async() {
self.lookForHorizon(imageBuffer: pixelBuffer)
}
// Other things
}
var requestForHorizon = [VNImageBasedRequest]()
lazy var horizonRequest: VNDetectHorizonRequest = {return VNDetectHorizonRequest(completionHandler: self.detectHorizonHandler) }()
func lookForHorizon(imageBuffer: CVImageBuffer){
frameUIImage = takeAFrameImage(imageBuffer: imageBuffer)
let cgUIImage = frameUIImage.cgImage!
DispatchQueue.main.async() {
var requestOption:[VNImageOption : Any] = [:]
if let camData = CMGetAttachment(cgUIImage,
key: kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix,
attachmentModeOut: nil) {
requestOption = [.cameraIntrinsics:camData]
}
let imageRequestHandler = VNImageRequestHandler(
cgImage: cgUIImage,
orientation: CGImagePropertyOrientation(rawValue: 6)!,
options: requestOption)
do {
try imageRequestHandler.perform(self.requestForHorizon)
} catch {
print("error with imageRequestHandler.perform \(error)")
}
}
}
func detectHorizonHandler(request: VNRequest, error: Error?) {
guard let observations = request.results else {
print("no result")
return
}
let result = observations.map({$0 as? VNHorizonObservation})
if result.count > 0 {
let horizonAngle = CGFloat(result[0]?.angle ?? 0)
print("angle = \(horizonAngle)")
} else {
print("no angle info")
}
}