I am developing an app that detects text real-time in portrait mode. However, on switching the camera to landscape mode it does not adapts to the changed orientation.
Screenshot in portrait mode:
Screenshot in landscape mode:
Camera Setup code:
fileprivate func initSetupLiveVideo() {
session.sessionPreset = .high
let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)
let deviceInput = try! AVCaptureDeviceInput(device: captureDevice!)
let deviceOutput = AVCaptureVideoDataOutput()
deviceOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
deviceOutput.setSampleBufferDelegate(self, queue: DispatchQueue.global(qos: DispatchQoS.QoSClass.default))
session.addInput(deviceInput)
session.addOutput(deviceOutput)
imageView.videoPreviewLayer.session = session
}
Code to highlight word:
func highlightWord(box: VNTextObservation) {
guard let boxes = box.characterBoxes else {
return
}
var minX: CGFloat = 0.0
var minY: CGFloat = 0.0
var maxY: CGFloat = CGFloat.greatestFiniteMagnitude
var maxX: CGFloat = CGFloat.greatestFiniteMagnitude
for char in boxes {
if char.bottomLeft.x < maxX {
maxX = char.bottomLeft.x
}
if char.bottomRight.x > minX {
minX = char.bottomRight.x
}
if char.bottomRight.y < maxY {
maxY = char.bottomRight.y
}
if char.topRight.y > minY {
minY = char.topRight.y
}
}
let xCord = maxX * imageView.frame.size.width
let yCord = (1 - minY) * imageView.frame.size.height
let width = (minX - maxX) * imageView.frame.size.width
let height = (minY - maxY) * imageView.frame.size.height
let outline = CALayer()
outline.frame = CGRect(x: xCord, y: yCord, width: width, height: height)
outline.borderWidth = 2.0
outline.borderColor = UIColor.red.cgColor
imageView.layer.addSublayer(outline)
}
Expected that on rotating the device, the view should adapt.