Androidの 顔検出API 笑顔判定 は「笑いながら怒る人」をどう認識するか

こんなのが公開されています.

Google Play サービスでの顔検出 - Google Developer Japan Blog

顔認識をして「笑顔かどうか」の判定もできる.

image01

公式リファレンスをみてみると.

com.google.android.gms.vision.face.Face

public float getIsSmilingProbability ()

Returns a value between 0.0 and 1.0 giving a probability that the face is smiling.

This returns UNCOMPUTED_PROBABILITY if the probability was not computed. The probability is not computed if smile classification is not enabled via setClassificationType(int) or the required landmarks are not found. The LEFT_MOUTH, RIGHT_MOUTH, and NOSE_BASE landmarks are required to compute a smile probability.

Returns
the probability that the face is smiling

画像内の顔が笑顔であるかどうかを0から1の数値で取得できます.

Googleの公開しているサンプルをみてみると,

android-vision/FaceGraphic.java at master · googlesamples/android-vision

...
canvas.drawText("happiness: " + String.format("%.2f", face.getIsSmilingProbability()), x - ID_X_OFFSET, y - ID_Y_OFFSET, mIdPaint);
...

カメラに写っている画像にオーバーレイして「happiness: 0.92」のように「笑顔度」を表示します.
1に近いほど笑顔という判定です.

いくつか画像を試してみました.

20150928-145235

20150928-145057

20150928-145340

20150928-144823

20150928-144744

20150928-144605

20150928-144452

20150928-144225

20150928-144123

ほぼ正しく認識します.

では「笑いながら怒る人」はどうなるか.


20150928-145803

笑顔度 0.96 と判定されました.

声とセリフだけが「怒ってる」んですねこの芸って.


Chrome CustomTabs 使い方 バックグランド処理で高速に

nexus2cee_stuff_thumb

[I/O 2015] Chrome Custom Tabs Will Add Easy And Rich Web Content To Any App With Chrome Capabilities

サービス経由で「ウォームアップ」,「URL先読み」を行って
高速に開く.

公開されている Googleサンプルのうち利用するヘルパークラスなどは
前回エントリを参照.

 

1. バインド

ウォームアップも同時に行う.
アクテビティのライフサイクルに合わせて.

public class SampleActivity extends AppCompatActivity
        implements CustomTabActivityHelper.ConnectionCallback {

    private CustomTabActivityHelper mCustomTabActivityHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mCustomTabActivityHelper = new CustomTabActivityHelper();
        mCustomTabActivityHelper.setConnectionCallback(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
        mCustomTabActivityHelper.bindCustomTabsService(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mCustomTabActivityHelper.unbindCustomTabsService(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCustomTabActivityHelper.setConnectionCallback(null);
    }

    @Override
    public void onCustomTabsConnected() {
    }

    @Override
    public void onCustomTabsDisconnected() {
    }

}

 

2. プリフェッチ

開くであろうURLが確定した時点でバックグランドで先読み.

mCustomTabActivityHelper.mayLaunchUrl(uri, null, null);

 

3. 起動

前回とほぼ同じ.
タイトル, アニメーションを付けてなめらかな体感に.
R.anim.slide_* はサンプルのものを利用.

CustomTabsIntent customTabsIntent =
    new CustomTabsIntent.Builder(mCustomTabActivityHelper.getSession())
        .setShowTitle(true)
        .setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left)
        .setExitAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right)
        .build();
CustomTabActivityHelper.openCustomTab(this, customTabsIntent, uri, new WebViewFallback());

 

以上をまとめてテンプレート的に

public class SampleActivity extends AppCompatActivity
        implements CustomTabActivityHelper.ConnectionCallback {

    private CustomTabActivityHelper mCustomTabActivityHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mCustomTabActivityHelper = new CustomTabActivityHelper();
        mCustomTabActivityHelper.setConnectionCallback(this);

    }

    private void preFetchUrl(Uri uri) {
        mCustomTabActivityHelper.mayLaunchUrl(uri, null, null);
    }

    private void open(Uri uri) {
         CustomTabsIntent customTabsIntent = 
            new CustomTabsIntent.Builder(mCustomTabActivityHelper.getSession())
            .setShowTitle(true)
            .setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left)
            .setExitAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right)
            .build();
         CustomTabActivityHelper.openCustomTab(this, customTabsIntent, uri, new WebViewFallback());
    }

    @Override
    protected void onStart() {
        super.onStart();
        mCustomTabActivityHelper.bindCustomTabsService(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mCustomTabActivityHelper.unbindCustomTabsService(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCustomTabActivityHelper.setConnectionCallback(null);
    }

    @Override
    public void onCustomTabsConnected() {
    }

    @Override
    public void onCustomTabsDisconnected() {
    }

}

イベントからプリフェッチしてからのオープンすればよい.

しかし「先読みのタイミング」て…

(つづく)

Chrome Custom Tabs を とりあえず数行で使う


Chrome Custom Tabs を とりあえず数行で使う

スマホでブラウザを起動する, っての最近では面倒.

LINEとかメッセージでリンクを送ってくる人とか.

20150910-215123

さりげなく開かずにテキトーな会話を続けたりします.

考えてみると「なんかブラウザって重いしトロい」から.

そこで, 最近登場したのが「Chrome Custom Tabs」というしくみ.

Chrome Custom Tabs - Google Chrome

performance

表示までが非常に高速化されてるようにに見える.

実装しようとサンプルをダウンロードしてみる.

GoogleChrome/custom-tabs-client

アプリ内モジュールやヘルパーやプロセス間の通信など
依存関係があるのでわかりづらい.

とりあえずシンプルにURLを渡すだけでで起動してみたいだけなので以下で.

build.gradle

compile 'com.android.support:customtabs:23.0.1'

サンプル内モジュール customtabs 以下は不要.

コピペするクラス

CustomTabsHelper
KeepAliveService
CustomTabActivityHelper
WebViewFallback

shared というモジュールは作らずベタクラスとしてコピペ.
アプリ内モジュール依存はなしで.

起動

...
Uri uri = Uri.parse("http://google.com/");
CustomTabsIntent customTabsIntent =
    new CustomTabsIntent.Builder().build();
CustomTabActivityHelper
    .openCustomTab(this, customTabsIntent, uri, new WebViewFallback());
...

UIカスタマイズはしない. するならここで.

WebViewFallback#openUri()

    ...
    //Intent intent = new Intent(activity, WebViewActivity.class);
    //intent.putExtra(WebActivity.EXTRA_URL, uri.toString());
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(uri);
    activity.startActivity(intent);
    ...

Chromeアプリ (stable/beta/dev) をインストールしてない場合は,
デフォルトブラウザなどに暗黙的に投げる.

で ?

起動はできる.
が, これだけでは恩恵にありつけない.

20150910-224352

サイト内検索などはできるようにはなるが
「なんかブラウザって重いしトロい」が解消せず.

以下の機能があるという.

Pre-warming of the Browser in the background, while avoiding stealing resources from the application.

Providing a likely URL in advance to the browser, which may perform speculative work, speeding up page load time.

これを使わないと意味ないな...

(つづく)