3rd-Party On-Device Models

Sep 19 2024 · Swift 6.0, iOS 18.0, Xcode 16.0

Lesson 03: Releasing Third-Party Models in Your App

Demo 1

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

Processing With Vision Detect

Now, you’ll process what the model finds in the image. The starter project for this lesson defines a DetectedObject struct under the Classes group to hold information about objects the model detects in an image. This struct stores a label and confidence for each detected object. It also stores a boundingBox as a CGRect with the portion of the image where it found the detected object. Add the following state property after cgImage:

@State private var detectedObjects: [DetectedObject] = []
// 1
if results.isEmpty {
  print("No results found.")
  return
}
// 2
for result in results {
  // 3
  if let firstIdentifier = result.labels.first {
    let confidence = firstIdentifier.confidence
    let label = firstIdentifier.identifier
    // 4
    let boundingBox = result.boundingBox
    // 5
    let object = DetectedObject(
      label: label,
      confidence: confidence,
      boundingBox: boundingBox
    )
    detectedObjects.append(object)
  }
}
detectedObjects = []
.onChange(of: cgImage) {
  runModel()
}
.overlay {
  ForEach(detectedObjects, id: \.self) { ident in
    `ObjectOverlayView`(object: ident)
  }
}
ForEach(detectedObjects, id: \.self) { obj in
  Text(obj.label) + Text(" (") + Text(obj.confidence, format: .percent) + Text(")")
}
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Demo 2