小鱼易连平台支持智能人脸识别相关功能,具体功能说明可参考小鱼易连产品说明文档。
人脸识别服务需要进行开通后才能使用,并且需要进行一定的前期准备工作,比如上传人脸照片。因此在集成之前请通过商务及技术支持获取相关协助。
SDK 封装了人脸识别场景中的功能接口,开发者可通过 API 快速获取人脸识别信息,之后可对获取到的人脸信息进行简单处理并显示到会议画面中。以此方式可实现典型的人脸识别应用场景。
在平台后台开通人脸识别并且上传用户照片信息后,会议中会收到 AIFaceRecv 人脸信息回调,回调中包含了参会者 pid,以及人脸位置信息。接下来需要调用 getFaceInfo 方法去查询每一个 faceId 对应的人脸信息。 获取到人脸信息后,可以将录入的人脸 faceId 对应的人名以及人脸 Postion 绘制到屏幕上。
/**
* 人脸位置信息
*
* @property { number } faceId 人脸的ID
* @property { number } left 检测人脸的左侧距离
* @property { number } right 检测人脸的右侧距离
* @property { number } top 检测人脸的顶部距离
* @property { number } bottom 检测人脸的底部距离
*
*/
interface IFacePosition {
faceId: number;
left: number;
right: number;
top: number;
bottom: number;
}
这里的人脸位置是检测的相对位置,不是显示 View 的具体位置。假设显示人脸的 video 的尺寸为 W 和 H,则某一个人脸的显示框的检测位置示意图如下:
计算公式如下:
由于本地视频为镜像视频,所以人脸位置也是需要镜像
const getFacePosition = (position) => {
let w = item.position.width;
let h = item.position.height;
const _videoWidth = item.roster.videoWidth;
const _videoHeight = item.roster.videoHeight;
let nW = w;
let nH = h;
let xOffset = 0;
let yOffset = 0;
if (_videoHeight > 0 && _videoWidth > 0) {
const scale = w / h;
const vScale = _videoWidth / _videoHeight;
if (vScale >= scale) {
if (_videoWidth >= w) {
nH = _videoHeight / (_videoWidth / w);
} else {
nH = _videoHeight * (w / _videoWidth);
}
yOffset = (h - nH) / 2;
} else {
if (_videoHeight >= h) {
nW = _videoWidth / (_videoHeight / h);
} else {
nW = _videoWidth * (h / _videoHeight);
}
xOffset = (w - nW) / 2;
}
}
const a = 10000; // 缩放系数
const x1 = (position.left * nW) / a + xOffset;
const y1 = (position.top * nH) / a + yOffset;
const x2 = (position.right * nW) / a + xOffset;
const y2 = (position.bottom * nH) / a + yOffset;
let startX = x1;
if (item.sourceId === "LocalPreviewID") {
startX = w - x2;
}
return {
startX,
startY: y1,
width: x2 - x1,
height: y2 - y1,
};
};
./models/*
;// 开启远端人脸识别模式
xyRTC.enableFaceDetectMode('Detect', true);
// 人脸坐标信息
xyRTC.on('AIFaceRecv', (e: IAIFaceRecv ) => {
const {positionArr=[]} = e;
let faceIds = [];
positionArr.forEach((position, index) => {
const {faceId} = position;
if (faceId > -1) {
faceIds.push(faceId)
}
});
// 批量获取人脸信息(名称等)
if (faceIds.length > 0) xyRTC.getFaceInfo(faceIds);
});
// getFaceInfo 回调
xyRTC.on('AIFaceInfo', (e: IAIFaceInfo[]) => {
})
从v3.10.1开始,sdk移除了getFaceInfo方法以及AIFaceInfo事件的监听,而人脸信息(名称等)等数据合并到AIFaceRecv事件中,因此新版本只需要监听AIFaceRecv事件即可,无需再调用getFaceInfo和监听AIFaceInfo回调