Skip to main content

ProcVision algorithm SDK

Project description

ProcVision Algorithm SDK

概述

  • 提供 BaseAlgorithm 抽象与最小配套能力:Session 状态共享、结构化日志、诊断数据与共享内存读图接口。
  • 算法方通过实现 get_infopre_executeexecute 与生命周期钩子,按 spec.md 与平台解耦集成。

安装

  • 从源码构建后安装:pip install dist/procvision_algorithm_sdk-<version>-py3-none-any.whl
  • 或直接安装:pip install procvision_algorithm_sdk

接口要点

  • BaseAlgorithm.__init__() 不绑定 PID;每次调用通过参数传入 pid
  • pre_execute(step_index, pid, session, user_params, shared_mem_id, image_meta)
  • execute(step_index, pid, session, user_params, shared_mem_id, image_meta)
  • 日志时间戳字段统一为 timestamp_ms
  • 共享内存传图 JPEG-only,image_meta 最小集合:width/height/timestamp_ms/camera_id
  • pre_execute 不返回真实检测结果;execute 的业务判定在 data.result_statusOK/NG

快速开始

  • 最小目录:
    • your_algo/main.py
    • manifest.json
    • requirements.txt
  • 代码示例:
from typing import Any, Dict
from procvision_algorithm_sdk import BaseAlgorithm, Session, read_image_from_shared_memory

class MyAlgo(BaseAlgorithm):
    def __init__(self) -> None:
        super().__init__()
        self._supported_pids = ["p001", "p002"]

    def get_info(self) -> Dict[str, Any]:
        return {
            "name": "my_algo",
            "version": "1.0",
            "supported_pids": self._supported_pids,
            "steps": [{"index": 0, "name": "示例", "params": [{"key": "threshold", "type": "float", "default": 0.5, "min": 0.0, "max": 1.0}]}],
        }

    def pre_execute(self, step_index: int, pid: str, session: Session, user_params: Dict[str, Any], shared_mem_id: str, image_meta: Dict[str, Any]) -> Dict[str, Any]:
        if pid not in self._supported_pids:
            return {"status": "ERROR", "message": f"不支持的产品型号: {pid}", "error_code": "1001"}
        img = read_image_from_shared_memory(shared_mem_id, image_meta)
        if img is None:
            return {"status": "ERROR", "message": "图像数据为空", "error_code": "1002"}
        return {"status": "OK", "message": "准备就绪", "debug": {"latency_ms": 0.0}}

    def execute(self, step_index: int, pid: str, session: Session, user_params: Dict[str, Any], shared_mem_id: str, image_meta: Dict[str, Any]) -> Dict[str, Any]:
        img = read_image_from_shared_memory(shared_mem_id, image_meta)
        if img is None:
            return {"status": "ERROR", "message": "图像数据为空", "error_code": "1002"}
        return {"status": "OK", "data": {"result_status": "OK", "defect_rects": [], "debug": {"latency_ms": 0.0}}}

CLI(Dev Runner)

  • 程序名:procvision-cli
  • 校验算法包:
    • procvision-cli validate ./your_algo_project --full(适配器子进程握手+调用)
    • 显式入口:procvision-cli validate ./your_algo_project --full --entry your_pkg.main:YourAlgorithm
    • 旧路径:procvision-cli validate ./your_algo_project --legacy-validate
    • 输出日志:procvision-cli validate ./your_algo_project --full --tail-logs
  • 本地模拟运行:
    • procvision-cli run ./your_algo_project --pid p001 --image ./test.jpg --json
    • 运行机制:以适配器子进程方式,通过帧协议通信与共享内存读写;如需旧的本地直接导入执行,追加 --legacy-run
    • 输出日志:procvision-cli run ./your_algo_project --pid p001 --image ./test.jpg --tail-logs

适配器启动(Runner 集成)

  • 简化命令:
    • Windows:<deployed_dir>\venv\Scripts\python.exe -m procvision_algorithm_sdk.adapter
    • Linux:<deployed_dir>/venv/bin/python -m procvision_algorithm_sdk.adapter
  • 自动发现入口优先级:--entry > PROC_ENTRY_POINT > manifest.json/manifest.yaml > pyproject.toml [tool.procvision.algorithm] > 默认 algorithm.main:Algorithm

离线交付

  • 生成 requirements.txtpip freeze > requirements.txt
  • 下载 wheels:
pip download -r requirements.txt -d wheels/ --platform win_amd64 --python-version 3.10 --implementation cp --abi cp310
  • 打包 zip:包含源码目录、manifest.jsonrequirements.txtwheels/ 与可选 assets/

本地打包与发布(pip 包)

  • 安装构建与发布工具:
    • pip install -U build twine
  • 构建 wheel 与源码包(基于 pyproject.toml):
    • python -m build
    • 产物输出在 dist/(如:procvision_algorithm_sdk-<version>-py3-none-any.whlprocvision_algorithm_sdk-<version>.tar.gz
  • 本地安装验证:
    • pip install dist/procvision_algorithm_sdk-<version>-py3-none-any.whl
  • 发布到内部 PyPI(示例,仅供参考):
    • twine upload --repository-url <your-internal-pypi-url> dist/*
    • 建议在环境变量或凭据管理中配置用户与令牌,避免将敏感信息写入命令行
  • 版本号更新:
    • 编辑 pyproject.tomlversion 字段(当前:pyproject.toml:7)并重新构建
    • 建议在 CI 中基于标签或提交自动生成版本并构建

GitHub CI/CD

  • 工作流文件:.github/workflows/sdk-build-and-publish.yml
  • 关键步骤:安装依赖、运行测试、python -m build 构建产物、按标签发布到包仓库
  • 运行单元测试:python -m unittest discover -s tests -p "test_*.py" -v

目录与文件

  • 包路径:procvision_algorithm_sdk
  • 打包配置:pyproject.toml
  • 单元测试:tests/

版本与兼容

  • 要求 Python >=3.10
  • 依赖:numpy>=1.21
  • 当前版本:v0.0.6(新增适配器模块与 CLI 改动)

参考

  • protocol_adapter_spec.mdrunner_spec.mdalgorithm_dev_tutorial.md 提供接口契约、通信协议与开发指南
  • 版本变更:docs/release-notes/v0.0.6.md

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

procvision_algorithm_sdk-0.0.6.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

procvision_algorithm_sdk-0.0.6-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

Details for the file procvision_algorithm_sdk-0.0.6.tar.gz.

File metadata

  • Download URL: procvision_algorithm_sdk-0.0.6.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for procvision_algorithm_sdk-0.0.6.tar.gz
Algorithm Hash digest
SHA256 8d1a0871e8fb4900b6f972db268e8d67e30c9a4455cfdf25b8d81b1cd20730b5
MD5 f9513f5ae033c73d8d4c0fdb54dad343
BLAKE2b-256 a598e949dd8c607f2b234b8b10f8083dd706b4ef1371505ae422ceecc0d2f3de

See more details on using hashes here.

File details

Details for the file procvision_algorithm_sdk-0.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for procvision_algorithm_sdk-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7d1f54dc450c6f96ce5e8c88fd0a2d885f350a23a8a6c61721ba9d9fc19a4134
MD5 99868c7c2bdae88b195e1d5ead8bf752
BLAKE2b-256 9e531d770cc237d54eeaad12f304f4e86c8ceb0e076b9353a8cf2209acfe94f8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page