Learn more about Russian war crimes in Ukraine.

Webcam video filter

We assume we have a video stream, e.g.:

let captureSession = AVCaptureSession()
captureSession.sessionPreset = AVCaptureSessionPresetPhoto

Then we add an output to the capture session:

let videoOutput = AVCaptureVideoDataOutput()

Then we set a delegate for the videoOutput:

videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "sample buffer delegate"))

The videoOutput will call captureOutput(AVCaptureOutput!, didOutputSampleBuffer: CMSampleBuffer!, from: AVCaptureConnection!) on its delegate whenever a new buffer is received from the captureSession, i.e. a new frame from the webcam. So we start to implement that:

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
    // ...

From the sample buffer, we can get a CVImageBuffer (a “pixel buffer”) with:

let pxbuf = CMSampleBufferGetImageBuffer(sampleBuffer)!

To apply a filter to this, we need to get it into the CoreImage framework, i.e. a CIImage:

let camImg = CIImage(cvImageBuffer: pxbuf)

We choose an arbitrary filter:

let comicEffect = CIFilter(name: "CIComicEffect")!

We call the filter with the CIImage as input and get a CIImage as output:

comicEffect.setValue(camImg, forKey: kCIInputImageKey)
let filteredCIImage = comicEffect.value(forKey: kCIOutputImageKey) as! CIImage!

Now we just need to repeatedly display the output CIImage.

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

More by Jim

Tagged . All content copyright James Fisher 2017. This post is not associated with my employer. Found an error? Edit this page.