Files
pfm-ocr/test/blur_detector_test.dart
T

68 lines
2.6 KiB
Dart

import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:image/image.dart' as img;
import 'package:app_pfm_ocr_v2/features/camera/blur_detector.dart';
void main() {
test('BlurDetector TDD algorithm test with document borders', () async {
final tempDir = Directory.systemTemp.createTempSync();
final sharpFile = File('${tempDir.path}/sharp_doc.png');
final blurFile = File('${tempDir.path}/blur_doc.png');
// 1. Generate a sharp document with a black background
// Size: 200x200
final sharpImg = img.Image(width: 200, height: 200);
// Fill with black (background table)
img.fill(sharpImg, color: img.ColorRgb8(0, 0, 0));
// Draw white document in the center (from 40 to 160)
for (int y = 40; y < 160; y++) {
for (int x = 40; x < 160; x++) {
// Draw sharp alternating stripes inside the document to simulate sharp text
final isStripe = (x % 6 < 3);
final colorVal = isStripe ? 0 : 255;
sharpImg.setPixelRgba(x, y, colorVal, colorVal, colorVal, 255);
}
}
sharpFile.writeAsBytesSync(img.encodePng(sharpImg));
// 2. Generate a blurry document with the same black background
final blurImg = img.Image(width: 200, height: 200);
img.fill(blurImg, color: img.ColorRgb8(0, 0, 0));
// Draw white document in the center (from 40 to 160)
for (int y = 40; y < 160; y++) {
for (int x = 40; x < 160; x++) {
// Draw solid white or very blurry gray stripes (low contrast)
blurImg.setPixelRgba(x, y, 220, 220, 220, 255);
}
}
// To make it look like a blurred document, we also blur the edges of the white card slightly
final blurredDocImg = img.gaussianBlur(blurImg, radius: 2);
blurFile.writeAsBytesSync(img.encodePng(blurredDocImg));
// 3. Run BlurDetector
final sharpResult = await BlurDetector.checkBlur(sharpFile.path);
final blurResult = await BlurDetector.checkBlur(blurFile.path);
print('TDD Sharp Doc Score: ${sharpResult.score} (Raw: ${sharpResult.rawScore}, IsBlur: ${sharpResult.isBlur})');
print('TDD Blur Doc Score: ${blurResult.score} (Raw: ${blurResult.rawScore}, IsBlur: ${blurResult.isBlur})');
// 4. Assertions
// The sharp document must be detected as sharp (score >= 80.0)
expect(sharpResult.score, greaterThanOrEqualTo(80.0));
expect(sharpResult.isBlur, isFalse);
// The blurry document must be detected as blurry (score < 80.0)
expect(blurResult.score, lessThan(80.0));
expect(blurResult.isBlur, isTrue);
// Clean up
try {
sharpFile.deleteSync();
blurFile.deleteSync();
tempDir.deleteSync();
} catch (_) {}
});
}