Slide 1

Slide 1 text

Machine Learning for Android Developers Moyinoluwa Adeyemi November 25, 2016

Slide 2

Slide 2 text

Android @SwiftaSystems GDG Lagos Co-organizer Android Developer Nanodegree Graduate Half Marathoner

Slide 3

Slide 3 text

How Can You Get Started with Machine Learning? Three ways, with varying complexity: (1) Use a Cloud-based or Mobile API (Vision, Natural Language, etc.) (2) Use an existing model architecture, and retrain it or fine tune on your dataset (3) Develop your own machine learning models for new problems More flexible, but more effort required

Slide 4

Slide 4 text

Mobile Vision API Providing on-device vision for applications

Slide 5

Slide 5 text

Face API faces, facial landmarks, eyes open, smiling Barcode API 1D and 2D barcodes Text API Latin-based text / structure Common Mobile Vision API Support for fast image and video on-device detection and tracking. NEW!

Slide 6

Slide 6 text

Googly Eyes Android App Video credit Google 1. Create a face detector for facial landmarks (e.g., eyes) 3. For each face, draw the eyes FaceDetector detector = new FaceDetector.Builder() .setLandmarkType(FaceDetector.ALL_LANDMARKS) .build(); SparseArray faces = detector.detect(image); for (int i = 0; i < faces.size(); ++i) { Face face = faces.valueAt(i); for (Landmark landmark : face.getLandmarks()) { // Draw eyes 2. Detect faces in the image

Slide 7

Slide 7 text

Face API Photo credit developers.google.com/vision

Slide 8

Slide 8 text

Easy to use Java API image detected items Detector 1. Create a detector object 2. detectedItems = detector.detect(image) Photo credit developers.google.com/vision

Slide 9

Slide 9 text

Text Detection Latin based language Understand text structure Photo credit Getty Images

Slide 10

Slide 10 text

Text Structure Blocks Lines Words Lines Words Words Words

Slide 11

Slide 11 text

Barcode Detection 1D barcodes EAN-13/8 UPC-A/E Code-39/93/128 ITF Codabar 2D barcodes QR Code Data Matrix PDF-417 AZTEC UPC DataMatrix QR Code PDF 417 Video and image credit Google

Slide 12

Slide 12 text

Combined Vision & Translation

Slide 13

Slide 13 text

Code Walkthrough

Slide 14

Slide 14 text

Import the Google Play Services SDK for the Mobile Vision API compile 'com.google.android.gms:play-services-vision:9.8.0'

Slide 15

Slide 15 text

Face Detection manifest file meta-data

Slide 16

Slide 16 text

Barcode Detection manifest file meta-data

Slide 17

Slide 17 text

Text Recognition manifest file meta-data

Slide 18

Slide 18 text

Create simple layout with static image Process

Slide 19

Slide 19 text

Convert the static image into a Bitmap Bitmap barcodeBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cute_cat_image);

Slide 20

Slide 20 text

Using the FaceDetector API FaceDetector faceDetector = new FaceDetector.Builder(this) .setTrackingEnabled(false) .setLandmarkType(FaceDetector.ALL_LANDMARKS) .build();

Slide 21

Slide 21 text

Using the BarcodeDetector API BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this) .setBarcodeFormats(Barcode.ALL_FORMATS) .build();

Slide 22

Slide 22 text

Using the TextRecognizer API TextRecognizer textRecognizer = new TextRecognizer.Builder(this) .build();

Slide 23

Slide 23 text

Check if the face detector is ready if (!faceDetector.isOperational()) { new AlertDialog.Builder(this) .setMessage("Face Detector could not be set up on your device") .show(); return; }

Slide 24

Slide 24 text

Check if the barcode detector is ready if (!barcodeDetector.isOperational()) { new AlertDialog.Builder(this) .setMessage("Barcode Detector could not be set up on your device") .show(); return; }

Slide 25

Slide 25 text

Check if the text recognizer is ready if (!textRecognizer.isOperational()) { new AlertDialog.Builder(this) .setMessage("Text Recognizer could not be set up on your device") .show(); return; }

Slide 26

Slide 26 text

Create a frame using the face bitmap Frame frame = new Frame.Builder().setBitmap(textBitmap).build(); SparseArray face = faceDetector.detect(frame);

Slide 27

Slide 27 text

Create a frame using the barcode bitmap Frame frame = new Frame.Builder().setBitmap(textBitmap).build(); SparseArray barcode = barcodeDetector.detect(frame);

Slide 28

Slide 28 text

Create a frame using the text bitmap Frame frame = new Frame.Builder().setBitmap(textBitmap).build(); SparseArray text = textRecognizer.detect(frame);

Slide 29

Slide 29 text

Use the values from the SparseArray for (int i = 0; i < sparseArray.size(); i++) { Face face = sparseArray.valueAt(i); float left = face.getPosition().x; float top = face.getPosition().y; float right = left + face.getWidth(); float bottom = right + face.getHeight(); float cornerRadius = 2.0f; RectF rectF = new RectF(left, top, right, bottom); canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, rectPaint); }

Slide 30

Slide 30 text

Use the values from the SparseArray for (int i = 0; i < sparseArray.size(); i++) { Face face = sparseArray.valueAt(i); float left = face.getPosition().x; float top = face.getPosition().y; float right = left + face.getWidth(); float bottom = right + face.getHeight(); float cornerRadius = 2.0f; RectF rectF = new RectF(left, top, right, bottom); canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, rectPaint); }

Slide 31

Slide 31 text

Use the values from the SparseArray if (size == 0) { textView.setText("No information available"); } else { for (int i = 0; i < size; i++) { barcodeValue += (barcode.valueAt(i).displayValue + "\n"); } textView.setText(barcodeValue); }

Slide 32

Slide 32 text

for (int i = 0; i < text.size(); i++) { TextBlock textBlock = text.valueAt(i); if (textBlock != null && textBlock.getValue() != null) { detectedText += textBlock.getValue(); } detectedTextView.setText(detectedText); } Use the values from the SparseArray

Slide 33

Slide 33 text

faceDetector.release(); Remember to release resources

Slide 34

Slide 34 text

barcodeDetector.release(); Remember to release resources

Slide 35

Slide 35 text

textRecognizer.release(); Remember to release resources

Slide 36

Slide 36 text

Results - Face Detection API

Slide 37

Slide 37 text

Results - Barcode Detection API

Slide 38

Slide 38 text

Results - Text Recognition API

Slide 39

Slide 39 text

Mobile Vision: Codelabs and Samples Googly Eyes Code Sample github.com/googlesamples/android-vision/tree/master/visionSamples/googly-eyes Codelabs codelabs.developers.google.com/codelabs/face-detection/ codelabs.developers.google.com/codelabs/bar-codes/ codelabs.developers.google.com/codelabs/mobile-vision-ocr/ Mobile Vision Developers developers.google.com/vision/ GitHub Code Samples github.com/googlesamples/android-vision Stack Overflow Find and ask questions under the android-vision tag.

Slide 40

Slide 40 text

Questions?

Slide 41

Slide 41 text

@moyheen @moyinoluwa @moyheen Thank you! Moyinoluwa Adeyemi Android @SwiftaSystems GDG Lagos co-organizer