-
Notifications
You must be signed in to change notification settings - Fork 765
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
004cb41
commit a8b4da8
Showing
45 changed files
with
803 additions
and
22 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
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.
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
68 changes: 68 additions & 0 deletions
68
packages/example/lib/vision_detector_views/painters/subject_segmentation_painter.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,68 @@ | ||
import 'package:camera/camera.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:google_mlkit_subject_segmentation/google_mlkit_subject_segmentation.dart'; | ||
import 'coordinates_translator.dart'; | ||
|
||
class SubjectSegmentationPainter extends CustomPainter { | ||
final SubjectSegmenterMask mask; | ||
final Size imageSize; | ||
final Color color = Colors.red; | ||
final InputImageRotation rotation; | ||
final CameraLensDirection cameraLensDirection; | ||
|
||
SubjectSegmentationPainter( | ||
this.mask, | ||
this.imageSize, | ||
this.rotation, | ||
this.cameraLensDirection, | ||
); | ||
|
||
@override | ||
void paint(Canvas canvas, Size size) { | ||
final int width = mask.width; | ||
final int height = mask.height; | ||
final List<Subject> subjects = mask.subjects; | ||
|
||
final paint = Paint()..style = PaintingStyle.fill; | ||
|
||
for (final Subject subject in subjects) { | ||
final int startX = subject.startX; | ||
final int startY = subject.startY; | ||
final int subjectWidth = subject.subjectWidth; | ||
final int subjectHeight = subject.subjectHeight; | ||
final List<double> confidences = subject.confidences; | ||
|
||
for (int y = 0; y < subjectHeight; y++) { | ||
for (int x = 0; y < subjectWidth; x++) { | ||
final int absoluteX = startX; | ||
final int absoluteY = startY; | ||
|
||
final int tx = translateX( | ||
absoluteX.toDouble(), | ||
size, | ||
Size(width.toDouble(), height.toDouble()), | ||
rotation, | ||
cameraLensDirection) | ||
.round(); | ||
|
||
final int ty = translateY( | ||
absoluteY.toDouble(), | ||
size, | ||
Size(width.toDouble(), height.toDouble()), | ||
rotation, | ||
cameraLensDirection) | ||
.round(); | ||
|
||
final double opacity = confidences[(y * subjectWidth) + x] * 0.5; | ||
paint.color = color.withOpacity(opacity); | ||
canvas.drawCircle(Offset(tx.toDouble(), ty.toDouble()), 2, paint); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@override | ||
bool shouldRepaint(SubjectSegmentationPainter oldDelegate) { | ||
return oldDelegate.mask != mask; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
packages/example/lib/vision_detector_views/subject_segmenter_view.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,68 @@ | ||
import 'package:camera/camera.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:google_mlkit_subject_segmentation/google_mlkit_subject_segmentation.dart'; | ||
|
||
import 'detector_view.dart'; | ||
import 'painters/subject_segmentation_painter.dart'; | ||
|
||
class SubjectSegmenterView extends StatefulWidget { | ||
@override | ||
State<SubjectSegmenterView> createState() => _SubjectSegmenterViewState(); | ||
} | ||
|
||
class _SubjectSegmenterViewState extends State<SubjectSegmenterView> { | ||
final SubjectSegmenter _segmenter = SubjectSegmenter(); | ||
bool _canProcess = true; | ||
bool _isBusy = false; | ||
CustomPaint? _customPaint; | ||
String? _text; | ||
var _cameraLensDirection = CameraLensDirection.back; | ||
|
||
@override | ||
void dispose() async { | ||
_canProcess = false; | ||
_segmenter.close(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return DetectorView( | ||
title: 'Subject Segmenter', | ||
customPaint: _customPaint, | ||
text: _text, | ||
onImage: _processImage, | ||
initialCameraLensDirection: _cameraLensDirection, | ||
onCameraLensDirectionChanged: (value) => _cameraLensDirection = value, | ||
); | ||
} | ||
|
||
Future<void> _processImage(InputImage inputImage) async { | ||
if (!_canProcess) return; | ||
if (_isBusy) return; | ||
_isBusy = true; | ||
setState(() { | ||
_text = ''; | ||
}); | ||
final SubjectSegmenterMask mask = await _segmenter.processImage(inputImage); | ||
if (inputImage.metadata?.size != null && | ||
inputImage.metadata?.rotation != null) { | ||
final painter = SubjectSegmentationPainter( | ||
mask, | ||
inputImage.metadata!.size, | ||
inputImage.metadata!.rotation, | ||
_cameraLensDirection, | ||
); | ||
_customPaint = CustomPaint(painter: painter); | ||
} else { | ||
// TODO: set _customPaint to draw on top of image | ||
_text = 'There is a mask with ${mask.subjects.length} subjects'; | ||
|
||
_customPaint = null; | ||
} | ||
_isBusy = false; | ||
if (mounted) { | ||
setState(() {}); | ||
} | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Miscellaneous | ||
*.class | ||
*.log | ||
*.pyc | ||
*.swp | ||
.DS_Store | ||
.atom/ | ||
.buildlog/ | ||
.history | ||
.svn/ | ||
migrate_working_dir/ | ||
|
||
# IntelliJ related | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ | ||
|
||
# The .vscode folder contains launch configuration and tasks you configure in | ||
# VS Code which you may wish to be included in version control, so this line | ||
# is commented out by default. | ||
#.vscode/ | ||
|
||
# Flutter/Dart/Pub related | ||
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. | ||
/pubspec.lock | ||
**/doc/api/ | ||
.dart_tool/ | ||
build/ |
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,33 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled and should not be manually edited. | ||
|
||
version: | ||
revision: "300451adae589accbece3490f4396f10bdf15e6e" | ||
channel: "stable" | ||
|
||
project_type: plugin | ||
|
||
# Tracks metadata for the flutter migrate command | ||
migration: | ||
platforms: | ||
- platform: root | ||
create_revision: 300451adae589accbece3490f4396f10bdf15e6e | ||
base_revision: 300451adae589accbece3490f4396f10bdf15e6e | ||
- platform: android | ||
create_revision: 300451adae589accbece3490f4396f10bdf15e6e | ||
base_revision: 300451adae589accbece3490f4396f10bdf15e6e | ||
- platform: ios | ||
create_revision: 300451adae589accbece3490f4396f10bdf15e6e | ||
base_revision: 300451adae589accbece3490f4396f10bdf15e6e | ||
|
||
# User provided section | ||
|
||
# List of Local paths (relative to this file) that should be | ||
# ignored by the migrate tool. | ||
# | ||
# Files that are not part of the templates will be ignored by default. | ||
unmanaged_files: | ||
- 'lib/main.dart' | ||
- 'ios/Runner.xcodeproj/project.pbxproj' |
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,3 @@ | ||
## 0.0.1 | ||
|
||
* Initial release. |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Francisco Bernal, Bharat Biradar and Benson Arafat. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Oops, something went wrong.