Skip to content

Commit

Permalink
For SUbmission
Browse files Browse the repository at this point in the history
  • Loading branch information
thirdygayares committed Dec 12, 2023
1 parent b83d398 commit 1bdaa97
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 148 deletions.
8 changes: 5 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
android:supportsRtl="true"
android:theme="@style/Theme.Motion_2"
android:debuggable="true"

android:largeHeap="true"
android:hardwareAccelerated="true"
tools:targetApi="31">
<activity
android:name="com.finquant.Activity.Fish_count_list2"
Expand Down Expand Up @@ -75,12 +76,13 @@


<activity
android:name="com.finquant.Activity.SplashActivity"
android:name="com.finquant.Yolov5.DetectorActivity"

android:theme="@style/AppTheme"
android:exported="false" />

<activity
android:name="com.finquant.Yolov5.DetectorActivity"
android:name="com.finquant.Activity.SplashActivity"
android:exported="true"
android:theme="@style/AppTheme"

Expand Down
Binary file modified app/src/main/assets/best-fp16.tflite
Binary file not shown.
8 changes: 1 addition & 7 deletions app/src/main/java/com/finquant/Utils/Dialog_utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,7 @@ public void onClick(DialogPlus dialog, View view) {
activity.overridePendingTransition(0, 0);
countAct.finishActivity();
} else if (view.getId() == R.id.cancelButton) {
countAct.reinitializeCamera();
Intent i = new Intent(activity.getApplicationContext(), front_page.class);
countAct.reinitializeCamera();
countAct.onCameraViewStopped();
activity.startActivity(i);
activity.overridePendingTransition(0, 0);
countAct.finishActivity();
dialog.dismiss();
}
}
})
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/finquant/Yolov5/CameraActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
Expand Down Expand Up @@ -109,6 +110,7 @@ public abstract class CameraActivity extends AppCompatActivity
int currentModel = -1;
int currentNumThreads = -1;

private Button btnCount;
ArrayList<String> deviceStrings = new ArrayList<String>();

@Override
Expand All @@ -119,6 +121,7 @@ protected void onCreate(final Bundle savedInstanceState) {

setContentView(R.layout.tfe_od_activity_camera);
Toolbar toolbar = findViewById(R.id.toolbar);
btnCount = findViewById(R.id.btnCount);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);

Expand Down Expand Up @@ -535,7 +538,7 @@ public void onPreviewSizeChosen(final Size size, final int rotation) {
fragment = camera2Fragment;
} else {
fragment =
new LegacyCameraConnectionFragment(this, getLayoutId(), getDesiredPreviewFrameSize());
new LegacyCameraConnectionFragment(this, getLayoutId(), getDesiredPreviewFrameSize(), btnCount);
}

getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
Expand Down
52 changes: 0 additions & 52 deletions app/src/main/java/com/finquant/Yolov5/DetectorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,60 +332,8 @@ protected void setNumThreads(final int numThreads) {
runInBackground(() -> detector.setNumThreads(numThreads));
}

private void captureAndSaveImageAutomatically() {
Handler captureHandler = new Handler();
captureHandler.postDelayed(() -> {
// This method should return a Bitmap of the current camera frame.
Bitmap capturedBitmap = captureFrame();

// Check if we successfully captured the frame
if (capturedBitmap != null) {
// Now save the Bitmap to storage
saveImageToStorage(capturedBitmap);
} else {
Log.e("TAG", "Failed to capture image.");
}
}, 15000); // Delay in milliseconds
}

private void saveImageToStorage(Bitmap bitmap) {
// Define the file name and directory
String fileName = "Fish_Tank" + System.currentTimeMillis() + ".png";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

// Create the file
File imageFile = new File(storageDir, fileName);

try (FileOutputStream out = new FileOutputStream(imageFile)) {
// Compress the bitmap and write to the output stream
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
Log.d("TAG", "Image saved to " + imageFile.getAbsolutePath());
} catch (IOException e) {
Log.e("TAG", "Error saving image", e);
}

}


private Bitmap captureFrame() {
if (textureView == null) {
Log.e("TAG", "captureFrame: TextureView is null.");
return null;
}

// Get the bitmap from TextureView
Bitmap bitmap = textureView.getBitmap();

// You may need to rotate the bitmap based on the sensor orientation
if (sensorOrientation != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(sensorOrientation);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

return bitmap;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@ public void onSurfaceTextureUpdated(final SurfaceTexture texture) {}
};
/** An additional thread for running tasks that shouldn't block the UI. */
private HandlerThread backgroundThread;
private Button btnCount;

public LegacyCameraConnectionFragment(
final Camera.PreviewCallback imageListener, final int layout, final Size desiredSize) {
final Camera.PreviewCallback imageListener, final int layout, final Size desiredSize, Button btnCount) {
this.imageListener = imageListener;
this.layout = layout;
this.desiredSize = desiredSize;
this.btnCount = btnCount;
}

@Override
Expand Down Expand Up @@ -197,17 +199,18 @@ public void onResume() {

// Method to capture and save image automatically
private void captureAndSaveImageAutomatically() {
Handler captureHandler = new Handler();
captureHandler.postDelayed(() -> {
btnCount.setOnClickListener(v -> {
Bitmap capturedBitmap = captureFrame();
if (capturedBitmap != null) {
saveImageToStorage(capturedBitmap);

} else {
Log.e("TAG", "Failed to capture image.");
}
}, 20000); // Change delay time as needed
}
});

} // Change delay time as needed


// Method to capture the current frame from TextureView
private Bitmap captureFrame() {
Expand Down
122 changes: 71 additions & 51 deletions app/src/main/java/com/finquant/Yolov5/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,39 @@
public class MainActivity extends AppCompatActivity {

public static final float MINIMUM_CONFIDENCE_TF_OD_API = 0.3f;
int count = 0; // Initialize the counter outside the loop.

private Button btnTryAgain, btnSave;

private static final Logger LOGGER = new Logger();

public static final int TF_OD_API_INPUT_SIZE = 640;

private static final boolean TF_OD_API_IS_QUANTIZED = false;

private static final String TF_OD_API_MODEL_FILE = "best-fp16.tflite";

private static final String TF_OD_API_LABELS_FILE = "file:///android_asset/fish.txt";

// Minimum detection confidence to track a detection.
private static final boolean MAINTAIN_ASPECT = true;
private Integer sensorOrientation = 90;

private Classifier detector;

private Matrix frameToCropTransform;
private Matrix cropToFrameTransform;
private MultiBoxTracker tracker;
private OverlayView trackingOverlay;

protected int previewWidth = 0;
protected int previewHeight = 0;

private Bitmap sourceBitmap;
private Bitmap cropBitmap;

private Button cameraButton, detectButton;
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -48,6 +81,13 @@ protected void onCreate(Bundle savedInstanceState) {
cameraButton = findViewById(R.id.cameraButton);
detectButton = findViewById(R.id.detectButton);
imageView = findViewById(R.id.imageView);
btnTryAgain = findViewById(R.id.btnTryAgain);
btnSave = findViewById(R.id.btnSave);
btnTryAgain.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, DetectorActivity.class);
startActivity(intent);
finish();
});

cameraButton.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, DetectorActivity.class)));

Expand Down Expand Up @@ -76,75 +116,49 @@ protected void onCreate(Bundle savedInstanceState) {
}


// detectButton.performClick();


detectButton.setOnClickListener(v -> {
Handler handler1 = new Handler();
// detectButton.setOnClickListener(v -> {
// Handler handler1 = new Handler();
//
// new Thread(() -> {
// final List<Classifier.Recognition> results = detector.recognizeImage(cropBitmap);
// handler1.post(new Runnable() {
// @Override
// public void run() {
// handleResult(cropBitmap, results);
// }
// });
// }).start();
//
// });

new Thread(() -> {
final List<Classifier.Recognition> results = detector.recognizeImage(cropBitmap);
handler1.post(new Runnable() {
@Override
public void run() {
handleResult(cropBitmap, results);
}
});
}).start();

});


Handler handler = new Handler(Looper.getMainLooper());
btnSave.setOnClickListener(v -> {
Dialog_utils.showFishCountDialog(count, MainActivity.this);
});

// This will post the Runnable to be executed after 1000 milliseconds (1 second)
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Simulate the button click
detectButton.performClick();
}
}, 1000); // 1000 milliseconds delay

initBox();



initBox();
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();

System.err.println(Double.parseDouble(configurationInfo.getGlEsVersion()));
System.err.println(configurationInfo.reqGlEsVersion >= 0x30000);
System.err.println(String.format("%X", configurationInfo.reqGlEsVersion));
}

private static final Logger LOGGER = new Logger();

public static final int TF_OD_API_INPUT_SIZE = 640;

private static final boolean TF_OD_API_IS_QUANTIZED = false;

private static final String TF_OD_API_MODEL_FILE = "best-fp16.tflite";

private static final String TF_OD_API_LABELS_FILE = "file:///android_asset/fish.txt";

// Minimum detection confidence to track a detection.
private static final boolean MAINTAIN_ASPECT = true;
private Integer sensorOrientation = 90;
final List<Classifier.Recognition> results = detector.recognizeImage(cropBitmap);
handleResult(cropBitmap, results);

private Classifier detector;

private Matrix frameToCropTransform;
private Matrix cropToFrameTransform;
private MultiBoxTracker tracker;
private OverlayView trackingOverlay;

protected int previewWidth = 0;
protected int previewHeight = 0;
}

private Bitmap sourceBitmap;
private Bitmap cropBitmap;

private Button cameraButton, detectButton;
private ImageView imageView;

private void initBox() {
previewHeight = TF_OD_API_INPUT_SIZE;
Expand Down Expand Up @@ -207,7 +221,6 @@ private void handleResult(Bitmap bitmap, List<Classifier.Recognition> results) {
final List<Classifier.Recognition> mappedRecognitions =
new LinkedList<Classifier.Recognition>();

int count = 0; // Initialize the counter outside the loop.

for (final Classifier.Recognition result : results) {
final RectF location = result.getLocation();
Expand All @@ -224,10 +237,17 @@ private void handleResult(Bitmap bitmap, List<Classifier.Recognition> results) {

Log.d("TAG", "Count: " + count);

Dialog_utils.showFishCountDialog(count, MainActivity.this);


// tracker.trackResults(f, new Random().nextInt());
// trackingOverlay.postInvalidate();
//imageView.setImageBitmap(bitmap);
}

@Override
protected void onStart() {
super.onStart();


}
}
Loading

0 comments on commit 1bdaa97

Please sign in to comment.