Last active
April 2, 2019 11:12
-
-
Save nodamu/3dac072ae3336e14c59997c745cb60f8 to your computer and use it in GitHub Desktop.
Revisions
-
nodamu revised this gist
Apr 2, 2019 . 1 changed file with 17 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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){ } } } -
nodamu revised this gist
Mar 31, 2019 . 1 changed file with 48 additions and 42 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,44 +1,50 @@ @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); } }); } @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){ } } } } -
nodamu revised this gist
Mar 31, 2019 . 1 changed file with 22 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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."); -
nodamu renamed this gist
Mar 31, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
nodamu created this gist
Mar 31, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); }