From 3e5a8772d7c63e4cd77113e9f7a6fccaa8743fcd Mon Sep 17 00:00:00 2001 From: SaiD Date: Tue, 25 Nov 2025 20:35:23 +0530 Subject: [PATCH] Animal Rating --- .gitignore | 15 + .idea/.gitignore | 3 + .idea/AndroidProjectSystem.xml | 6 + .idea/compiler.xml | 6 + .idea/deploymentTargetSelector.xml | 18 ++ .idea/deviceManager.xml | 13 + .idea/gradle.xml | 19 ++ .idea/migrations.xml | 10 + .idea/misc.xml | 9 + .idea/runConfigurations.xml | 17 + .idea/studiobot.xml | 6 + app/.gitignore | 1 + app/build.gradle.kts | 74 +++++ app/proguard-rules.pro | 21 ++ .../animalrating/ExampleInstrumentedTest.kt | 24 ++ app/src/main/AndroidManifest.xml | 52 +++ .../example/animalrating/CameraProcessor.kt | 301 ++++++++++++++++++ .../animalrating/CowSelectionActivity.kt | 214 +++++++++++++ .../example/animalrating/FrameProcessor.kt | 96 ++++++ .../animalrating/FullScreenImageActivity.kt | 29 ++ .../example/animalrating/GalleryActivity.kt | 167 ++++++++++ .../com/example/animalrating/HomeActivity.kt | 21 ++ .../example/animalrating/ml/CowAnalyzer.kt | 18 ++ .../animalrating/ml/ImageProxyExt.kt.kt | 43 +++ .../example/animalrating/ui/MaskOverlay.kt | 24 ++ .../animalrating/ui/SilhouetteOverlay.kt | 74 +++++ .../example/animalrating/ui/theme/Color.kt | 11 + .../example/animalrating/ui/theme/Theme.kt | 58 ++++ .../com/example/animalrating/ui/theme/Type.kt | 34 ++ app/src/main/res/drawable/angle.png | Bin 0 -> 6087 bytes app/src/main/res/drawable/back.png | Bin 0 -> 5005 bytes app/src/main/res/drawable/back2.png | Bin 0 -> 3864 bytes app/src/main/res/drawable/front.png | Bin 0 -> 3772 bytes .../res/drawable/ic_launcher_background.xml | 170 ++++++++++ .../res/drawable/ic_launcher_foreground.xml | 30 ++ app/src/main/res/drawable/left.png | Bin 0 -> 27312 bytes app/src/main/res/drawable/right.png | Bin 0 -> 6115 bytes .../res/layout/activity_cow_selection.xml | 133 ++++++++ .../res/layout/activity_full_screen_image.xml | 24 ++ app/src/main/res/layout/activity_gallery.xml | 26 ++ app/src/main/res/layout/activity_home.xml | 64 ++++ app/src/main/res/layout/activity_main.xml | 40 +++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 6 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 6 + app/src/main/res/mipmap-hdpi/ic_launcher.webp | Bin 0 -> 1404 bytes .../res/mipmap-hdpi/ic_launcher_round.webp | Bin 0 -> 2898 bytes app/src/main/res/mipmap-mdpi/ic_launcher.webp | Bin 0 -> 982 bytes .../res/mipmap-mdpi/ic_launcher_round.webp | Bin 0 -> 1772 bytes .../main/res/mipmap-xhdpi/ic_launcher.webp | Bin 0 -> 1900 bytes .../res/mipmap-xhdpi/ic_launcher_round.webp | Bin 0 -> 3918 bytes .../main/res/mipmap-xxhdpi/ic_launcher.webp | Bin 0 -> 2884 bytes .../res/mipmap-xxhdpi/ic_launcher_round.webp | Bin 0 -> 5914 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.webp | Bin 0 -> 3844 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.webp | Bin 0 -> 7778 bytes app/src/main/res/values/colors.xml | 10 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/themes.xml | 5 + app/src/main/res/xml/backup_rules.xml | 13 + .../main/res/xml/data_extraction_rules.xml | 19 ++ .../example/animalrating/ExampleUnitTest.kt | 17 + build.gradle.kts | 6 + gradle.properties | 23 ++ gradle/libs.versions.toml | 32 ++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 45457 bytes gradle/wrapper/gradle-wrapper.properties | 8 + gradlew | 251 +++++++++++++++ gradlew.bat | 94 ++++++ settings.gradle.kts | 24 ++ 68 files changed, 2388 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/AndroidProjectSystem.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/deploymentTargetSelector.xml create mode 100644 .idea/deviceManager.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/migrations.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/studiobot.xml create mode 100644 app/.gitignore create mode 100644 app/build.gradle.kts create mode 100644 app/proguard-rules.pro create mode 100644 app/src/androidTest/java/com/example/animalrating/ExampleInstrumentedTest.kt create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/com/example/animalrating/CameraProcessor.kt create mode 100644 app/src/main/java/com/example/animalrating/CowSelectionActivity.kt create mode 100644 app/src/main/java/com/example/animalrating/FrameProcessor.kt create mode 100644 app/src/main/java/com/example/animalrating/FullScreenImageActivity.kt create mode 100644 app/src/main/java/com/example/animalrating/GalleryActivity.kt create mode 100644 app/src/main/java/com/example/animalrating/HomeActivity.kt create mode 100644 app/src/main/java/com/example/animalrating/ml/CowAnalyzer.kt create mode 100644 app/src/main/java/com/example/animalrating/ml/ImageProxyExt.kt.kt create mode 100644 app/src/main/java/com/example/animalrating/ui/MaskOverlay.kt create mode 100644 app/src/main/java/com/example/animalrating/ui/SilhouetteOverlay.kt create mode 100644 app/src/main/java/com/example/animalrating/ui/theme/Color.kt create mode 100644 app/src/main/java/com/example/animalrating/ui/theme/Theme.kt create mode 100644 app/src/main/java/com/example/animalrating/ui/theme/Type.kt create mode 100644 app/src/main/res/drawable/angle.png create mode 100644 app/src/main/res/drawable/back.png create mode 100644 app/src/main/res/drawable/back2.png create mode 100644 app/src/main/res/drawable/front.png create mode 100644 app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 app/src/main/res/drawable/ic_launcher_foreground.xml create mode 100644 app/src/main/res/drawable/left.png create mode 100644 app/src/main/res/drawable/right.png create mode 100644 app/src/main/res/layout/activity_cow_selection.xml create mode 100644 app/src/main/res/layout/activity_full_screen_image.xml create mode 100644 app/src/main/res/layout/activity_gallery.xml create mode 100644 app/src/main/res/layout/activity_home.xml create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.webp create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher_round.webp create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.webp create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher_round.webp create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.webp create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.webp create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/themes.xml create mode 100644 app/src/main/res/xml/backup_rules.xml create mode 100644 app/src/main/res/xml/data_extraction_rules.xml create mode 100644 app/src/test/java/com/example/animalrating/ExampleUnitTest.kt create mode 100644 build.gradle.kts create mode 100644 gradle.properties create mode 100644 gradle/libs.versions.toml create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100644 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle.kts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/AndroidProjectSystem.xml b/.idea/AndroidProjectSystem.xml new file mode 100644 index 0000000..4a53bee --- /dev/null +++ b/.idea/AndroidProjectSystem.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..b86273d --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml new file mode 100644 index 0000000..cca7557 --- /dev/null +++ b/.idea/deploymentTargetSelector.xml @@ -0,0 +1,18 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/deviceManager.xml b/.idea/deviceManager.xml new file mode 100644 index 0000000..91f9558 --- /dev/null +++ b/.idea/deviceManager.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..639c779 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/migrations.xml b/.idea/migrations.xml new file mode 100644 index 0000000..f8051a6 --- /dev/null +++ b/.idea/migrations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b2c751a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..16660f1 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/.idea/studiobot.xml b/.idea/studiobot.xml new file mode 100644 index 0000000..539e3b8 --- /dev/null +++ b/.idea/studiobot.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts new file mode 100644 index 0000000..25b8a5e --- /dev/null +++ b/app/build.gradle.kts @@ -0,0 +1,74 @@ +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.kotlin.compose) +} + +android { + namespace = "com.example.animalrating" + compileSdk { + version = release(36) + } + + defaultConfig { + applicationId = "com.example.animalrating" + minSdk = 24 + targetSdk = 36 + versionCode = 1 + versionName = "1.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + kotlinOptions { + jvmTarget = "11" + } + buildFeatures { + compose = true + } +} + +dependencies { + val cameraxVersion = "1.5.1" + implementation("androidx.appcompat:appcompat:1.7.0") + implementation("androidx.cardview:cardview:1.0.0") + implementation("androidx.camera:camera-core:$cameraxVersion") + implementation("androidx.camera:camera-camera2:$cameraxVersion") + implementation("androidx.camera:camera-lifecycle:$cameraxVersion") + implementation("androidx.camera:camera-view:$cameraxVersion") + implementation("androidx.camera:camera-mlkit-vision:$cameraxVersion") + + // ML Kit Object Detection + implementation("com.google.mlkit:object-detection:17.0.2") + // ML Kit Subject Segmentation (Google Play Services version) + implementation("com.google.android.gms:play-services-mlkit-subject-segmentation:16.0.0-beta1") + + implementation(libs.androidx.core.ktx) + implementation(libs.androidx.lifecycle.runtime.ktx) + implementation(libs.androidx.activity.compose) + implementation(platform(libs.androidx.compose.bom)) + implementation(libs.androidx.compose.ui) + implementation(libs.androidx.compose.ui.graphics) + implementation(libs.androidx.compose.ui.tooling.preview) + implementation(libs.androidx.compose.material3) + testImplementation(libs.junit) + androidTestImplementation(libs.androidx.junit) + androidTestImplementation(libs.androidx.espresso.core) + androidTestImplementation(platform(libs.androidx.compose.bom)) + androidTestImplementation(libs.androidx.compose.ui.test.junit4) + debugImplementation(libs.androidx.compose.ui.tooling) + debugImplementation(libs.androidx.compose.ui.test.manifest) +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/animalrating/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/example/animalrating/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..9100cbf --- /dev/null +++ b/app/src/androidTest/java/com/example/animalrating/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.example.animalrating + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.example.animalrating", appContext.packageName) + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..9987565 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/animalrating/CameraProcessor.kt b/app/src/main/java/com/example/animalrating/CameraProcessor.kt new file mode 100644 index 0000000..fd57a73 --- /dev/null +++ b/app/src/main/java/com/example/animalrating/CameraProcessor.kt @@ -0,0 +1,301 @@ +package com.example.animalrating + +import android.Manifest +import android.content.pm.ActivityInfo +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.Color +import android.graphics.Matrix +import android.os.Bundle +import android.util.Log +import android.widget.Button +import android.widget.ImageView +import android.widget.Toast +import androidx.activity.result.contract.ActivityResultContracts +import androidx.appcompat.app.AppCompatActivity +import androidx.camera.core.ImageAnalysis +import androidx.camera.core.ImageCapture +import androidx.camera.core.ImageCaptureException +import androidx.camera.core.ImageProxy +import androidx.camera.lifecycle.ProcessCameraProvider +import androidx.camera.view.PreviewView +import androidx.core.content.ContextCompat +import com.example.animalrating.ml.CowAnalyzer +import com.example.animalrating.ui.SilhouetteOverlay +import java.io.File +import java.io.FileOutputStream +import java.util.concurrent.Executors + +class CameraProcessor : AppCompatActivity(), CowAnalyzer.CowListener { + + private lateinit var previewView: PreviewView + private lateinit var overlay: SilhouetteOverlay + private lateinit var segmentationOverlay: ImageView + private lateinit var savedMaskOverlay: ImageView + private var imageCapture: ImageCapture? = null + private lateinit var frameProcessor: FrameProcessor + + private val cameraExecutor = Executors.newSingleThreadExecutor() + + private var cowName: String? = null + private var orientation: String? = null + private var currentMask: Bitmap? = null + private var savedMaskBitmap: Bitmap? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + cowName = intent.getStringExtra("COW_NAME") + orientation = intent.getStringExtra("ORIENTATION") + + // Set orientation based on selected view + if (orientation == "front" || orientation == "back") { + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT + } else { + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + } + + previewView = findViewById(R.id.cameraPreview) + overlay = findViewById(R.id.silhouetteOverlay) + segmentationOverlay = findViewById(R.id.segmentationOverlay) + savedMaskOverlay = findViewById(R.id.savedMaskOverlay) + + frameProcessor = FrameProcessor { maskBitmap -> + runOnUiThread { + if (maskBitmap != null && (orientation == "front" || orientation == "back")) { + try { + val matrix = Matrix() + matrix.postRotate(90f) + val rotatedBitmap = Bitmap.createBitmap( + maskBitmap, 0, 0, maskBitmap.width, maskBitmap.height, matrix, true + ) + currentMask = rotatedBitmap + segmentationOverlay.setImageBitmap(rotatedBitmap) + } catch (e: Exception) { + Log.e("CameraProcessor", "Error rotating mask", e) + currentMask = maskBitmap + segmentationOverlay.setImageBitmap(maskBitmap) + } + } else { + currentMask = maskBitmap + segmentationOverlay.setImageBitmap(maskBitmap) + } + } + } + + val btnSave = findViewById