في هذا الكود class MainActivity : AppCompatActivity() { private lateinit var imageView:…
- User
في هذا الكود
class MainActivity : AppCompatActivity() {
private lateinit var imageView: ImageView private lateinit var btnImprove: Button private lateinit var model: Interpreter private val TAG = MainActivity::class.java.simpleName override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) imageView = findViewById(R.id.imageView) btnImprove = findViewById(R.id.btn_improve) val bitmap = BitmapFactory.decodeResource(this.resources, R.drawable.test) imageView.setImageBitmap(bitmap) try { model = Interpreter(loadModelFile()) } catch (e: IOException) { Log.e(TAG, "Failed to load model", e) } btnImprove.setOnClickListener { improveQuality(bitmap) } } private fun improveQuality(bitmap: Bitmap) { val inputBitmap = Bitmap.createScaledBitmap(bitmap, 224, 224, true) val input = convertBitmapToByteBuffer(inputBitmap) val output = Array(1) { Array(224) { Array(224) { FloatArray(3) } } } model.runForMultipleInputsOutputs(arrayOf(input), mapOf(0 to output)) val outputBitmap = Bitmap.createBitmap(224, 224, Bitmap.Config.ARGB_8888) for (i in 0 until 224) { for (j in 0 until 224) { val pixelValue = output[0][i][j] val pixel = -0x1000000 or (pixelValue[0].toInt() shl 16) or (pixelValue[1].toInt() shl 8) or pixelValue[2].toInt() outputBitmap.setPixel(i, j, pixel) } } imageView.setImageBitmap(outputBitmap) } private fun loadModelFile(): MappedByteBuffer { val fileDescriptor: AssetFileDescriptor = assets.openFd("lite-model_esrgan-tf2_1.tflite") val inputStream: FileInputStream = FileInputStream(fileDescriptor.fileDescriptor) val fileChannel: FileChannel = inputStream.channel val startOffset: Long = fileDescriptor.startOffset val declaredLength: Long = fileDescriptor.declaredLength return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength) } private fun convertBitmapToByteBuffer(bitmap: Bitmap): ByteBuffer { val inputShape = intArrayOf(1, 224, 224, 3) // Batch size, Height, Width, Depth val inputSize = 224 * 224 * 3 * 4 // Multiply by 4 to account for the batch dimension val byteBuffer = ByteBuffer.allocateDirect(inputSize) byteBuffer.order(ByteOrder.nativeOrder()) val pixels = IntArray(224 * 224) bitmap.getPixels(pixels, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height) var pixel = 0 for (i in 0 until 224) { for (j in 0 until 224) { val value = pixels[pixel++] byteBuffer.putFloat(((value shr 16) and 0xFF) / 255.0f) byteBuffer.putFloat(((value shr 8) and 0xFF) / 255.0f) byteBuffer.putFloat((value and 0xFF) / 255.0f) } } byteBuffer.rewind() return byteBuffer }}
يحدث خطا
FATAL EXCEPTION: main Process: com.dm.myapplication, PID: 11713 java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (input_0) with 30000 bytes from a Java Buffer with 602112 bytes. at org.tensorflow.lite.Tensor.throwIfSrcShapeIsIncompatible(Tensor.java:500) at org.tensorflow.lite.Tensor.setTo(Tensor.java:226) at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:200) at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:344) at com.dm.myapplication.MainActivity.improveQuality(MainActivity.kt:56) at com.dm.myapplication.MainActivity.onCreate0(MainActivity.kt:46) at com.dm.myapplication.MainActivity.lambda$A2vv6RaSUDqdxWrk71BKwGjPy6g(Unknown Source:0) at com.dm.myapplication.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
- Assistant
يبدو أن الخطأ يحدث بسبب التعارض بين حجم البيانات المدخلة وحجم الحقل الذي يتوقعه الموديل. يرجى التحقق من حجم الصورة المدخلة ومقارنتها مع الحجم المتوقع من الموديل، والتحقق من نوع الصورة (يجب أن تكون float32). كما يرجى ملاحظة أن النموذج يتطلب بيانات الصورة التي تم التعامل معها بطريقة معينة، وهي تحجيم الصورة بشكل ثنائي الأبعاد قبل تنفيذ النموذج. يرجى التحقق من تنفيذ هذا الإجراء أيضًا.