diff --git a/object_detection_android-main/.gitignore b/object_detection_android-main/.gitignore new file mode 100644 index 0000000..eea3534 --- /dev/null +++ b/object_detection_android-main/.gitignore @@ -0,0 +1,16 @@ +/app/build/ +*.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/object_detection_android-main/.idea/.gitignore b/object_detection_android-main/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/object_detection_android-main/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/object_detection_android-main/.idea/.name b/object_detection_android-main/.idea/.name new file mode 100644 index 0000000..bb96f6d --- /dev/null +++ b/object_detection_android-main/.idea/.name @@ -0,0 +1 @@ +object_detection \ No newline at end of file diff --git a/object_detection_android-main/.idea/compiler.xml b/object_detection_android-main/.idea/compiler.xml new file mode 100644 index 0000000..b589d56 --- /dev/null +++ b/object_detection_android-main/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/object_detection_android-main/.idea/deploymentTargetDropDown.xml b/object_detection_android-main/.idea/deploymentTargetDropDown.xml new file mode 100644 index 0000000..0c0c338 --- /dev/null +++ b/object_detection_android-main/.idea/deploymentTargetDropDown.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/object_detection_android-main/.idea/gradle.xml b/object_detection_android-main/.idea/gradle.xml new file mode 100644 index 0000000..0897082 --- /dev/null +++ b/object_detection_android-main/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/object_detection_android-main/.idea/kotlinc.xml b/object_detection_android-main/.idea/kotlinc.xml new file mode 100644 index 0000000..b1077fb --- /dev/null +++ b/object_detection_android-main/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/object_detection_android-main/.idea/migrations.xml b/object_detection_android-main/.idea/migrations.xml new file mode 100644 index 0000000..f8051a6 --- /dev/null +++ b/object_detection_android-main/.idea/migrations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/object_detection_android-main/.idea/misc.xml b/object_detection_android-main/.idea/misc.xml new file mode 100644 index 0000000..ae00e88 --- /dev/null +++ b/object_detection_android-main/.idea/misc.xml @@ -0,0 +1,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/object_detection_android-main/README.md b/object_detection_android-main/README.md new file mode 100644 index 0000000..c96d1fa --- /dev/null +++ b/object_detection_android-main/README.md @@ -0,0 +1,3 @@ +# object_detection_android + +code in refrence to youtube tutorial : https://youtu.be/RDARCjVpg8Q diff --git a/object_detection_android-main/app/.gitignore b/object_detection_android-main/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/object_detection_android-main/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/object_detection_android-main/app/build.gradle b/object_detection_android-main/app/build.gradle new file mode 100644 index 0000000..1c77ee8 --- /dev/null +++ b/object_detection_android-main/app/build.gradle @@ -0,0 +1,49 @@ +plugins { + id 'com.android.application' + id 'org.jetbrains.kotlin.android' +} + +android { + compileSdk 32 + + defaultConfig { + applicationId "com.programminghut.object_detection" + minSdk 24 + targetSdk 32 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = '1.8' + } + buildFeatures { + mlModelBinding true + } +} + +dependencies { + + implementation 'androidx.core:core-ktx:1.7.0' + implementation 'androidx.appcompat:appcompat:1.5.1' + implementation 'com.google.android.material:material:1.7.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'org.tensorflow:tensorflow-lite-support:0.1.0' + implementation 'org.tensorflow:tensorflow-lite-metadata:0.1.0' + implementation 'org.tensorflow:tensorflow-lite-gpu:2.3.0' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.4' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' +} \ No newline at end of file diff --git a/object_detection_android-main/app/proguard-rules.pro b/object_detection_android-main/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/object_detection_android-main/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/object_detection_android-main/app/src/androidTest/java/com/programminghut/object_detection/ExampleInstrumentedTest.kt b/object_detection_android-main/app/src/androidTest/java/com/programminghut/object_detection/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..461cc53 --- /dev/null +++ b/object_detection_android-main/app/src/androidTest/java/com/programminghut/object_detection/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.programminghut.object_detection + +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.programminghut.object_detection", appContext.packageName) + } +} \ No newline at end of file diff --git a/object_detection_android-main/app/src/main/AndroidManifest.xml b/object_detection_android-main/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..333f2ca --- /dev/null +++ b/object_detection_android-main/app/src/main/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/object_detection_android-main/app/src/main/assets/labels.txt b/object_detection_android-main/app/src/main/assets/labels.txt new file mode 100644 index 0000000..3168376 --- /dev/null +++ b/object_detection_android-main/app/src/main/assets/labels.txt @@ -0,0 +1,6 @@ +b_fully_ripened +b_half_ripened +b_green +l_fully_ripened +l_half_ripened +l_green diff --git a/object_detection_android-main/app/src/main/java/com/programminghut/object_detection/MainActivity.kt b/object_detection_android-main/app/src/main/java/com/programminghut/object_detection/MainActivity.kt new file mode 100644 index 0000000..eb67fd0 --- /dev/null +++ b/object_detection_android-main/app/src/main/java/com/programminghut/object_detection/MainActivity.kt @@ -0,0 +1,97 @@ +package com.programminghut.object_detection + +import android.content.Intent +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.os.Bundle +import android.provider.MediaStore +import android.util.Log +import android.widget.Button +import android.widget.ImageView +import androidx.appcompat.app.AppCompatActivity +import com.programminghut.object_detection.ml.BestFloat16 +import org.tensorflow.lite.support.common.FileUtil +import org.tensorflow.lite.support.image.ImageProcessor +import org.tensorflow.lite.support.image.TensorImage +import org.tensorflow.lite.support.image.ops.ResizeOp + +class MainActivity : AppCompatActivity() { + + val paint = Paint() + lateinit var btn: Button + lateinit var imageView: ImageView + lateinit var bitmap: Bitmap + var colors = listOf(Color.BLUE, Color.GREEN, Color.RED, Color.CYAN, Color.GRAY, Color.BLACK, + Color.DKGRAY, Color.MAGENTA, Color.YELLOW, Color.RED) + lateinit var labels: List + lateinit var model: BestFloat16 + val imageProcessor = ImageProcessor.Builder().add(ResizeOp(300, 300, ResizeOp.ResizeMethod.BILINEAR)).build() + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + labels = FileUtil.loadLabels(this, "labels.txt") + model = BestFloat16.newInstance(this) + + paint.setColor(Color.BLUE) + paint.style = Paint.Style.STROKE + paint.strokeWidth = 5.0f +// paint.textSize = paint.textSize*3 + + Log.d("labels", labels.toString()) + + val intent = Intent() + intent.setAction(Intent.ACTION_GET_CONTENT) + intent.setType("image/*") + + btn = findViewById(R.id.btn) + imageView = findViewById(R.id.imaegView) + + btn.setOnClickListener { + startActivityForResult(intent, 101) + } + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + if(requestCode == 101){ + var uri = data?.data + bitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, uri) + get_predictions() + } + } + + override fun onDestroy() { + super.onDestroy() + model.close() + } + + fun get_predictions(){ + +// Creates inputs for reference. + val image = TensorImage.fromBitmap(bitmap) + +// Runs model inference and gets result. + val outputs = model.process(image) + val output = outputs.outputAsCategoryList + +// Releases model resources if no longer used. + + + val mutable = bitmap.copy(Bitmap.Config.ARGB_8888, true) + var canvas = Canvas(mutable) + var h = mutable.height + var w = mutable.width + + + paint.textSize = h/15f + paint.strokeWidth = h/85f + + + imageView.setImageBitmap(mutable) + + } +} \ No newline at end of file diff --git a/object_detection_android-main/app/src/main/ml/best_float16.tflite b/object_detection_android-main/app/src/main/ml/best_float16.tflite new file mode 100644 index 0000000..93d147b Binary files /dev/null and b/object_detection_android-main/app/src/main/ml/best_float16.tflite differ diff --git a/object_detection_android-main/app/src/main/ml/ssd_mobilenet_v1_1_metadata_1.tflite b/object_detection_android-main/app/src/main/ml/ssd_mobilenet_v1_1_metadata_1.tflite new file mode 100644 index 0000000..80d1fe5 Binary files /dev/null and b/object_detection_android-main/app/src/main/ml/ssd_mobilenet_v1_1_metadata_1.tflite differ diff --git a/object_detection_android-main/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/object_detection_android-main/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/object_detection_android-main/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/object_detection_android-main/app/src/main/res/drawable/ic_launcher_background.xml b/object_detection_android-main/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/object_detection_android-main/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/object_detection_android-main/app/src/main/res/layout/activity_main.xml b/object_detection_android-main/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..74fd133 --- /dev/null +++ b/object_detection_android-main/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,25 @@ + + + +