Rajatkalsotra / Face-Recognition-Flutter

Realtime face recognition with Flutter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Compare from base64 image

andrefantinato opened this issue · comments

Hello,

I'm trying compare image from base64 with the camera, but I'm stuck in this step:

//GET IMAGE BASE64
 var strImage = this.widget.base64Photo.substring(22);
 Uint8List bytes = base64.decode(strImage);

//CONVERT TO LIST AS YOUR EXEMPLE CODE
  imglib.Image img2 = imglib.Image.fromBytes(128, 128, bytes);
  List input = imageToByteListFloat32(img2, 112, 128, 128);
  input = input.reshape([1, 112, 112, 3]);
  List output = List(1 * 192).reshape([1, 192]);
  interpreter.run(input, output);
  output = output.reshape([192]);
  e2 = List.from(output);

and compare Lists, where currEmb is my face detection from camera:

String compare(List currEmb) {
    double minDist = 999;
    double currDist = 0.0;
    String predRes = 'NOT RECOGNIZED';
    currDist = euclideanDistance(e2, currEmb);

    if (currDist <= threshold && currDist < minDist) {
      minDist = currDist;
      predRes = 'RECOGNIZED';
    }

    print(minDist.toString() + ' ' + predRes);
    return predRes;
}

but always return NOT RECOGNIZED. Any sugestion for recognize from base64?

  imglib.Image _convertCameraImage(
      CameraImage image, CameraLensDirection _dir) {
    int width = image.width;
    int height = image.height;
    // imglib -> Image package from https://pub.dartlang.org/packages/image
    var img = imglib.Image(width, height); // Create Image buffer
    const int hexFF = 0xFF000000;
    final int uvyButtonStride = image.planes[1].bytesPerRow;
    final int uvPixelStride = image.planes[1].bytesPerPixel!;
    for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {
        final int uvIndex =
            uvPixelStride * (x / 2).floor() + uvyButtonStride * (y / 2).floor();
        final int index = y * width + x;
        final yp = image.planes[0].bytes[index];
        final up = image.planes[1].bytes[uvIndex];
        final vp = image.planes[2].bytes[uvIndex];
        // Calculate pixel color
        int r = (yp + vp * 1436 / 1024 - 179).round().clamp(0, 255);
        int g = (yp - up * 46549 / 131072 + 44 - vp * 93604 / 131072 + 91)
            .round()
            .clamp(0, 255);
        int b = (yp + up * 1814 / 1024 - 227).round().clamp(0, 255);
        // color: 0x FF  FF  FF  FF
        //           A   B   G   R
        img.data[index] = hexFF | (b << 16) | (g << 8) | r;
      }
    }
    var img1 = (_dir == CameraLensDirection.front)
        ? imglib.copyRotate(img, -90)
        : imglib.copyRotate(img, 90);
    return img1;
  }
 String _recog(imglib.Image img) {
    List input = imageToByteListFloat32(img, 112, 128, 128);
    input = input.reshape([1, 112, 112, 3]);
    List output = List.filled(1 * 192, null, growable: false).reshape([1, 192]);
    interpreter.run(input, output);
    output = output.reshape([192]);
    e1 = List.from(output);
    return compare(e1!).toUpperCase();
  }
  String compare(List currEmb) {
    if (data.length == 0) return "No Face saved";
    double minDist = 999;
    double currDist = 0.0;
    String predRes = "NOT RECOGNIZED";
    for (String label in data.keys) {
      currDist = euclideanDistance(data[label], currEmb);
      if (currDist <= threshold && currDist < minDist) {
        minDist = currDist;
        predRes = label;
      }
    }
    // print(minDist.toString() + " " + predRes);
    return predRes;
  }