6个版本 (3个重大变更)

0.4.1 2023年5月21日
0.4.0 2023年4月6日
0.3.1 2023年3月14日
0.2.0 2023年1月19日
0.1.0 2023年1月12日

#271 in WebAssembly

每月28次下载

MIT 许可证

61KB
1K SLoC

Hypetrigger ⚡

Github | Crates.io | Docs.rs | NPM | 网站 | Discord

对流媒体视频进行高效的帧操作。

它做什么?

👉 如果你只是需要一个围绕FFmpeg的最小包装器,而不需要额外的图像处理和计算机视觉绑定,你应该直接使用 ffmpeg-sidecar crate。 🏍

架构图

简单版本

Video → FFMPEG → Tensorflow/Tesseract/Custom → Callback

标注版本

                                 metadata,
                                 progress,
                                  errors
                                    ▲
                                    │
                            ┌───────┴───┐   ┌────────────┐
                         ┌──► stderr    │ ┌─► tesseract  ├─────► callback
                         │  └───────────┘ │ └────────────┘
                         │                │
 ┌─────────────┐ ┌───────┴┐ ┌───────────┐ │ ┌────────────┐
 │ Input Video ├─► ffmpeg ├─► stdout    ├─┼─► tensorflow ├─────► callback
 └─────────────┘ └───────┬┘ └───────────┘ │ └────────────┘
  - Video files          │                │
  - Static images        │  ┌───────────┐ │ ┌────────────────┐
  - HTTP URLs            └──► stdin     │ └─► custom trigger ├─► callback
  - Live streams            └───────▲───┘   └────────────────┘
  - Desktop capture                 │
  - Webcam video                    |
                               pause/stop
                                commands

 └─────────────┘ └───────────────────────┘ └─────────────────┘   └──────┘
   MEDIA SOURCE        VIDEO DECODING        COMPUTER VISION     CALLBACK

入门(Rust)

cargo add hypetrigger
use hypetrigger::{Hypetrigger, SimpleTrigger};

fn main() {
    Hypetrigger::new()
        .test_input()
        .add_trigger(SimpleTrigger::new(|frame| {
            println!("received frame {}: {}x{}",
                frame.frame_num,
                frame.image.width(),
                frame.image.height()
            );
            // Now do whatever you want with it...
        }))
        .run();
}

深入示例(Rust)

这是一段略微简化的示例代码。它不会立即编译并运行,除非有正确的输入源和参数,但它说明了如何使用API来解决现实世界的问题。

问题声明: 在世界杯比赛的实时视频中检测何时进球。⚽

Cargo.toml

[dependencies]
hypetrigger = { version = "0.2.0", features = ["photon", "tesseract"] }
# enable the `tesseract` feature and its `photon` dependency for image processing
# see the "Native Dependencies" section in `README.md` if you have trouble building

main.rs

use hypetrigger::{Hypetrigger, SimpleTrigger};
use hypetrigger::photon::{Crop, ThresholdFilter};
use hypetrigger::tesseract::{TesseractTrigger, init_tesseract}

fn main() {
    // First, init a Tesseract instance with default params
    let tesseract = init_tesseract(None, None)?;

    // Initialize some state (use an Rc or Arc<Mutex> if needed)
    let mut last_score: Option<u32> = None;

    // Create a trigger that will be used to detect the scoreboard
    let trigger = TesseractTrigger {
        tesseract, // pass in the Tesseract instance

        // Identify the rectangle of the video that contains
        // the scoreboard (probably the bottom-middle of the
        // screen)
        crop: Some(Crop {
            left_percent: 25.0,
            top_percent: 25.0,
            width_percent: 10.0,
            height_percent: 10.0,
        }),

        // Filter the image to black and white
        // based on text color. This preprocessing improves Tesseract's
        // ability to recognize text. You could replace it with
        // your own custom preprocessing, like edge-detection,
        // sharpening, or anything else.
        threshold_filter: Some(ThresholdFilter {
          r: 255,
          g: 255,
          b: 255,
          threshold: 42,
        }),

        // Attach the callback which will run on every frame with the
        // recognized text
        callback: |result| {
          let parsed_score: u32 = result.text.parse();
          if parsed_score.is_err() {
            return Ok(()) // no score detected; continue to next frame
          }

          // Check for different score than last frame
          if last_score.unwrap() == parsed_score.unwrap() {
            println!("A goal was scored!");

            // Do something:
            todo!("celebrate 🎉");
            todo!("tell your friends");
            todo!("record a clip");
            todo!("send a tweet");
            todo!("cut to commercial break");
          }

          // Update state
          last_score = parsed_score;
        },

        // Using this option will pause after every frame,
        // so you can see the effect of your crop + filter settings
        enable_debug_breakpoints: false,
    };

    // Create a pipeline using the input video and your customized trigger
    Hypetrigger::new()
        .input("https://example.com/world-cup-broadcast.m3u8")
        .add_trigger(trigger)
        .run();

    // `run()` will block the main thread until the job completes,
    // but the callback will be invoked in realtime as frames are processed!
}

入门(TypeScript)

浏览器和Node通过使用优秀的Photon.js图像处理库将图像预处理代码编译为WASM来支持。之后使用Tesseract.js进行文本识别。

npm add hypetrigger
const videoElem = document.getElementById('video')
const pipeline = new Hypetrigger(videoElem)
  .addTrigger(frame => {
    console.log({ frame })
    // do whatever you want with the frame
  })
  .autoRun()

局限性

TS版本并不是Rust库的完整移植;而是更接近于具有部分完整功能的并行工具包。

目前还没有Tensorflow.js绑定,帧直接从媒体源中提取,完全消除了FFMPEG的使用。

更多信息,请参阅文档中的此页面:与其他语言的使用

原生依赖项

Visual Studio构建工具

  • 必须安装"Visual Studio Build Tools 2017" -- 当前版本15.9.50
  • 还必须安装包含以下"桌面开发使用C++"工作负载组件的"Visual Studio Community 2019"
    • MSVC v142 - VS 2019 C++ x65/x86构建工具
    • Windows的C++ CMake工具
    • 适用于最新v142构建工具的C++ ATL

构建工具由Cargo要求,使用VS 2019编译和链接原生依赖项

Tensorflow

应该由Cargo自动安装。

Tesseract

使用vcpkg手动安装(Github

git clone https://github.com/microsoft/vcpkg
cd vcpkg
./bootstrap-vcpkg.bat
./vcpkg integrate install
./vcpkg install leptonica:x64-windows-static-md
./vcpkg install tesseract:x64-windows-static-md

还要安装包含在最新LLVM发布版中的libclang

当前版本:https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.6/LLVM-14.0.6-win64.exe

wasm-pack

cargo install wasm-pack

如果您遇到类似于这样的OpenSSL/Perl错误

此perl实现不会生成Windows-like路径

尝试从Windows cmd.exe运行一次,而不是从VSCode集成终端和/或git bash。

贡献

欢迎在Github页面上提交拉取请求、错误报告和功能请求。

依赖项

~22–44MB
~520K SLoC