使用tflite_flutter在两个图像之间进行人脸识别比较,但在代码中存在问题

ymdaylpp  于 2023-04-07  发布在  Flutter
关注(0)|答案(1)|浏览(385)

用户将自拍,我将比较这张照片与后端照片,所以我有两个图像,我想验证是否是同一个人,我使用tflite_flutter 0.9.1并安装了install.bat这是我从chatGPT-4获得的功能

Future<bool> compareImages(String imagePath1, String imagePath2) async {
final inputImage1 = InputImage.fromFilePath(imagePath1);
final inputImage2 = InputImage.fromFilePath(imagePath2);

final faceDetector = GoogleMlKit.vision.faceDetector();
final faces1 = await faceDetector.processImage(inputImage1);
final faces2 = await faceDetector.processImage(inputImage2);

if (faces1.isEmpty || faces2.isEmpty) {
  return false;
}

// Load the TFLite model
const modelPath = 'mobilefacenet.tflite';
// final modelFile = File(modelPath);
final model =
  await Interpreter.fromAsset(modelPath, options: InterpreterOptions());

// Define input and output shapes
final inputShape = model.getInputTensor(0).shape;
final outputShape = model.getOutputTensor(0).shape;

// Prepare input tensors
final input1 = _prepareInputTensor(inputImage1, inputShape);
final input2 = _prepareInputTensor(inputImage2, inputShape);

// Run inference
final output1 = List.filled(outputShape[1], 0.0).reshape([1, outputShape[1]]);
final output2 = List.filled(outputShape[1], 0.0).reshape([1, outputShape[1]]);
dev.log("outputShape $outputShape");
dev.log("output1 $output1");
dev.log("output2 $output2");
model.run(input1, output1);
model.run(input2, output2);

// Compute the distance between the embeddings
final distance = _euclideanDistance(output1[0], output2[0]);

return distance < 0.6;
}

Float32List _prepareInputTensor(InputImage inputImage, List<int> inputShape) {
// Pre-process the input image according to the model's requirements
// For example: resize, normalize, etc.
final inputData = Float32List(
  inputShape[0] * inputShape[1] * inputShape[2] * inputShape[3]);
// Fill `inputData` with pre-processed image data
return inputData;
}

double _euclideanDistance(List<double> a, List<double> b) {
double sum = 0.0;
for (int i = 0; i < a.length; i++) {
double diff = a[i] - b[i];
sum += diff * diff;
}
return sqrt(sum);
}

#这是终端输出

[log] outputShape [1, 192]

E/tflite  (28254): tensorflow/lite/kernels/conv.cc:346 input->dims->size != 4 (1 != 4)

E/tflite  (28254): Node number 0 (CONV_2D) failed to prepare.

E/flutter (28254): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad 
state: failed precondition

E/flutter (28254): #0      checkState

check.dart:74
E/flutter (28254): #1      Interpreter.allocateTensors

interpreter.dart:142
E/flutter (28254): #2      Interpreter.runForMultipleInputs

interpreter.dart:180
E/flutter (28254): #3      Interpreter.run
interpreter.dart:157
E/flutter (28254): #4      compareImages

face_detection_helper.dart:40

E/flutter (28254): <asynchronous suspension>

E/flutter (28254): #5      _ISignSignOutScreenState.getCurrentView.<anonymous closure>

ISignSignOutScreen.dart:493

E/flutter (28254): <asynchronous suspension>

E/flutter (28254): #6      _CustomButtonState.build.<anonymous closure>

CustomButton.dart:84

E/flutter (28254): <asynchronous suspension>

E/flutter (28254):

[log] output1 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 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0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]

我也想知道为什么所有的输出都是零如何修复它

tsm1rwdh

tsm1rwdh1#

this is chatGPT-4 answer似乎问题与输入Tensor维度有关,错误提示输入Tensor应该有4个维度,但它只有1个。要解决这个问题,应该检查输入Tensor的维度,并将其重塑为所需的维度。在调用runForMultipleInputs或run之前,输入Tensor的形状应该是[1,height,width,channels],其中height和width是输入图像的维度,channels是颜色通道的数量(对于RGB图像通常为3)。

您可以使用reshape函数对输入Tensor进行整形。以下是如何在将输入图像传递到TFLite模型之前对其进行预处理的示例:

import 'dart:typed_data';
import 'package:image/image.dart' as img;
import 'package:tflite_flutter/tflite_flutter.dart';

// Load the image file and decode it
img.Image image1 = img.decodeImage(await File('path/to/image1.jpg').readAsBytes());
img.Image image2 = img.decodeImage(await File('path/to/image2.jpg').readAsBytes());

// Resize the images to the dimensions expected by the model (e.g., 128x128)
img.Image resizedImage1 = img.copyResize(image1, height: 128, width: 128);
img.Image resizedImage2 = img.copyResize(image2, height: 128, width: 128);

// Convert the images to Float32List format
Float32List input1 = imageToFloat32List(resizedImage1);
Float32List input2 = imageToFloat32List(resizedImage2);

// Run the model
interpreter.runForMultipleInputs([input1, input2], output);

// Helper function to convert an image to a Float32List
Float32List imageToFloat32List(img.Image image) {
  var convertedBytes = Float32List(1 * image.width * image.height * 3);
  var buffer = Float32List.view(convertedBytes.buffer);
  int pixelIndex = 0;
  for (int i = 0; i < image.height; i++) {
    for (int j = 0; j < image.width; j++) {
      int pixel = image.getPixel(j, i);
      buffer[pixelIndex++] = (img.getRed(pixel)) / 255.0;
      buffer[pixelIndex++] = (img.getGreen(pixel)) / 255.0;
      buffer[pixelIndex++] = (img.getBlue(pixel)) / 255.0;
    }
  }
  return convertedBytes;
}

确保根据模型预期的输入大小调整尺寸(例如,128 x128)。这应该可以解决问题,并且您应该能够运行TFLite模型而不会出错。

相关问题