Last active
July 29, 2023 21:48
-
-
Save opedge/1e3a80528e2d30d2238bc7b18e0a2020 to your computer and use it in GitHub Desktop.
CoreML style transfer with Vision (workaround while VNPixelBufferObservation.pixelBuffer is broken)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
import CoreML | |
import Vision | |
// Quick and dirty example of how to fix Vision VNPixelBufferObservation BAD_ACCESS. | |
// Please handle errors in swift by yourself, this code sample intended only for demonstration. | |
// MLModel must have multiArray output and Image input. | |
// Before using this functions install CoreMLHelpers from https://github.com/hollance/CoreMLHelpers | |
func deprocessArray(_ array: MLMultiArray) -> MLMultiArray { | |
let bias = [103.939, 116.779, 123.68] | |
var arr = MultiArray<Double>(array) | |
for h in 0..<arr.shape[1] { | |
for w in 0..<arr.shape[2] { | |
// Add bias | |
let b = arr[0, h, w] + bias[0] | |
let g = arr[1, h, w] + bias[1] | |
let r = arr[2, h, w] + bias[2] | |
// BGR -> RGB | |
arr[0, h, w] = r | |
arr[1, h, w] = g | |
arr[2, h, w] = b | |
} | |
} | |
return arr.array | |
} | |
func stylize(cgImage: CGImage) -> CGImage { | |
let model = try! VNCoreMLModel(for: feathers_array().model) | |
let request = VNCoreMLRequest(model: model) | |
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:]) | |
try! handler.perform([request]) | |
let result = request.results![0] as! VNCoreMLFeatureValueObservation | |
let array = deprocessArray(result.featureValue.multiArrayValue!) | |
let img = array.image(offset: 0.0, scale: 1.0)! | |
return img.cgImage! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment