Commit 931157a4 authored by Buwaneka 's avatar Buwaneka

add ImageClassification.java

parent 0c833066
package com.example.count;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import com.example.count.imageclsicifiaction.Classifier;
import com.example.count.imageclsicifiaction.TensorFlowImageClassifier;
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.TimeZone;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class ImageClassification extends Activity {
private static final String MODEL_PATH = "model.tflite";
private static final boolean QUANT = false;
private static final String LABEL_PATH = "labels.txt";
private static final int INPUT_SIZE = 128;
private Classifier classifier;
private Executor executor = Executors.newSingleThreadExecutor();
public ArrayList<Bitmap> byteArrayPicture = new ArrayList<Bitmap>();
public Boolean Othertype=false;
public int OtherImagePosition;
public Boolean DetectOrNot=false;
public Boolean DetectOrNotmodel=false;
public Boolean freeOrNot=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clasifiation_layout);
backgroundCacification newbackGP = new backgroundCacification();
newbackGP.start();
freeOrNot=false;
}
class backgroundCacification extends Thread{
List<Integer> listDitect= new ArrayList<Integer>();
List<Integer> listMarkOnly= new ArrayList<Integer>();
@Override
public void run(){
initTensorFlowAndLoadModel();
while(true){
if (DetectOrNotmodel){
//GlobleVariable.bitmapArrayPicture.size()
for (int i = 0; i <GlobleVariable.bitmapArrayPicture.size() ; i++) {
final Bitmap bmpimageDetected = Bitmap.createScaledBitmap(GlobleVariable.bitmapArrayPicture.get(i), INPUT_SIZE, INPUT_SIZE, false);
final List<Classifier.Recognition> results = classifier.recognizeImage(bmpimageDetected);
if(results.get(0).getTitle().equals("Y")){
listMarkOnly.add(i);
}
final int k=i;
try {
// code runs in a thread
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ImageClassification.this, Integer.toString(k), Toast.LENGTH_SHORT).show();
}
});
} catch (final Exception ex) {
Log.i("---","Exception in thread");
}
}
break;
}
}
int Right=0;
int Wrong=0;
for(int x=0;x<listMarkOnly.size();x++){
if(GlobleVariable.listOriginal.get(listMarkOnly.get(x))==3 || GlobleVariable.listOriginal.get(listMarkOnly.get(x))==4){
Right=Right+1;
}
else{
Wrong=Wrong+1;
}
}
GlobleVariable.RightGloble=Right;
GlobleVariable.WrongGloble=Wrong;
GlobleVariable.resultList.add(Right-Wrong);
startActivity(new Intent(ImageClassification.this, ResultActivity.class));
}
private void initTensorFlowAndLoadModel() {
executor.execute(new Runnable() {
@Override
public void run() {
try {
classifier = TensorFlowImageClassifier.create(
getAssets(),
MODEL_PATH,
LABEL_PATH,
INPUT_SIZE,
QUANT);
DetectOrNotmodel=true;
} catch (final Exception e) {
throw new RuntimeException("Error initializing TensorFlow!", e);
}
}
});
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment