capacitor-plugin-camera-forked 3.1.122 → 3.1.124
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -56,7 +56,7 @@ import java.util.concurrent.atomic.AtomicReference;
|
|
|
56
56
|
public class BlurDetectionHelper {
|
|
57
57
|
private static final String TAG = "BlurDetectionHelper";
|
|
58
58
|
private static final String MODEL_FILENAME = "blur_detection_model.tflite";
|
|
59
|
-
private static int INPUT_SIZE =
|
|
59
|
+
private static int INPUT_SIZE = 600; // Will be updated based on actual model input size
|
|
60
60
|
private static final int NUM_CLASSES = 2; // blur, sharp
|
|
61
61
|
|
|
62
62
|
// Timeout settings
|
|
@@ -68,9 +68,9 @@ public class BlurDetectionHelper {
|
|
|
68
68
|
private static final double AT_LEAST_N_PERCENT_OF_AVERAGE_CONFIDENCE = 0.85; // 85% of average confidence
|
|
69
69
|
|
|
70
70
|
// Method based confidence threshold
|
|
71
|
-
private static final double MIN_SHARP_CONFIDENCE_FOR_OBJECT_DETECTION = 0.
|
|
72
|
-
private static final double MIN_SHARP_CONFIDENCE_FOR_TEXT_DETECTION = 0.
|
|
73
|
-
private static final double MIN_SHARP_CONFIDENCE_FOR_FULL_IMAGE = 0.
|
|
71
|
+
private static final double MIN_SHARP_CONFIDENCE_FOR_OBJECT_DETECTION = 0.45; // 45% confidence threshold
|
|
72
|
+
private static final double MIN_SHARP_CONFIDENCE_FOR_TEXT_DETECTION = 0.09; // 9% confidence threshold
|
|
73
|
+
private static final double MIN_SHARP_CONFIDENCE_FOR_FULL_IMAGE = 0.65; // 65% confidence threshold
|
|
74
74
|
|
|
75
75
|
// TFLite components
|
|
76
76
|
private Interpreter tflite;
|
|
@@ -96,10 +96,9 @@ public class BlurDetectionHelper {
|
|
|
96
96
|
|
|
97
97
|
|
|
98
98
|
public BlurDetectionHelper() {
|
|
99
|
-
// Initialize image processor for MobileNetV2 preprocessing
|
|
99
|
+
// Initialize image processor for MobileNetV2 preprocessing with aspect ratio preservation
|
|
100
100
|
imageProcessor = new ImageProcessor.Builder()
|
|
101
|
-
.add(new ResizeWithCropOrPadOp(INPUT_SIZE, INPUT_SIZE))
|
|
102
|
-
.add(new ResizeOp(INPUT_SIZE, INPUT_SIZE, ResizeOp.ResizeMethod.BILINEAR))
|
|
101
|
+
.add(new ResizeWithCropOrPadOp(INPUT_SIZE, INPUT_SIZE)) // This preserves aspect ratio by cropping/padding
|
|
103
102
|
.build();
|
|
104
103
|
|
|
105
104
|
// Initialize common words dictionary
|
|
@@ -151,10 +150,9 @@ public class BlurDetectionHelper {
|
|
|
151
150
|
if (modelInputSize != INPUT_SIZE) {
|
|
152
151
|
INPUT_SIZE = modelInputSize;
|
|
153
152
|
|
|
154
|
-
// Recreate image processor with correct size
|
|
153
|
+
// Recreate image processor with correct size and aspect ratio preservation
|
|
155
154
|
imageProcessor = new ImageProcessor.Builder()
|
|
156
|
-
.add(new ResizeWithCropOrPadOp(INPUT_SIZE, INPUT_SIZE))
|
|
157
|
-
.add(new ResizeOp(INPUT_SIZE, INPUT_SIZE, ResizeOp.ResizeMethod.BILINEAR))
|
|
155
|
+
.add(new ResizeWithCropOrPadOp(INPUT_SIZE, INPUT_SIZE)) // Preserves aspect ratio
|
|
158
156
|
.build();
|
|
159
157
|
}
|
|
160
158
|
}
|
|
@@ -334,6 +332,9 @@ public class BlurDetectionHelper {
|
|
|
334
332
|
// Preprocess image for model (resize and potential enhancement)
|
|
335
333
|
inputImageBuffer.load(processedBitmap);
|
|
336
334
|
inputImageBuffer = imageProcessor.process(inputImageBuffer);
|
|
335
|
+
|
|
336
|
+
// Ensure black padding for better accuracy (matches iOS implementation)
|
|
337
|
+
ensureBlackPadding(inputImageBuffer);
|
|
337
338
|
|
|
338
339
|
// Get tensor buffer
|
|
339
340
|
ByteBuffer tensorBuffer = inputImageBuffer.getBuffer();
|
|
@@ -425,6 +426,37 @@ public class BlurDetectionHelper {
|
|
|
425
426
|
return normalizedBuffer;
|
|
426
427
|
}
|
|
427
428
|
|
|
429
|
+
/**
|
|
430
|
+
* Ensure black padding in the processed image buffer for better accuracy
|
|
431
|
+
* @param tensorImage Processed tensor image
|
|
432
|
+
*/
|
|
433
|
+
private void ensureBlackPadding(TensorImage tensorImage) {
|
|
434
|
+
ByteBuffer buffer = tensorImage.getBuffer();
|
|
435
|
+
DataType dataType = tensorImage.getDataType();
|
|
436
|
+
|
|
437
|
+
if (dataType == DataType.FLOAT32) {
|
|
438
|
+
// For float32, ensure padding areas are 0.0 (black)
|
|
439
|
+
FloatBuffer floatBuffer = buffer.asFloatBuffer();
|
|
440
|
+
int totalPixels = INPUT_SIZE * INPUT_SIZE * 3;
|
|
441
|
+
|
|
442
|
+
// Check if we need to fill with zeros (black)
|
|
443
|
+
for (int i = 0; i < totalPixels; i++) {
|
|
444
|
+
if (floatBuffer.get(i) < 0.001f) { // Near zero values
|
|
445
|
+
floatBuffer.put(i, 0.0f); // Ensure exact zero (black)
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
} else if (dataType == DataType.UINT8) {
|
|
449
|
+
// For uint8, ensure padding areas are 0 (black)
|
|
450
|
+
buffer.rewind();
|
|
451
|
+
while (buffer.hasRemaining()) {
|
|
452
|
+
byte value = buffer.get();
|
|
453
|
+
if (value == 0) {
|
|
454
|
+
buffer.put(buffer.position() - 1, (byte) 0); // Ensure exact zero
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
428
460
|
/**
|
|
429
461
|
* Normalize image buffer from uint8 [0,255] to float32 [0,1]
|
|
430
462
|
* @param uint8Buffer Input buffer with uint8 pixel values
|
|
@@ -530,6 +562,9 @@ public class BlurDetectionHelper {
|
|
|
530
562
|
// Preprocess image for model (resize and potential enhancement)
|
|
531
563
|
inputImageBuffer.load(processedBitmap);
|
|
532
564
|
inputImageBuffer = imageProcessor.process(inputImageBuffer);
|
|
565
|
+
|
|
566
|
+
// Ensure black padding for better accuracy (matches iOS implementation)
|
|
567
|
+
ensureBlackPadding(inputImageBuffer);
|
|
533
568
|
|
|
534
569
|
// Get tensor buffer
|
|
535
570
|
ByteBuffer tensorBuffer = inputImageBuffer.getBuffer();
|
|
@@ -767,6 +802,13 @@ public class BlurDetectionHelper {
|
|
|
767
802
|
|
|
768
803
|
Log.d(TAG, "Combined " + boundingBoxes.size() + " text areas into single bounding box: " +
|
|
769
804
|
"(" + minLeft + ", " + minTop + ", " + maxRight + ", " + maxBottom + ")");
|
|
805
|
+
|
|
806
|
+
// Minimum width and height
|
|
807
|
+
int minWidth = 100;
|
|
808
|
+
int minHeight = 40;
|
|
809
|
+
if (combinedRect.width() < minWidth || combinedRect.height() < minHeight) {
|
|
810
|
+
return new ArrayList<>();
|
|
811
|
+
}
|
|
770
812
|
|
|
771
813
|
List<Rect> result = new ArrayList<>();
|
|
772
814
|
result.add(combinedRect);
|
|
@@ -12,8 +12,8 @@ import MLKitVision
|
|
|
12
12
|
class BlurDetectionHelper {
|
|
13
13
|
|
|
14
14
|
private static let TAG = "BlurDetectionHelper"
|
|
15
|
-
private static let INPUT_WIDTH =
|
|
16
|
-
private static let INPUT_HEIGHT =
|
|
15
|
+
private static let INPUT_WIDTH = 600 // Model's expected input width
|
|
16
|
+
private static let INPUT_HEIGHT = 600 // Model's expected input height
|
|
17
17
|
private static let BATCH_SIZE = 1 // Model expects a batch size of 1
|
|
18
18
|
private static let NUM_CHANNELS = 3 // RGB
|
|
19
19
|
private static let NUM_CLASSES = 2 // blur, sharp
|
|
@@ -27,9 +27,9 @@ class BlurDetectionHelper {
|
|
|
27
27
|
private static let AT_LEAST_N_PERCENT_OF_AVERAGE_CONFIDENCE: Double = 0.85 // 85% of average confidence
|
|
28
28
|
|
|
29
29
|
// Method based confidence threshold
|
|
30
|
-
private static let MIN_SHARP_CONFIDENCE_FOR_OBJECT_DETECTION: Double = 0.
|
|
31
|
-
private static let MIN_SHARP_CONFIDENCE_FOR_TEXT_DETECTION: Double = 0.09 //
|
|
32
|
-
private static let MIN_SHARP_CONFIDENCE_FOR_FULL_IMAGE: Double = 0.
|
|
30
|
+
private static let MIN_SHARP_CONFIDENCE_FOR_OBJECT_DETECTION: Double = 0.45 // 45% confidence threshold
|
|
31
|
+
private static let MIN_SHARP_CONFIDENCE_FOR_TEXT_DETECTION: Double = 0.09 // 9% confidence threshold
|
|
32
|
+
private static let MIN_SHARP_CONFIDENCE_FOR_FULL_IMAGE: Double = 0.65 // 65% confidence threshold
|
|
33
33
|
|
|
34
34
|
private var interpreter: Interpreter?
|
|
35
35
|
private var isInitialized = false
|
|
@@ -391,11 +391,37 @@ class BlurDetectionHelper {
|
|
|
391
391
|
}
|
|
392
392
|
|
|
393
393
|
/**
|
|
394
|
-
* Resize image to target size
|
|
394
|
+
* Resize image to target size while maintaining aspect ratio
|
|
395
395
|
*/
|
|
396
396
|
private func resizeImage(_ image: UIImage, to size: CGSize) -> UIImage? {
|
|
397
|
-
|
|
398
|
-
|
|
397
|
+
let targetSize = size
|
|
398
|
+
let imageSize = image.size
|
|
399
|
+
|
|
400
|
+
// Calculate aspect ratio preserving dimensions
|
|
401
|
+
let widthRatio = targetSize.width / imageSize.width
|
|
402
|
+
let heightRatio = targetSize.height / imageSize.height
|
|
403
|
+
let scaleFactor = min(widthRatio, heightRatio)
|
|
404
|
+
|
|
405
|
+
let scaledSize = CGSize(
|
|
406
|
+
width: imageSize.width * scaleFactor,
|
|
407
|
+
height: imageSize.height * scaleFactor
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
// Create a square canvas with the target size
|
|
411
|
+
UIGraphicsBeginImageContextWithOptions(targetSize, false, 1.0)
|
|
412
|
+
|
|
413
|
+
// Calculate position to center the scaled image
|
|
414
|
+
let x = (targetSize.width - scaledSize.width) / 2
|
|
415
|
+
let y = (targetSize.height - scaledSize.height) / 2
|
|
416
|
+
let drawRect = CGRect(x: x, y: y, width: scaledSize.width, height: scaledSize.height)
|
|
417
|
+
|
|
418
|
+
// Fill background with black (or white) to pad the image
|
|
419
|
+
UIColor.black.setFill()
|
|
420
|
+
UIRectFill(CGRect(origin: .zero, size: targetSize))
|
|
421
|
+
|
|
422
|
+
// Draw the scaled image centered
|
|
423
|
+
image.draw(in: drawRect)
|
|
424
|
+
|
|
399
425
|
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
|
|
400
426
|
UIGraphicsEndImageContext()
|
|
401
427
|
return resizedImage
|
|
@@ -702,6 +728,13 @@ class BlurDetectionHelper {
|
|
|
702
728
|
}
|
|
703
729
|
|
|
704
730
|
let combinedRect = CGRect(x: minX, y: minY, width: maxX - minX, height: maxY - minY)
|
|
731
|
+
|
|
732
|
+
// Minimum width and height
|
|
733
|
+
let minWidth = 100
|
|
734
|
+
let minHeight = 40
|
|
735
|
+
if combinedRect.width < minWidth || combinedRect.height < minHeight {
|
|
736
|
+
return []
|
|
737
|
+
}
|
|
705
738
|
|
|
706
739
|
print("\(Self.TAG): Combined \(rects.count) text areas into single bounding box: " +
|
|
707
740
|
"(\(minX), \(minY), \(maxX), \(maxY))")
|
package/package.json
CHANGED