Coder Social home page Coder Social logo

kairosinc / kairos-sdk-android Goto Github PK

View Code? Open in Web Editor NEW
2.0 5.0 0.0 447 KB

The Android client for the Kairos Face Recognition API. Includes example Facial Recognition project and SDK jar library.

Home Page: https://www.kairos.com/face-recognition-api

License: MIT License

Java 100.00%

kairos-sdk-android's Introduction

Kairos SDK (Android)

Kairos is the easist way add Face-Recognition to your Android apps. Our API provides a full-featured and robust Face-Recognition backend, right out of the box. This is the Android client for the Kairos Facial Recognition API. The package includes both the SDK .jar file, as well as an example app project (IntelliJ IDEA). Continue reading to learn how to integrate Kairos into your Android apps.

We also have official SDKs for iOS, Javascript and PHP.

*Thanks to fan contributions, we also have unofficial wrappers for Ruby and .NET. do

What You'll Need

  • An Android app project
  • Internet connectivity for your device or emulator

Limitations in this Version

This is an early version and has a few limitations. First of which, the SDK is bundled with the loopj network library (v 1.4.6) and will cause conflicts if your android app also uses loopj. Please use a different network library to avoid conflicts, if possible. Secondly, this version does not provide any camera views for image capture -- although this feature is on the roadmap for a near-future update. Finally, the example app project included was built using an IDE called IntelliJ IDEA. If you use a different IDE you'll need to move the source code files manually into your project.


How to do a Quick Demo

If you just want to do a quick test run, open the example app project (Intellij IDEA) that is included with the SDK and follow these steps:

  1. Create your free developer account
  2. Log into the Kairos Developer Dashboard
  3. Create an application and copy your App Id & App Key
  4. Paste them into the lines above the authentication method in MyActivity.java
  5. Run the app on your device, and watch for the API response in the log output.

How to Install Kairos in your own App

  1. Create your free Kairos developer account if you don't already have one.
  2. Log into the dashboard and create a new app.
  3. Copy your App ID & App Key (you'll need them later).
  4. Download the SDK and unzip the package.
  5. Navigate to the 'libs' folder which contains the SDK library file KairosSDK.jar.
  6. Add the SDK library file (KairosSDK.jar) into your Android project.
  7. Import the library files wherever you want to use the SDK
import com.kairos.*;

Authenticate Once

Before you can make API calls you'll need to create a Kairos object and set your credentials App Id and App Key (You only need to do this once). Add your App Id and App Key into the setAuthentication() method like so:

// instantiate a new kairos instance
Kairos myKairos = new Kairos();

// set authentication
String app_id = "your_app_id";
String api_key = "your_application_key";
myKairos.setAuthentication(this, app_id, api_key);

The KairosListener

Before we can start calling methods, we'll need a place to receive notifications and data when the methods return. For this purpose, the SDK provides a listener class to return the results of all asynchronous method calls. A Kairos listener is like a callback method. Instantiate a listener like so, and pass the instance into your desired kairos method. You can add your own code inside of the provided success and fail code blocks and use the provided response object as you like:

// Create an instance of the KairosListener
KairosListener listener = new KairosListener() {

  @Override
  public void onSuccess(String response) {
     // your code here!
     Log.d("KAIROS DEMO", response);
  }

  @Override
  public void onFail(String response) {
     // your code here!
     Log.d("KAIROS DEMO", response);
  }
};

The Detect Method

The detect method returns you detailed information on face attributes for any images you send it. It can provide you with coordinates of features such as eyes, nose, mouth, etc. See the examples below:

###Parameters:

  • Bitmap image, or String image
  • String selector (optional: can be null)
  • String minHeadScale (optional: can be null)
  • KairosListener listener
  • (See the API Docs for details on what the parameters do)

###Returns:

  • String response (face attribute data)

###Example Usage:

// Bare-essentials Example:
// This example uses only an image url, setting optional params to null
String image = "http://media.kairos.com/liz.jpg";
myKairos.detect(image, null, null, listener);


// Fine-grained Example:  
// This example uses a bitmap image and also optional parameters
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.liz);
String selector = "FULL";
String minHeadScale = "0.25"; 
myKairos.detect(image, selector, minHeadScale, listener);
            

The Enroll Method

The enroll methods let you register a face for later recognition. You can enroll a new face, or enroll different photos of an already enrolled for better recognition accuracy. See the examples below: ###Parameters:

  • Bitmap image, or String image
  • String galleryId
  • String subjectId
  • String selector (optional: can be null)
  • String multipleFaces (optional: can be null)
  • String minHeadScale (optional: can be null)
  • KairosListener listener
  • (See the API Docs for details on what the parameters do)

###Returns:

  • String response (details of the enroll request)

###Example Usage:

// Bare-essentials Example:
// This example uses only an image url, setting optional params to null
String image = "http://media.kairos.com/liz.jpg";
String subjectId = "Elizabeth";
String galleryId = "friends";
myKairos.enroll(image, subjectId, galleryId, null, null, null, listener);
            
         
// Fine-grained Example:     
// This example uses a bitmap image and also optional parameters
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.liz);
String subjectId = "Elizabeth";
String galleryId = "friends";
String selector = "FULL";
String multipleFaces = "false";
String minHeadScale = "0.25";
myKairos.enroll(image, 
				subjectId, 
				galleryId, 
				selector, 
				multipleFaces, 
				minHeadScale, 
				listener);
            

The Recognize Method

The recognize method attempts to recognize an image against a specified gallery, and returns a list of subjectIds and their confidence values as possible matches. For best recognition accuracy, it's reccomended that you have multiple enrollments (different images) per face. See examples below:

###Parameters:

  • Bitmap image, or String image
  • String galleryId
  • String selector (optional: can be null)
  • String threshold (optional: can be null)
  • String minHeadScale (optional: can be null)
  • String maxNumResults (optional: can be null)
  • KairosListener listener
  • (See the API Docs for details on what the parameters do)

###Returns:

  • String response (list of possible matching subjects and their confidence values)

###Example Usage:

// Bare-essentials Example:
// This example uses only an image url, setting optional params to null
String image = "http://media.kairos.com/liz.jpg";
String galleryId = "friends";
myKairos.recognize(image, galleryId, null, null, null, null, listener);
            
        
// Fine-grained Example:    
// This example uses a bitmap image and also optional parameters
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.liz);
String galleryId = "friends";
String selector = "FULL";
String threshold = "0.75";
String minHeadScale = "0.25";
String maxNumResults = "25";
myKairos.recognize(image, 
				    galleryId, 
				    selector, 
				    threshold, 
				    minHeadScale, 
				    maxNumResults, 
				    listener);
            

List All Galleries

This method lists out all galleries you have created.

###Parameters:

  • KairosListener listener

###Returns:

  • String response (list of all galleries)

###Example Usage:

// List out all your galleries
myKairos.listGalleries(listener);
            

List All Subjects in a Gallery

This method lists out all subjects enrolled in a given gallery.

###Parameters:

  • String galleryId
  • KairosListener listener

###Returns:

  • String response (list of all subjects in a given gallery)

###Example Usage:

// List out all subjects in a given gallery
String galleryId = "friends";
myKairos.listSubjectsForGallery(galleryId, listener);
            

Delete a Gallery

This method deletes a given gallery and all of its subjects.

###Parameters:

  • String galleryId
  • KairosListener listener

###Returns:

  • String response (confirmation of the results of the request)

###Example Usage:

// List out all subjects in a given gallery
String galleryId = "friends";
myKairos.deleteGallery(galleryId, listener);
            

Delete a Subject from a Gallery

This method deletes a given subject. All enrollments for the given subject will be removed from the gallery.

###Parameters:

  • String subjectId
  • String galleryId
  • KairosListener listener

###Returns:

  • String response (confirmation of the results of the request)

###Example Usage:

// List out all subjects in a given gallery
String subjectId = "Tyrion";
String galleryId = "friends";
myKairos.deleteSubject(subjectId, galleryId, listener);
            

View the Examples

Also see provided examples included in the example project. It contains clear examples on how to use all of the available methods in the file MyActivity.java. Just make sure to plug in your application credentials (Create an account if you don't have one). Also, check out the API documentation at https://www.kairos.com/docs/face-recognition

Stack Share


Source Code

Note: We have added the raw source code to the SDK so that you can make any changes / modifications as needed. In the future we plan to add better documentation around the source code, build instructions, etc. but we wanted to get it out there as soon as possible as requested by a number of our customers. Feel free to use / modify / redistribute this as needed in your apps.

If you see anything you'd like to change, fix, etc. please feel free to submit a pull request, that we will happily accept.


##Support Have an issue? Visit our Support page or create an issue on GitHub

kairos-sdk-android's People

Contributors

coleca avatar dr-xperience avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

kairos-sdk-android's Issues

only matching images ..different images with same face are not recogizing

In my demo I am adding one image of mine using enroll api with subject id "test".
Now I am again choose same image from gallery and call recognize api ,it's giving me proper response.

But I choose second image of mine (i.e faces of both images are same) but now recognize api giving now search found.pls help about this

java.net.SocketTimeoutException

I'm getting a java.net.SocketTimeoutException. Previous requests and calls done with kairos have been successful. When I'm deleting a user from a particular gallery, all is functioning well. It appears the java.net.SocketTimeoutException appears only when I try to Verify or Enroll images.

What could be the cause?

Backup Registered Faces

Is there any function to backup registered faces? In case application will be used in another devices.

only matching images ..different images with same face are not recogizing

I am adding image in gallery by getting image from gallery using enroll api.
Now for recognition I am using recognition api.If I pick same image from gallery ,Its giving me result properly.But I pick other image of same person (That is face is same as image i have enrolled) ,its giving me no result found .please help.

Run project

How do you run the project? There isn't a build.gradle file.
How should I set up the build.gradle?

Cannot catch JSON response

Enroll request is successful, I see it on Kairos Dashboard. Unfortunately I cannot catch JSON formatted response. I use this class for connection:

public class Connection {
    URL url1 = null;
    HttpURLConnection httpURLConnection= null;
    BufferedReader reader;
    String json=null;
    public String connect(String url){
        try {
            url1= new URL(url);
            httpURLConnection=(HttpURLConnection)url1.openConnection();
            httpURLConnection.connect();
            InputStream in = httpURLConnection.getInputStream();
            reader=new BufferedReader(new InputStreamReader(in));
            StringBuffer buffer= new StringBuffer();
            String line="";
            while((line=reader.readLine())!=null){
                buffer.append(line);
            }
            json=buffer.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(httpURLConnection!=null){
                httpURLConnection.disconnect();
            }
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return json;
    }
}

Call:

        Connection con = new Connection();
        parsedString = con.connect("https://api.kairos.com/enroll");

And this for parsing:

        try {
            JSONObject jsonRootObject = new JSONObject(text);
            JSONArray jsonArray = jsonRootObject.optJSONArray("images");

            for(int i=0; i < jsonArray.length(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                String status = jsonObject.optString("status").toString();
                String face_id = jsonObject.optString("face_id").toString();

                toast(status);
                toast(face_id);
            }
        } catch (JSONException e){
            e.printStackTrace();
        }

I get always NullPointerException for the text string.

Handling Network errors

Hi

I tried to enroll a face when the network is turned off (Its works perfect when the network is ON). I get a NullPointerException and app crashes. I've put try-catch around enroll method but I it's still the same. Is there a way to handle this exception?

My Code:


if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
            photo = (Bitmap) data.getExtras().get("data");
            String subjectId = nameInput.getText().toString();
            String galleryId = serialNo;
            String selector = "FULL";
            String multipleFaces = "true";
            String minHeadScale = "0.25";
            try {
                myKairos.enroll(photo,
                        subjectId,
                        galleryId,
                        selector,
                        multipleFaces,
                        minHeadScale,
                        listener);
            } catch (JSONException | UnsupportedEncodingException | NullPointerException e ) {
                e.printStackTrace();
            }
        }

Runtime error

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.vaddi.hemanth.oyna, PID: 11180
                                                                        java.lang.NullPointerException: Attempt to get length of null array
                                                                            at java.lang.StringFactory.newStringFromBytes(StringFactory.java:45)
                                                                            at com.kairos.Kairos$8.onFailure(Kairos.java:464)
                                                                            at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:319)
                                                                            at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:138)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

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.