Untitled

 avatar
unknown
plain_text
3 months ago
1.5 kB
5
Indexable
void _cropAndDisplayFace(CameraImage image, Rect boundingBox) async {
    if (_interpreter == null || _inputTensor == null || _outputTensor == null) {
      print('Model or tensors not initialized');
      return;
    }

    final croppedFace = await cropFaceFromImage(image, boundingBox);

    if (croppedFace != null) {
      final faceImage = img.decodePng(croppedFace);

      if (faceImage != null) {
        img.Image? resizedImage = img.copyResize(faceImage,
            width: _inputTensor!.shape[1], height: _inputTensor!.shape[2]);

        // Create input buffer with float32 normalization
        final imageMatrix = List.generate(
          resizedImage.height,
          (y) => List.generate(
            resizedImage.width,
            (x) {
              final pixel = resizedImage.getPixel(x, y);
              return [
                pixel.r.toDouble(),
                pixel.g.toDouble(),
                pixel.b.toDouble()
              ];
            },
          ),
        );
        
        final input = [imageMatrix];
        final outputValue = [List<double>.filled(_outputTensor!.shape[1], 0.0)];

        try {
          _interpreter!.run(input, outputValue);

          print(outputValue);

          final gender = outputValue[0][0] > 0.5 ? 'Male' : 'Female';

          print('Predicted gender: $gender');
          _addDetectedGender(gender);
        } catch (e) {
          print('Error running inference: $e');
        }
      }
    }
  }
Editor is loading...
Leave a Comment