tktechnico-react-face-detection 1.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,156 @@
1
+ # @tktechnico/react-face-detection
2
+
3
+ A self-contained face detection component for React and Ionic applications. Uses TensorFlow.js with BlazeFace model for real-time face detection with native camera support via Capacitor.
4
+
5
+ ## Features
6
+
7
+ - 🎯 **Real-time Face Detection** - Uses BlazeFace model for fast, accurate face detection
8
+ - 📱 **Native Camera Support** - Works with Capacitor for iOS and Android
9
+ - 🎨 **Visual Feedback** - Bounding box overlay with confidence scores
10
+ - âš¡ **Offline Ready** - Model runs locally after initial download
11
+ - 🔧 **Highly Configurable** - Customize thresholds, colors, and behavior
12
+ - 📦 **Zero Config** - Works out of the box with sensible defaults
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install @tktechnico/react-face-detection
18
+ ```
19
+
20
+ ### Peer Dependencies
21
+
22
+ ```bash
23
+ npm install react react-dom
24
+ ```
25
+
26
+ ### Required Dependencies (installed automatically)
27
+
28
+ - `@tensorflow/tfjs` - TensorFlow.js runtime
29
+ - `@tensorflow-models/blazeface` - Face detection model
30
+ - `@capacitor/camera` - Native camera access
31
+
32
+ ## Quick Start
33
+
34
+ ```tsx
35
+ import { FaceDetector } from '@tktechnico/react-face-detection';
36
+
37
+ function App() {
38
+ return (
39
+ <FaceDetector
40
+ onDetectionComplete={(result) => {
41
+ if (result.success) {
42
+ console.log('Face detected with confidence:', result.confidence);
43
+ }
44
+ }}
45
+ />
46
+ );
47
+ }
48
+ ```
49
+
50
+ ## API Reference
51
+
52
+ ### FaceDetector Props
53
+
54
+ | Prop | Type | Default | Description |
55
+ |------|------|---------|-------------|
56
+ | `confidenceThreshold` | `number` | `0.7` | Minimum confidence for detection (0-1) |
57
+ | `maxImageSize` | `number` | `640` | Max image dimension for processing |
58
+ | `showOverlay` | `boolean` | `true` | Show face bounding box overlay |
59
+ | `showResult` | `boolean` | `true` | Show detection result message |
60
+ | `defaultFacing` | `'front' \| 'rear'` | `'front'` | Default camera direction |
61
+ | `allowCameraSwitch` | `boolean` | `true` | Allow switching cameras |
62
+ | `quality` | `number` | `90` | Image quality (1-100) |
63
+ | `onDetectionComplete` | `(result: DetectionResult) => void` | - | Called when detection completes |
64
+ | `onError` | `(error: string) => void` | - | Called on error |
65
+ | `className` | `string` | `''` | Additional CSS classes |
66
+
67
+ ### DetectionResult Type
68
+
69
+ ```typescript
70
+ interface DetectionResult {
71
+ success: boolean;
72
+ face: FaceDetection | null;
73
+ confidence: number;
74
+ isLowConfidence: boolean;
75
+ errorMessage: string | null;
76
+ }
77
+
78
+ interface FaceDetection {
79
+ topLeft: [number, number];
80
+ bottomRight: [number, number];
81
+ probability: number;
82
+ landmarks?: number[][];
83
+ }
84
+ ```
85
+
86
+ ## Individual Components
87
+
88
+ You can also use individual components for more control:
89
+
90
+ ```tsx
91
+ import {
92
+ useFaceDetectionCore,
93
+ ImagePreviewWithOverlay,
94
+ DetectionResultDisplay,
95
+ CaptureButton,
96
+ LoadingState
97
+ } from '@tktechnico/react-face-detection';
98
+
99
+ function CustomFaceDetector() {
100
+ const { isModelLoading, modelError, detectFace, retryModelLoad } = useFaceDetectionCore({
101
+ confidenceThreshold: 0.8,
102
+ });
103
+
104
+ // Build your own UI with these components
105
+ return (
106
+ <div>
107
+ {isModelLoading && <LoadingState isLoading={true} error={null} onRetry={retryModelLoad} />}
108
+ {/* ... */}
109
+ </div>
110
+ );
111
+ }
112
+ ```
113
+
114
+ ## Platform Setup
115
+
116
+ ### iOS
117
+
118
+ Add to `Info.plist`:
119
+
120
+ ```xml
121
+ <key>NSCameraUsageDescription</key>
122
+ <string>This app needs camera access for face detection.</string>
123
+ <key>NSPhotoLibraryUsageDescription</key>
124
+ <string>This app needs photo library access.</string>
125
+ ```
126
+
127
+ ### Android
128
+
129
+ Add to `AndroidManifest.xml`:
130
+
131
+ ```xml
132
+ <uses-permission android:name="android.permission.CAMERA" />
133
+ ```
134
+
135
+ ## Usage with Ionic
136
+
137
+ ```tsx
138
+ import { IonPage, IonContent } from '@ionic/react';
139
+ import { FaceDetector } from '@tktechnico/react-face-detection';
140
+
141
+ function FaceDetectionPage() {
142
+ return (
143
+ <IonPage>
144
+ <IonContent>
145
+ <FaceDetector
146
+ onDetectionComplete={(result) => console.log(result)}
147
+ />
148
+ </IonContent>
149
+ </IonPage>
150
+ );
151
+ }
152
+ ```
153
+
154
+ ## License
155
+
156
+ MIT
@@ -0,0 +1,15 @@
1
+ type CameraFacing = 'front' | 'rear';
2
+ interface CaptureButtonProps {
3
+ onImageCaptured: (imageDataUrl: string) => void;
4
+ isProcessing?: boolean;
5
+ showRetake?: boolean;
6
+ onRetake?: () => void;
7
+ defaultFacing?: CameraFacing;
8
+ allowCameraSwitch?: boolean;
9
+ quality?: number;
10
+ onError?: (error: string) => void;
11
+ className?: string;
12
+ }
13
+ export declare function CaptureButton({ onImageCaptured, isProcessing, showRetake, onRetake, defaultFacing, allowCameraSwitch, quality, onError, className, }: CaptureButtonProps): import("react/jsx-runtime").JSX.Element;
14
+ export {};
15
+ //# sourceMappingURL=CaptureButton.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CaptureButton.d.ts","sourceRoot":"","sources":["../../src/components/CaptureButton.tsx"],"names":[],"mappings":"AAGA,KAAK,YAAY,GAAG,OAAO,GAAG,MAAM,CAAC;AAErC,UAAU,kBAAkB;IAC1B,eAAe,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,IAAI,CAAC;IAChD,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAC;IACtB,aAAa,CAAC,EAAE,YAAY,CAAC;IAC7B,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,wBAAgB,aAAa,CAAC,EAC5B,eAAe,EACf,YAAoB,EACpB,UAAkB,EAClB,QAAQ,EACR,aAAuB,EACvB,iBAAwB,EACxB,OAAY,EACZ,OAAO,EACP,SAAc,GACf,EAAE,kBAAkB,2CAyJpB"}
@@ -0,0 +1,11 @@
1
+ import type { DetectionResult } from '../types';
2
+ interface DetectionResultDisplayProps {
3
+ result: DetectionResult | null;
4
+ className?: string;
5
+ successColor?: string;
6
+ warningColor?: string;
7
+ errorColor?: string;
8
+ }
9
+ export declare function DetectionResultDisplay({ result, className, successColor, warningColor, errorColor, }: DetectionResultDisplayProps): import("react/jsx-runtime").JSX.Element | null;
10
+ export {};
11
+ //# sourceMappingURL=DetectionResultDisplay.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DetectionResultDisplay.d.ts","sourceRoot":"","sources":["../../src/components/DetectionResultDisplay.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD,UAAU,2BAA2B;IACnC,MAAM,EAAE,eAAe,GAAG,IAAI,CAAC;IAC/B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,wBAAgB,sBAAsB,CAAC,EACrC,MAAM,EACN,SAAc,EACd,YAAwB,EACxB,YAAwB,EACxB,UAAsB,GACvB,EAAE,2BAA2B,kDAuF7B"}
@@ -0,0 +1,24 @@
1
+ import type { FaceDetectorProps } from '../types';
2
+ /**
3
+ * FaceDetector - A self-contained face detection component
4
+ *
5
+ * This component can be used in any React or Ionic application.
6
+ * It includes camera capture, face detection using TensorFlow.js BlazeFace,
7
+ * and visual feedback with bounding boxes.
8
+ *
9
+ * @example
10
+ * ```tsx
11
+ * import { FaceDetector } from '@tktechnico/react-face-detection';
12
+ *
13
+ * function App() {
14
+ * return (
15
+ * <FaceDetector
16
+ * onDetectionComplete={(result) => console.log(result)}
17
+ * confidenceThreshold={0.8}
18
+ * />
19
+ * );
20
+ * }
21
+ * ```
22
+ */
23
+ export declare function FaceDetector({ confidenceThreshold, maxImageSize, showOverlay, showResult, onDetectionComplete, onError, defaultFacing, allowCameraSwitch, quality, className, }: FaceDetectorProps): import("react/jsx-runtime").JSX.Element;
24
+ //# sourceMappingURL=FaceDetector.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FaceDetector.d.ts","sourceRoot":"","sources":["../../src/components/FaceDetector.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAkC,MAAM,UAAU,CAAC;AAElF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,YAAY,CAAC,EAE3B,mBAAyB,EACzB,YAAkB,EAClB,WAAkB,EAClB,UAAiB,EACjB,mBAAmB,EACnB,OAAO,EAEP,aAAuB,EACvB,iBAAwB,EACxB,OAAY,EAEZ,SAAc,GACf,EAAE,iBAAiB,2CAqHnB"}
@@ -0,0 +1,11 @@
1
+ import type { FaceDetection } from '../types';
2
+ interface FaceOverlayProps {
3
+ imageElement: HTMLImageElement | null;
4
+ face: FaceDetection | null;
5
+ containerWidth: number;
6
+ containerHeight: number;
7
+ color?: string;
8
+ }
9
+ export declare function FaceOverlay({ imageElement, face, containerWidth, containerHeight, color, }: FaceOverlayProps): import("react/jsx-runtime").JSX.Element | null;
10
+ export {};
11
+ //# sourceMappingURL=FaceOverlay.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FaceOverlay.d.ts","sourceRoot":"","sources":["../../src/components/FaceOverlay.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE9C,UAAU,gBAAgB;IACxB,YAAY,EAAE,gBAAgB,GAAG,IAAI,CAAC;IACtC,IAAI,EAAE,aAAa,GAAG,IAAI,CAAC;IAC3B,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;IACxB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,wBAAgB,WAAW,CAAC,EAC1B,YAAY,EACZ,IAAI,EACJ,cAAc,EACd,eAAe,EACf,KAAiB,GAClB,EAAE,gBAAgB,kDA6HlB"}
@@ -0,0 +1,13 @@
1
+ import type { FaceDetection } from '../types';
2
+ interface ImagePreviewWithOverlayProps {
3
+ imageSrc: string | null;
4
+ face: FaceDetection | null;
5
+ onImageLoad: (img: HTMLImageElement) => void;
6
+ showOverlay?: boolean;
7
+ overlayColor?: string;
8
+ placeholderText?: string;
9
+ className?: string;
10
+ }
11
+ export declare function ImagePreviewWithOverlay({ imageSrc, face, onImageLoad, showOverlay, overlayColor, placeholderText, className, }: ImagePreviewWithOverlayProps): import("react/jsx-runtime").JSX.Element;
12
+ export {};
13
+ //# sourceMappingURL=ImagePreviewWithOverlay.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ImagePreviewWithOverlay.d.ts","sourceRoot":"","sources":["../../src/components/ImagePreviewWithOverlay.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE9C,UAAU,4BAA4B;IACpC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,IAAI,EAAE,aAAa,GAAG,IAAI,CAAC;IAC3B,WAAW,EAAE,CAAC,GAAG,EAAE,gBAAgB,KAAK,IAAI,CAAC;IAC7C,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,wBAAgB,uBAAuB,CAAC,EACtC,QAAQ,EACR,IAAI,EACJ,WAAW,EACX,WAAkB,EAClB,YAAwB,EACxB,eAAuD,EACvD,SAAc,GACf,EAAE,4BAA4B,2CAwG9B"}
@@ -0,0 +1,11 @@
1
+ interface LoadingStateProps {
2
+ isLoading: boolean;
3
+ error: string | null;
4
+ onRetry: () => void;
5
+ loadingText?: string;
6
+ errorText?: string;
7
+ className?: string;
8
+ }
9
+ export declare function LoadingState({ isLoading, error, onRetry, loadingText, errorText, className, }: LoadingStateProps): import("react/jsx-runtime").JSX.Element | null;
10
+ export {};
11
+ //# sourceMappingURL=LoadingState.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LoadingState.d.ts","sourceRoot":"","sources":["../../src/components/LoadingState.tsx"],"names":[],"mappings":"AAEA,UAAU,iBAAiB;IACzB,SAAS,EAAE,OAAO,CAAC;IACnB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,wBAAgB,YAAY,CAAC,EAC3B,SAAS,EACT,KAAK,EACL,OAAO,EACP,WAAsC,EACtC,SAAkC,EAClC,SAAc,GACf,EAAE,iBAAiB,kDAsEnB"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @tktechnico/react-face-detection
3
+ *
4
+ * A self-contained face detection solution for React and Ionic applications.
5
+ * Uses TensorFlow.js with BlazeFace model for real-time face detection.
6
+ *
7
+ * @example Basic usage
8
+ * ```tsx
9
+ * import { FaceDetector } from '@tktechnico/react-face-detection';
10
+ *
11
+ * function App() {
12
+ * return <FaceDetector onDetectionComplete={(result) => console.log(result)} />;
13
+ * }
14
+ * ```
15
+ *
16
+ * @example Using individual components
17
+ * ```tsx
18
+ * import {
19
+ * useFaceDetectionCore,
20
+ * ImagePreviewWithOverlay,
21
+ * DetectionResultDisplay,
22
+ * CaptureButton
23
+ * } from '@lovable/react-face-detection';
24
+ * ```
25
+ */
26
+ export { FaceDetector } from './components/FaceDetector';
27
+ export { ImagePreviewWithOverlay } from './components/ImagePreviewWithOverlay';
28
+ export { DetectionResultDisplay } from './components/DetectionResultDisplay';
29
+ export { LoadingState } from './components/LoadingState';
30
+ export { CaptureButton } from './components/CaptureButton';
31
+ export { FaceOverlay } from './components/FaceOverlay';
32
+ export { useFaceDetectionCore } from './useFaceDetectionCore';
33
+ export type { FaceDetection, DetectionResult, FaceDetectorConfig, CameraConfig, FaceDetectorProps, } from './types';
34
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAGzD,OAAO,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAC;AAC/E,OAAO,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAC7E,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAGvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAG9D,YAAY,EACV,aAAa,EACb,eAAe,EACf,kBAAkB,EAClB,YAAY,EACZ,iBAAiB,GAClB,MAAM,SAAS,CAAC"}