detection-lib
Advanced tools
Comparing version
{ | ||
"name": "detection-lib", | ||
"version": "1.0.15", | ||
"version": "1.0.16", | ||
"main": "src/DetectorFactory.js", | ||
@@ -5,0 +5,0 @@ |
import { Detector } from './types.js'; | ||
import { FaceDetection } from '@mediapipe/face_detection'; | ||
import ImageUrl from './image.png'; | ||
import { FACE_DETECTOR_CODES } from './faceDetectorCodes.js'; | ||
export class FaceDetector extends Detector { | ||
@@ -72,35 +73,43 @@ constructor() { | ||
/** | ||
* @param {HTMLVideoElement|HTMLImageElement|HTMLCanvasElement} input | ||
* @returns {Promise<import('./types.js').DetectionResult>} | ||
*/ | ||
async detect(input) { | ||
if (!this.modelReady) { | ||
throw new Error("FaceDetector not initialized"); | ||
} | ||
/** | ||
* @param {HTMLVideoElement|HTMLImageElement|HTMLCanvasElement} input | ||
* @returns {Promise<{status: number, message: string, boxes: Array}>} | ||
*/ | ||
async detect(input) { | ||
if (!this.modelReady) { | ||
const {code,message}= FACE_DETECTOR_CODES.NOT_INITIALIZED; | ||
return { status: code, message, boxes: [] }; | ||
} | ||
try{ | ||
await this.faceDetector.send({ image: input }); | ||
}catch(e){ | ||
const {code,message}= FACE_DETECTOR_CODES.MODEL_ERROR; | ||
return { status: code, message, boxes: [] }; | ||
} | ||
const results = this._latestResults; | ||
if (!results || !results.detections) { | ||
return { type: 'face', boxes: [] }; | ||
const results = this._latestResults; | ||
const detections = results?.detections || []; | ||
let statusObj = FACE_DETECTOR_CODES.OK; | ||
if (detections.length === 0) { | ||
statusObj = FACE_DETECTOR_CODES.NO_FACE_DETECTED; | ||
} else if (detections.length > 1) { | ||
statusObj = FACE_DETECTOR_CODES.MULTIPLE_FACES_DETECTED; | ||
} | ||
const boxes = results.detections.map((det) => { | ||
const b = det.boundingBox; | ||
return { | ||
x: (b.xCenter - b.width / 2), | ||
y: (b.yCenter - b.height / 2), | ||
w: b.width, | ||
h: b.height, | ||
score: det.score?.[0] || undefined, | ||
}; | ||
}); | ||
const boxes = detections.map((det) => { | ||
const b = det.boundingBox; | ||
return { | ||
type: 'face', | ||
boxes, | ||
data: results, | ||
x: b.xCenter - b.width / 2, | ||
y: b.yCenter - b.height / 2, | ||
w: b.width, | ||
h: b.height, | ||
score: det.score?.[0] || undefined, | ||
}; | ||
} | ||
}); | ||
return { | ||
status: statusObj.code, | ||
message: statusObj.message, | ||
boxes, | ||
}; | ||
} | ||
} |
24668518
74.68%22
10%2630
0.69%