Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions .github/workflows/cpp-model-images.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: C++ Rockchip model images

on:
pull_request:
paths:
- 'src/cpp/**'
- '.github/workflows/cpp-model-images.yml'
push:
branches: [main]
paths:
- 'src/cpp/**'
- '.github/workflows/cpp-model-images.yml'
workflow_dispatch:
inputs:
model:
description: Catalog model ID (empty builds the full catalog)
type: string
required: false

permissions:
contents: read

concurrency:
group: cpp-model-images-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
images:
runs-on: ubuntu-24.04-arm
timeout-minutes: 120
permissions:
contents: read
packages: write
defaults:
run:
working-directory: src/cpp
env:
RUNTIME_IMAGE: rockchip-cv-runtime:${{ github.sha }}
PUBLISH: ${{ github.ref == 'refs/heads/main' && github.event_name != 'pull_request' }}
MODEL_ID: ${{ inputs.model }}
steps:
- uses: actions/checkout@v6
with:
persist-credentials: false
lfs: false
sparse-checkout: src/cpp
- uses: actions/setup-python@v6
with:
python-version: '3.12'
- name: Validate model catalog
run: |
python -m pip install 'PyYAML==6.0.3'
python tools/models.py validate
- uses: docker/login-action@v4
if: env.PUBLISH == 'true'
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Build or reuse the shared runtime
shell: bash
run: |
namespace="ghcr.io/${GITHUB_REPOSITORY,,}"
remote_runtime="$namespace/runtime:$GITHUB_SHA"
echo "IMAGE_NAMESPACE=$namespace" >> "$GITHUB_ENV"
if [[ "$PUBLISH" == true ]]; then
if output=$(docker pull --platform linux/arm64 "$remote_runtime" 2>&1); then
echo "$output"
docker image tag "$remote_runtime" "$RUNTIME_IMAGE"
exit 0
fi
echo "$output"
if ! grep -Eqi 'manifest unknown|not found|name unknown' <<< "$output"; then
exit 1
fi
fi
docker build --builder default --platform linux/arm64 --target runtime \
--label "org.opencontainers.image.source=https://github.com/$GITHUB_REPOSITORY" \
--label "org.opencontainers.image.revision=$GITHUB_SHA" \
--tag "$RUNTIME_IMAGE" .
if [[ "$PUBLISH" == true ]]; then
docker image tag "$RUNTIME_IMAGE" "$remote_runtime"
docker push "$remote_runtime"
fi
- name: Build and verify model packages
shell: bash
run: |
args=()
if [[ -n "$MODEL_ID" ]]; then args+=(--model "$MODEL_ID"); fi
if [[ "$PUBLISH" == true ]]; then args+=(--publish); fi
python tools/build_images.py --runtime "$RUNTIME_IMAGE" --repository "$IMAGE_NAMESPACE" \
--revision "$GITHUB_SHA" "${args[@]}"
17 changes: 12 additions & 5 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ on:
- '!docker/rv182x-rk3588/**'
tags: ['v*.*.*']
pull_request:
paths-ignore:
- 'src/cpp/**'
- '.github/workflows/cpp-model-images.yml'
workflow_dispatch:
inputs:
platform:
Expand Down Expand Up @@ -84,11 +87,6 @@ jobs:
- rk3576_ppocr

steps:
- name: Checkout Code
uses: actions/checkout@v6
with:
lfs: true

- name: Check if build is needed
id: check_changes
run: |
Expand All @@ -107,6 +105,15 @@ jobs:
echo "build=true" >> $GITHUB_OUTPUT
fi

- name: Checkout Code
if: steps.check_changes.outputs.build == 'true'
uses: actions/checkout@v6
with:
lfs: true
sparse-checkout: |
src/${{ matrix.platform }}
docker

- name: Prepare Image Name
if: steps.check_changes.outputs.build == 'true'
run: |
Expand Down
18 changes: 18 additions & 0 deletions src/cpp/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.git
.venv
__pycache__
*.pyc
**/__pycache__
**/*.pyc
**/*.so
build
media
.local
*.log
._*
**/._*
.DS_Store
**/.DS_Store
.env
.env.*
.model-package/
13 changes: 13 additions & 0 deletions src/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
__pycache__/
*.pyc
*.so
build/
.venv/
media/
.env
.env.*
.local/
.DS_Store
*.log
._*
.model-package/
35 changes: 35 additions & 0 deletions src/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
cmake_minimum_required(VERSION 3.18)
project(rk_cpp_api LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
find_package(pybind11 CONFIG REQUIRED)
find_package(PkgConfig REQUIRED)
find_package(Threads REQUIRED)
find_package(nlohmann_json REQUIRED)
pkg_check_modules(AV REQUIRED IMPORTED_TARGET libavformat libavcodec libavutil libavdevice)
set(ROCKCHIP_SDK /opt/rockchip CACHE PATH "Rockchip SDK prefix")
find_path(RGA_INCLUDE im2d.hpp HINTS ${ROCKCHIP_SDK}/rga/include REQUIRED)
find_path(RKNN_INCLUDE rknn_api.h HINTS ${ROCKCHIP_SDK}/rknn/include REQUIRED)
find_path(MPP_INCLUDE rk_mpi.h HINTS ${ROCKCHIP_SDK}/mpp/include PATH_SUFFIXES rockchip REQUIRED)
find_library(RGA_LIB rga HINTS ${ROCKCHIP_SDK}/rga/lib REQUIRED)
find_library(RKNN_LIB rknnrt HINTS ${ROCKCHIP_SDK}/rknn/lib REQUIRED)
find_library(MPP_LIB rockchip_mpp HINTS ${ROCKCHIP_SDK}/mpp/lib REQUIRED)
pybind11_add_module(_native
native/bindings.cpp
native/engine/geometry.cpp
native/engine/postprocess.cpp
native/engine/frame.cpp
native/engine/frame_queue.cpp
native/engine/dma_buffer.cpp
native/engine/decoder.cpp
native/engine/preprocess.cpp
native/engine/task_model.cpp
native/engine/task_decode.cpp
native/engine/task_dense.cpp
native/engine/task_json.cpp
native/engine/native_output.cpp
native/engine/detector.cpp
native/engine/detector_pool.cpp
native/engine/jpeg_preview.cpp)
target_include_directories(_native PRIVATE native ${RGA_INCLUDE} ${RKNN_INCLUDE} ${MPP_INCLUDE})
target_link_libraries(_native PRIVATE PkgConfig::AV Threads::Threads nlohmann_json::nlohmann_json ${RGA_LIB} ${RKNN_LIB} ${MPP_LIB})
install(TARGETS _native LIBRARY DESTINATION rkapi)
58 changes: 58 additions & 0 deletions src/cpp/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
FROM ubuntu:26.04 AS build
ENV DEBIAN_FRONTEND=noninteractive
ARG UBUNTU_MIRROR=http://ports.ubuntu.com/ubuntu-ports
RUN sed -i "s|http://ports.ubuntu.com/ubuntu-ports/|${UBUNTU_MIRROR}/|g" /etc/apt/sources.list.d/ubuntu.sources \
&& apt-get update && apt-get install -y --no-install-recommends \
build-essential cmake pkg-config git curl ca-certificates \
python3-dev python3-yaml pybind11-dev nlohmann-json3-dev libdrm-dev \
libavformat-dev libavcodec-dev libavutil-dev libavdevice-dev \
&& rm -rf /var/lib/apt/lists/*
COPY tools/setup-sdk.sh /tmp/setup-sdk.sh
RUN bash /tmp/setup-sdk.sh
WORKDIR /source
COPY CMakeLists.txt ./
COPY native ./native
RUN cmake -S . -B /build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/opt/rkapi \
&& cmake --build /build --parallel 4 && cmake --install /build

FROM ubuntu:26.04 AS runtime
ARG UBUNTU_MIRROR=http://ports.ubuntu.com/ubuntu-ports
ENV DEBIAN_FRONTEND=noninteractive \
PYTHONUNBUFFERED=1 \
OPENCV_IO_MAX_IMAGE_PIXELS=67108864 \
LD_LIBRARY_PATH=/opt/rockchip/rga/lib:/opt/rockchip/rknn/lib:/opt/rockchip/mpp/lib \
PATH=/opt/venv/bin:$PATH
RUN sed -i "s|http://ports.ubuntu.com/ubuntu-ports/|${UBUNTU_MIRROR}/|g" /etc/apt/sources.list.d/ubuntu.sources \
&& apt-get update && apt-get install -y --no-install-recommends \
python3-venv libavdevice62 libavformat62 libavcodec62 libavutil60 libdrm2 ca-certificates \
&& rm -rf /var/lib/apt/lists/* \
&& python3 -m venv /opt/venv
COPY requirements.txt /tmp/requirements.txt
ARG PIP_INDEX_URL=https://pypi.org/simple
RUN pip install --no-cache-dir --only-binary=:all: --index-url "$PIP_INDEX_URL" -r /tmp/requirements.txt
COPY --from=build /opt/rockchip/rga/lib/librga.so /opt/rockchip/rga/lib/
COPY --from=build /opt/rockchip/rknn/lib/librknnrt.so /opt/rockchip/rknn/lib/
COPY --from=build /opt/rockchip/mpp/lib/librockchip_mpp.so.0 /opt/rockchip/mpp/lib/
RUN ln -s librockchip_mpp.so.0 /opt/rockchip/mpp/lib/librockchip_mpp.so.1 \
&& ln -s librockchip_mpp.so.1 /opt/rockchip/mpp/lib/librockchip_mpp.so
COPY --from=build /opt/rkapi /opt/rkapi
COPY rkapi /opt/rkapi/rkapi
WORKDIR /opt/rkapi
ENV RK_PACKAGE=/opt/model/package.json
LABEL org.opencontainers.image.title="Rockchip CV API" \
org.opencontainers.image.description="FastAPI service backed by the Rockchip C++ RGA/DMA/RKNN engine"
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s \
CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=2)" || exit 1
CMD ["uvicorn", "rkapi.app:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1", "--limit-concurrency", "16"]

# The catalog selects weights and adapter; no Dockerfile edit per new model.
FROM build AS package
COPY rkapi /source/rkapi
COPY tools/models.py /source/tools/models.py
COPY models /source/models
ARG MODEL_ID
RUN python3 tools/models.py prepare --model "$MODEL_ID" --output /model-package

FROM runtime AS model
COPY --from=package /model-package/ /opt/model/
3 changes: 3 additions & 0 deletions src/cpp/Dockerfile.model
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ARG RUNTIME_IMAGE=rockchip-cv-runtime:local
FROM ${RUNTIME_IMAGE}
COPY package/ /opt/model/
Loading