Skip to content

Instantly share code, notes, and snippets.

@nodamu
Last active April 2, 2019 11:12
Show Gist options
  • Save nodamu/3dac072ae3336e14c59997c745cb60f8 to your computer and use it in GitHub Desktop.
Save nodamu/3dac072ae3336e14c59997c745cb60f8 to your computer and use it in GitHub Desktop.

Revisions

  1. nodamu revised this gist Apr 2, 2019. 1 changed file with 17 additions and 3 deletions.
    20 changes: 17 additions & 3 deletions ImageClassifier.java
    Original file line number Diff line number Diff line change
    @@ -14,26 +14,40 @@ protected void onCreate(Bundle savedInstanceState) {
    progressBar = Snackbar.make(imageView,"PROCESSING IMAGE",Snackbar.LENGTH_INDEFINITE);


    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
    PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
    REQUEST_PERMISSION);
    }

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
    PackageManager.PERMISSION_GRANTED){
    ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA},
    REQUEST_PERMISSION);
    }
    final FloatingActionButton predict = (FloatingActionButton) findViewById(R.id.predict);
    predict.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

    // openCameraIntent();
    Intent photoCapIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(photoCapIntent,requestCode);



    }
    });

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(this.requestCode == requestCode && resultCode == RESULT_OK){
    Bitmap bitmap = (Bitmap)data.getExtras().get("data");


    try{

    imageView.setImageBitmap(bitmap);
    @@ -45,6 +59,6 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
    catch (Exception e){

    }

    }
    }
    }
  2. nodamu revised this gist Mar 31, 2019. 1 changed file with 48 additions and 42 deletions.
    90 changes: 48 additions & 42 deletions ImageClassifier.java
    Original file line number Diff line number Diff line change
    @@ -1,44 +1,50 @@
    /** Writes Image data into a {@code ByteBuffer}. */
    private void convertBitmapToByteBuffer(Bitmap bitmap) {
    if (imgData == null) {
    return;
    }
    imgData.rewind();
    bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    // Convert the image to floating point.
    int pixel = 0;
    long startTime = SystemClock.uptimeMillis();
    for (int i = 0; i < getImageSizeX(); ++i) {
    for (int j = 0; j < getImageSizeY(); ++j) {
    final int val = intValues[pixel++];
    addPixelValue(val);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);



    //initialize tensorflow with the AssetManager and the Model
    tf = new TensorFlowInferenceInterface(getAssets(),MODEL_PATH);

    imageView = (ImageView) findViewById(R.id.imageview);
    resultView = (TextView) findViewById(R.id.results);
    progressBar = Snackbar.make(imageView,"PROCESSING IMAGE",Snackbar.LENGTH_INDEFINITE);


    final FloatingActionButton predict = (FloatingActionButton) findViewById(R.id.predict);
    predict.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

    Intent photoCapIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(photoCapIntent,requestCode);



    }
    });

    }
    long endTime = SystemClock.uptimeMillis();
    Log.d(TAG, "Timecost to put values into ByteBuffer: " + Long.toString(endTime - startTime));
    }



    void classifyFrame(Bitmap bitmap, SpannableStringBuilder builder) {
    if (tflite == null) {
    Log.e(TAG, "Image classifier has not been initialized; Skipped.");
    builder.append(new SpannableString("Uninitialized Classifier."));

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(this.requestCode == requestCode && resultCode == RESULT_OK){
    Bitmap bitmap = (Bitmap)data.getExtras().get("data");
    try{

    imageView.setImageBitmap(bitmap);

    progressBar.show();

    predict(bitmap);
    }
    catch (Exception e){

    }
    }
    }
    convertBitmapToByteBuffer(bitmap);
    // Here's where the magic happens!!!
    long startTime = SystemClock.uptimeMillis();
    runInference();
    long endTime = SystemClock.uptimeMillis();
    Log.d(TAG, "Timecost to run model inference: " + Long.toString(endTime - startTime));

    // Smooth the results across frames.
    applyFilter();

    // Print the results.
    printTopKLabels(builder);
    long duration = endTime - startTime;
    SpannableString span = new SpannableString(duration + " ms");
    span.setSpan(new ForegroundColorSpan(android.graphics.Color.LTGRAY), 0, span.length(), 0);
    builder.append(span);
    }
    }
  3. nodamu revised this gist Mar 31, 2019. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions ImageClassifier.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,25 @@
    /** Writes Image data into a {@code ByteBuffer}. */
    private void convertBitmapToByteBuffer(Bitmap bitmap) {
    if (imgData == null) {
    return;
    }
    imgData.rewind();
    bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    // Convert the image to floating point.
    int pixel = 0;
    long startTime = SystemClock.uptimeMillis();
    for (int i = 0; i < getImageSizeX(); ++i) {
    for (int j = 0; j < getImageSizeY(); ++j) {
    final int val = intValues[pixel++];
    addPixelValue(val);
    }
    }
    long endTime = SystemClock.uptimeMillis();
    Log.d(TAG, "Timecost to put values into ByteBuffer: " + Long.toString(endTime - startTime));
    }



    void classifyFrame(Bitmap bitmap, SpannableStringBuilder builder) {
    if (tflite == null) {
    Log.e(TAG, "Image classifier has not been initialized; Skipped.");
  4. nodamu renamed this gist Mar 31, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. nodamu created this gist Mar 31, 2019.
    22 changes: 22 additions & 0 deletions .java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    void classifyFrame(Bitmap bitmap, SpannableStringBuilder builder) {
    if (tflite == null) {
    Log.e(TAG, "Image classifier has not been initialized; Skipped.");
    builder.append(new SpannableString("Uninitialized Classifier."));
    }
    convertBitmapToByteBuffer(bitmap);
    // Here's where the magic happens!!!
    long startTime = SystemClock.uptimeMillis();
    runInference();
    long endTime = SystemClock.uptimeMillis();
    Log.d(TAG, "Timecost to run model inference: " + Long.toString(endTime - startTime));

    // Smooth the results across frames.
    applyFilter();

    // Print the results.
    printTopKLabels(builder);
    long duration = endTime - startTime;
    SpannableString span = new SpannableString(duration + " ms");
    span.setSpan(new ForegroundColorSpan(android.graphics.Color.LTGRAY), 0, span.length(), 0);
    builder.append(span);
    }