A simple Web AI model deployment tool using JavaScript based on OpenCV.js and ONNXRuntime

Overview

WebAI.js

1. 简介

  • WebAI.js 是一个基于 OpenCV.jsONNXRuntime 开发的一个 Web 前端 AI 模型部署工具

  • 可通过在线体验网站 Hello WebAI.js 进行快速的体验试用

2. 特性

  • WebAI.js 支持 HTML script 标签引入和 node.js 两种方式进行使用

  • 目前支持目标检测 (Yolo / ssd / ...)、图像分类 (MobileNet / EfficientNet / ...)、图像分割(BiseNet / PPSeg / ...) 三类 CV 模型

  • 目前支持 PaddleDetection / PaddleClas / PaddleSeg 三个套件部分导出模型的部署

  • 2.x 版本添加了比较完善的 ts 支持,优化了代码大小,将内联的 opencv.wasm 改为文件加载,API 进行了一些改动,主要修改了模型加载的 API

  • 2.x 添加了全新的 API 文档,可直接跳转至 API 参考 页面进行查看

3. 安装

  1. HTML script 标签引入

    <!-- 最新版本 -->
    <script src='https://cdn.jsdelivr.net/npm/webai-js/dist/webai.min.js'></script>
    
    <!-- 2.0.2 版本 -->
    <script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/webai.min.js'></script>
  2. Npm 安装

    $ npm install webai-js

4. 模型

  • WebAI.js 使用 ONNX 模型进行模型推理,通过配置文件对模型的预处理进行配置

  • 一个常规的模型包含如下两个文件: model.onnx / configs.json

  • 其中 model.onnx 为模型文件,记录了模型的计算图和每层的参数,configs.json 为配置文件,记录了模型预处理的一些配置,如下为一个配置文件的具体内容:

    {
        "Preprocess": [
            {
                "type": "Decode", // 图像解码
                "mode": "RGB" // RGB 或 BGR
            },
            {
                "type": "Resize", //  图像缩放
                "interp": 1, // 插值方式
                "keep_ratio": false, // 保持长宽比
                "limit_max": false, // 限制图片尺寸
                "target_size": [300, 300] // 目标尺寸
                
            },
            {
                "type": "Normalize", // 归一化
                "is_scale": false, // 是否缩放 (img /= 255.0)
                "mean": [127.5, 127.5, 127.5], // 均值
                "std": [127.5, 127.5, 127.5] // 标准差
            },
            {
                "type": "Permute" // 转置 (HWC -> CHW)
            }
        ],
        "label_list": [
            "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", 
            "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", 
            "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"
        ] // 标签列表
    }
  • 项目中提供了多个已经过测试的预训练模型文件,具体文件位于 ./docs/pretrained_models 目录,也可在在线体验网页 Hello WebAI.js 中快速试用如下的模型,以下模型均来自 PaddleDetection / PaddleClas / PaddleSeg 提供预训练模型,具体的导出教程和兼容性表格将很快更新,更多其他套件、工具链的兼容适配也在稳步进行

    Model Type Source
    BlazeFace_1000e Detection PaddleDetection
    PPYOLO_tiny_650e_coco Detection PaddleDetection
    SSD_mobilenet_v1_300_120e_voc Detection PaddleDetection
    SSDLite_mobilenet_v3_small_320_coco Detection PaddleDetection
    EfficientNetB0_imagenet Classification PaddleClas
    MobileNetV3_small_x0_5_imagenet Classification PaddleClas
    PPLCNet_x0_25_imagenet Classification PaddleClas
    PPSEG_lite_portrait_398x224 Segmentation PaddleSeg
    STDC1_seg_voc12aug_512x512_40k Segmentation PaddleSeg
    BiseNet_cityscapes_1024x1024_160k Segmentation PaddleSeg

5. API

  • 模型加载

    // Base model
    new WebAI.Model(modelURL, sessionOption = { logSeverityLevel: 4 }, init = null, preProcess = null, postProcess = null) -> model
    
    // Base CV model
    new WebAI.CV(modelURL, inferConfig, sessionOption = { logSeverityLevel: 4 }, getFeeds = null, postProcess = null) -> modelCV
    
    // Detection model
    new WebAI.Det(modelURL, inferConfig, sessionOption = { logSeverityLevel: 4 }, getFeeds = null, postProcess = null) -> modelDet
    
    // Classification model
    new WebAI.Cls(modelURL, inferConfig, sessionOption = { logSeverityLevel: 4 }, getFeeds = null, postProcess = null) -> modelCls
    
    // Segmentation model
    new WebAI.Seg(modelURL, inferConfig, sessionOption = { logSeverityLevel: 4 }, getFeeds = null, postProcess = null) -> modelSeg    
      modelURL(string): 模型链接/路径
      inferConfig(string): 模型配置文件链接/路径
      sessionOption(object): ONNXRuntime session 的配置
      getFeeds(function(imgTensor: ort.Tensor, imScaleX: number, imScaleY: number) => feeds:object): 自定义模型输入函数
      init(function(model: WebAI.Model) => void): 自定义模型初始化函数
      preProcess(function(...args) => feeds: object): 自定义模型预处理函数
      postProcess(function(resultsTensors: object, ...args) => result: any): 自定义模型后处理函数
    
  • 模型推理

    // Base model
    (async) model.infer(...args)
    
    // Base CV model
    (async) modelCV.infer(...args)
    
    // Detection model
    (async) modelDet.infer(imgRGBA, drawThreshold=0.5) ->  bboxes
    
    // Classification model
    (async) modelCls.infer(imgRGBA, topK=5) ->  probs
    
    // Segmentation model
    (async) modelSeg.infer(imgRGBA) ->  segResults
      // 注:目前只能实现 BatchSize=1 的模型推理
    
      imgRGBA(cv.Mat): 输入图像
      drawThreshold(number): 检测阈值
      topK(number): 返回置信度前 K (K>0) 个结果,如果 K<0 返回所有结果
    
      bboxes({
          label: string, // 标签
          score: number, // 置信度
          color: number[], // 颜色(RGBA)
          x1: number, // 左上角 x 坐标
          y1: number, // 左上角 y 坐标
          x2: number, // 右下角 x 坐标
          y2: number // 右下角 y 坐标
      }[]): 目标检测包围框结果
      probs({
          label: string, // 标签
          prob: number // 置信度
      }[]): 图像分类置信度结果
      segResults({
          gray: cv.Mat, // 最大值索引图像(Gray)
          colorRGBA: cv.Mat, // 伪彩色图(RGBA)
          colorMap: { // 调色板
              lable: string, // 标签
              color: number[] // 颜色(RGBA)
          }[]
      }): 图像分割结果
    
  • 更多 API 请参考文档:API 参考

6. 部署

  • 在线体验网页:Hello WebAI.js

  • 除了在线体验网页,也可以通过 node.js 借助 vite 构建工具快速在本地部署这个体验网页

    # 安装依赖
    $ npm install
    
    # 启动服务器调试
    $ npm run dev
  • 部署完成后,就可以使用浏览器访问 http://localhost:3000/ 进行体验使用

7. 更多

You might also like...

A tool to inject javascript into the Steam Deck client.

Steam Deck UI Inject A tool to inject javascript into the Steam Deck client. How it works This tool works by taking advantage of the remote debugging

Dec 5, 2022

Edvora App is a web application based on an external API, showing data about different types of products and the user can filter these data by choosing a specific state, city or product name. Build with React.js

 Edvora App is a web application based on an external API, showing data about different types of products and the user can filter these data by choosing a specific state, city or product name. Build with React.js

Edvora App is a web application based on an external API, showing data about different types of products and the user can filter these data by choosing a specific state, city or product name. Build with React.js

Mar 11, 2022

A React-based UI toolkit for the web

A React-based UI toolkit for the web

Blueprint Blueprint is a React-based UI toolkit for the web. It is optimized for building complex, data-dense web interfaces for desktop applications

Jan 3, 2023

Web Audio API based Pitch Tuner application made with ReactJS.

Pitch Tuner Pitch Tuner is a ReactJS application based on WebAudio API. You can tune your guitar/ukelele from online without any application! The algo

Jul 11, 2022

A desktop app optimized for people to play web-based incremental game

plaza-app A desktop app optimized for people to play web-based incremental game Setup yarn to install dependencies Local testing yarn start to open lo

Dec 24, 2022

A component library based on Material Design 3 & Web Components

material-web-entity Material Web Entity A component library based on Material Design & Web Components Go to link to see what components this library s

Jun 3, 2022

Boilerplate to get started building React-based interfaces for Crestron using CH5

Getting Started with Crestron UI This project was bootstrapped with Create React App. Example component communicating with the Crestron-CH5 library: i

Apr 25, 2022

Challenge [Frontend Mentor] - In this challenge, JavaScript was used to filter jobs based on the selected categories. Technologies used: HTML5, CSS3 and React.

Frontend Mentor - Job listings with filtering Front-end challenge focused on javascript logic, where a list of fictitious vacancies is presented and t

Apr 13, 2022

This app simulates a simple bookstore, and it was created using ReactJS and Redux.

Bookstore About The Bookstore is a website similar to the "Awesome Books" website built in the previous Microverse module (see live version). My goal

Oct 3, 2021
Comments
  • 模型转换问题

    模型转换问题

    作者你好啊,感谢您开源这么优秀的项目,但我有2个疑问,想请教下……疑问1:在项目中你所用的都是paddle的预训练模型,是用paddlejsconverter --modelPath=user_model_path --paramPath=user_model_params_path --outputDir=model_saved_path --useGPUOpt=True 这个转换成你的model.onnx.json的嘛?如果不是,可以提供一个简易的转换脚本供参考嘛?……疑问2:是不是可以通过torch转成onnx,然后也可以在onnxruntime里面找转换脚本,转成你所需的model.onnx.json?可以说明下相应的处理流程嘛?万分感谢……

    opened by alwan1989 3
Releases(2.0.2)
Owner
AgentMaker
Focus on deep learning and robotics
AgentMaker
Mobile app development framework and SDK using HTML5 and JavaScript. Create beautiful and performant cross-platform mobile apps. Based on Web Components, and provides bindings for Angular 1, 2, React and Vue.js.

Onsen UI - Cross-Platform Hybrid App and PWA Framework Onsen UI is an open source framework that makes it easy to create native-feeling Progressive We

null 8.7k Jan 8, 2023
Grid-tool - Small tool that allows you to integrate a predefined or generated grid into your front-end development environment.

Grid tool Small tool that allows you to integrate a predefined or generated grid into your front-end development environment. Tool installation includ

hmongouachon 2 Jan 4, 2022
shouganaiyo-loader is a cross-platform Frida-based Node.js command-line tool that forces Java processes to load a Java/JVMTI agent regardless of whether or not the JVM has disabled the agent attach API.

shouganaiyo-loader: Forced Entry for Java Agents shouganaiyo-loader is a cross-platform Frida-based Node.js command-line tool that forces Java process

NCC Group Plc 20 Sep 19, 2022
Webrtc, & web socket based streaming live video streaming and chatting platform. Written in Node, Typescript, and Javascript!

Live Streaming!! Welcome to my implementation of live streaming along with real time chat. I'm going to make a live streaming platform that will supoo

Hamdaan Khalid 19 Nov 23, 2022
A tiny package for JavaScript Web App's state management based on RxJS & Immer

A tiny package for JavaScript Web App's state management based on RxJS & Immer

Xiao Junjiang 12 Oct 19, 2022
A simple tool that tells you what food is safe for your dog.

Can My Dog Eat A simple tool that tells you what food is safe for your dog. View website Features Can My Dog Eat is a simple website where you can loo

Isabelle Viktoria Maciohsek 25 Dec 11, 2022
Simple React Native marquee component,fully implemented using reanimated v2,support to iOS/Android/Web.

@react-native-reanimated-community/react-native-reanimated-marquee Simple React Native marquee component,fully implemented using reanimated v2,support

react-native-reanimated-community 6 Sep 25, 2022
Recipe Buddy is a tool that enables the easy adding of recipes to Grocy through web scraping.

Recipe Buddy The problem I am getting sick of manually importing recipes into Grocy. The solution Overcomplication, naturally. Recipe Buddy is a web a

George Gebbett 44 Dec 17, 2022
Annotation-Wizard - a web-based image annotation platform that allows authors to create annotation tasks and annotators to take tasks and annotate images.

Annotation Wizard Annotation Wizard is a web-based image annotation platform. Functionalies User login and register create your own image annotation t

null 2 Aug 12, 2022
An easy to use and simple masonry layout for React Js based on flexbox column.

React Masonry An easy to use and simple masonry layout for React Js based on flexbox column. Live Preview / Demo In Here Basic Usage Masonry Layout <M

null 16 Dec 23, 2022