0

I need to resolve the error that I'm getting and want to successfully run this activity.

DetectionActivity.kt

package-name

import ai.fritz.core.Fritz
import ai.fritz.fritzvisionobjectmodel.FritzVisionObjectPredictor
import ai.fritz.vision.inputs.FritzVisionImage
import android.graphics.Bitmap
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.renderscript.Allocation
import android.renderscript.Element
import android.renderscript.RenderScript
import android.renderscript.ScriptIntrinsicYuvToRGB
import kotlinx.android.synthetic.main.activity_detection.*

class DetectionActivity : AppCompatActivity() {

    private lateinit var renderScript: RenderScript
    private lateinit var yuvToRGB: ScriptIntrinsicYuvToRGB
    private var yuvDataLength: Int = 0
    private lateinit var allocationIn: Allocation
    private lateinit var allocationOut: Allocation
    private lateinit var bitmapOut: Bitmap

    private val itemMap by lazy {
        hashMapOf<String, Int>()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Fritz.configure(this, "Fritz-token-key")

        //val onDeviceModel = ObjectDetectionOnDeviceModel()
        //val objectPredictor = FritzVision.ObjectDetection.getPredictor(onDeviceModel)
        val objectPredictor = FritzVisionObjectPredictor.getInstance(this)
        var fritzVisionImage: FritzVisionImage

        cameraView.addFrameProcessor {

            if (yuvDataLength == 0) {
                //Run this only once
                initializeData()
            }

            //Camera Preview returns NV21, so convert it to Bitmap :
            //https://stackoverflow.com/a/43551798/5471095
            allocationIn.copyFrom(it.data)
            yuvToRGB.forEach(allocationOut)
            allocationOut.copyTo(bitmapOut)
            fritzVisionImage = FritzVisionImage.fromBitmap(bitmapOut, it.rotation)
            //val visionResult = objectPredictor.predict(fritzVisionImage)
            val visionObjects = objectPredictor.predict(fritzVisionImage)

            //Clear the existing map
            itemMap.clear()

            //Convert the list of objects detected into a Map so that we can track count of similar items
            //visionResult.visionObjects.forEach { visionObject ->
            visionObjects.forEach{ visionObject ->
                if (itemMap.containsKey(visionObject.visionLabel.text))
                    itemMap[visionObject.visionLabel.text] = itemMap[visionObject.visionLabel.text]!! + 1
                itemMap[visionObject.visionLabel.text] = 1
            }

            //Print the detected items on the screen
            runOnUiThread {
                tvDetectedItem.text = ""
                itemMap.forEach { map ->
                    tvDetectedItem.append("Detected ${map.value} ${map.key}\n")
                }
            }
        }
    }

    private fun initializeData() {
        yuvDataLength = cameraView.previewSize?.height!! * cameraView.previewSize?.width!! * 3 / 2
        renderScript = RenderScript.create(baseContext)
        yuvToRGB = ScriptIntrinsicYuvToRGB.create(renderScript, Element.U8_4(renderScript))
        allocationIn = Allocation.createSized(renderScript, Element.U8(renderScript), yuvDataLength)
        bitmapOut = Bitmap.createBitmap(cameraView.previewSize?.width!!, cameraView.previewSize?.height!!, Bitmap.Config.ARGB_8888)
        allocationOut = Allocation.createFromBitmap(renderScript, bitmapOut)
        yuvToRGB.setInput(allocationIn)
    }

    override fun onStart() {
        super.onStart()
        cameraView.start()
    }

    override fun onStop() {
        super.onStop()
        cameraView.stop()
    }
}

activity_detection.xml --Layout File for the above respective activity

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.arca.DetectionActivity">

    <com.otaliastudios.cameraview.CameraView
            android:id="@+id/cameraView"
            android:keepScreenOn="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    <TextView
            android:layout_marginBottom="32dp"
            android:textSize="32sp"
            android:textStyle="bold"
            android:textColor="#B09954"
            android:id="@+id/tvDetectedItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal" />
</FrameLayout>

I even added the required dependencies and all but getting this Runtime Error.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: pakage-name, PID: pid-number
    java.lang.NoSuchMethodError: No static method isInitialized(Landroid/content/Context;)Z in class Lai/fritz/core/Fritz; or its super classes (declaration of 'ai.fritz.core.Fritz' appears in /data/app/pakage-name-79eLdVK0LI_fYwGGKlCPVQ==/base.apk)
        at com.example.arca.DetectionActivity.onCreate(DetectionActivity.kt:35)
D/GetRequestTask: Api Request: https://api.fritz.ai/sdk/v1/session/settings 

Please tell me the problem and how can I fix it?? Please tell me a workable solution for I have tried and got irritated on the same. If not, what can I use for adding object detection functionality to my project android application which is better to use with free resources and such? Do tell the steps in detail for the same.

sxpz23
  • 3
  • 3

1 Answers1

0

One of the Android engineers at Fritz here. It looks like you're using a pretty old version of our SDK. The latest one is 5.1.1. I'd try updating your app/build.gradle dependency to use that one and then trying the directions here: https://docs.fritz.ai/develop/vision/object-detection/android.html.

Eric Hsiao
  • 13
  • 3