0.1.51 • Published 3 months ago
g-ai-robot3 v0.1.51
g-ai-robot3
简介
封装了QA问答和语音问答功能(vue3版本)
安装(npm or yarn)
npm install g-ai-robot3 --save
使用
import gAiRobot from "g-ai-robot";
import "g-ai-robot3/dist/g-ai-robot3.css"
<g-ai-robot
:robotCss="{ zIndex: 9999, left: '10px', bottom: '300px' }"
placement="top-end"
>
</g-ai-robot>
//添加到引用组件的代码中(不在组件中引入)
const video = ref(null);
const canvas = ref(null);
const ctx = ref(null);
const canvas_tmp = ref(null);
const ctx_tmp = ref(null);
const init = () => {
ctx.value = canvas.value.getContext('2d');
// 创建的canvas宽高最好与显示图片的canvas、video宽高一致
canvas_tmp.value = document.createElement('canvas');
canvas_tmp.value.setAttribute('width', 300);
canvas_tmp.value.setAttribute('height', 300);
ctx_tmp.value = canvas_tmp.value.getContext('2d');
video.value.addEventListener('play', computeFrame);
}
const numToPoint = (num, width) => {
let col = num % width;
let row = Math.floor(num / width);
row = col === 0 ? row : row + 1;
col = col === 0 ? width : col;
return [row, col];
}
const pointToNum = (point, width) => {
let [row, col] = point;
return (row - 1) * width + col
}
const getAroundPoint = (point, width, height, area) => {
let [row, col] = point;
let allAround = [];
if (row > height || col > width || row < 0 || col < 0) return allAround;
for (let i = 0; i < area; i++) {
let pRow = row - 1 + i;
for (let j = 0; j < area; j++) {
let pCol = col - 1 + j;
if (i === area % 2 && j === area % 2) continue;
allAround.push([pRow, pCol]);
}
}
return allAround.filter(([iRow, iCol]) => {
return (iRow > 0 && iCol > 0) && (iRow <= height && iCol <= width);
})
}
const computeFrame = () => {
if (video.value) {
if (video.value.paused || video.value.ended) return;
}
// 如果视频比例和canvas比例不正确可能会出现显示形变, 调整除的值进行比例调整
ctx_tmp.value.drawImage(video.value, 0, 0, video.value.clientWidth / 1, video.value.clientHeight / 1);
// 获取到绘制的canvas的所有像素rgba值组成的数组
let frame = ctx_tmp.value.getImageData(0, 0, video.value.clientWidth, video.value.clientHeight);
//----- emergence ----------
const height = frame.height;
const width = frame.width;
const pointLens = frame.data.length / 4;
for (let i = 0; i < pointLens; i++) {
let r = frame.data[i * 4];
let g = frame.data[i * 4 + 1];
let b = frame.data[i * 4 + 2];
if (r < 100 && g > 190 && b < 200) {
frame.data[i * 4 + 3] = 0;
}
}
const tempData = [...frame.data]
for (let i = 0; i < pointLens; i++) {
if (frame.data[i * 4 + 3] === 0) continue
const currentPoint = numToPoint(i + 1, width);
const arroundPoint = getAroundPoint(currentPoint, width, height, 3);
let opNum = 0;
let rSum = 0;
let gSum = 0;
let bSum = 0;
arroundPoint.forEach((position) => {
const index = pointToNum(position, width);
rSum = rSum + tempData[(index - 1) * 4];
gSum = gSum + tempData[(index - 1) * 4 + 1];
bSum = bSum + tempData[(index - 1) * 4 + 2];
if (tempData[(index - 1) * 4 + 3] !== 255) opNum++;
})
let alpha = (255 / arroundPoint.length) * (arroundPoint.length - opNum);
if (alpha !== 255) {
// debugger
frame.data[i * 4] = parseInt(rSum / arroundPoint.length);
frame.data[i * 4 + 1] = parseInt(gSum / arroundPoint.length);
frame.data[i * 4 + 2] = parseInt(bSum / arroundPoint.length);
frame.data[i * 4 + 3] = parseInt(alpha);
}
}
//------------------------
ctx.value.putImageData(frame, 0, 0);
setTimeout(computeFrame, 0);
}
onMounted(() => {
init();
})
props
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
isDebug | 开启debug打印 | Boolean | - | - |
title | 问答标题 | String | - | - |
greet | 问答问候语 | String | - | - |
waitTxt | 问答等候语 | String | - | - |
systemName | 系统编码 | String | - | - |
robotCss | 机器人在视口中的位置 | object | - | left: "10px", bottom: "10px" |
placement | 问答弹窗位置 | String | left/right/top/bottom/top-start/top-end/bottom-start/bottom-end | top-end |
useAudio | 语音功能 | Boolean | true/false | true |
space | 监听时间间隔 | Number | - | 3000 |
mode | 交互模式 | String | text/audio | text |
openInstruct | 是否开启指令控制 | Boolean | true/false | true |
qaServer | 问答服务地址 | String | - | - |
audioServer | 语音服务地址 | String | - | - |
cozeInfo | 扣字大模型参数 | Object | - | - |
wsServer | 语音监听地址 | String | - | - |
instructWs | 指令监听地址 | String | - | - |
eventFun | 触发事件 | Array | - | - |
searchTextCallback | 扣子大模型事件流回调函数 | Function | - | - |
cozeInfo
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
iss | String | - | - | |
kid | String | - | - | |
private_key | String | - | - | |
bot_id | String | - | - |
eventFun
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
keywords | 触发关键字 | Array | - | - |
trigger | 回调触发时机 | String | after/together | - |
fun | 触发的回调函数 | Function | - | - |
方法
参数 | 说明 | 参数 |
---|---|---|
searchText | 问答接口 | 接收一个参数:{searchText} 问题的字符串 |
startMonitorAudio | 开启语音监听 | - |
uploadWavFile | 语音转文字接口 | 接收一个参数(formData类型):{modelName,audio} modelName:语音模型(tiny/base/small/medium/large), audio:语音文件流 |
Slot
参数 | 说明 |
---|---|
reference | 触发问答弹窗显示的HTML元素 |
0.1.50
3 months ago
0.1.51
3 months ago
0.1.49
4 months ago
0.1.46
4 months ago
0.1.47
4 months ago
0.1.48
4 months ago
0.1.42
5 months ago
0.1.43
5 months ago
0.1.44
5 months ago
0.1.45
5 months ago
0.1.41
5 months ago
0.1.40
7 months ago
0.1.39
7 months ago
0.1.34
7 months ago
0.1.35
7 months ago
0.1.36
7 months ago
0.1.37
7 months ago
0.1.38
7 months ago
0.1.30
7 months ago
0.1.31
7 months ago
0.1.32
7 months ago
0.1.33
7 months ago
0.1.28
7 months ago
0.1.29
7 months ago
0.1.27
8 months ago
0.1.25
8 months ago
0.1.26
8 months ago
0.1.23
8 months ago
0.1.24
8 months ago
0.1.21
10 months ago
0.1.22
10 months ago
0.1.20
10 months ago
0.1.18
10 months ago
0.1.19
10 months ago
0.1.15
10 months ago
0.1.16
10 months ago
0.1.17
10 months ago
0.1.13
10 months ago
0.1.14
10 months ago
0.1.10
11 months ago
0.1.11
11 months ago
0.1.12
10 months ago
0.1.9
11 months ago
0.1.8
11 months ago
0.1.7
11 months ago
0.1.4
11 months ago
0.1.3
11 months ago
0.1.6
11 months ago
0.1.5
11 months ago
0.1.1
11 months ago