title: Convert Image to CVPixelBuffer for Machine Learning Swift
body: <p>I am trying to get Apple's sample Core ML Models that were demoed at the 2017 WWDC to function correctly. I am using the GoogLeNet to try and classify images (see the <a href="https://developer.apple.com/machine-learning/" rel="noreferrer">Apple Machine Learning Page</a>). The model takes a CVPixelBuffer as an input. I have an image called imageSample.jpg that I'm using for this demo. My code is below:</p>
  
  <pre><code>        var sample = UIImage(named: "imageSample")?.cgImage
          let bufferThree = getCVPixelBuffer(sample!)
  
          let model = GoogLeNetPlaces()
          guard let output = try? model.prediction(input: GoogLeNetPlacesInput.init(sceneImage: bufferThree!)) else {
              fatalError("Unexpected runtime error.")
          }
  
          print(output.sceneLabel)
  </code></pre>
  
  <p>I am always getting the unexpected runtime error in the output rather than an image classification. My code to convert the image is below:</p>
  
  <pre><code>func getCVPixelBuffer(_ image: CGImage) -&gt; CVPixelBuffer? {
          let imageWidth = Int(image.width)
          let imageHeight = Int(image.height)
  
          let attributes : [NSObject:AnyObject] = [
              kCVPixelBufferCGImageCompatibilityKey : true as AnyObject,
              kCVPixelBufferCGBitmapContextCompatibilityKey : true as AnyObject
          ]
  
          var pxbuffer: CVPixelBuffer? = nil
          CVPixelBufferCreate(kCFAllocatorDefault,
                              imageWidth,
                              imageHeight,
                              kCVPixelFormatType_32ARGB,
                              attributes as CFDictionary?,
                              &amp;pxbuffer)
  
          if let _pxbuffer = pxbuffer {
              let flags = CVPixelBufferLockFlags(rawValue: 0)
              CVPixelBufferLockBaseAddress(_pxbuffer, flags)
              let pxdata = CVPixelBufferGetBaseAddress(_pxbuffer)
  
              let rgbColorSpace = CGColorSpaceCreateDeviceRGB();
              let context = CGContext(data: pxdata,
                                      width: imageWidth,
                                      height: imageHeight,
                                      bitsPerComponent: 8,
                                      bytesPerRow: CVPixelBufferGetBytesPerRow(_pxbuffer),
                                      space: rgbColorSpace,
                                      bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue)
  
              if let _context = context {
                  _context.draw(image, in: CGRect.init(x: 0, y: 0, width: imageWidth, height: imageHeight))
              }
              else {
                  CVPixelBufferUnlockBaseAddress(_pxbuffer, flags);
                  return nil
              }
  
              CVPixelBufferUnlockBaseAddress(_pxbuffer, flags);
              return _pxbuffer;
          }
  
          return nil
      }
  </code></pre>
  
  <p>I got this code from a previous StackOverflow post (last answer <a href="https://stackoverflow.com/questions/33053412/how-to-initialise-cvpixelbufferref-in-swift/42045825#42045825">here</a>). I recognize that the code may not be correct, but I have no idea of how to do this myself. I believe that this is the section that contains the error. The model calls for the following type of input: <code>Image&lt;RGB,224,224&gt;</code></p>