【问题标题】:Flutter TFLite Error: "metal_delegate.h" File Not FoundFlutter TFLite 错误:找不到“metal_delegate.h”文件
【发布时间】:2020-07-28 17:05:02
【问题描述】:

我正在尝试在我的项目中使用 Tensorflow Lite ML 模型,但不幸的是,我在运行我的项目时遇到了错误:

↳
    ** BUILD FAILED **
Xcode's output:
↳
    /Users/tejasravishankar/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/tflite-1.1.1/ios/Classes/TflitePlugin.mm:21:9: fatal error: 'metal_delegate.h' file not found
    #import "metal_delegate.h"
            ^~~~~~~~~~~~~~~~~~
    1 error generated.
    note: Using new build system
    note: Building targets in parallel
    note: Planning build
    note: Constructing build description
Could not build the application for the simulator.
Error launching application on iPhone 11 Pro Max.

我尝试过flutter clean,并尝试从ios 目录中删除PodfilePodfile.lock,但这并没有改变任何东西。

这是我的代码:

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:tflite/tflite.dart';
import 'package:image_picker/image_picker.dart';

void main() => runApp(TensorflowApp());

const String pet = 'Pet Recognizer';

class TensorflowApp extends StatefulWidget {
  @override
  _TensorflowAppState createState() => _TensorflowAppState();
}

class _TensorflowAppState extends State<TensorflowApp> {
  String _model = pet;
  File _image;
  double _imageWidth;
  double _imageHeight;
  // ignore: unused_field
  bool _isLoading = false;
  List _predictions;

  _selectFromImagePicker() async {
    PickedFile _pickedImage =
        await ImagePicker().getImage(source: ImageSource.gallery);
    File _pickedImageFile = _pickedFileFormatter(_pickedImage);
    if (_pickedImage == null) {
      return;
    } else {
      setState(() {
        _isLoading = true;
      });
      _predictImage(_pickedImageFile);
    }
  }

  _predictImage(File image) async {
    await _petRecognizerV1(image);
    FileImage(image).resolve(ImageConfiguration()).addListener(
      ImageStreamListener(
        (ImageInfo info, bool _) {
          setState(() {
            _imageWidth = info.image.height.toDouble();
            _imageHeight = info.image.height.toDouble();
          });
        },
      ),
    );

    setState(() {
      _image = image;
      _isLoading = false;
    });
  }

  _petRecognizerV1(File image) async {
    List<dynamic> _modelPredictions = await Tflite.detectObjectOnImage(
      path: image.path,
      model: pet,
      threshold: 0.3,
      imageMean: 0.0,
      imageStd: 255.0,
      numResultsPerClass: 1,
    );
    setState(() {
      _predictions = _modelPredictions;
    });
  }

  _pickedFileFormatter(PickedFile pickedFile) {
    File formattedFile = File(pickedFile.path);
    return formattedFile;
  }

  renderBoxes(Size screen) {
    if (_predictions == null) {
      return [];
    } else {
      if (_imageHeight == null || _imageWidth == null) {
        return [];
      }
      double factorX = screen.width;
      double factorY = _imageHeight / _imageHeight * screen.width;

      return _predictions.map((prediction) {
        return Positioned(
          left: prediction['rect']['x'] * factorX,
          top: prediction['rect']['y'] * factorY,
          width: prediction['rect']['w'] * factorX,
          height: prediction['rect']['h'] * factorY,
          child: Container(
            decoration: BoxDecoration(
              border: Border.all(color: Colors.green, width: 3.0),
            ),
            child: Text(
              '${prediction["detectedClass"]} ${(prediction["confidenceInClass"]) * 100.toStringAsFixed(0)}',
              style: TextStyle(
                background: Paint()..color = Colors.green,
                color: Colors.white,
                fontSize: 15.0,
              ),
            ),
          ),
        );
      }).toList();
    }
  }

  @override
  void initState() {
    super.initState();
    _isLoading = true;
    _loadModel().then((value) {
      setState(() {
        _isLoading = false;
      });
    });
  }

  _loadModel() async {
    Tflite.close();
    try {
      String response;
      if (_model == pet) {
        response = await Tflite.loadModel(
          model: 'assets/pet_recognizer.tflite',
          labels: 'assets/pet_recognizer.txt',
        );
      }
    } catch (error) {
      print(error);
    }
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;

    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.white,
          title: Text('TFLite Test'),
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.image),
          tooltip: 'Pick Image From Gallery',
          onPressed: () => _selectFromImagePicker,
        ),
        body: Stack(
          children: <Widget>[
            Positioned(
              top: 0.0,
              left: 0.0,
              width: size.width,
              child: _image == null
                  ? Text('No Image Selected')
                  : Image.file(_image),
            ),
            renderBoxes(size),
          ],
        ),
      ),
    );
  }
}


我个人认为我的代码没有问题,我尝试运行

flutter pub get

成功代码 0 成功运行了几次,但还没有解决问题...

我不太确定该怎么做才能继续进行此操作,并且非常感谢我收到的任何帮助!谢谢,干杯,感谢您的帮助:)

【问题讨论】:

    标签: tensorflow flutter machine-learning dart tensor


    【解决方案1】:

    将 TensorFlowLiteC 降级到 2.2.0 对我有用

    1. 将 /ios/Podfile.lock 中的 TensorFlowLiteC 降级到 2.2.0
    2. 在 /ios 文件夹中运行 pod install

    https://github.com/shaqian/flutter_tflite/issues/139#issuecomment-668252599

    【讨论】:

    • 如何降级?
    • 哦,别担心
    • 有没有办法在不显式更改 .lock 文件的情况下做到这一点
    【解决方案2】:

    在您的Podfile 中添加到末尾:

    pod 'TensorFlowLiteC', '2.2.0'
    

    删除Podfile.lock

    删除Pods文件夹

    删除.symlinks文件夹

    flutter run
    

    【讨论】:

      【解决方案3】:

      我通过从 TensorFlowLiteC 2.5.0 降级到 2.2.0 解决了我的问题

      【讨论】:

        猜你喜欢
        • 2021-11-18
        • 2021-07-21
        • 2021-03-26
        • 2021-11-20
        • 1970-01-01
        • 2020-01-04
        • 2021-09-04
        • 2021-03-06
        • 1970-01-01
        相关资源
        最近更新 更多