void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
// dependencies {
// implementation 'org.tensorflow:tensorflow-lite:2.5.0'
// implementation 'org.tensorflow:tensorflow-lite-support:0.2.0'
// }
// package com.example.teachablemachineapp;
// import android.graphics.Bitmap;
// import android.os.Bundle;
// import android.util.Log;
// import android.view.View;
// import android.widget.Button;
// import android.widget.ImageView;
// import androidx.appcompat.app.AppCompatActivity;
// import org.tensorflow.lite.Interpreter;
// import org.tensorflow.lite.support.image.TensorImage;
// import org.tensorflow.lite.support.label.TensorLabel;
// import java.io.BufferedReader;
// import java.io.FileInputStream;
// import java.io.FileNotFoundException;
// import java.io.IOException;
// import java.io.InputStreamReader;
// import java.nio.ByteBuffer;
// import java.nio.ByteOrder;
// import java.nio.channels.FileChannel;
// import java.util.ArrayList;
// import java.util.List;
// public class MainActivity extends AppCompatActivity {
// private static final String MODEL_PATH = "model.tflite";
// private static final String LABELS_PATH = "labels.txt";
// private static final int INPUT_SIZE = 224;
// private Interpreter interpreter;
// private List<String> labels;
// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
// ImageView imageView = findViewById(R.id.imageView);
// Button classifyButton = findViewById(R.id.classifyButton);
// try {
// interpreter = new Interpreter(loadModelFile(MODEL_PATH));
// labels = loadLabelFile(LABELS_PATH);
// } catch (IOException e) {
// e.printStackTrace();
// }
// classifyButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// // Get the image from ImageView
// imageView.setDrawingCacheEnabled(true);
// Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
// imageView.setDrawingCacheEnabled(false);
// // Call the classifyImage method
// classifyImage(bitmap);
// }
// });
// }
// private ByteBuffer loadModelFile(String filePath) throws IOException {
// try (FileInputStream fileInputStream = new FileInputStream(getAssets().openFd(filePath).getFileDescriptor());
// FileChannel fileChannel = fileInputStream.getChannel()) {
// long startOffset = getAssets().openFd(filePath).getStartOffset();
// long declaredLength = getAssets().openFd(filePath).getDeclaredLength();
// return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength).order(ByteOrder.nativeOrder());
// }
// }
// private List<String> loadLabelFile(String filePath) throws IOException {
// List<String> labels = new ArrayList<>();
// BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open(filePath)));
// String line;
// while ((line = reader.readLine()) != null) {
// labels.add(line);
// }
// return labels;
// }
// private TensorImage convertBitmapToTensorImage(Bitmap bitmap) {
// Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, INPUT_SIZE, INPUT_SIZE, false);
// ByteBuffer byteBuffer = ByteBuffer.allocateDirect(INPUT_SIZE * INPUT_SIZE * 3 * 4);
// byteBuffer.order(ByteOrder.nativeOrder());
// resizedBitmap.copyPixelsToBuffer(byteBuffer);
// TensorImage tensorImage = new TensorImage();
// tensorImage.load(byteBuffer);
// return tensorImage;
// }
// private void classifyImage(Bitmap bitmap) {
// TensorImage tensorImage = convertBitmapToTensorImage(bitmap);
// TensorImage inputImageBuffer = TensorImage.fromBitmap(bitmap);
// float[][] output = new float[1][labels.size()];
// interpreter.run(inputImageBuffer.getBuffer(), output);
// for (int i = 0; i < labels.size(); i++) {
// Log.d("Classification", labels.get(i) + ": " + output[0][i]);
// }
// }
// }
// <?xml version="1.0" encoding="utf-8"?>
// <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
// xmlns:tools="http://schemas.android.com/tools"
// android:layout_width="match_parent"
// android:layout_height="match_parent"
// tools:context=".MainActivity">
// <ImageView
// android:id="@+id/imageView"
// android:layout_width="wrap_content"
// android:layout_height="wrap_content"
// android:layout_centerInParent="true"
// android:contentDescription="@string/image_desc"
// android:src="@drawable/ic_launcher_foreground" />
// <Button
// android:id="@+id/classifyButton"
// android:layout_width="wrap_content"
// android:layout_height="wrap_content"
// android:layout_below="@id/imageView"
// android:layout_centerHorizontal="true"
// android:layout_marginTop="16dp"
// android:text="@string/classify_button_text" />
// </RelativeLayout>