Skip to content

Commit 7fce946

Browse files
committed
add gpu support
1 parent 8fe010f commit 7fce946

17 files changed

+209
-118
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ void dispose() {
7575
```txt
7676
Live: true
7777
Score (P(spoof)): 0.023
78-
Clarity (laplacian): 1812
79-
Inference time: 29 ms
78+
Clarity (laplacian): 5939.0
79+
Inference time: 66 ms
8080
```
8181

8282
## ⚙️ Settings
File renamed without changes.

example/assets/sample_face_2.jpg

773 KB
Loading

example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
3173E160E5449276B2B212C5 /* Pods-RunnerTests.release.xcconfig */,
9898
98AF406AFF01706C9F422A1A /* Pods-RunnerTests.profile.xcconfig */,
9999
);
100-
name = Pods;
101100
path = Pods;
102101
sourceTree = "<group>";
103102
};
@@ -453,14 +452,14 @@
453452
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
454453
CLANG_ENABLE_MODULES = YES;
455454
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
456-
DEVELOPMENT_TEAM = D3YZ8WV7Q2;
455+
DEVELOPMENT_TEAM = QSNJ9UDHM7;
457456
ENABLE_BITCODE = NO;
458457
INFOPLIST_FILE = Runner/Info.plist;
459458
LD_RUNPATH_SEARCH_PATHS = (
460459
"$(inherited)",
461460
"@executable_path/Frameworks",
462461
);
463-
PRODUCT_BUNDLE_IDENTIFIER = com.example.example;
462+
PRODUCT_BUNDLE_IDENTIFIER = dev.knottx.liveness;
464463
PRODUCT_NAME = "$(TARGET_NAME)";
465464
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
466465
SWIFT_VERSION = 5.0;
@@ -636,14 +635,14 @@
636635
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
637636
CLANG_ENABLE_MODULES = YES;
638637
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
639-
DEVELOPMENT_TEAM = D3YZ8WV7Q2;
638+
DEVELOPMENT_TEAM = QSNJ9UDHM7;
640639
ENABLE_BITCODE = NO;
641640
INFOPLIST_FILE = Runner/Info.plist;
642641
LD_RUNPATH_SEARCH_PATHS = (
643642
"$(inherited)",
644643
"@executable_path/Frameworks",
645644
);
646-
PRODUCT_BUNDLE_IDENTIFIER = com.example.example;
645+
PRODUCT_BUNDLE_IDENTIFIER = dev.knottx.liveness;
647646
PRODUCT_NAME = "$(TARGET_NAME)";
648647
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
649648
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
@@ -659,14 +658,14 @@
659658
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
660659
CLANG_ENABLE_MODULES = YES;
661660
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
662-
DEVELOPMENT_TEAM = D3YZ8WV7Q2;
661+
DEVELOPMENT_TEAM = QSNJ9UDHM7;
663662
ENABLE_BITCODE = NO;
664663
INFOPLIST_FILE = Runner/Info.plist;
665664
LD_RUNPATH_SEARCH_PATHS = (
666665
"$(inherited)",
667666
"@executable_path/Frameworks",
668667
);
669-
PRODUCT_BUNDLE_IDENTIFIER = com.example.example;
668+
PRODUCT_BUNDLE_IDENTIFIER = dev.knottx.liveness;
670669
PRODUCT_NAME = "$(TARGET_NAME)";
671670
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
672671
SWIFT_VERSION = 5.0;

example/ios/Runner/Info.plist

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>CADisableMinimumFrameDurationOnPhone</key>
6+
<true/>
57
<key>CFBundleDevelopmentRegion</key>
68
<string>$(DEVELOPMENT_LANGUAGE)</string>
79
<key>CFBundleDisplayName</key>
@@ -24,6 +26,8 @@
2426
<string>$(FLUTTER_BUILD_NUMBER)</string>
2527
<key>LSRequiresIPhoneOS</key>
2628
<true/>
29+
<key>UIApplicationSupportsIndirectInputEvents</key>
30+
<true/>
2731
<key>UILaunchStoryboardName</key>
2832
<string>LaunchScreen</string>
2933
<key>UIMainStoryboardFile</key>
@@ -41,9 +45,5 @@
4145
<string>UIInterfaceOrientationLandscapeLeft</string>
4246
<string>UIInterfaceOrientationLandscapeRight</string>
4347
</array>
44-
<key>CADisableMinimumFrameDurationOnPhone</key>
45-
<true/>
46-
<key>UIApplicationSupportsIndirectInputEvents</key>
47-
<true/>
4848
</dict>
4949
</plist>

example/lib/main.dart

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,54 @@ class MyHomePage extends StatefulWidget {
3232
}
3333

3434
class _MyHomePageState extends State<MyHomePage> {
35-
FlutterLiveness? liveness;
36-
LivenessResult? result;
35+
FlutterLiveness? _liveness;
36+
37+
LivenessResult? _result;
38+
39+
bool _isLoading = false;
3740

3841
@override
3942
void initState() {
4043
super.initState();
41-
_init();
44+
WidgetsBinding.instance.addPostFrameCallback((_) {
45+
_createLiveness();
46+
});
4247
}
4348

4449
@override
4550
void dispose() {
46-
liveness?.dispose();
51+
_liveness?.dispose();
4752
super.dispose();
4853
}
4954

50-
Future<void> _init() async {
51-
final l = await FlutterLiveness.create();
52-
setState(() => liveness = l);
55+
Future<void> _createLiveness() async {
56+
setState(() => _isLoading = true);
57+
_liveness = await FlutterLiveness.create();
58+
setState(() => _isLoading = false);
5359
}
5460

5561
Future<void> _run() async {
62+
setState(() => _isLoading = true);
5663
// Replace with your face crop bytes (asset/file/camera)
5764
final bytes = await rootBundle.load('assets/sample_face_2.jpg');
5865
final img = imglib.decodeImage(bytes.buffer.asUint8List());
59-
if (img == null || liveness == null) return;
60-
final res = await liveness!.analyze(img);
61-
setState(() => result = res);
66+
if (img == null || _liveness == null) return;
67+
final result = await _liveness!.analyze(img);
68+
setState(() {
69+
_result = result;
70+
_isLoading = false;
71+
});
6272
}
6373

6474
@override
6575
Widget build(BuildContext context) {
6676
final String message;
67-
if (result != null) {
77+
if (_result != null) {
6878
message = [
69-
'Liveness: ${result!.isLive ? 'LIVE' : 'SPOOF'}',
70-
'Score: ${result!.score.toStringAsFixed(3)}',
71-
'Laplacian: ${result!.laplacian.toStringAsFixed(1)}',
72-
'Duration: ${result!.duration.inMilliseconds} ms',
79+
'Liveness: ${_result!.isLive ? 'LIVE' : 'SPOOF'}',
80+
'Score: ${_result!.score.toStringAsFixed(3)}',
81+
'Laplacian: ${_result!.laplacian.toStringAsFixed(1)}',
82+
'Duration: ${_result!.duration.inMilliseconds} ms',
7383
].join('\n');
7484
} else {
7585
message = 'Tap ▶ to analyze';
@@ -80,13 +90,15 @@ class _MyHomePageState extends State<MyHomePage> {
8090
title: const Text('flutter_liveness demo'),
8191
),
8292
body: Center(
83-
child: Text(
84-
message,
85-
textAlign: TextAlign.center,
86-
),
93+
child: _isLoading
94+
? const CircularProgressIndicator()
95+
: Text(
96+
message,
97+
textAlign: TextAlign.center,
98+
),
8799
),
88100
floatingActionButton: FloatingActionButton(
89-
onPressed: _run,
101+
onPressed: _isLoading ? null : _run,
90102
child: const Icon(Icons.play_arrow),
91103
),
92104
);

example/pubspec.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,17 @@ packages:
9090
dependency: "direct dev"
9191
description:
9292
name: flutter_lints
93-
sha256: "5398f14efa795ffb7a33e9b6a08798b26a180edac4ad7db3f231e40f82ce11e1"
93+
sha256: "3105dc8492f6183fb076ccf1f351ac3d60564bff92e20bfc4af9cc1651f4e7e1"
9494
url: "https://pub.dev"
9595
source: hosted
96-
version: "5.0.0"
96+
version: "6.0.0"
9797
flutter_liveness:
9898
dependency: "direct main"
9999
description:
100100
path: ".."
101101
relative: true
102102
source: path
103-
version: "0.0.2"
103+
version: "1.1.0"
104104
flutter_test:
105105
dependency: "direct dev"
106106
description: flutter
@@ -142,10 +142,10 @@ packages:
142142
dependency: transitive
143143
description:
144144
name: lints
145-
sha256: c35bb79562d980e9a453fc715854e1ed39e24e7d0297a880ef54e17f9874a9d7
145+
sha256: a5e2b223cb7c9c8efdc663ef484fdd95bb243bff242ef5b13e26883547fce9a0
146146
url: "https://pub.dev"
147147
source: hosted
148-
version: "5.1.1"
148+
version: "6.0.0"
149149
matcher:
150150
dependency: transitive
151151
description:

example/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version: 1.0.0+1
66

77
environment:
88
sdk: ^3.9.2
9+
910
dependencies:
1011
flutter:
1112
sdk: flutter
@@ -17,7 +18,7 @@ dependencies:
1718
dev_dependencies:
1819
flutter_test:
1920
sdk: flutter
20-
flutter_lints: ^5.0.0
21+
flutter_lints: ^6.0.0
2122

2223
flutter:
2324
uses-material-design: true

lib/src/flutter_liveness.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class FlutterLiveness {
1616
static Future<FlutterLiveness> create({
1717
LivenessOptions options = const LivenessOptions(),
1818
}) async {
19-
final runner = await TFLiteRunner.create();
19+
final runner = await TFLiteRunner.create(useGpu: options.useGpu);
2020
final engine = LivenessEngine(runner, options);
2121
return FlutterLiveness._(engine, runner);
2222
}

lib/src/image_to_nhwc.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:image/image.dart' as img;
2+
3+
List<List<List<List<double>>>> toNHWC(img.Image resized) {
4+
final bytes = resized.getBytes(order: img.ChannelOrder.rgb);
5+
int index = 0;
6+
return [
7+
List.generate(
8+
224,
9+
(_) => List.generate(224, (_) {
10+
final r = bytes[index++].toInt() / 255.0;
11+
final g = bytes[index++].toInt() / 255.0;
12+
final b = bytes[index++].toInt() / 255.0;
13+
14+
return [r, g, b];
15+
}),
16+
),
17+
];
18+
}

0 commit comments

Comments
 (0)