0.1.51 • Published 3 months ago

g-ai-robot3 v0.1.51

Weekly downloads
-
License
-
Repository
-
Last release
3 months ago

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问答弹窗位置Stringleft/right/top/bottom/top-start/top-end/bottom-start/bottom-endtop-end
useAudio语音功能Booleantrue/falsetrue
space监听时间间隔Number-3000
mode交互模式Stringtext/audiotext
openInstruct是否开启指令控制Booleantrue/falsetrue
qaServer问答服务地址String--
audioServer语音服务地址String--
cozeInfo扣字大模型参数Object--
wsServer语音监听地址String--
instructWs指令监听地址String--
eventFun触发事件Array--
searchTextCallback扣子大模型事件流回调函数Function--

cozeInfo

参数说明类型可选值默认值
issString--
kidString--
private_keyString--
bot_idString--

eventFun

参数说明类型可选值默认值
keywords触发关键字Array--
trigger回调触发时机Stringafter/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