Skip to content

Commit

Permalink
Moved pre-processing step to Vision class (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliot Boschwitz authored Mar 30, 2021
1 parent 4f4f70c commit 807f4fa
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Lobe_iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@
9B7B03DE2475DBF300D34020 = {
isa = PBXGroup;
children = (
B5B2D938260A4D5B00BABFAB /* LobeModel.mlmodel */,
9B7B03E92475DBF300D34020 /* Lobe_iOS */,
B5B2D938260A4D5B00BABFAB /* LobeModel.mlmodel */,
B513D84325809D62009D91E2 /* LobeTests */,
9B7B03E82475DBF300D34020 /* Products */,
B5A9B5252557373E00FD595B /* Frameworks */,
Expand Down
14 changes: 1 addition & 13 deletions Lobe_iOS/Models/CaptureSessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,7 @@ extension UIImage {
var isLandscape: Bool { size.width > size.height }
var breadth: CGFloat { min(size.width, size.height) }
var breadthSize: CGSize { .init(width: breadth, height: breadth) }

func squared(isOpaque: Bool = false) -> UIImage? {
guard let cgImage = cgImage?
.cropping(to: .init(origin: .init(x: isLandscape ? ((size.width-size.height)/2).rounded(.down) : 0,
y: isPortrait ? ((size.height-size.width)/2).rounded(.down) : 0),
size: breadthSize)) else { return nil }
let format = imageRendererFormat
format.opaque = isOpaque
return UIGraphicsImageRenderer(size: breadthSize, format: format).image { _ in
UIImage(cgImage: cgImage, scale: 1, orientation: imageOrientation)
.draw(in: .init(origin: .zero, size: breadthSize))
}
}

public convenience init?(pixelBuffer: CVPixelBuffer) {
var cgImage: CGImage?
VTCreateCGImageFromCVPixelBuffer(pixelBuffer, options: nil, imageOut: &cgImage)
Expand Down
5 changes: 5 additions & 0 deletions Lobe_iOS/Models/PredictionLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class PredictionLayer: NSObject {
}
onComplete(request)
})

/// Set center cropping for the expected image size.
/// NOTE: Although center cropping is currently expected with Lobe's model version,
/// please be mindful of future changes which may affect the expected preprocessing steps.
request.imageCropAndScaleOption = .centerCrop
return request
}
}
6 changes: 1 addition & 5 deletions Lobe_iOS/PlayViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ class PlayViewModel: ObservableObject {
.compactMap { $0 } // remove non-nill values
.receive(on: DispatchQueue.global(qos: .userInitiated))
.sink(receiveValue: { [weak self] image in
guard let squaredImage = image.squared() else {
print("Could not create squared image in PlayViewModel.")
return
}
self?.imagePredicter.getPrediction(forImage: squaredImage)
self?.imagePredicter.getPrediction(forImage: image)
})
.store(in: &disposables)

Expand Down

0 comments on commit 807f4fa

Please sign in to comment.