Git Product home page Git Product logo

smiledetector's Introduction

iOS Smile Detector using MLKit

This small ios sample application uses Google's MLKit to analyze your face and put some nice hearts when you are smiling by using Machine Learning algorithms.

To fetch images from the camera it uses OpenTok library

Application Setup

You will need to create a Firebase project, download your GoogleService-info.plist file containing your project credentials and replace the empty placeholder file which lives in SmileDetector folder.

When you have your firebase dep solved, please run:

$ pod install
$ open SmileDetector.xcworkspace

When Xcode opens the project, click on run and see the cute hearts raining when you smile at the camera.

Code

In order to get camera frames from the device when using OpenTok, you need to build a CustomCapturer, in this sample that is achieved by ExampleVideoCapture class.

That capturer will call its delegate whenever it has a frame from the camera passing the CVPixelBuffer content of the frame.

For MLKit to recognize the image, we need to convert that CVPixelBuffer to a UIImage, we do that with this code:

extension UIImage {
  convenience init(pixelBuffer: CVPixelBuffer, withRotation rotation: OTVideoOrientation) {
    let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
    let width = CGFloat(CVPixelBufferGetWidth(pixelBuffer))
    let height = CGFloat(CVPixelBufferGetHeight(pixelBuffer))

    let imgRotation: Double = {
        switch (rotation)
        {
        case .up: return 0
        case .right: return .pi / 2
        case .down: return .pi
        case .left: return -.pi / 2
        }
    }()

    var tx = CGAffineTransform(translationX: width/2.0, y: height/2.0)
    tx = tx.rotated(by: CGFloat(imgRotation))
    tx = tx.translatedBy(x: -width/2, y: -height/2)
    let transformed = ciImage.transformed(by: tx)

    let context = CIContext(options: nil)
    let cgImage = context.createCGImage(transformed, from: CGRect(x: 0, y: 0, width: width, height: height))
    self.init(cgImage: cgImage!, scale: 1.0, orientation: .up)
  }
}

It creates the UIImage rotating the Pixel buffer accordingly to the device orientation

Once we have a UIImage, we feed MLKit with it by using:

let img = UIImage(pixelBuffer: frame, withRotation: orientation)
if let detector = self.detector {
    let visionImage = VisionImage(image: img)
    detector.detect(in: visionImage) { (faces, err) in
      ...
    }
}

We will have in faces variable the outcome of the detection.

smiledetector's People

Contributors

robjperez avatar ggarber avatar

Stargazers

stonehye avatar  avatar Michael Westbrooks avatar SmallwolfiOS avatar Jiangang Yang avatar sj avatar  avatar Fabio Dias Rollo avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.