Skip to content

Instantly share code, notes, and snippets.

@iosdevie
iosdevie / coreml-mlfeaturevalue.swift
Created May 26, 2021 05:46
Create an MLFeatureValue instance from an image.
let featureValue = try MLFeatureValue(cgImage: image.cgImage!, constraint: imageConstraint, options: nil)
@iosdevie
iosdevie / coreml-inputDescriptionsByName-imageconstraint.swift
Created May 26, 2021 05:48
retrieve the image constraint object from the CoreML model
let imageConstraint = model?.modelDescription.inputDescriptionsByName["image"]!.imageConstraint!
@iosdevie
iosdevie / coreml-mlupdatetask.swift
Created May 26, 2021 05:49
pseudo code for re-training model data on a device
let updateTask = try MLUpdateTask(forModelAt: updatableModelURL, trainingData: trainingData, configuration: model.configuration, completionHandler: { context in
}
updateTask.resume()
private func loadModel(url: URL) -> MLModel? {
do {
let config = MLModelConfiguration()
config.computeUnits = .all
return try MLModel(contentsOf: url, configuration: config)
} catch {
print("Error loading model: \(error)")
return nil
}
}
@iosdevie
iosdevie / predict-coreml-image.swift
Created May 26, 2021 05:50
Predict an image using MLModel
func predict(image: UIImage) -> Animal? {
let imageConstraint = model.modelDescription.inputDescriptionsByName["image"]!.imageConstraint!
do{
let imageOptions: [MLFeatureValue.ImageOption: Any] = [
.cropAndScale: VNImageCropAndScaleOption.scaleFill.rawValue
]
let featureValue = try MLFeatureValue(cgImage: image.cgImage!, constraint: imageConstraint, options: imageOptions)
@iosdevie
iosdevie / coreml-batch-provider.swift
Created May 26, 2021 05:51
Create a batch provider CoreML
private func batchProvider() -> MLArrayBatchProvider
{
var batchInputs: [MLFeatureProvider] = []
let imageOptions: [MLFeatureValue.ImageOption: Any] = [
.cropAndScale: VNImageCropAndScaleOption.scaleFill.rawValue
]
for (image,label) in imageLabelDictionary {
do{
let featureValue = try MLFeatureValue(cgImage: image.cgImage!, constraint: imageConstraint, options: imageOptions)
@iosdevie
iosdevie / coreml-filemanager-retrieve.swift
Created May 26, 2021 05:52
Retrieve URL of the MLModel
let fileManager = FileManager.default
let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:true)
let modelURL = documentDirectory.appendingPathComponent("CatDog.mlmodelc")
@iosdevie
iosdevie / UIImage-symbolconfig.swift
Created May 26, 2021 06:06
Customising System Icons
let config = UIImage.SymbolConfiguration(textStyle: .largeTitle)
UIImage(systemName: "paperplane", withConfiguration : config)
@iosdevie
iosdevie / collectionview-contextmenu-long-press.swift
Created May 26, 2021 06:08
display a ContextMenu when a user long-presses on a CollectionView cell
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil){ action in
let viewMenu = UIAction(title: "View", image: UIImage(systemName: "eye.fill"), identifier: UIAction.Identifier(rawValue: "view")) {_ in
print("button clicked..")
}
let rotate = UIAction(title: "Rotate", image: UIImage(systemName: "arrow.counterclockwise"), identifier: nil, state: .on, handler: {action in
print("rotate clicked.")
})
let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash.fill"), identifier: nil, discoverabilityTitle: nil, attributes: .destructive, state: .on, handler: {action in
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let configuration = UIContextMenuConfiguration(identifier: "\(indexPath.row)" as NSCopying, previewProvider: {
return SecondViewController(index: indexPath.row)
}){ action in
//add your uimenu as earlier
}
}