AI vision processing code structure

AI vision processing code structureIntroductionImporting modulesCustom detection classExecute the current routineEnd the current routineMain Process

 

Introduction

In this section, we will briefly introduce the general prerequisite knowledge and code structure of K230's subsequent AI-related routines

image-20250206101556697

Let's take the simplest face recognition Face_detection as an example

The example code is located in [Source code/07.Face/01.face_detection]

Note: The following code is to demonstrate the code structure of AIDemo, not all of it is from face detection, because face detection does not require the complete code structure. Directly copying the following code will not run successfully. The complete executable code has been placed in the [Source Code] directory

Importing modules

Below is the import list of FaceDection

Custom detection class

We can customize a detection class based on the AIBase class, which generally contains the following methods:

  1. init Initialization function, used to configure basic information of AI tasks

  2. config_preprocess configures image processing information and some preprocessing operations

  3. PreProcess Image preprocessing

  4. run is the core step for AI reasoning. For AI tasks without preprocessing, this function needs to be rewritten in the subclass.

  1. PostProcess: post-process the results of AI recognition

  2. DrawResult Draws the result on the screen

Not all AIBase methods need to be rewritten. We can divide tasks into four categories: single-model tasks, multi-model tasks, custom preprocessing tasks, and no preprocessing tasks. Different tasks require different code implementations, as shown in the following figure:

image-20250206102802517

Execute the current routine

We define the exce_demo(pl) method to execute the current AI routine

The parameter pl passed in is a Pipeline instance

This design allows this AI routine to be called externally. Pipeline manages the global camera and display output of K230, so there can only be one Pipeline instance in a program run.

The code flow of this part is basically the same. Let's take the face detection routine as an example.

The execution flow chart is as follows:

异常
Exception
开始
Start
初始化显示模式和分辨率参数
Init display mode & resolution
设置模型路径和其它参数
Set model path & other params
加载 anchors 数据
Load anchors data
try 代码块
try block
初始化人脸检测应用
Init face detection app
配置预处理
Configure preprocessing
主循环
Main loop
获取当前帧
Get current frame
执行推理
Run inference
绘制结果
Draw results
显示结果
Display result
垃圾回收
Garbage collection
短暂休眠
Sleep briefly
打印退出信息
Print exit message
反初始化
Deinitialize
结束
End

In the definition of Pipeline, the Camera outputs two images by default: one in the format of YUV420SP (Sensor.YUV420SP), which is directly provided to the Display; the other in the format of RGBP888 (Sensor.RGBP888), which is used for AI processing. AI is mainly responsible for the pre-processing, reasoning and post-processing of the task. After the processing is completed, the result will be drawn on the OSD image instance and sent to the Display for overlay display.

End the current routine

The end routine part is very simple, just release the instance we created. Take face recognition as an example

Main Process

When we want to call a routine in other programs, we need to import the routine, create an object of the routine and call the exce_demo() method, passing in the Pipeline instance.

If we want to run this routine directly in K230, we need to create a Pipeline instance in the main function

The code is as follows