diff --git a/ML/CGImage+CVPixelBuffer.swift b/ML/CGImage+CVPixelBuffer.swift index 358067c4..a0a729c0 100644 --- a/ML/CGImage+CVPixelBuffer.swift +++ b/ML/CGImage+CVPixelBuffer.swift @@ -2,97 +2,6 @@ import CoreGraphics import CoreImage import VideoToolbox -extension CGImage { - /** - Converts the image to an ARGB `CVPixelBuffer`. - */ - public func pixelBuffer() -> CVPixelBuffer? { - return pixelBuffer(width: width, height: height, orientation: .up) - } - - /** - Resizes the image to `width` x `height` and converts it to an ARGB - `CVPixelBuffer`. - */ - public func pixelBuffer(width: Int, height: Int, - orientation: CGImagePropertyOrientation) -> CVPixelBuffer? { - return pixelBuffer(width: width, height: height, - pixelFormatType: kCVPixelFormatType_32ARGB, - colorSpace: CGColorSpaceCreateDeviceRGB(), - alphaInfo: .noneSkipFirst, - orientation: orientation) - } - - /** - Converts the image to a grayscale `CVPixelBuffer`. - */ - public func pixelBufferGray() -> CVPixelBuffer? { - return pixelBufferGray(width: width, height: height, orientation: .up) - } - - /** - Resizes the image to `width` x `height` and converts it to a grayscale - `CVPixelBuffer`. - */ - public func pixelBufferGray(width: Int, height: Int, - orientation: CGImagePropertyOrientation) -> CVPixelBuffer? { - return pixelBuffer(width: width, height: height, - pixelFormatType: kCVPixelFormatType_OneComponent8, - colorSpace: CGColorSpaceCreateDeviceGray(), - alphaInfo: .none, - orientation: orientation) - } - - /** - Resizes the image to `width` x `height` and converts it to a `CVPixelBuffer` - with the specified pixel format, color space, and alpha channel. - */ - public func pixelBuffer(width: Int, height: Int, - pixelFormatType: OSType, - colorSpace: CGColorSpace, - alphaInfo: CGImageAlphaInfo, - orientation: CGImagePropertyOrientation) -> CVPixelBuffer? { - - // TODO: If the orientation is not .up, then rotate the CGImage. - // See also: https://stackoverflow.com/a/40438893/ - assert(orientation == .up) - - var maybePixelBuffer: CVPixelBuffer? - let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue, - kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue] - let status = CVPixelBufferCreate(kCFAllocatorDefault, - width, - height, - pixelFormatType, - attrs as CFDictionary, - &maybePixelBuffer) - - guard status == kCVReturnSuccess, let pixelBuffer = maybePixelBuffer else { - return nil - } - - let flags = CVPixelBufferLockFlags(rawValue: 0) - guard kCVReturnSuccess == CVPixelBufferLockBaseAddress(pixelBuffer, flags) else { - return nil - } - defer { CVPixelBufferUnlockBaseAddress(pixelBuffer, flags) } - - guard let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer), - width: width, - height: height, - bitsPerComponent: 8, - bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer), - space: colorSpace, - bitmapInfo: alphaInfo.rawValue) - else { - return nil - } - - context.draw(self, in: CGRect(x: 0, y: 0, width: width, height: height)) - return pixelBuffer - } -} - extension CGImage { /** Creates a new CGImage from a CVPixelBuffer. @@ -105,42 +14,4 @@ extension CGImage { VTCreateCGImageFromCVPixelBuffer(pixelBuffer, options: nil, imageOut: &cgImage) return cgImage } - - /* - // Alternative implementation: - public static func create(pixelBuffer: CVPixelBuffer) -> CGImage? { - // This method creates a bitmap CGContext using the pixel buffer's memory. - // It currently only handles kCVPixelFormatType_32ARGB images. To support - // other pixel formats too, you'll have to change the bitmapInfo and maybe - // the color space for the CGContext. - - guard kCVReturnSuccess == CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly) else { - return nil - } - defer { CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly) } - - if let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer), - width: CVPixelBufferGetWidth(pixelBuffer), - height: CVPixelBufferGetHeight(pixelBuffer), - bitsPerComponent: 8, - bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer), - space: CGColorSpaceCreateDeviceRGB(), - bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue), - let cgImage = context.makeImage() { - return cgImage - } else { - return nil - } - } - */ - - /** - Creates a new CGImage from a CVPixelBuffer, using Core Image. - */ - public static func create(pixelBuffer: CVPixelBuffer, context: CIContext) -> CGImage? { - let ciImage = CIImage(cvPixelBuffer: pixelBuffer) - let rect = CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(pixelBuffer), - height: CVPixelBufferGetHeight(pixelBuffer)) - return context.createCGImage(ciImage, from: rect) - } } diff --git a/ML/ImageClassifier.swift b/ML/ImageClassifier.swift index 8d2160c3..56c95ad7 100644 --- a/ML/ImageClassifier.swift +++ b/ML/ImageClassifier.swift @@ -62,7 +62,7 @@ class ModelImageClassifier { } let boundingBox = face.boundingBox - + let size = CGSize( width: boundingBox.width * image.size.width, height: boundingBox.height * image.size.height @@ -71,7 +71,7 @@ class ModelImageClassifier { x: boundingBox.minX * image.size.width, y: (1 - boundingBox.minY) * image.size.height - size.height ) - + let faceRect = CGRect(origin: origin, size: size) guard let croppedImage = cgImage.cropping(to: faceRect) else { diff --git a/ML/UIImage+CVPixelBuffer.swift b/ML/UIImage+CVPixelBuffer.swift index aaf66769..206bd6b4 100644 --- a/ML/UIImage+CVPixelBuffer.swift +++ b/ML/UIImage+CVPixelBuffer.swift @@ -2,13 +2,6 @@ import UIKit import VideoToolbox extension UIImage { - /** - Converts the image to an ARGB `CVPixelBuffer`. - */ - public func pixelBuffer() -> CVPixelBuffer? { - return pixelBuffer(width: Int(size.width), height: Int(size.height)) - } - /** Resizes the image to `width` x `height` and converts it to an ARGB `CVPixelBuffer`. @@ -20,24 +13,6 @@ extension UIImage { alphaInfo: .noneSkipFirst) } - /** - Converts the image to a grayscale `CVPixelBuffer`. - */ - public func pixelBufferGray() -> CVPixelBuffer? { - return pixelBufferGray(width: Int(size.width), height: Int(size.height)) - } - - /** - Resizes the image to `width` x `height` and converts it to a grayscale - `CVPixelBuffer`. - */ - public func pixelBufferGray(width: Int, height: Int) -> CVPixelBuffer? { - return pixelBuffer(width: width, height: height, - pixelFormatType: kCVPixelFormatType_OneComponent8, - colorSpace: CGColorSpaceCreateDeviceGray(), - alphaInfo: .none) - } - /** Resizes the image to `width` x `height` and converts it to a `CVPixelBuffer` with the specified pixel format, color space, and alpha channel. @@ -101,24 +76,4 @@ extension UIImage { return nil } } - - /* - // Alternative implementation: - public convenience init?(pixelBuffer: CVPixelBuffer) { - // This converts the image to a CIImage first and then to a UIImage. - // Does not appear to work on the simulator but is OK on the device. - self.init(ciImage: CIImage(cvPixelBuffer: pixelBuffer)) - } - */ - - /** - Creates a new UIImage from a CVPixelBuffer, using a Core Image context. - */ - public convenience init?(pixelBuffer: CVPixelBuffer, context: CIContext) { - if let cgImage = CGImage.create(pixelBuffer: pixelBuffer, context: context) { - self.init(cgImage: cgImage) - } else { - return nil - } - } }