Last active
August 28, 2023 05:51
-
-
Save dmikey/a565c697e04eb27d381e84142e57399d to your computer and use it in GitHub Desktop.
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
// resize gan example | |
func upscaleImage(_ orgImage: NSImage) -> NSImage { | |
let newSize = CGSize(width: 2048, height: 2048) | |
guard let cgImage = orgImage.cgImage(forProposedRect: nil, context: nil, hints: nil), | |
let resizedImage = resize(cgImage, to: newSize) else { | |
return orgImage | |
} | |
guard let model = try? VNCoreMLModel(for: realesrgan512(configuration: MLModelConfiguration()).model) else { | |
fatalError("model initialization failed") | |
} | |
let coreMLRequest = VNCoreMLRequest(model: model) | |
coreMLRequest.imageCropAndScaleOption = .scaleFill | |
let handler = VNImageRequestHandler(cgImage: resizedImage) | |
try? handler.perform([coreMLRequest]) | |
guard let result = coreMLRequest.results?.first as? VNPixelBufferObservation else { | |
return orgImage | |
} | |
let ciContext = CIContext() | |
let ciImage = CIImage(cvImageBuffer: result.pixelBuffer) | |
guard let upImage = ciContext.createCGImage(ciImage, from: ciImage.extent) else { | |
return orgImage | |
} | |
let nsImage = NSImage(cgImage: upImage, size: NSZeroSize) | |
return nsImage | |
} | |
func resize(_ image: CGImage, to newSize: CGSize) -> CGImage? { | |
let bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.premultipliedLast.rawValue | |
guard let context = CGContext( | |
data: nil, | |
width: Int(newSize.width), | |
height: Int(newSize.height), | |
bitsPerComponent: 8, | |
bytesPerRow: 0, | |
space: CGColorSpaceCreateDeviceRGB(), | |
bitmapInfo: bitmapInfo | |
) else { return nil } | |
context.interpolationQuality = .high | |
context.draw(image, in: CGRect(origin: .zero, size: newSize)) | |
return context.makeImage() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment