Crafting iOS with Vision Framework & In-App Pur...

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Crafting iOS with Vision Framework & In-App Purchases (STRV iOS Meetup)

Prague meetup focused on Vision Framework and in-app purchases.

Tomas Parizek

November 20, 2024

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

49 slides
  1. 8

  2. VISION FRAMEWORK • Official Computer Vision solution from Apple •

    Available from iOS 11 (2017) • Neural Engine (hardware acceleration) 14
  3. CORE IMAGE FRAMEWORK • Official Computer Vision solution from Apple

    • Available from iOS 11 (2017) • Neural Engine (hardware acceleration) 15
  4. CORE IMAGE FRAMEWORK • Was official Computer Vision solution from

    Apple • Available from iOS 11 (2017) • Neural Engine (hardware acceleration) 16
  5. CORE IMAGE FRAMEWORK • Was official Computer Vision solution from

    Apple • Available from iOS 5 (2011) • Neural Engine (hardware acceleration) 17
  6. CORE IMAGE FRAMEWORK • Was official Computer Vision solution from

    Apple • Available from iOS 5 (2011); on macOS since early 2000s under different name • Neural Engine (hardware acceleration) 18
  7. CORE IMAGE FRAMEWORK • Was official Computer Vision solution from

    Apple • Available from iOS 5 (2011); on macOS since early 2000s under different name • Neural Engine (hardware acceleration) 19
  8. CUSTOM ML MODEL: TensorFlow and PyTorch models • Core ML

    is the iOS native ML model format • Core ML Tools mac utility can convert a TensorFlow or PyTorch model to Core ML 20
  9. CUSTOM ML MODEL: Code example let classifier = try CarImageClassifier(configuration:

    .init()) let input = try CarImageClassifierInput(imageWith: image.cgImage!) let output = try classifier.prediction(input: input) guard let probability = output.targetProbability[output.target] else { return } let formattedProbability = String(format: "%.2f", probability * 100) resultLabel.text = "\(output.target) (\(formattedProbability)%)" 22
  10. CUSTOM ML MODEL: BlazeFace from Google • Face recognition model

    • TensorFlow Lite format • Available for iOS and Android • Used in Google’s mobile apps like Google Meet 23
  11. GOALS • • • Tested models: • Vision Framework •

    Core Image Framework • BlazeFace Model Comparison: • Accuracy • Speed Context: • Different devices • Different images 25
  12. VISION FRAMEWORK ELEMENTS • • Request • Task definition •

    Result callback RequestHandler • • Performs request on a given dataset Observation • Detection result data structure 35
  13. VISION FRAMEWORK ELEMENTS let request = VNGenerateObjectnessBasedSaliencyImageRequest { result, error

    in // Handle observations and errors } let requestHandler = VNImageRequestHandler( cgImage: image.cgImage!, options: [:] ) try requestHandler.perform([request]) 36
  14. SALIENCY DETECTION • Detection of most important image regions •

    Attention based and object based modes • Up to three results 39
  15. SALIENCY DETECTION let request = VNGenerateObjectnessBasedSaliencyImageRequest { request, error in

    DispatchQueue.main.async { if let results = request.results { self.handleSaliencyResults( results, originalImage: image, mode: mode ) } if let error { self.handleError(error) } } } let requestHandler = VNImageRequestHandler(cgImage: image.cgImage!, options: [:]) do { try requestHandler.perform([request]) } catch { handleError(error) } 40
  16. SALIENCY DETECTION func handleSaliencyResults( _ results: [VNObservation], originalImage: UIImage, mode:

    SaliencyMode ) { guard let saliencyObservation = results.first(ofType: VNSaliencyImageObservation.self), let salientObjects = saliencyObservation.salientObjects, !salientObjects.isEmpty else { cropImageView.image = originalImageView.image updateInfoLabel(saliencies: []) return } … 41
  17. SALIENCY DETECTION switch mode { case .union: var unionOfSalientRegions =

    salientObjects.first!.boundingBox for salientObject in salientObjects.dropFirst() { unionOfSalientRegions = unionOfSalientRegions.union(salientObject.boundingBox) } salientBox = VNImageRectForNormalizedRect( unionOfSalientRegions, Int(originalImage.size.width), Int(originalImage.size.height) ) case .confidency: let salientObject = salientObjects.sorted(by: \.confidence, isAscending: false).first! salientBox = VNImageRectForNormalizedRect( salientObject.boundingBox, Int(originalImage.size.width), Int(originalImage.size.height) ) } 42
  18. SALIENCY DETECTION let croppedImage = originalImage.cgImage!.cropping(to: salientBox)! cropImageView.image = UIImage(

    cgImage: croppedImage, scale: originalImage.scale, orientation: originalImage.imageOrientation ) 43
  19. REAL TIME FACE TRACKING • “Tracking the User’s Face in

    Real Time” Apple example project • Detect faces => Track faces • Periodic detection to manage leaving and new faces • Tracking works with raw pixel buffer from Camera 46
  20. KEY TAKEAWAYS • Computer Vision: Machine understanding visual data •

    It’s not only for camera app • Available iOS solutions: • Core Image Framework • Vision Framework • Custom Model 47

More Decks by Tomas Parizek

Other Decks in Programming