-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86a1d83
commit ad79c61
Showing
16 changed files
with
405 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 220 additions & 0 deletions
220
packages/google_vision_flutter/example/lib/multiple_face_detections.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:google_vision/google_vision.dart' as gv; | ||
import 'package:google_vision_flutter/google_vision_flutter.dart'; | ||
|
||
class MultipleFaceDetection extends StatefulWidget { | ||
const MultipleFaceDetection({super.key, required this.title}); | ||
|
||
final String title; | ||
|
||
@override | ||
State<MultipleFaceDetection> createState() => _MyHomePageState(); | ||
} | ||
|
||
class _MyHomePageState extends State<MultipleFaceDetection> { | ||
FutureOr<gv.GoogleVision>? _googleVision; | ||
|
||
static const assetName1 = 'assets/young-man-smiling.jpg'; | ||
|
||
static const assetName2 = 'assets/dj.jpg'; | ||
|
||
final _processImage1 = Image.asset( | ||
assetName1, | ||
fit: BoxFit.fitWidth, | ||
height: 300, | ||
); | ||
|
||
final _processImage2 = Image.asset( | ||
assetName2, | ||
fit: BoxFit.fitWidth, | ||
height: 300, | ||
); | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
|
||
WidgetsBinding.instance.addPostFrameCallback((_) { | ||
_getGoogleVision(); | ||
}); | ||
} | ||
|
||
FutureOr<void> _getGoogleVision() async { | ||
_googleVision ??= await GoogleVision.withAsset( | ||
'assets/service_credentials.json', | ||
); | ||
|
||
setState(() {}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) => SafeArea( | ||
child: Scaffold( | ||
appBar: AppBar( | ||
leading: IconButton( | ||
icon: const Icon(Icons.arrow_back, color: Colors.black), | ||
onPressed: () => Navigator.of(context).pop(), | ||
), | ||
title: Text(widget.title), | ||
), | ||
body: SingleChildScrollView( | ||
child: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.start, | ||
children: [ | ||
Row( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
Column(children: [ | ||
const Padding( | ||
padding: EdgeInsets.all(8.0), | ||
child: Text(assetName1), | ||
), | ||
Padding( | ||
padding: const EdgeInsets.all(8.0), | ||
child: _processImage1, | ||
), | ||
]), | ||
Column(children: [ | ||
const Padding( | ||
padding: EdgeInsets.all(8.0), | ||
child: Text(assetName2), | ||
), | ||
Padding( | ||
padding: const EdgeInsets.all(8.0), | ||
child: _processImage2, | ||
), | ||
]), | ||
], | ||
), | ||
const Padding( | ||
padding: EdgeInsets.all(8.0), | ||
child: Text( | ||
'Processed images will appear below:', | ||
), | ||
), | ||
Padding( | ||
padding: const EdgeInsets.all(8.0), | ||
child: _googleVision == null | ||
? const CircularProgressIndicator() | ||
: GoogleVisionImageBuilder.faceDetection( | ||
googleVision: _googleVision!, | ||
imageProvider: _processImage1.image, | ||
builder: ( | ||
BuildContext context, | ||
List<FaceAnnotation>? faceAnnotations, | ||
) => | ||
CustomPaint( | ||
foregroundPainter: AnnotationPainter( | ||
faceAnnotations: faceAnnotations, | ||
), | ||
child: Image(image: _processImage1.image), | ||
), | ||
), | ||
), | ||
Padding( | ||
padding: const EdgeInsets.all(8.0), | ||
child: _googleVision == null | ||
? const CircularProgressIndicator() | ||
: GoogleVisionImageBuilder.faceDetection( | ||
googleVision: Future.value(_googleVision), | ||
imageProvider: _processImage2.image, | ||
builder: ( | ||
BuildContext context, | ||
List<FaceAnnotation>? faceAnnotations, | ||
) => | ||
CustomPaint( | ||
foregroundPainter: AnnotationPainter( | ||
faceAnnotations: faceAnnotations, | ||
), | ||
child: Image(image: _processImage2.image), | ||
), | ||
), | ||
) | ||
], | ||
), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
|
||
class AnnotationPainter extends CustomPainter { | ||
final List<FaceAnnotation>? faceAnnotations; | ||
|
||
AnnotationPainter({ | ||
required this.faceAnnotations, | ||
}); | ||
|
||
@override | ||
void paint( | ||
Canvas canvas, | ||
Size size, | ||
) { | ||
// face detection | ||
for (var faceAnnotation in faceAnnotations!) { | ||
drawAnnotationsRect( | ||
vertices: faceAnnotation.boundingPoly.vertices, | ||
canvas: canvas, | ||
); | ||
|
||
drawString( | ||
text: 'Face - ${(faceAnnotation.detectionConfidence * 100).toInt()}%', | ||
offset: faceAnnotation.boundingPoly.vertices.first.toOffset(), | ||
canvas: canvas, | ||
size: size, | ||
); | ||
} | ||
} | ||
|
||
void drawString({ | ||
required String text, | ||
required Offset offset, | ||
required Canvas canvas, | ||
required Size size, | ||
Color? color, | ||
}) { | ||
color ??= Colors.red.shade900; | ||
|
||
final tp = TextPainter( | ||
text: TextSpan( | ||
text: text, | ||
style: TextStyle(color: color), | ||
), | ||
textAlign: TextAlign.left, | ||
textDirection: TextDirection.ltr, | ||
); | ||
|
||
tp.layout(); | ||
|
||
tp.paint(canvas, offset); | ||
} | ||
|
||
void drawAnnotationsRect({ | ||
required List<Vertex> vertices, | ||
required Canvas canvas, | ||
Color? color, | ||
double strokeWidth = 1, | ||
}) { | ||
color ??= Colors.red.shade400; | ||
|
||
final paint = Paint(); | ||
|
||
paint.style = PaintingStyle.stroke; | ||
paint.strokeWidth = strokeWidth; | ||
paint.color = color; | ||
|
||
canvas.drawRect( | ||
Rect.fromPoints( | ||
vertices.first.toOffset(), | ||
vertices[2].toOffset(), | ||
), | ||
paint, | ||
); | ||
} | ||
|
||
@override | ||
bool shouldRepaint(CustomPainter oldDelegate) => false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.