Vision Framework

Sep 18 2024 · Swift 5.10, 6.0, iOS 17.5, 18b4, Xcode 15.4, 16b6

Lesson 02: Building Basic Vision Requests

Demo 01

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

There’s a starter project in the materials repository for this lesson. Here’s the project, which uses the photos picker from the last lesson. If you want to swap out the camera or the Document picker, you should be able to just bring those ViewModels into this project. You’ll need to update the toolbar code here. And change the value for the photoPickerViewModel. Working with the photos picker on the simulator is pretty easy, so that’s what the demo uses.

if let results = request.results as?
  [VNRecognizedObjectObservation] {

}
let sortedResults = results
  .sorted(by: { $0.confidence > $1.confidence })
  .map { "\($0.labels.first?.identifier ?? "Unknown" )
    - \((Int($0.confidence * 100)))%" }
if let topResult = sortedResults.first {
    self?.classification = topResult
  } else {
    self?.classification = "Unknown"
}
.filter { $0.confidence > 0.01 }
.map { "\($0.identifier) - \((Int($0.confidence * 100)))%" }
.joined(separator: ", ")
if !sortedResults.isEmpty {
  self?.classification = sortedResults
} else {
  self?.classification = "Unknown"
}
if let objects = try? request.supportedIdentifiers() {
  for object in objects {
    logger.debug("Object: \(object)")
  }
}
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 01 Next: Instruction 02