├── .gitignore ├── .idea ├── .gitignore ├── compiler.xml ├── deploymentTargetDropDown.xml ├── gradle.xml └── misc.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── programminghut │ │ └── object_detection │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── assets │ │ └── labels.txt │ ├── java │ │ └── com │ │ │ └── programminghut │ │ │ └── object_detection │ │ │ └── MainActivity.kt │ ├── ml │ │ └── ssd_mobilenet_v1_1_metadata_1.tflite │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── values-night │ │ └── themes.xml │ │ ├── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ │ └── xml │ │ ├── backup_rules.xml │ │ └── data_extraction_rules.xml │ └── test │ └── java │ └── com │ └── programminghut │ └── object_detection │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | /app/build/ 2 | *.iml 3 | .gradle 4 | /local.properties 5 | /.idea/caches 6 | /.idea/libraries 7 | /.idea/modules.xml 8 | /.idea/workspace.xml 9 | /.idea/navEditor.xml 10 | /.idea/assetWizardSettings.xml 11 | .DS_Store 12 | /build 13 | /captures 14 | .externalNativeBuild 15 | .cxx 16 | local.properties 17 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/deploymentTargetDropDown.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # object_detection_android 2 | 3 | code in refrence to youtube tutorial : https://youtu.be/RDARCjVpg8Q 4 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'org.jetbrains.kotlin.android' 4 | } 5 | 6 | android { 7 | compileSdk 32 8 | 9 | defaultConfig { 10 | applicationId "com.programminghut.object_detection" 11 | minSdk 24 12 | targetSdk 32 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | compileOptions { 26 | sourceCompatibility JavaVersion.VERSION_1_8 27 | targetCompatibility JavaVersion.VERSION_1_8 28 | } 29 | kotlinOptions { 30 | jvmTarget = '1.8' 31 | } 32 | buildFeatures { 33 | mlModelBinding true 34 | } 35 | } 36 | 37 | dependencies { 38 | 39 | implementation 'androidx.core:core-ktx:1.7.0' 40 | implementation 'androidx.appcompat:appcompat:1.5.1' 41 | implementation 'com.google.android.material:material:1.7.0' 42 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 43 | implementation 'org.tensorflow:tensorflow-lite-support:0.1.0' 44 | implementation 'org.tensorflow:tensorflow-lite-metadata:0.1.0' 45 | testImplementation 'junit:junit:4.13.2' 46 | androidTestImplementation 'androidx.test.ext:junit:1.1.4' 47 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' 48 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/com/programminghut/object_detection/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.programminghut.object_detection 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.programminghut.object_detection", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/assets/labels.txt: -------------------------------------------------------------------------------- 1 | person 2 | bicycle 3 | car 4 | motorcycle 5 | airplane 6 | bus 7 | train 8 | truck 9 | boat 10 | traffic light 11 | fire hydrant 12 | ??? 13 | stop sign 14 | parking meter 15 | bench 16 | bird 17 | cat 18 | dog 19 | horse 20 | sheep 21 | cow 22 | elephant 23 | bear 24 | zebra 25 | giraffe 26 | ??? 27 | backpack 28 | umbrella 29 | ??? 30 | ??? 31 | handbag 32 | tie 33 | suitcase 34 | frisbee 35 | skis 36 | snowboard 37 | sports ball 38 | kite 39 | baseball bat 40 | baseball glove 41 | skateboard 42 | surfboard 43 | tennis racket 44 | bottle 45 | ??? 46 | wine glass 47 | cup 48 | fork 49 | knife 50 | spoon 51 | bowl 52 | banana 53 | apple 54 | sandwich 55 | orange 56 | broccoli 57 | carrot 58 | hot dog 59 | pizza 60 | donut 61 | cake 62 | chair 63 | couch 64 | potted plant 65 | bed 66 | ??? 67 | dining table 68 | ??? 69 | ??? 70 | toilet 71 | ??? 72 | tv 73 | laptop 74 | mouse 75 | remote 76 | keyboard 77 | cell phone 78 | microwave 79 | oven 80 | toaster 81 | sink 82 | refrigerator 83 | ??? 84 | book 85 | clock 86 | vase 87 | scissors 88 | teddy bear 89 | hair drier 90 | toothbrush -------------------------------------------------------------------------------- /app/src/main/java/com/programminghut/object_detection/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.programminghut.object_detection 2 | 3 | import android.content.Intent 4 | import android.graphics.* 5 | import androidx.appcompat.app.AppCompatActivity 6 | import android.os.Bundle 7 | import android.os.FileUtils 8 | import android.provider.MediaStore 9 | import android.util.Log 10 | import android.widget.Button 11 | import android.widget.ImageView 12 | import com.programminghut.object_detection.ml.SsdMobilenetV11Metadata1 13 | import org.tensorflow.lite.support.common.FileUtil 14 | import org.tensorflow.lite.support.image.ImageProcessor 15 | import org.tensorflow.lite.support.image.TensorImage 16 | import org.tensorflow.lite.support.image.ops.ResizeOp 17 | 18 | class MainActivity : AppCompatActivity() { 19 | 20 | val paint = Paint() 21 | lateinit var btn: Button 22 | lateinit var imageView: ImageView 23 | lateinit var bitmap: Bitmap 24 | var colors = listOf(Color.BLUE, Color.GREEN, Color.RED, Color.CYAN, Color.GRAY, Color.BLACK, 25 | Color.DKGRAY, Color.MAGENTA, Color.YELLOW, Color.RED) 26 | lateinit var labels: List 27 | lateinit var model: SsdMobilenetV11Metadata1 28 | val imageProcessor = ImageProcessor.Builder().add(ResizeOp(300, 300, ResizeOp.ResizeMethod.BILINEAR)).build() 29 | 30 | override fun onCreate(savedInstanceState: Bundle?) { 31 | super.onCreate(savedInstanceState) 32 | setContentView(R.layout.activity_main) 33 | 34 | labels = FileUtil.loadLabels(this, "labels.txt") 35 | model = SsdMobilenetV11Metadata1.newInstance(this) 36 | 37 | paint.setColor(Color.BLUE) 38 | paint.style = Paint.Style.STROKE 39 | paint.strokeWidth = 5.0f 40 | // paint.textSize = paint.textSize*3 41 | 42 | Log.d("labels", labels.toString()) 43 | 44 | val intent = Intent() 45 | intent.setAction(Intent.ACTION_GET_CONTENT) 46 | intent.setType("image/*") 47 | 48 | btn = findViewById(R.id.btn) 49 | imageView = findViewById(R.id.imaegView) 50 | 51 | btn.setOnClickListener { 52 | startActivityForResult(intent, 101) 53 | } 54 | } 55 | 56 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 57 | super.onActivityResult(requestCode, resultCode, data) 58 | if(requestCode == 101){ 59 | var uri = data?.data 60 | bitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, uri) 61 | get_predictions() 62 | } 63 | } 64 | 65 | override fun onDestroy() { 66 | super.onDestroy() 67 | model.close() 68 | } 69 | 70 | fun get_predictions(){ 71 | 72 | var image = TensorImage.fromBitmap(bitmap) 73 | image = imageProcessor.process(image) 74 | val outputs = model.process(image) 75 | val locations = outputs.locationsAsTensorBuffer.floatArray 76 | val classes = outputs.classesAsTensorBuffer.floatArray 77 | val scores = outputs.scoresAsTensorBuffer.floatArray 78 | val numberOfDetections = outputs.numberOfDetectionsAsTensorBuffer.floatArray 79 | 80 | 81 | 82 | val mutable = bitmap.copy(Bitmap.Config.ARGB_8888, true) 83 | var canvas = Canvas(mutable) 84 | var h = mutable.height 85 | var w = mutable.width 86 | 87 | 88 | paint.textSize = h/15f 89 | paint.strokeWidth = h/85f 90 | scores.forEachIndexed { index, fl -> 91 | if(fl > 0.5){ 92 | var x = index 93 | x *= 4 94 | paint.setColor(colors.get(index)) 95 | paint.style = Paint.Style.STROKE 96 | canvas.drawRect(RectF(locations.get(x+1)*w, locations.get(x)*h, locations.get(x+3)*w, locations.get(x+2)*h), paint) 97 | paint.style = Paint.Style.FILL 98 | canvas.drawText(labels[classes.get(index).toInt()] + " " + fl.toString(), locations.get(x+1)*w, locations.get(x)*h, paint) 99 | } 100 | } 101 | 102 | imageView.setImageBitmap(mutable) 103 | 104 | } 105 | } -------------------------------------------------------------------------------- /app/src/main/ml/ssd_mobilenet_v1_1_metadata_1.tflite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pawandeep-prog/object_detection_android/91a6d1d4ead87469364e7058eafe5a0dd3366036/app/src/main/ml/ssd_mobilenet_v1_1_metadata_1.tflite -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 |