Created
October 29, 2025 08:57
-
-
Save tripolskypetr/71b4bd76005ce91237aea17ede13e31b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const ROI_BORDER_RATIO = 0.01; | |
| interface IFaceDetect { | |
| detectId: string; | |
| bbox: { left: number; top: number; right: number; bottom: number }; | |
| detectionScore: number; | |
| lowQuality: boolean; | |
| } | |
| interface IBbox { | |
| left: number; | |
| top: number; | |
| right: number; | |
| bottom: number; | |
| } | |
| interface IDrawingFaces { | |
| bbox: IBbox; | |
| } | |
| const compareFaces = <T extends IDrawingFaces>(aFace: T, bFace?: T): T => { | |
| if (!bFace) { | |
| return aFace; | |
| } | |
| const a = aFace.bbox; | |
| const b = bFace.bbox; | |
| const aw = Math.abs(a.right - a.left); | |
| const ah = Math.abs(a.bottom - a.top); | |
| const bw = Math.abs(b.right - b.left); | |
| const bh = Math.abs(b.bottom - b.top); | |
| if (aw + ah > bw + bh) { | |
| return aFace; | |
| } | |
| return bFace; | |
| }; | |
| const getBiggestFace = <T extends IDrawingFaces = IDrawingFaces>( | |
| faces: T[] | |
| ): T | null => { | |
| if (!faces.length) { | |
| return null; | |
| } | |
| return faces.reduce(compareFaces); | |
| }; | |
| const calcRoi = (height: number, width: number): IBbox => { | |
| const left = Math.floor(width * ROI_BORDER_RATIO); | |
| const top = Math.floor(height * ROI_BORDER_RATIO); | |
| const roiWidth = Math.ceil(width * (1 - 2 * ROI_BORDER_RATIO)); | |
| const roiHeight = Math.ceil(height * (1 - 2 * ROI_BORDER_RATIO)); | |
| return { | |
| left, | |
| top, | |
| right: left + roiWidth, | |
| bottom: top + roiHeight | |
| }; | |
| }; | |
| const checkRoi = (rect: IBbox, roi: IBbox) => { | |
| const rectLeft = rect.left; | |
| const rectTop = rect.top; | |
| const rectRight = rect.right; | |
| const rectBottom = rect.bottom; | |
| const roiLeft = roi.left; | |
| const roiTop = roi.top; | |
| const roiRight = roi.right; | |
| const roiBottom = roi.bottom; | |
| return ( | |
| rectLeft >= roiLeft && | |
| rectTop >= roiTop && | |
| rectRight <= roiRight && | |
| rectBottom <= roiBottom | |
| ); | |
| } | |
| // ... | |
| const detection: IFaceDetect[] = | |
| await faceids.faceIdsGlobalService.detectFaceByBlob({ | |
| ...this.contextService.context, | |
| data: { | |
| blob, | |
| }, | |
| }); | |
| const biggestFace = getBiggestFace(detection.data); | |
| const roi = calcRoi(image.naturalHeight, image.naturalWidth); | |
| if (!biggestFace) { | |
| return false; | |
| } | |
| if (!checkRoi(biggestFace.bbox, roi)) { | |
| return false; | |
| } | |
| return true; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment