 
   func predict(image: UIImage) {
     if let pixelBuffer = image.pixelBuffer(width: YOLO.inputWidth, height: YOLO.inputHeight) {
-      predict(pixelBuffer: pixelBuffer)
-    }
-  }
-
-  func predict(pixelBuffer: CVPixelBuffer) {
+      predict(pixelBuffer: pixelBuffer, inflightIndex: 0)
+    }
+  }
+
+  func predict(pixelBuffer: CVPixelBuffer, inflightIndex: Int) {
     // Measure how long it takes to predict a single video frame.
     let startTime = CACurrentMediaTime()
-
-    // Resize the input with Core Image to 416x416.
-    guard let resizedPixelBuffer = resizedPixelBuffer else { return }
-    let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
-    let sx = CGFloat(YOLO.inputWidth) / CGFloat(CVPixelBufferGetWidth(pixelBuffer))
-    let sy = CGFloat(YOLO.inputHeight) / CGFloat(CVPixelBufferGetHeight(pixelBuffer))
-    let scaleTransform = CGAffineTransform(scaleX: sx, y: sy)
-    let scaledImage = ciImage.transformed(by: scaleTransform)
-    ciContext.render(scaledImage, to: resizedPixelBuffer)
 
     // This is an alternative way to resize the image (using vImage):
     //if let resizedPixelBuffer = resizePixelBuffer(pixelBuffer,
     //                                              width: YOLO.inputWidth,
-    //                                              height: YOLO.inputHeight)
-
-    // Give the resized input to our model.
-    if let result = try? yolo.predict(image: resizedPixelBuffer),
-       let boundingBoxes = result {
-      let elapsed = CACurrentMediaTime() - startTime
-      showOnMainThread(boundingBoxes, elapsed)
-    } else {
-      print("BOGUS")
+    //                                              height: YOLO.inputHeight) {
+
+    // Resize the input with Core Image to 416x416.
+    if let resizedPixelBuffer = resizedPixelBuffers[inflightIndex] {
+      let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
+      let sx = CGFloat(YOLO.inputWidth) / CGFloat(CVPixelBufferGetWidth(pixelBuffer))
+      let sy = CGFloat(YOLO.inputHeight) / CGFloat(CVPixelBufferGetHeight(pixelBuffer))
+      let scaleTransform = CGAffineTransform(scaleX: sx, y: sy)
+      let scaledImage = ciImage.transformed(by: scaleTransform)
+      ciContext.render(scaledImage, to: resizedPixelBuffer)
+
+      // Give the resized input to our model.
+      if let result = try? yolo.predict(image: resizedPixelBuffer),
+         let boundingBoxes = result {
+        let elapsed = CACurrentMediaTime() - startTime
+        showOnMainThread(boundingBoxes, elapsed)
+      } else {
+        print("BOGUS")
+      }
     }
 
     self.semaphore.signal()
   }
 
-  func predictUsingVision(pixelBuffer: CVPixelBuffer) {
+  func predictUsingVision(pixelBuffer: CVPixelBuffer, inflightIndex: Int) {
     // Measure how long it takes to predict a single video frame. Note that
     // predict() can be called on the next frame while the previous one is
     // still being processed. Hence the need to queue up the start times.

  var colors: [UIColor] = []

  let ciContext = CIContext()
  var resizedPixelBuffer: CVPixelBuffer?

  var framesDone = 0
  var frameCapturingStartTime = CACurrentMediaTime()

  }

  func setUpCoreImage() {
    // Since we might be running several requests in parallel, we also need
    // to do the resizing in different pixel buffers or we might overwrite a
    // pixel buffer that's already in use.
    for _ in 0..<YOLO.maxBoundingBoxes {
      var resizedPixelBuffer: CVPixelBuffer?
      let status = CVPixelBufferCreate(nil, YOLO.inputWidth, YOLO.inputHeight,
                                       kCVPixelFormatType_32BGRA, nil,
                                       &resizedPixelBuffer)

      if status != kCVReturnSuccess {
        print("Error: could not create resized pixel buffer", status)
      }
      resizedPixelBuffers.append(resizedPixelBuffer)
    }
  }

