0.1.51 • Published 11 months ago

g-ai-robot3 v0.1.51

Weekly downloads
-
License
-
Repository
-
Last release
11 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

11 months ago

0.1.51

11 months ago

0.1.49

12 months ago

0.1.46

12 months ago

0.1.47

12 months ago

0.1.48

12 months ago

0.1.42

1 year ago

0.1.43

1 year ago

0.1.44

1 year ago

0.1.45

1 year ago

0.1.41

1 year ago

0.1.40

1 year ago

0.1.39

1 year ago

0.1.34

1 year ago

0.1.35

1 year ago

0.1.36

1 year ago

0.1.37

1 year ago

0.1.38

1 year ago

0.1.30

1 year ago

0.1.31

1 year ago

0.1.32

1 year ago

0.1.33

1 year ago

0.1.28

1 year ago

0.1.29

1 year ago

0.1.27

1 year ago

0.1.25

1 year ago

0.1.26

1 year ago

0.1.23

1 year ago

0.1.24

1 year ago

0.1.21

1 year ago

0.1.22

1 year ago

0.1.20

1 year ago

0.1.18

1 year ago

0.1.19

1 year ago

0.1.15

1 year ago

0.1.16

1 year ago

0.1.17

1 year ago

0.1.13

1 year ago

0.1.14

1 year ago

0.1.10

1 year ago

0.1.11

1 year ago

0.1.12

1 year ago

0.1.9

1 year ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.1

2 years ago