Compress face thumbnail in regular isolate
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data' show Uint8List;
|
||||
|
||||
import "package:computer/computer.dart";
|
||||
import "package:logging/logging.dart";
|
||||
import "package:photos/models/ml/face/box.dart";
|
||||
import "package:photos/services/isolate_functions.dart";
|
||||
import "package:photos/services/isolate_service.dart";
|
||||
import "package:photos/utils/image_ml_util.dart";
|
||||
|
||||
final Computer _computer = Computer.shared();
|
||||
|
||||
class FaceThumbnailGenerator extends SuperIsolate {
|
||||
@override
|
||||
Logger get logger => _logger;
|
||||
@@ -36,12 +39,25 @@ class FaceThumbnailGenerator extends SuperIsolate {
|
||||
) async {
|
||||
final List<Map<String, dynamic>> faceBoxesJson =
|
||||
faceBoxes.map((box) => box.toJson()).toList();
|
||||
return await runInIsolate(
|
||||
final List<Uint8List> faces = await runInIsolate(
|
||||
IsolateOperation.generateFaceThumbnails,
|
||||
{
|
||||
'imagePath': imagePath,
|
||||
'faceBoxesList': faceBoxesJson,
|
||||
},
|
||||
).then((value) => value.cast<Uint8List>());
|
||||
final compressedFaces = <Future<Uint8List>>[];
|
||||
for (final face in faces) {
|
||||
if (!shouldCompressFaceThumbnail(face)) {
|
||||
compressedFaces.add(Future.value(face));
|
||||
} else {
|
||||
final compressedFace = _computer.compute<Map, Uint8List>(
|
||||
compressFaceThumbnail,
|
||||
param: {'pngBytes': face},
|
||||
);
|
||||
compressedFaces.add(compressedFace);
|
||||
}
|
||||
}
|
||||
return await Future.wait(compressedFaces);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -541,19 +541,15 @@ Future<Uint8List> _cropAndEncodeCanvas(
|
||||
width: width,
|
||||
height: height,
|
||||
);
|
||||
final pngBytes = await _encodeImageToPng(croppedImage);
|
||||
return await _compressFaceThumbnailIfNeeded(pngBytes);
|
||||
return await _encodeImageToPng(croppedImage);
|
||||
}
|
||||
|
||||
/// Compresses the face thumbnail if it's too large in size.
|
||||
///
|
||||
/// Returns compressed bytes if the original size exceeds [_maxFaceThumbnailSizeBytes],
|
||||
/// otherwise returns the original bytes.
|
||||
Future<Uint8List> _compressFaceThumbnailIfNeeded(Uint8List pngBytes) async {
|
||||
if (pngBytes.length <= _maxFaceThumbnailSizeBytes) {
|
||||
return pngBytes;
|
||||
}
|
||||
bool shouldCompressFaceThumbnail(Uint8List pngBytes) {
|
||||
return pngBytes.length > _maxFaceThumbnailSizeBytes;
|
||||
}
|
||||
|
||||
Future<Uint8List> compressFaceThumbnail(Map args) async {
|
||||
final pngBytes = args['pngBytes'] as Uint8List;
|
||||
try {
|
||||
final compressedBytes = await FlutterImageCompress.compressWithList(
|
||||
pngBytes,
|
||||
|
||||
Reference in New Issue
Block a user