Home | History | Annotate | Download | only in demo
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 package org.tensorflow.demo;
     17 
     18 import android.content.Context;
     19 import android.graphics.Canvas;
     20 import android.graphics.Paint;
     21 import android.util.AttributeSet;
     22 import android.util.TypedValue;
     23 import android.view.View;
     24 
     25 import org.tensorflow.demo.Classifier.Recognition;
     26 
     27 import java.util.List;
     28 
     29 public class RecognitionScoreView extends View implements ResultsView {
     30   private static final float TEXT_SIZE_DIP = 24;
     31   private List<Recognition> results;
     32   private final float textSizePx;
     33   private final Paint fgPaint;
     34   private final Paint bgPaint;
     35 
     36   public RecognitionScoreView(final Context context, final AttributeSet set) {
     37     super(context, set);
     38 
     39     textSizePx =
     40         TypedValue.applyDimension(
     41             TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE_DIP, getResources().getDisplayMetrics());
     42     fgPaint = new Paint();
     43     fgPaint.setTextSize(textSizePx);
     44 
     45     bgPaint = new Paint();
     46     bgPaint.setColor(0xcc4285f4);
     47   }
     48 
     49   @Override
     50   public void setResults(final List<Recognition> results) {
     51     this.results = results;
     52     postInvalidate();
     53   }
     54 
     55   @Override
     56   public void onDraw(final Canvas canvas) {
     57     final int x = 10;
     58     int y = (int) (fgPaint.getTextSize() * 1.5f);
     59 
     60     canvas.drawPaint(bgPaint);
     61 
     62     if (results != null) {
     63       for (final Recognition recog : results) {
     64         canvas.drawText(recog.getTitle() + ": " + recog.getConfidence(), x, y, fgPaint);
     65         y += fgPaint.getTextSize() * 1.5f;
     66       }
     67     }
     68   }
     69 }
     70