Commit 27fcc463 authored by Tobias Due Munk's avatar Tobias Due Munk

Add CoreML appendix

parent 16c0cd48
......@@ -18,6 +18,8 @@
5667747B22798BCA003F881E /* Task3.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5667747A22798BCA003F881E /* Task3.swift */; };
56A52E812345E8BC0061E5A3 /* Appendix12.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A52E802345E8BC0061E5A3 /* Appendix12.swift */; };
56A52E832346046D0061E5A3 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A52E822346046D0061E5A3 /* Extensions.swift */; };
56A52E8523462C9E0061E5A3 /* Appendix14.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A52E8423462C9E0061E5A3 /* Appendix14.swift */; };
56A52ECE234634110061E5A3 /* DeepLabV3.mlmodel in Sources */ = {isa = PBXBuildFile; fileRef = 56A52ECD234634110061E5A3 /* DeepLabV3.mlmodel */; };
56AD53392278513B005B1E87 /* BrowserViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56AD53382278513B005B1E87 /* BrowserViewController.swift */; };
56AD533B2278668B005B1E87 /* EditorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56AD533A2278668B005B1E87 /* EditorViewController.swift */; };
56AD534122787DEE005B1E87 /* Goat.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 56AD533C22787DED005B1E87 /* Goat.jpg */; };
......@@ -51,6 +53,8 @@
5667747A22798BCA003F881E /* Task3.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Task3.swift; sourceTree = "<group>"; };
56A52E802345E8BC0061E5A3 /* Appendix12.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Appendix12.swift; sourceTree = "<group>"; };
56A52E822346046D0061E5A3 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
56A52E8423462C9E0061E5A3 /* Appendix14.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Appendix14.swift; sourceTree = "<group>"; };
56A52ECD234634110061E5A3 /* DeepLabV3.mlmodel */ = {isa = PBXFileReference; lastKnownFileType = file.mlmodel; path = DeepLabV3.mlmodel; sourceTree = "<group>"; };
56AD53382278513B005B1E87 /* BrowserViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowserViewController.swift; sourceTree = "<group>"; };
56AD533A2278668B005B1E87 /* EditorViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorViewController.swift; sourceTree = "<group>"; };
56AD533C22787DED005B1E87 /* Goat.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Goat.jpg; sourceTree = "<group>"; };
......@@ -101,6 +105,7 @@
561112582344C283006900F1 /* Appendix11.swift */,
56A52E802345E8BC0061E5A3 /* Appendix12.swift */,
E3135F6523460C8000795492 /* Appendix13.swift */,
56A52E8423462C9E0061E5A3 /* Appendix14.swift */,
);
path = Appendices;
sourceTree = "<group>";
......@@ -117,6 +122,14 @@
path = Tasks;
sourceTree = "<group>";
};
56A52E8623462E7B0061E5A3 /* CoreML */ = {
isa = PBXGroup;
children = (
56A52ECD234634110061E5A3 /* DeepLabV3.mlmodel */,
);
path = CoreML;
sourceTree = "<group>";
};
56AD534622787DF1005B1E87 /* Sample Photos */ = {
isa = PBXGroup;
children = (
......@@ -156,6 +169,7 @@
565508C622AA882D00884E5C /* Appendices */,
E32ABCDD22730AE400D74B7C /* Kernels.metal */,
56AD534622787DF1005B1E87 /* Sample Photos */,
56A52E8623462E7B0061E5A3 /* CoreML */,
E32ABCD22273092400D74B7C /* Assets.xcassets */,
E32ABCD42273092400D74B7C /* LaunchScreen.storyboard */,
E32ABCD72273092400D74B7C /* Info.plist */,
......@@ -256,7 +270,9 @@
56AD53392278513B005B1E87 /* BrowserViewController.swift in Sources */,
561112592344C283006900F1 /* Appendix11.swift in Sources */,
56A52E832346046D0061E5A3 /* Extensions.swift in Sources */,
56A52ECE234634110061E5A3 /* DeepLabV3.mlmodel in Sources */,
E3D6AE6D22B0F94A00ACADC5 /* Appendix1.swift in Sources */,
56A52E8523462C9E0061E5A3 /* Appendix14.swift in Sources */,
E32ABCCE2273092300D74B7C /* MainViewController.swift in Sources */,
E3135F6623460C8000795492 /* Appendix13.swift in Sources */,
E3355E46232133790093589E /* Appendix9.swift in Sources */,
......
import CoreImage
import MetalPerformanceShaders
let appendix14: Effect = { originalImage, depthMap in
let originalImage = originalImage
.transformed(by: .init(scaleX: 0.2, y: 0.2))
.cropped(to: CGRect(x: 0, y: 0, width: 513, height: 513))
// let originalImage = originalImage.cropped(to: CGRect(x: 0, y: 0, width: 304, height: 228))
return try? CoreMLKernel.apply(
withExtent: originalImage.extent,
inputs: [originalImage],
arguments: [:]
)
}
private class CoreMLKernel: CIImageProcessorKernel {
override public class func process(with inputs: [CIImageProcessorInput]?, arguments: [String : Any]?, output: CIImageProcessorOutput) throws {
guard
let input = inputs?.first,
let pixelBuffer = input.pixelBuffer
else {
fatalError("No input")
}
let model = DeepLabV3()
let prediction = try model.prediction(image: pixelBuffer)
let semanticPredictions = prediction.semanticPredictions
let outputPixelCount = Int(output.region.width * output.region.height)
let out = output.baseAddress.bindMemory(to: Int8.self, capacity: outputPixelCount * 4)
for i in 0..<semanticPredictions.count {
let v: Int8 = semanticPredictions[i].int32Value > 0 ? 127 : 0
let o = out.advanced(by: i * 4)
o[0] = v // Blue
o[1] = v // Green
o[2] = v // Red
o[3] = 127 // Alpha
}
}
override class func formatForInput(at input: Int32) -> CIFormat {
.BGRA8
}
override class var outputFormat: CIFormat {
.BGRA8
}
}
......@@ -28,6 +28,7 @@ class EditorViewController: UIViewController {
appendix11,
appendix12,
appendix13,
appendix14,
]
private var selectedEffect: Int = UserDefaults.standard.integer(forKey: "selectedEffect") {
didSet {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment