Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to Kotlin is Complete #55

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ The App is created on top of `Deep Learning` and `Aritificial Neural Networks`.

[![Android](https://forthebadge.com/images/badges/built-for-android.svg)]()
[![PyTorch](https://img.shields.io/badge/Powered%20By-PyTorch-orange?style=for-the-badge&logo=pytorch)]()
[![JAVA](https://forthebadge.com/images/badges/made-with-java.svg)]()
[![KOTLIN](https://img.shields.io/badge/Made%20with-Kotlin-blue?style=for-the-badge&logo=kotlin)]()

- The App is created for and only for **`Android`**, `2 minutes of silence for other platforms`
- To power-up the Deepest Neural Network, the App uses **`PyTorch`**
- Written in *`Shuddh`* **`JAVA`**
- Written in *`Shuddh`* **`KOTLIN`**
- Uses [**`CameraX`**](https://developer.android.com/training/camerax) to Capture frames

## Neural Network
Expand Down
8 changes: 7 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30
Expand Down Expand Up @@ -42,9 +44,13 @@ dependencies {
implementation 'androidx.camera:camera-core:1.0.0-beta10'
implementation 'androidx.camera:camera-extensions:1.0.0-alpha17'
implementation 'androidx.camera:camera-lifecycle:1.0.0-beta10'
implementation 'androidx.camera:camera-view:1.0.0-alpha15'
implementation 'androidx.camera:camera-view:1.0.0-alpha17'
implementation 'org.pytorch:pytorch_android:1.6.0'
implementation 'org.pytorch:pytorch_android_torchvision:1.6.0'

//kotlin dependencies
implementation "androidx.core:core-ktx:1.3.2"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.arkadip.whatsthere

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith

/**
* 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
Assert.assertEquals("com.arkadip.whatsthere", appContext.packageName)
}
}
57 changes: 0 additions & 57 deletions app/src/main/java/com/arkadip/whatsthere/Classifier.java

This file was deleted.

54 changes: 54 additions & 0 deletions app/src/main/java/com/arkadip/whatsthere/Classifier.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2020 Arkadip Bhattacharya
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arkadip.whatsthere

import android.media.Image
import org.pytorch.IValue
import org.pytorch.Module
import org.pytorch.Tensor
import org.pytorch.torchvision.TensorImageUtils

class Classifier constructor(modelPath:String?) {
private val model:Module = Module.load(modelPath)

private fun preprocessor(image: Image, rotation:Int):Tensor {
return TensorImageUtils.imageYUV420CenterCropToFloat32Tensor(image, rotation, 224,
224, TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB)
}

private fun argMax(inputs:FloatArray):Int {
var maxIndex:Int = -1
var maxValue = 0.0f
inputs.forEachIndexed { i, input ->
if (input > maxValue) {
maxIndex = i
maxValue = input
}

}
return maxIndex
}

fun predict(image: Image, rotation: Int):Int {

val tensor = preprocessor(image,rotation)
val output = model.forward(IValue.from(tensor)).toTensor()
val scores: FloatArray = output.dataAsFloatArray
return argMax(scores)
}

}
Loading