我使用了示例中给出的确切代码片段。它仍然抛出错误:未处理的异常:类型 '_InternalLinkedHashMap
List<Face> _faces = await client.detect(
image: _image,
returnFaceAttributes: FaceAttributeType.values,
returnFaceLandmarks: true,
);
有人能告诉我如何纠正这个问题并从 API 获取 faceAttribute 值,无论 client.detect 返回什么。
完整代码如下:
final endpoint = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0";
final key = "3080b54bd7c64a27801465608ca06a3e";
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
File image;
List<Face> faces = [];
bool isLoading = false;
final client = FaceServiceClient(key, serviceHost: endpoint);
void _incrementCounter() async {
setState(() {
image = null;
faces = [];
});
var maxWidth = MediaQuery.of(context).size.width;
File _image = await ImagePicker.pickImage(
source: ImageSource.gallery,
maxWidth: maxWidth,
);
if (_image != null) {
_getImageSize(Image.file(_image, fit: BoxFit.fitWidth)).then((Size size) {
print('CROPPED IMAGE WIDTH: ${size.width} HEIGHT: ${size.height}');
});
setState(() {
image = _image;
isLoading = true;
});
List<Face> _faces = await client.detect(
image: _image,
returnFaceAttributes: FaceAttributeType.values,
returnFaceLandmarks: true,
);
print('DETECTED FACES: ${_faces.length}');
setState(() {
faces = _faces;
isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: <Widget>[
_buildImage(context),
FlatButton(
child: Text('Clear'),
onPressed: () {
setState(() {
image = null;
faces = null;
});
},
)
],
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
Widget _buildImage(BuildContext context) {
if (image != null) {
return Container(
// height: MediaQuery.of(context).size.width,
width: MediaQuery.of(context).size.width,
child: FutureBuilder<Size>(
future: _getImageSize(Image.file(image, fit: BoxFit.fitWidth)),
builder: (BuildContext context, AsyncSnapshot<Size> snapshot) {
if (snapshot.hasData) {
return Stack(children: [
Container(
width: snapshot.data.width,
height: snapshot.data.height,
foregroundDecoration: FaceDetectDecoration(
faces,
snapshot.data,
),
child: Image.file(
image,
fit: BoxFit.fitWidth,
),
),
isLoading
? Center(
child: CircularProgressIndicator(),
)
: Container(),
]);
} else {
return Text('Please wait...');
}
},
),
);
}
return Container(
height: MediaQuery.of(context).size.height - 250.0,
alignment: Alignment.center,
child: Text('no picture'),
);
}
Future<Size> _getImageSize(Image image) {
Completer<Size> completer = new Completer<Size>();
image.image.resolve(new ImageConfiguration()).addListener(
(ImageInfo info, bool _) => completer.complete(
Size(info.image.width.toDouble(), info.image.height.toDouble())));
return completer.future;
}
}
答案 0 :(得分:0)
问题似乎是由代码中 FaceAttributeType.values
的类型不匹配引起的。
您将代码更改为:
Map data= new Map<String, dynamic>.from(FaceAttributeType.values);
List<Face> _faces = await client.detect(
image: _image,
returnFaceAttributes: data,
returnFaceLandmarks: true,
);
然后再试一次。