@saveeye/saveeye-sdk-reactnative-meter-recognition 0.0.0-stage → 0.1.0

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.
Files changed (80) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +112 -2
  3. package/android/build.gradle +123 -0
  4. package/android/generated/java/com/saveeyemeterrecognition/NativeSaveeyeMeterRecognitionSpec.java +38 -0
  5. package/android/generated/jni/CMakeLists.txt +36 -0
  6. package/android/generated/jni/RNSaveeyeMeterRecognitionSpec-generated.cpp +32 -0
  7. package/android/generated/jni/RNSaveeyeMeterRecognitionSpec.h +31 -0
  8. package/android/generated/jni/react/renderer/components/RNSaveeyeMeterRecognitionSpec/RNSaveeyeMeterRecognitionSpecJSI-generated.cpp +30 -0
  9. package/android/generated/jni/react/renderer/components/RNSaveeyeMeterRecognitionSpec/RNSaveeyeMeterRecognitionSpecJSI.h +114 -0
  10. package/android/gradle.properties +5 -0
  11. package/android/src/main/AndroidManifest.xml +3 -0
  12. package/android/src/main/AndroidManifestNew.xml +2 -0
  13. package/android/src/main/java/com/saveeyemeterrecognition/ImageResizer.kt +113 -0
  14. package/android/src/main/java/com/saveeyemeterrecognition/SaveeyeMeterRecognitionModule.kt +42 -0
  15. package/android/src/main/java/com/saveeyemeterrecognition/SaveeyeMeterRecognitionPackage.kt +34 -0
  16. package/ios/SaveeyeMeterRecognition.h +6 -0
  17. package/ios/SaveeyeMeterRecognition.mm +82 -0
  18. package/ios/generated/RNSaveeyeMeterRecognitionSpec/RNSaveeyeMeterRecognitionSpec-generated.mm +39 -0
  19. package/ios/generated/RNSaveeyeMeterRecognitionSpec/RNSaveeyeMeterRecognitionSpec.h +64 -0
  20. package/ios/generated/RNSaveeyeMeterRecognitionSpecJSI-generated.cpp +30 -0
  21. package/ios/generated/RNSaveeyeMeterRecognitionSpecJSI.h +114 -0
  22. package/lib/commonjs/ImageUtil.js +83 -0
  23. package/lib/commonjs/ImageUtil.js.map +1 -0
  24. package/lib/commonjs/MeterImageFilter.js +103 -0
  25. package/lib/commonjs/MeterImageFilter.js.map +1 -0
  26. package/lib/commonjs/NativeSaveeyeMeterRecognition.js +11 -0
  27. package/lib/commonjs/NativeSaveeyeMeterRecognition.js.map +1 -0
  28. package/lib/commonjs/index.js +51 -0
  29. package/lib/commonjs/index.js.map +1 -0
  30. package/lib/commonjs/recognizeMeter.js +84 -0
  31. package/lib/commonjs/recognizeMeter.js.map +1 -0
  32. package/lib/commonjs/types.js +21 -0
  33. package/lib/commonjs/types.js.map +1 -0
  34. package/lib/module/ImageUtil.js +77 -0
  35. package/lib/module/ImageUtil.js.map +1 -0
  36. package/lib/module/MeterImageFilter.js +96 -0
  37. package/lib/module/MeterImageFilter.js.map +1 -0
  38. package/lib/module/NativeSaveeyeMeterRecognition.js +7 -0
  39. package/lib/module/NativeSaveeyeMeterRecognition.js.map +1 -0
  40. package/lib/module/index.js +6 -0
  41. package/lib/module/index.js.map +1 -0
  42. package/lib/module/recognizeMeter.js +79 -0
  43. package/lib/module/recognizeMeter.js.map +1 -0
  44. package/lib/module/types.js +17 -0
  45. package/lib/module/types.js.map +1 -0
  46. package/lib/typescript/commonjs/package.json +1 -0
  47. package/lib/typescript/commonjs/src/ImageUtil.d.ts +10 -0
  48. package/lib/typescript/commonjs/src/ImageUtil.d.ts.map +1 -0
  49. package/lib/typescript/commonjs/src/MeterImageFilter.d.ts +32 -0
  50. package/lib/typescript/commonjs/src/MeterImageFilter.d.ts.map +1 -0
  51. package/lib/typescript/commonjs/src/NativeSaveeyeMeterRecognition.d.ts +17 -0
  52. package/lib/typescript/commonjs/src/NativeSaveeyeMeterRecognition.d.ts.map +1 -0
  53. package/lib/typescript/commonjs/src/index.d.ts +4 -0
  54. package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
  55. package/lib/typescript/commonjs/src/recognizeMeter.d.ts +18 -0
  56. package/lib/typescript/commonjs/src/recognizeMeter.d.ts.map +1 -0
  57. package/lib/typescript/commonjs/src/types.d.ts +50 -0
  58. package/lib/typescript/commonjs/src/types.d.ts.map +1 -0
  59. package/lib/typescript/module/package.json +1 -0
  60. package/lib/typescript/module/src/ImageUtil.d.ts +10 -0
  61. package/lib/typescript/module/src/ImageUtil.d.ts.map +1 -0
  62. package/lib/typescript/module/src/MeterImageFilter.d.ts +32 -0
  63. package/lib/typescript/module/src/MeterImageFilter.d.ts.map +1 -0
  64. package/lib/typescript/module/src/NativeSaveeyeMeterRecognition.d.ts +17 -0
  65. package/lib/typescript/module/src/NativeSaveeyeMeterRecognition.d.ts.map +1 -0
  66. package/lib/typescript/module/src/index.d.ts +4 -0
  67. package/lib/typescript/module/src/index.d.ts.map +1 -0
  68. package/lib/typescript/module/src/recognizeMeter.d.ts +18 -0
  69. package/lib/typescript/module/src/recognizeMeter.d.ts.map +1 -0
  70. package/lib/typescript/module/src/types.d.ts +50 -0
  71. package/lib/typescript/module/src/types.d.ts.map +1 -0
  72. package/package.json +170 -4
  73. package/react-native.config.js +12 -0
  74. package/saveeye-sdk-meter-recognition.podspec +41 -0
  75. package/src/ImageUtil.ts +113 -0
  76. package/src/MeterImageFilter.ts +154 -0
  77. package/src/NativeSaveeyeMeterRecognition.ts +26 -0
  78. package/src/index.ts +22 -0
  79. package/src/recognizeMeter.ts +109 -0
  80. package/src/types.ts +55 -0
@@ -0,0 +1,154 @@
1
+ import BarcodeScanning from '@react-native-ml-kit/barcode-scanning';
2
+ import ImageLabeling, { type Label } from '@react-native-ml-kit/image-labeling';
3
+ import TextRecognition from '@react-native-ml-kit/text-recognition';
4
+
5
+ /**
6
+ * On-device pre-check run before uploading a photo to recognizeMeter, so
7
+ * obviously unrelated photos (pets, feet, selfies, food...) are rejected
8
+ * without a network round-trip. Photos of a SaveEye device itself (detected
9
+ * by its QR code) are rejected with their own reason so apps can tell the
10
+ * user to photograph the power meter instead. Biased towards accepting: the
11
+ * backend still rejects anything that slips through, while a false rejection
12
+ * blocks a real user.
13
+ */
14
+
15
+ export type MeterImageCheckReason =
16
+ | 'meter_text'
17
+ | 'meter_label'
18
+ | 'rejected_label'
19
+ | 'no_signal'
20
+ | 'saveeye_device'
21
+ | 'check_unavailable';
22
+
23
+ export type MeterImageCheck = {
24
+ isLikelyMeter: boolean;
25
+ reason: MeterImageCheckReason;
26
+ /** Highest-confidence label ML Kit returned, if any */
27
+ topLabel?: string;
28
+ /** Number of 4+ digit runs found by OCR (meter readings, serial numbers) */
29
+ digitRuns: number;
30
+ };
31
+
32
+ export const REJECT_LABEL_MIN_CONFIDENCE = 0.7;
33
+ export const METER_LABEL_MIN_CONFIDENCE = 0.5;
34
+
35
+ export const REJECT_LABELS = new Set([
36
+ 'Animal',
37
+ 'Bird',
38
+ 'Cat',
39
+ 'Dog',
40
+ 'Pet',
41
+ 'Person',
42
+ 'Selfie',
43
+ 'Smile',
44
+ 'Beard',
45
+ 'Hair',
46
+ 'Skin',
47
+ 'Hand',
48
+ 'Foot',
49
+ 'Leg',
50
+ 'Toe',
51
+ 'Shoe',
52
+ 'Sock',
53
+ 'Food',
54
+ 'Flower',
55
+ 'Plant',
56
+ 'Sky',
57
+ 'Car',
58
+ ]);
59
+
60
+ export const METER_LABELS = new Set([
61
+ 'Gadget',
62
+ 'Electronic device',
63
+ 'Measuring instrument',
64
+ 'Machine',
65
+ 'Display device',
66
+ 'Gauge',
67
+ 'Clock',
68
+ 'Calculator',
69
+ 'Metal',
70
+ ]);
71
+
72
+ const SAVEEYE_QR_PATTERN = /saveeye/i;
73
+ const METER_TEXT_PATTERN = /kWh|kvarh|imp\/kWh|\bMID\b|\bkW\b|\bkVA\b/i;
74
+ const DIGIT_RUN_PATTERN = /\d{4,}/g;
75
+ // OCR often splits counter wheels/segments into "0 1 2 3 4" or "01234.5"
76
+ const DIGIT_SEPARATOR_PATTERN = /(\d)[ .,](?=\d)/g;
77
+
78
+ export function countDigitRuns(text: string): number {
79
+ return (
80
+ text.replace(DIGIT_SEPARATOR_PATTERN, '$1').match(DIGIT_RUN_PATTERN)
81
+ ?.length ?? 0
82
+ );
83
+ }
84
+
85
+ export function evaluateMeterSignals(
86
+ labels: Pick<Label, 'text' | 'confidence'>[],
87
+ ocrText: string,
88
+ barcodeValues: string[] = []
89
+ ): MeterImageCheck {
90
+ const sorted = [...labels].sort((a, b) => b.confidence - a.confidence);
91
+ const topLabel = sorted[0]?.text;
92
+ const digitRuns = countDigitRuns(ocrText);
93
+
94
+ if (barcodeValues.some((value) => SAVEEYE_QR_PATTERN.test(value))) {
95
+ return {
96
+ isLikelyMeter: false,
97
+ reason: 'saveeye_device',
98
+ topLabel,
99
+ digitRuns,
100
+ };
101
+ }
102
+
103
+ const hasMeterText = METER_TEXT_PATTERN.test(ocrText) || digitRuns > 0;
104
+ if (hasMeterText) {
105
+ return { isLikelyMeter: true, reason: 'meter_text', topLabel, digitRuns };
106
+ }
107
+
108
+ const rejectLabel = sorted.find(
109
+ (l) =>
110
+ l.confidence >= REJECT_LABEL_MIN_CONFIDENCE && REJECT_LABELS.has(l.text)
111
+ );
112
+ if (rejectLabel) {
113
+ return {
114
+ isLikelyMeter: false,
115
+ reason: 'rejected_label',
116
+ topLabel: rejectLabel.text,
117
+ digitRuns,
118
+ };
119
+ }
120
+
121
+ const hasMeterLabel = sorted.some(
122
+ (l) =>
123
+ l.confidence >= METER_LABEL_MIN_CONFIDENCE && METER_LABELS.has(l.text)
124
+ );
125
+ if (hasMeterLabel) {
126
+ return { isLikelyMeter: true, reason: 'meter_label', topLabel, digitRuns };
127
+ }
128
+
129
+ return { isLikelyMeter: false, reason: 'no_signal', topLabel, digitRuns };
130
+ }
131
+
132
+ /**
133
+ * Classifies a local image URI as likely/unlikely to show an electricity
134
+ * meter using on-device ML Kit OCR, image labeling and QR scanning. Never
135
+ * throws: if ML Kit fails, the image is accepted (`check_unavailable`).
136
+ */
137
+ export async function checkMeterImage(uri: string): Promise<MeterImageCheck> {
138
+ try {
139
+ const [labels, text, barcodes] = await Promise.all([
140
+ ImageLabeling.label(uri),
141
+ TextRecognition.recognize(uri).then((result) => result.text),
142
+ // A failed QR scan shouldn't fail the whole check
143
+ BarcodeScanning.scan(uri).catch(() => []),
144
+ ]);
145
+ return evaluateMeterSignals(
146
+ labels,
147
+ text,
148
+ barcodes.map((b) => b.value)
149
+ );
150
+ } catch (error) {
151
+ console.warn('SaveEye meter recognition: image check failed', error);
152
+ return { isLikelyMeter: true, reason: 'check_unavailable', digitRuns: 0 };
153
+ }
154
+ }
@@ -0,0 +1,26 @@
1
+ import type { TurboModule } from 'react-native';
2
+ import { TurboModuleRegistry } from 'react-native';
3
+
4
+ export type ResizedImage = {
5
+ uri: string;
6
+ size: number;
7
+ };
8
+
9
+ export interface Spec extends TurboModule {
10
+ /**
11
+ * Decodes the image at `uri` (any format the OS can read, incl. HEIC),
12
+ * applies its EXIF orientation, scales it down so neither side exceeds
13
+ * `maxDimension` and writes it to a temp file as `format` ('JPEG' | 'PNG')
14
+ * with `quality` 0-100 (JPEG only).
15
+ */
16
+ resizeImage(
17
+ uri: string,
18
+ maxDimension: number,
19
+ quality: number,
20
+ format: string
21
+ ): Promise<ResizedImage>;
22
+ }
23
+
24
+ // `get` (not `getEnforcing`) so importing the package doesn't throw where the
25
+ // native module isn't linked (e.g. Jest); callers check for null.
26
+ export default TurboModuleRegistry.get<Spec>('SaveeyeMeterRecognition');
package/src/index.ts ADDED
@@ -0,0 +1,22 @@
1
+ export {
2
+ recognizeMeter,
3
+ type MeterRecognitionResult,
4
+ type RecognizeMeterOptions,
5
+ } from './recognizeMeter';
6
+ export {
7
+ checkMeterImage,
8
+ evaluateMeterSignals,
9
+ REJECT_LABELS,
10
+ METER_LABELS,
11
+ type MeterImageCheck,
12
+ type MeterImageCheckReason,
13
+ } from './MeterImageFilter';
14
+ export {
15
+ MeterInterfaceType,
16
+ AccessoryRequirement,
17
+ type RecognitionImageFile,
18
+ type RecognizedMeter,
19
+ type RecognizedPowerMeter,
20
+ type CompatibleProduct,
21
+ type CompatibleProductAccessory,
22
+ } from './types';
@@ -0,0 +1,109 @@
1
+ import { gql } from '@apollo/client';
2
+ import SaveeyeSdk from '@saveeye/saveeye-sdk-reactnative';
3
+ import { prepareImageForUpload } from './ImageUtil';
4
+ import { checkMeterImage, type MeterImageCheck } from './MeterImageFilter';
5
+ import type {
6
+ CompatibleProduct,
7
+ RecognitionImageFile,
8
+ RecognizedMeter,
9
+ RecognizedPowerMeter,
10
+ } from './types';
11
+
12
+ const RECOGNIZE_METER = gql`
13
+ mutation RecognizeMeter($input: RecognizeMeterInput!) {
14
+ recognizeMeter(input: $input) {
15
+ hasAccess
16
+ success
17
+ meterRecognitionResult {
18
+ powerMeter {
19
+ manufacturer
20
+ model
21
+ }
22
+ compatibleProducts {
23
+ id
24
+ name
25
+ sku
26
+ meterInterfaceType
27
+ accessories {
28
+ countryId
29
+ requirement
30
+ description
31
+ isAvailable
32
+ product {
33
+ id
34
+ name
35
+ sku
36
+ }
37
+ }
38
+ }
39
+ }
40
+ }
41
+ }
42
+ `;
43
+
44
+ type RecognizeMeterResponse = {
45
+ recognizeMeter?: {
46
+ hasAccess: boolean;
47
+ success: boolean;
48
+ meterRecognitionResult?: {
49
+ powerMeter?: RecognizedPowerMeter | null;
50
+ compatibleProducts: CompatibleProduct[];
51
+ } | null;
52
+ } | null;
53
+ };
54
+
55
+ export type MeterRecognitionResult = RecognizedMeter & {
56
+ /** True when the on-device check rejected the image; nothing was uploaded */
57
+ rejectedLocally: boolean;
58
+ localCheck?: MeterImageCheck;
59
+ };
60
+
61
+ export type RecognizeMeterOptions = {
62
+ /** Skip the on-device "does this look like a meter" check */
63
+ skipLocalCheck?: boolean;
64
+ };
65
+
66
+ /**
67
+ * Checks the photo on-device and, if it plausibly shows an electricity
68
+ * meter, converts/resizes it and uploads it for recognition. The core SaveEye
69
+ * SDK must be initialized first, as it provides authentication.
70
+ */
71
+ export async function recognizeMeter(
72
+ image: RecognitionImageFile,
73
+ countryId?: string,
74
+ options?: RecognizeMeterOptions
75
+ ): Promise<MeterRecognitionResult> {
76
+ if (!options?.skipLocalCheck) {
77
+ const localCheck = await checkMeterImage(image.uri);
78
+ if (!localCheck.isLikelyMeter) {
79
+ return {
80
+ hasAccess: false,
81
+ success: false,
82
+ powerMeter: null,
83
+ compatibleProducts: [],
84
+ rejectedLocally: true,
85
+ localCheck,
86
+ };
87
+ }
88
+ }
89
+
90
+ const uploadImage = await prepareImageForUpload(image);
91
+ const result = await SaveeyeSdk.getInstance()
92
+ .getGraphQLClient()
93
+ .mutate<RecognizeMeterResponse>({
94
+ mutation: RECOGNIZE_METER,
95
+ variables: { input: { image: uploadImage, countryId } },
96
+ });
97
+ const payload = result.data?.recognizeMeter;
98
+ if (!payload) {
99
+ throw new Error('No response from recognizeMeter');
100
+ }
101
+ return {
102
+ hasAccess: payload.hasAccess,
103
+ success: payload.success,
104
+ powerMeter: payload.meterRecognitionResult?.powerMeter ?? null,
105
+ compatibleProducts:
106
+ payload.meterRecognitionResult?.compatibleProducts ?? [],
107
+ rejectedLocally: false,
108
+ };
109
+ }
package/src/types.ts ADDED
@@ -0,0 +1,55 @@
1
+ export type RecognitionImageFile = {
2
+ uri: string;
3
+ type: string;
4
+ name: string;
5
+ };
6
+
7
+ export enum MeterInterfaceType {
8
+ Base = 'BASE',
9
+ Echelon = 'ECHELON',
10
+ HanRj45 = 'HAN_RJ45',
11
+ Ir = 'IR',
12
+ Kamstrup = 'KAMSTRUP',
13
+ P1 = 'P1',
14
+ }
15
+
16
+ export enum AccessoryRequirement {
17
+ Optional = 'OPTIONAL',
18
+ Required = 'REQUIRED',
19
+ }
20
+
21
+ export type RecognizedPowerMeter = {
22
+ manufacturer: string;
23
+ model: string;
24
+ };
25
+
26
+ export type CompatibleProductAccessory = {
27
+ /** Set when the accessory is country-specific */
28
+ countryId?: string | null;
29
+ requirement: AccessoryRequirement;
30
+ description?: string | null;
31
+ isAvailable: boolean;
32
+ product: {
33
+ id: string;
34
+ name: string;
35
+ sku: string;
36
+ };
37
+ };
38
+
39
+ export type CompatibleProduct = {
40
+ /** Product identifier, usable when creating an order line */
41
+ id: string;
42
+ name: string;
43
+ sku: string;
44
+ meterInterfaceType: MeterInterfaceType;
45
+ accessories: CompatibleProductAccessory[];
46
+ };
47
+
48
+ export type RecognizedMeter = {
49
+ /** Whether this app/partner may access recognition details */
50
+ hasAccess: boolean;
51
+ /** Whether the image matched a known electricity meter */
52
+ success: boolean;
53
+ powerMeter?: RecognizedPowerMeter | null;
54
+ compatibleProducts: CompatibleProduct[];
55
+ };