【问题标题】:Can't seem to understand the error while using Fritz for Object Detection使用 Fritz 进行对象检测时似乎无法理解错误
【发布时间】:2020-02-23 10:50:48
【问题描述】:

我需要解决我遇到的错误并希望成功运行此活动。

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 --上述活动的布局文件

<?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>

我什至添加了所需的依赖项,但得到了这个运行时错误。

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 

请告诉我问题以及如何解决? 请告诉我一个可行的解决方案,因为我已经尝试过并且对此感到恼火。如果没有,我可以使用什么来向我的项目 android 应用程序添加对象检测功能,哪个更好地与免费资源等一起使用?请详细说明相同的步骤。

【问题讨论】:

    标签: android kotlin object-detection


    【解决方案1】:

    这里是 Fritz 的一位 Android 工程师。看起来您正在使用我们的 SDK 的旧版本。最新的是 5.1.1。我会尝试更新您的 app/build.gradle 依赖项以使用该依赖项,然后尝试此处的说明:https://docs.fritz.ai/develop/vision/object-detection/android.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      • 2017-05-31
      • 2022-11-24
      • 1970-01-01
      相关资源
      最近更新 更多