> For the complete documentation index, see [llms.txt](https://1684.gitbook.io/bmnnsdk2-1684-2-0-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://1684.gitbook.io/bmnnsdk2-1684-2-0-1/python-bian-cheng-xiang-jie.md).

# Python编程详解

这个章节将会选取BMNNSDK2中的SSD检测算法作为示例(examples/SSD\_object/py\_ffmpeg\_bmcv\_sail), 来介绍python接口编程。在进行python编程介绍前有必要先阅读[**Sophon\_Inference\_zh.pdf**](https://sophon-edge.gitbook.io/sophon-inference/)中2.1节对sail封装的几个类的介绍。

本章主要介绍以下三点内容：

* 加载模型
* 预处理
* 推理

## 1. 加载模型

```
import sophon.sail as sail
engine = sail.Engine(0)
engine.load(bmodel_path)
```

## 2. 预处理

```
class PreProcessor:
  def __init__(self, bmcv, scale):
    self.bmcv  = bmcv
    self.ab    = [x * scale for x in [1, -123, 1, -117, 1, -104]]

  def process(self, input, output):
    tmp = self.bmcv.vpp_resize(input, 300, 300)
    self.bmcv.convert_to(tmp, output, ((self.ab[0], self.ab[1]), (self.ab[2], self.ab[3]), (self.ab[4], self.ab[5])))

bmcv = sail.Bmcv(handle)   #图形处理加速模块
scale = engine.get_input_scale(graph_name, input_name)
pre_processor = PreProcessor(bmcv, scale)  #预处理初始化

img0 = decoder.read(handle)            #解码视频输出image
img1 = bmcv.tensor_to_bm_image(input)  #将推理的输入地址挂载到image

pre_processor.process(img0, img1)      #预处理
```

## 3. 推理

```
graph_name = engine.get_graph_names()[0]
engine.set_io_mode(graph_name, sail.IOMode.SYSO)

input_name   = engine.get_input_names(graph_name)[0]
output_name  = engine.get_output_names(graph_name)[0]

input_shape  = [1, 3, 300, 300]
output_shape = [1, 1, 200, 7]

handle = engine.get_handle()

input_dtype  = engine.get_input_dtype(graph_name, input_name)
output_dtype = engine.get_output_dtype(graph_name, output_name)

input  = sail.Tensor(handle, input_shape,  input_dtype,  False, True)
output = sail.Tensor(handle, output_shape, output_dtype, True,  True)

input_tensors  = { input_name:  input  }
output_tensors = { output_name: output }
...
#此处省略 解码，预处理 代码
...
engine.process(graph_name, input_tensors, output_tensors) #推理
out = output.asnumpy()

dets = post_processor.process(out, img0.width(), img0.height())  #后处理
...
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://1684.gitbook.io/bmnnsdk2-1684-2-0-1/python-bian-cheng-xiang-jie.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
