feat(inventory): photo-first drafts + on-device OCR (digitization R2+R4)
Lower the bulk-digitization cliff with two more routes on top of the already-landed CSV import and "save and add another": - Photo-first drafts (capture now, catalogue later): burst-capture photos (camera or multi-gallery) into unnamed draft varieties, shown in a "to catalogue" tray, hidden from the main list until named. Adds Variety.isDraft (schema), addDraftVariety/watchDrafts/nameDraft, the triage sheet and the inventory banner. - On-device OCR label suggestion (Tesseract, offline, no Google): a "Suggest name from photo" button in the naming dialog behind a LabelTextExtractor interface (Tesseract on Android/iOS, no-op elsewhere). Reads the largest print via hOCR bounding boxes, drops boilerplate/low-confidence noise, preprocesses (grayscale, contrast, upscale) and sweeps rotations (0-315 deg) so tilted packets still read. Bundles tessdata_fast eng+spa; validated on-device against real packets. The photo is written to a temp file deleted immediately in a finally block (the plugin needs a path) - a bounded, documented exception to no-plaintext-at-rest. This commit also carries the co-developed schema evolution v5 to v8 that shares these files (organic flag, species viability years, crop calendar, lot provenance/abundance/preservation format, condition checks) plus their exports/migrations and i18n. Tests: CSV/draft/OCR unit + widget + migration green in isolation. Note: the full widget suite currently hangs (>10 min) - under investigation.
This commit is contained in:
parent
12a2ee2d64
commit
6809dc6143
89 changed files with 17141 additions and 228 deletions
100
apps/app_seeds/test/services/ocr/ocr_language_test.dart
Normal file
100
apps/app_seeds/test/services/ocr/ocr_language_test.dart
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:tane/services/ocr/ocr_language.dart';
|
||||
|
||||
void main() {
|
||||
group('parseAvailableOcrLanguages', () {
|
||||
test('strips the .traineddata suffix', () {
|
||||
final available = parseAvailableOcrLanguages(
|
||||
'{"files": ["eng.traineddata", "spa.traineddata"]}',
|
||||
);
|
||||
expect(available, {'eng', 'spa'});
|
||||
});
|
||||
|
||||
test('empty / missing files yields empty set', () {
|
||||
expect(parseAvailableOcrLanguages('{}'), isEmpty);
|
||||
expect(parseAvailableOcrLanguages('{"files": []}'), isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group('tesseractCodeForLocale', () {
|
||||
test('maps ISO 639-1 to Tesseract codes', () {
|
||||
expect(tesseractCodeForLocale('es'), 'spa');
|
||||
expect(tesseractCodeForLocale('en'), 'eng');
|
||||
expect(tesseractCodeForLocale('ar'), 'ara');
|
||||
expect(tesseractCodeForLocale('zh'), 'chi_sim');
|
||||
});
|
||||
|
||||
test('accepts full locale tags and is case-insensitive', () {
|
||||
expect(tesseractCodeForLocale('es_ES'), 'spa');
|
||||
expect(tesseractCodeForLocale('zh-Hans-CN'), 'chi_sim');
|
||||
expect(tesseractCodeForLocale('DE'), 'deu');
|
||||
});
|
||||
|
||||
test('unmapped locale returns null', () {
|
||||
expect(tesseractCodeForLocale('xx'), isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('resolveOcrLanguages', () {
|
||||
const bundled = {'eng', 'spa'};
|
||||
|
||||
test('preferred locale comes first, baseline appended', () {
|
||||
expect(
|
||||
resolveOcrLanguages(preferredLocales: ['es'], available: bundled),
|
||||
'spa+eng',
|
||||
);
|
||||
});
|
||||
|
||||
test('English preferred is not duplicated by the baseline', () {
|
||||
expect(
|
||||
resolveOcrLanguages(preferredLocales: ['en'], available: bundled),
|
||||
'eng',
|
||||
);
|
||||
});
|
||||
|
||||
test('order follows preference, de-duplicated', () {
|
||||
expect(
|
||||
resolveOcrLanguages(
|
||||
preferredLocales: ['es', 'en', 'es'],
|
||||
available: bundled,
|
||||
),
|
||||
'spa+eng',
|
||||
);
|
||||
});
|
||||
|
||||
test('unbundled locale falls back to the Latin baseline', () {
|
||||
// German pack is not bundled here — degrade to eng, never fail.
|
||||
expect(
|
||||
resolveOcrLanguages(preferredLocales: ['de'], available: bundled),
|
||||
'eng',
|
||||
);
|
||||
});
|
||||
|
||||
test('a future RTL/CJK pack lights up automatically once bundled', () {
|
||||
expect(
|
||||
resolveOcrLanguages(
|
||||
preferredLocales: ['ar'],
|
||||
available: {'eng', 'spa', 'ara'},
|
||||
),
|
||||
'ara+eng',
|
||||
);
|
||||
});
|
||||
|
||||
test('no baseline available: falls back to every bundled pack, sorted', () {
|
||||
expect(
|
||||
resolveOcrLanguages(
|
||||
preferredLocales: ['fr'],
|
||||
available: {'spa', 'deu'},
|
||||
),
|
||||
'deu+spa',
|
||||
);
|
||||
});
|
||||
|
||||
test('empty available yields empty string', () {
|
||||
expect(
|
||||
resolveOcrLanguages(preferredLocales: ['es'], available: const {}),
|
||||
'',
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:tane/services/ocr/tesseract_label_extractor.dart';
|
||||
|
||||
/// Builds one hOCR word span with a bounding box (height = y1 - y0) and an
|
||||
/// optional recognition confidence.
|
||||
String word(String text, int x0, int y0, int x1, int y1, {int conf = 90}) =>
|
||||
"<span class='ocrx_word' title='bbox $x0 $y0 $x1 $y1; x_wconf $conf'>"
|
||||
'$text</span>';
|
||||
|
||||
String page(List<String> words) =>
|
||||
"<div class='ocr_page'>${words.join('\n')}</div>";
|
||||
|
||||
void main() {
|
||||
group('pickLabelFromHocr picks the largest print, not the longest line', () {
|
||||
test('Aubergine Black Beauty packet → the variety name, not boilerplate', () {
|
||||
// "Organic Product" (14 letters) is longer than "Black Beauty" (11) but
|
||||
// printed smaller — the old longest-line heuristic wrongly picked it.
|
||||
final hocr = page([
|
||||
word('BonPrime', 120, 300, 300, 335), // brand, tiny
|
||||
word('Organic', 130, 1050, 320, 1095), // boilerplate, small
|
||||
word('Product', 130, 1100, 340, 1145),
|
||||
word('AUBERGINE', 380, 1180, 900, 1300), // the title (tallest)
|
||||
word('BLACK', 380, 1330, 620, 1410),
|
||||
word('BEAUTY', 640, 1330, 900, 1410),
|
||||
word('HERB', 430, 1470, 560, 1510), // boilerplate, small
|
||||
word('SEEDS', 580, 1470, 740, 1510),
|
||||
]);
|
||||
expect(pickLabelFromHocr(hocr), 'Aubergine Black Beauty');
|
||||
});
|
||||
|
||||
test('Cucumber Supermarketer packet → the variety name', () {
|
||||
final hocr = page([
|
||||
word('BonPrime', 120, 300, 300, 335),
|
||||
word('Organic', 130, 1050, 320, 1095),
|
||||
word('Product', 130, 1100, 340, 1145),
|
||||
word('CUCUMBER', 300, 1200, 820, 1320), // tallest
|
||||
word('SUPERMARKETER', 320, 1350, 900, 1430),
|
||||
word('HERB', 430, 1470, 560, 1510),
|
||||
word('SEEDS', 580, 1470, 740, 1510),
|
||||
]);
|
||||
expect(pickLabelFromHocr(hocr), 'Cucumber Supermarketer');
|
||||
});
|
||||
|
||||
test('reads left-to-right within the same row', () {
|
||||
final hocr = page([
|
||||
word('CHERRY', 400, 1000, 560, 1090),
|
||||
word('TOMATO', 580, 1000, 760, 1090),
|
||||
]);
|
||||
expect(pickLabelFromHocr(hocr), 'Cherry Tomato');
|
||||
});
|
||||
|
||||
test('a page of only boilerplate yields null', () {
|
||||
final hocr = page([
|
||||
word('Organic', 130, 1050, 320, 1095),
|
||||
word('Product', 130, 1100, 340, 1145),
|
||||
word('HERB', 430, 1470, 560, 1510),
|
||||
word('SEEDS', 580, 1470, 740, 1510),
|
||||
]);
|
||||
expect(pickLabelFromHocr(hocr), isNull);
|
||||
});
|
||||
|
||||
test('empty hOCR yields null', () {
|
||||
expect(pickLabelFromHocr(page(const [])), isNull);
|
||||
expect(pickLabelFromHocr(''), isNull);
|
||||
});
|
||||
|
||||
test('short OCR fragments ("Re Ma") are dropped → no suggestion', () {
|
||||
// Blurry misread into 1–2 char blobs: better nothing than "Re Ma".
|
||||
final hocr = page([
|
||||
word('Re', 300, 1200, 420, 1320, conf: 40),
|
||||
word('Ma', 460, 1200, 600, 1320, conf: 40),
|
||||
]);
|
||||
expect(pickLabelFromHocr(hocr), isNull);
|
||||
});
|
||||
|
||||
test('a plausibly-misread title word is kept (user edits it)', () {
|
||||
// "Sucumber" (C read as S) is useful as an editable prefill.
|
||||
final hocr = page([
|
||||
word('Sucumber', 300, 1200, 820, 1320, conf: 45),
|
||||
word('Organic', 130, 1050, 320, 1095, conf: 80),
|
||||
]);
|
||||
expect(pickLabelFromHocr(hocr), 'Sucumber');
|
||||
});
|
||||
|
||||
test('a near-zero confidence word is dropped by the floor', () {
|
||||
final hocr = page([word('Xzqwp', 300, 1200, 820, 1320, conf: 8)]);
|
||||
expect(pickLabelFromHocr(hocr), isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue