mmdeploy/demo/python/pipeline.py
Li Zhang bf887cc8bc
[Enhancement] Refactor SDK pipeline (#938)
* unify C API naming

* fix demo and move apis/c/* -> apis/c/mmdeploy/*

* fix lint

* WIP refactor pipeline

* backward compatibility

* working pipeline demo

* add text det-recog demo

* add det-pose demo

* fix build

* fix demo

* add environment interface

* add environment to pass scheduler & model info at runtime

* update demos

* add pipeline API for Python

* fix `FromPyObject`

* fix for opencv-4.2

* environment -> context, improve pipeline

* python model interface

* fix cmake

* fix python & cmake

* context & C++ pipeline API

* minor fix

* improve API

* fix shared libs

* refresh C/Python API

* propagate context

* fix  python demo

* fix

* add namespace

* fix namespace

* fix mis-changed strings

* fix

* fix python api

* rename

* clean-up

* fix pose detector

* clean-up

* clean-up

* clean-up

* fix python API build

* fix CI

* fix lint

* fix lint

* fix lint & demo

* install pipeline.hpp

* fix MSVC shared library build

* fix sample

* fix MSVC monolithic build

* minor fix
2022-09-26 16:11:14 +08:00

68 lines
1.9 KiB
Python

# Copyright (c) OpenMMLab. All rights reserved.
import argparse
import json
import cv2
from mmdeploy_python import Context, Device, Model, Pipeline
def parse_args():
parser = argparse.ArgumentParser(
description='Demo of MMDeploy SDK pipeline API')
parser.add_argument('device', help='name of device, cuda or cpu')
parser.add_argument('det_model_path', help='path of detection model')
parser.add_argument('cls_model_path', help='path of classification model')
parser.add_argument('image_path', help='path to test image')
args = parser.parse_args()
return args
def main():
args = parse_args()
det_model = Model(args.det_model_path)
reg_model = Model(args.cls_model_path)
config = dict(
type='Pipeline',
input='img',
tasks=[
dict(
type='Inference',
input='img',
output='dets',
params=dict(model=det_model)),
dict(
type='Pipeline',
# flatten dets ([[a]] -> [a]) and broadcast img
input=['boxes=*dets', 'imgs=+img'],
tasks=[
dict(
type='Task',
module='CropBox',
input=['imgs', 'boxes'],
output='patches'),
dict(
type='Inference',
input='patches',
output='labels',
params=dict(model=reg_model))
],
# unflatten labels ([a] -> [[a]])
output='*labels')
],
output=['dets', 'labels'])
device = Device(args.device)
pipeline = Pipeline(config, Context(device))
img = cv2.imread(args.image_path)
output = pipeline(dict(ori_img=img))
print(json.dumps(output, indent=4))
if __name__ == '__main__':
main()