iOS Revisited: CoreML

Hot

Post Top Ad

Showing posts with label CoreML. Show all posts
Showing posts with label CoreML. Show all posts

5 Aug 2017

Face Detection using Vision Framework - Swift, iOS11

8/05/2017 03:36:00 am 1

Face Detection using Vision Framework - Swift, iOS11


In iOS 11 Apple provide frameworks for specific areas. We will dive into Vision API. Using Vision framework tools we can process image or video to detect and recognize face, detect barcode, detect text, detect and track object, etc.

For detecting objects using Machine Learning Image Analysis follow this link - Real Time Camera Object Detection with Machine Learning - CoreML: Swift 4

In this article, we will bash out face detection. In vision API there are three roles.

Getting Started:

1. Request :

     Ex: VNDetectFaceRectanglesRequest to detect face in an image.

2. Request handler :

    Ex: VNImageRequestHandler, VNSequenceRequestHandler. VNImageRequestHandler for single image and VNSequenceRequestHandler is for a sequence of multiple images.

3. Observation :

     Provide informations like bounding box.


First create a new project -> open Xcode -> File -> New -> Project -> Single View App, then tap next button. Type product name as 'Face Recognization' then tap next and select the folder to save project.

Before start, download one sample image with faces and add to Assests.xcassets and name it as 'sample1'.

Now it's time to start writing a code. Open ViewController.swift add this line in viewDidLoad() method. 


 guard let image = UIImage(named: "sample1") else {
    return
}

Here image is an UIImage object used to detect faces. Then for displaying image add UIImageView as subview. Add the following code in viewDidLoad() method after else part.

 /........

let scaledHeight = view.frame.width / image.size.width * image.size.height
let imageView = UIImageView(image: image)
imageView.frame = CGRect(x: 0, y: 20, width: view.frame.width, height: scaledHeight)
view.addSubview(imageView)
Here scaledHeight is the imageView height calculated from ratio of device width and image size.

Now Build and Run , You will see image with aspect size based on device size.




We start with Vision API to detect face rectangles. For that we need to import vision add below 'import UIKit'.

 import Vision

As we mentioned earlier we are using three roles in this face detection. First we are going to create request using VNDetectFaceRectanglesRequest. Second step is to use request handler in this we are analyzing single image so we will use VNImageRequestHandler. This is an asynchronous so it's better to put VNImageRequestHandler in background thread. Third one Observations, we will use inside request completion handler. Let implement everything using code. Add the following code to the end  of viewDidLoad() method.

 /........

let request = VNDetectFaceRectanglesRequest { (req, error) in
    if let error = error  {
        print("Failed to detect faces",error)
        return
    }
    print(req.results)
}

guard let cgImage = image.cgImage else {
    return
}

DispatchQueue.global(qos: .background).async {
    let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
    
    do {
        try handler.perform([request])
    } catch let reqError {
        print("Error in req",reqError)
    }
}

Execution starts from VNDetectFaceRectanglesRequest after that it will not call completion handler. Then the flow goes to cgImage -> handler then we will perform request on handler. If the request succeded then it calls VNDetectFaceRectanglesRequest completion handler. It will analyze image and give results as array of VNFaceObservation.

Now Build and Run , Great You will see VNFaceObservation object in console.


Finally, we are getting rectangle. Parse VNFaceObservation to draw rectangles on detected faces in an image. For that copy the following lines of code and replace the line

'print(req.results)'.

guard let observations = req.results as? [VNFaceObservation]
    else { fatalError("unexpected result type") }



observations.forEach({ (observation) in
    DispatchQueue.main.async {
        print(observation.boundingBox)
        let x = self.view.frame.width * observation.boundingBox.origin.x
        let width = self.view.frame.width * observation.boundingBox.size.width
        let height = scaledHeight * observation.boundingBox.size.height
        let y = scaledHeight * (1 - observation.boundingBox.origin.y) - height
        let redSquare = UIView()
        redSquare.backgroundColor = UIColor.clear
        redSquare.layer.borderColor = UIColor.red.cgColor
        redSquare.layer.borderWidth = 2.0
        redSquare.frame = CGRect(x: x, y: y, width: width, height: height)
        self.view.addSubview(redSquare)
    }
})
Now Build and Run , Great detected faces in an image with red borders.


Download sample project with examples :

Read More

1 Aug 2017

Real Time Camera Object Detection with Machine Learning - CoreML: Swift 4

8/01/2017 04:05:00 am 1
This iOS machine learning tutorial will introduce you to Core ML and Vision, two brand-new frameworks introduced in iOS 11. For this we need Xcode 9 or greater, iOS 11 or greater.


Getting Started:

First create a new project -> open Xcode -> File -> New -> Project -> Single View Application, then tap next button. Type product name as 'Object Detector' then tap next and select the folder to save project.

For detecting objects we need to access device camera. Open ViewController.swift and import this framework, just below 'import UIKit'.

import AVKit
Now its time to write some code, open ViewController.swift and inside viewDidLoad() method write the following code to access camera.

 let captureSession = AVCaptureSession()
 captureSession.sessionPreset = .photo
 guard let captureDevice = AVCaptureDevice.default(for: .video) else {
      return
 }
 guard let input = try? AVCaptureDeviceInput(device: captureDevice) else {
      return
 }
 captureSession.addInput(input)
 captureSession.startRunning()
Build and run, Ouchh app crashes. No worries we need to add cameraUsageDescription in info.plist. Open info.plist add 'Privacy - Camera Usage Description' and description as 'We need to access camera for detecting objects'.

 
Now build and run. Great we see camera permission alert then tap ok.



We need to add camera to the view for that create 'PreviewLayer' as the following. Add these lines of code after 'captureSession.startRunning()' line in viewDidLoad().

let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
view.layer.addSublayer(previewLayer)
previewLayer.frame = view.frame
Build and run , now you can see camera running on your device.

Great, now for detecting object we need image containg object for that we need to get frames from the camera. So use the following code at the end of viewDidLoad().

let dataOutput = AVCaptureVideoDataOutput()
dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
captureSession.addOutput(dataOutput)
Add 'AVCaptureVideoDataOutputSampleBufferDelegate' delgate to the ViewController.swift class.

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
.....
}
 Add delegate method, it will call every time when camera is going to capture a frame.

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

}
Now it's time to start using 'Machine Learning'. Open ViewController.swift and import this framework, just below 'import AVKit'.

import Vision
Go to this url 'https://developer.apple.com/machine-learning/' and download Resnet50 file. Drag that ML file to our project.

Add the following code inside delegate method.

guard let pixelBuffer : CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
return }

guard let model = try? VNCoreMLModel(for: Resnet50().model) else {
return }
let request = VNCoreMLRequest(model: model) { (finishedReq, error) in
print("results ==",finishedReq.results)
}

try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])
VNImageRequestHandler will perform all operation on image using VNCoreMLRequest.

VNCoreMLRequest accepts a VNCoreMLModel, here our model is Resnet50 model.

Build and Run, you will see the output in console with VNClassificationObservation objects.



Great we are getting some data. Lets parse the data using following code.
Replace 'print("results ==",finishedReq.results)' with the code below.


guard let results = finishedReq.results as? [VNClassificationObservation] else {
return
}

guard let firstObservation = results.first else {
return
}

print(firstObservation.identifier, firstObservation.confidence)
Build and Run the project we will see detected objects with confidence in console.



Download sample project with examples :

Read More

Post Top Ad