[ad_1]
I am utilizing the iOS Imaginative and prescient framework to detect rectangles in real-time with the digital camera on an iPhone and it really works effectively. The reside preview shows a shifting yellow rectangle across the detected form.
Nonetheless, when the identical code is run on an iPad, the yellow rectangle tracks precisely alongside the X axis, however on the Y it’s at all times barely offset from the centre and it isn’t accurately scaled. The included picture exhibits each units monitoring the identical take a look at sq. to higher illustrate. In each circumstances, after I seize the picture and plot the rectangle on the complete digital camera body (1920 x 1080), the whole lot appears to be like nice. It is simply the reside preview on the iPad that doesn’t monitor correctly.
I imagine the difficulty is attributable to how the iPad display has a 4:3 side ratio. The iPhone’s full display preview scales its 1920 x 1080 uncooked body right down to 414 x 718, the place each X and Y dims are scaled down by the identical issue (about 2.6). Nonetheless, the iPad scales the 1920 x 1080 body right down to 810 x 964, which warps the picture and causes the error alongside the Y axis.
A tough answer may very well be to set a preview layer dimension smaller than the complete display and have it’s scaled down uniformly in a 16:9 ratio matching 1920 x 1080, however I would like to make use of the complete display. Has anybody right here come throughout this challenge and located a rework that may correctly translate and scale the rect remark onto the iPad display?
Instance take a look at pictures and code snippet are under.
let rect: VNRectangleObservation
//Digital camera preview (reside) picture dimensions
let previewWidth = self.previewLayer!.bounds.width
let previewHeight = self.previewLayer!.bounds.top
//Dimensions of uncooked captured frames from the digital camera (1920 x 1080)
let frameWidth = self.body!.width
let frameHeight = self.body!.top
//Remodel to alter detected rectangle from Imaginative and prescient framework's coordinate system to SwiftUI
let rework = CGAffineTransform(scaleX: 1, y: -1).translatedBy(x: 0, y: -(previewHeight))
let scale = CGAffineTransform.identification.scaledBy(x: previewWidth, y: previewHeight)
//Convert the detected rectangle from normalized [0, 1] coordinates with backside left origin to SwiftUI high left origin
//and scale the normalized rect to preview window dimensions.
var bounds: CGRect = rect.boundingBox.making use of(scale).making use of(rework)
//Remainder of code attracts the bounds CGRect in yellow onto the preview window, as proven within the picture.
[ad_2]

