Allow setting the iteration of ZeroDCE

This commit is contained in:
Ming Ming 2022-05-16 04:25:46 +08:00
parent c0c49361b8
commit afefa0c136
7 changed files with 90 additions and 10 deletions

View file

@ -1189,6 +1189,10 @@
"@enhanceLowLightTitle": {
"description": "Enhance a photo taken in low-light environment"
},
"enhanceLowLightParamBrightnessLabel": "Brightness",
"@enhanceLowLightParamBrightnessLabel": {
"description": "This parameter sets how much brighter the output will be"
},
"collectionEnhancedPhotosLabel": "Enhanced photos",
"@collectionEnhancedPhotosLabel": {
"description": "List photos enhanced by the app"

View file

@ -93,6 +93,7 @@
"enhanceIntroDialogTitle",
"enhanceIntroDialogDescription",
"enhanceLowLightTitle",
"enhanceLowLightParamBrightnessLabel",
"collectionEnhancedPhotosLabel",
"deletePermanentlyLocalConfirmationDialogContent",
"enhancePortraitBlurTitle",
@ -208,6 +209,7 @@
"enhanceIntroDialogTitle",
"enhanceIntroDialogDescription",
"enhanceLowLightTitle",
"enhanceLowLightParamBrightnessLabel",
"collectionEnhancedPhotosLabel",
"deletePermanentlyLocalConfirmationDialogContent",
"enhancePortraitBlurTitle",
@ -378,6 +380,7 @@
"enhanceIntroDialogTitle",
"enhanceIntroDialogDescription",
"enhanceLowLightTitle",
"enhanceLowLightParamBrightnessLabel",
"collectionEnhancedPhotosLabel",
"deletePermanentlyLocalConfirmationDialogContent",
"enhancePortraitBlurTitle",
@ -399,6 +402,7 @@
"enhanceIntroDialogTitle",
"enhanceIntroDialogDescription",
"enhanceLowLightTitle",
"enhanceLowLightParamBrightnessLabel",
"collectionEnhancedPhotosLabel",
"deletePermanentlyLocalConfirmationDialogContent",
"enhancePortraitBlurTitle",
@ -415,6 +419,7 @@
"enhanceIntroDialogTitle",
"enhanceIntroDialogDescription",
"enhanceLowLightTitle",
"enhanceLowLightParamBrightnessLabel",
"collectionEnhancedPhotosLabel",
"deletePermanentlyLocalConfirmationDialogContent",
"enhancePortraitBlurTitle",
@ -435,6 +440,7 @@
"enhanceIntroDialogTitle",
"enhanceIntroDialogDescription",
"enhanceLowLightTitle",
"enhanceLowLightParamBrightnessLabel",
"collectionEnhancedPhotosLabel",
"deletePermanentlyLocalConfirmationDialogContent",
"enhancePortraitBlurTitle",
@ -472,6 +478,7 @@
"enhanceIntroDialogTitle",
"enhanceIntroDialogDescription",
"enhanceLowLightTitle",
"enhanceLowLightParamBrightnessLabel",
"collectionEnhancedPhotosLabel",
"deletePermanentlyLocalConfirmationDialogContent",
"enhancePortraitBlurTitle",
@ -488,6 +495,7 @@
"enhanceIntroDialogTitle",
"enhanceIntroDialogDescription",
"enhanceLowLightTitle",
"enhanceLowLightParamBrightnessLabel",
"collectionEnhancedPhotosLabel",
"deletePermanentlyLocalConfirmationDialogContent",
"enhancePortraitBlurTitle",
@ -504,6 +512,7 @@
"enhanceIntroDialogTitle",
"enhanceIntroDialogDescription",
"enhanceLowLightTitle",
"enhanceLowLightParamBrightnessLabel",
"collectionEnhancedPhotosLabel",
"deletePermanentlyLocalConfirmationDialogContent",
"enhancePortraitBlurTitle",
@ -520,6 +529,7 @@
"enhanceIntroDialogTitle",
"enhanceIntroDialogDescription",
"enhanceLowLightTitle",
"enhanceLowLightParamBrightnessLabel",
"collectionEnhancedPhotosLabel",
"deletePermanentlyLocalConfirmationDialogContent",
"enhancePortraitBlurTitle",
@ -536,6 +546,7 @@
"enhanceIntroDialogTitle",
"enhanceIntroDialogDescription",
"enhanceLowLightTitle",
"enhanceLowLightParamBrightnessLabel",
"collectionEnhancedPhotosLabel",
"deletePermanentlyLocalConfirmationDialogContent",
"enhancePortraitBlurTitle",

View file

@ -55,6 +55,7 @@ class EnhanceHandler {
file.filename,
Pref().getEnhanceMaxWidthOr(),
Pref().getEnhanceMaxHeightOr(),
args["iteration"] ?? 8,
headers: {
"Authorization": Api.getAuthorizationHeaderValue(account),
},
@ -180,13 +181,64 @@ class EnhanceHandler {
BuildContext context, _Algorithm selected) async {
switch (selected) {
case _Algorithm.zeroDce:
return {};
return _getZeroDceArgs(context);
case _Algorithm.deepLab3Portrait:
return _getDeepLab3PortraitArgs(context);
}
}
Future<Map<String, dynamic>?> _getZeroDceArgs(BuildContext context) async {
var current = .8;
final iteration = await showDialog<int>(
context: context,
builder: (context) => AppTheme(
child: AlertDialog(
title: Text(L10n.global().enhanceLowLightParamBrightnessLabel),
contentPadding: const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 0),
content: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisSize: MainAxisSize.max,
children: [
Icon(
Icons.brightness_low,
color: AppTheme.getSecondaryTextColor(context),
),
Expanded(
child: StatefulSlider(
initialValue: current,
onChangeEnd: (value) {
current = value;
},
),
),
Icon(
Icons.brightness_high,
color: AppTheme.getSecondaryTextColor(context),
),
],
),
],
),
actions: [
TextButton(
onPressed: () {
final iteration = (current * 10).round().clamp(1, 10);
Navigator.of(context).pop(iteration);
},
child: Text(L10n.global().enhanceButtonLabel),
),
],
),
),
);
_log.info("[_getZeroDceArgs] iteration: $iteration");
return iteration?.run((it) => {"iteration": it});
}
Future<Map<String, dynamic>?> _getDeepLab3PortraitArgs(
BuildContext context) async {
var current = .5;

View file

@ -25,6 +25,7 @@ class ImageProcessorChannelHandler(context: Context) :
call.argument("filename")!!,
call.argument("maxWidth")!!,
call.argument("maxHeight")!!,
call.argument("iteration")!!,
result
)
} catch (e: Throwable) {
@ -62,10 +63,13 @@ class ImageProcessorChannelHandler(context: Context) :
private fun zeroDce(
fileUrl: String, headers: Map<String, String>?, filename: String,
maxWidth: Int, maxHeight: Int, result: MethodChannel.Result
maxWidth: Int, maxHeight: Int, iteration: Int,
result: MethodChannel.Result
) = method(
fileUrl, headers, filename, maxWidth, maxHeight,
ImageProcessorService.METHOD_ZERO_DCE, result
ImageProcessorService.METHOD_ZERO_DCE, result, onIntent = {
it.putExtra(ImageProcessorService.EXTRA_ITERATION, iteration)
}
)
private fun deepLab3Portrait(

View file

@ -34,6 +34,7 @@ class ImageProcessorService : Service() {
const val EXTRA_MAX_WIDTH = "maxWidth"
const val EXTRA_MAX_HEIGHT = "maxHeight"
const val EXTRA_RADIUS = "radius"
const val EXTRA_ITERATION = "iteration"
private const val ACTION_CANCEL = "cancel"
@ -109,8 +110,13 @@ class ImageProcessorService : Service() {
}
}
private fun onZeroDce(startId: Int, extras: Bundle) =
onMethod(startId, extras, METHOD_ZERO_DCE)
private fun onZeroDce(startId: Int, extras: Bundle) {
return onMethod(
startId, extras, METHOD_ZERO_DCE, args = mapOf(
"iteration" to extras.getIntOrNull(EXTRA_ITERATION)
)
)
}
private fun onDeepLapPortrait(startId: Int, extras: Bundle) {
return onMethod(
@ -432,7 +438,8 @@ private open class ImageProcessorCommandTask(context: Context) :
TAG, "[handleCommand] Elapsed time", {
when (cmd.method) {
ImageProcessorService.METHOD_ZERO_DCE -> ZeroDce(
context, cmd.maxWidth, cmd.maxHeight
context, cmd.maxWidth, cmd.maxHeight,
cmd.args["iteration"] as? Int ?: 8
).infer(
fileUri
)

View file

@ -11,18 +11,17 @@ import java.nio.FloatBuffer
import java.nio.IntBuffer
import kotlin.math.pow
class ZeroDce(context: Context, maxWidth: Int, maxHeight: Int) {
class ZeroDce(context: Context, maxWidth: Int, maxHeight: Int, iteration: Int) {
companion object {
private const val TAG = "ZeroDce"
private const val MODEL = "zero_dce_lite_200x300_iter8_60.tflite"
private const val WIDTH = 300
private const val HEIGHT = 200
private const val ITERATION = 8
}
fun infer(imageUri: Uri): Bitmap {
val alphaMaps = inferAlphaMaps(imageUri)
return enhance(imageUri, alphaMaps, ITERATION)
return enhance(imageUri, alphaMaps, iteration)
}
private fun inferAlphaMaps(imageUri: Uri): Bitmap {
@ -105,4 +104,5 @@ class ZeroDce(context: Context, maxWidth: Int, maxHeight: Int) {
private val context = context
private val maxWidth = maxWidth
private val maxHeight = maxHeight
private val iteration = iteration
}

View file

@ -8,7 +8,8 @@ class ImageProcessor {
String fileUrl,
String filename,
int maxWidth,
int maxHeight, {
int maxHeight,
int iteration, {
Map<String, String>? headers,
}) =>
_methodChannel.invokeMethod("zeroDce", <String, dynamic>{
@ -17,6 +18,7 @@ class ImageProcessor {
"filename": filename,
"maxWidth": maxWidth,
"maxHeight": maxHeight,
"iteration": iteration,
});
static Future<void> deepLab3Portrait(