Skip to content

Instantly share code, notes, and snippets.

View createwithswift's full-sized avatar

Create with Swift createwithswift

View GitHub Profile

This is sample code to use Core ML models in Swift Playgrounds. For this code snippet, the MobileNetV2 image classification model and YOLOv3 object detection model are used. The Core ML model files and sample projects for usage in Xcode are provided by Apple. The models can be used to classify images or detect dominant objects in a camera frame or image. You can replace these placeholder models with you own custom models to leverage the power of machine learning inside Swift Playgrounds.

For Core ML models to be used in Playground, combined binary files or the model as well as corresponding model classes are needed. The procedure to obtain them is described below. The compiled binary file needs to be places in the Resources folder or the Playground page, the model class can conveniently be places in a Swift file in the Sources folder of the Playground page.

ImageClassificationInPlayground.swift

The Image Classificatio

@createwithswift
createwithswift / UIImage+Resize+CVPixelBuffer.swift
Created June 7, 2021 07:37
An Extension to UIImage to resize the image object to a provided CGSize and return the resized Image as well as convert the image to a CVPixelBuffer. The CVPixelBuffer type is required by Core ML models as an input parameter. Core ML models also require squared images of specific sizes, e.g. 224x224, 416x416 or other resolutions for which the ex…
// UIImage+Resize+CVPixelBuffer.swift
//
// Created by Moritz Philip Recke for Create with Swift on 10 February 2021.
//
import Foundation
import UIKit
extension UIImage {
@createwithswift
createwithswift / UIImage+CVPixelBuffer.swift
Created June 7, 2021 07:34
An Extension to UIImage to convert the image to a CVPixelBuffer. The CVPixelBuffer type is required by Core ML models as an input parameter.
// UIImage+CVPixelBuffer.swift
//
// Created by Moritz Philip Recke for Create with Swift on 7 May 2021.
//
import Foundation
import UIKit
extension UIImage {
@createwithswift
createwithswift / UIImage+Resize.swift
Last active June 7, 2021 07:36
An Extension to UIImage to resize the image object to a provided CGSize and return the resized Image. Resizing images might come in handy in many situations. Core ML models for example require squared images of specific sizes, e.g. 224x224, 416x416 or other resolutions for which the extension is helpful.
// UIImage+Resize.swift
//
// Created by Moritz Philip Recke for Create with Swift on 07 May 2021.
//
import Foundation
import UIKit
extension UIImage {