Skip to content

Vehicle Detection

Revision History

NOVersionDescriptionDate
1Ver1.0Initial creation2026/06/29

The information in this document may be changed without prior notice for the purpose of improving the document. Please refer to our website for the latest version.

Reproduction in any form without the written permission of Nissho Technology Co., Ltd. is strictly prohibited.

1. Vehicle DetectionOverview

Vehicle detection is an object detection algorithm that detects vehicle positions based on deep learning. It can be widely used in various scenarios such as campus management and traffic analysis, and serves as the basic algorithm for illegal parking detection, congestion detection, traffic flow counting, and other algorithms.

The performance of this vehicle detection algorithm on the dataset is as follows:

Vehicle Detection AlgorithmmAP@0.5
CAR0.78029

The execution efficiency on the CSUN RV1126B board is as follows:

Algorithm TypeExecution Efficiency
car_detect73ms

2. Quick Start

2.1 Preparing the Development Environment

If you are reading this document for the first time, refer to Getting Started / Preparing and Updating the Development Compilation Environment, and deploy the compilation environment according to the related steps.

On the Ubuntu system on the PC side, run the run script to enter the Docker development environment. The steps are as follows:

Terminal window
cd ~/linuxshare/work/rv1126b/jp/embedded/images/develop_environment
./run.sh 2204

Figure 2-1 Starting the Docker Development Environment

Figure 2-1 Starting the Docker Development Environment

2.2 Downloading the Source Code

In the Docker development environment, create a management directory for storing the source code repository.

Terminal window
cd /opt/linuxshare/work/rv1126b/jp/AI/demo/ai-algorithm

Use the git tool to clone the remote repository into the management directory.

Terminal window
git clone https://github.com/csunltd/rv1126b-ai-toolkit.git

※ Even when downloading from the GitHub web page, download the entire repository. Do not download only the directory corresponding to this sample.

※ If the repository has already been downloaded, skip this step.

2.3 Model Deployment

To run the algorithm Demo, first download the vehicle detection algorithm model.

Download link:

https://dl.dragonwake.com/download/rv1126b/AI/demo/07_car_detect.zip

Then copy the downloaded vehicle detection algorithm model to the Release/ directory.

Figure 2-2 Files in the Release Directory

Figure 2-2 Files in the Release Directory

2.4 Building the Sample

Move to the directory containing the sample and execute the build. The specific commands are as follows:

Terminal window
cd rv1126b-ai-toolkit/Demos/algorithm-car/
Terminal window
./build.sh cpres

* Because the dependent libraries are located on the development board, keep /mnt mounted during cross-compilation.

Terminal window
sudo mount -t nfs -o vers=3,proto=tcp,mountproto=tcp,nolock,retrans=5,timeo=5 192.168.10.85:/ /mnt

* When the cpres parameter is specified for the build.sh script, all resources in the Release/ directory are copied to the development board.

Figure 2-3 Sample Build Result

Figure 2-3 Sample Build Result

2.5 Sample Execution and Results

Copy the compiled files to the board.

Terminal window
cp Release/\* /mnt/userdata/Demo/algorithm-car/

※ If you build with ./build.sh cpres, the files are copied automatically.

Enter the backend of the development board through serial debugging or SSH debugging, and move to the sample deployment directory as follows:

Terminal window
cd /userdata/Demo/algorithm-car/

The command for running the sample is as follows:

Terminal window
./test-car_detect car_detect.model test.jpg

Figure 2-4 Sample Execution Result

Figure 2-4 Sample Execution Result

In the Docker development environment, copy the test Result Image from the board.

Terminal window
cp /mnt/userdata/Demo/algorithm-car/result.jpg .

Figure 2-5 Command for Copying the Result Image

Figure 2-5 Command for Copying the Result Image

The recognition result is shown in the following figure.

Figure 2-6 Vehicle Detection Result Image

Figure 2-6 Vehicle Detection Result Image

For detailed API descriptions and API calls (the source code of this sample), refer to the following.

3. Vehicle DetectionAPI Description

3.1 Reference Method

To allow users to call the EASY EAI API library directly from a local project, the libraries and header files that need to be linked in this project are listed below. Users can add them directly.

ItemDescription
Header File Directoryeasyeai-api/algorithm/car_detect
Library File Directoryeasyeai-api/algorithm/car_detect
Library Link Parameter-lcar_detect

3.2 Vehicle DetectionInitialization Function

The prototype of the vehicle detection initialization function is as follows.

int car_detect_init(rknn_context \*ctx, const char \* path)

The details are as follows.

Function Name: car_detect_init()
Header Filecar_detect.h
Input Parameterctx: rknn_context handle
Input Parameterpath:algorithm model path
Return ValueReturn value on success: 0
Return value on failure: -1
NotesNone

3.3 Vehicle DetectionExecution Function

The prototype of the vehicle detection execution function car_detect_run is as follows.

int car_detect_run(rknn_context ctx, cv::Mat input_image, person_detect_result_group_t \*detect_result_group)

The details are as follows.

Function Name:car_detect_run()
Header Filecar_detect.h
Input Parameterctx: rknn_context handle
Input Parameterinput_image:image data input(cv::Mat is an OpenCV type)
Output Parameteroutput_dets:object detection box output
Return ValueReturn value on success: 0
Return value on failure: -1
NotesNone

3.4 Vehicle DetectionRelease Function

The prototype of the vehicle detection release function is as follows.

int car_detect_release(rknn_context ctx)
Function Name:car_detect_release ()
Header Filecar_detect.h
Input Parameterctx: rknn_context handle
Return ValueReturn value on success: 0
Return value on failure: -1
NotesNone

4. Vehicle Detection Algorithm Sample

The sample directory is Demos/algorithm-car/test-car_detect.cpp, and the operation flow is as follows.

Figure 4-1 Vehicle Detection Algorithm Processing flow

Figure 4-1 Vehicle Detection Algorithm Processing flow

The reference sample is as follows.

#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <sys/time.h>
#include"car_detect.h"
using namespace cv;
using namespace std;
static Scalar colorArray[10]={
Scalar(255, 0, 0, 255),
Scalar(0, 255, 0, 255),
Scalar(0,0,139,255),
Scalar(0,100,0,255),
Scalar(139,139,0,255),
Scalar(209,206,0,255),
Scalar(0,127,255,255),
Scalar(139,61,72,255),
Scalar(0,255,0,255),
Scalar(255,0,0,255),
};
int plot_one_box(Mat src, int x1, int x2, int y1, int y2, char *label, char colour)
{
int tl = round(0.002 * (src.rows + src.cols) / 2) + 1;
rectangle(src, cv::Point(x1, y1), cv::Point(x2, y2), colorArray[(unsigned char)colour], 3);
int tf = max(tl -1, 1);
int base_line = 0;
cv::Size t_size = getTextSize(label, FONT_HERSHEY_SIMPLEX, (float)tl/3, tf, &base_line);
int x3 = x1 + t_size.width;
int y3 = y1 - t_size.height - 3;
rectangle(src, cv::Point(x1, y1), cv::Point(x3, y3), colorArray[(unsigned char)colour], -1);
putText(src, label, cv::Point(x1, y1 - 2), FONT_HERSHEY_SIMPLEX, (float)tl/3, cv::Scalar(255, 255, 255, 255), tf, 8);
return 0;
}
int main(int argc, char **argv)
{
if (argc != 3)
{
printf("%s <model_path> <image_path>\n", argv[0]);
return -1;
}
const char *model_path = argv[1];
const char *image_path = argv[2];
/* パラメータ初期化 */
detect_result_group_t detect_result_group;
/* アルゴリズムモデル初期化 */
rknn_context ctx;
car_detect_init(&ctx, model_path);
/* アルゴリズム実行 */
cv::Mat src;
src = cv::imread(image_path, 1);
struct timeval start;
struct timeval end;
float time_use=0;
gettimeofday(&start,NULL);
car_detect_run(ctx, src, &detect_result_group);
gettimeofday(&end,NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//マイクロ秒
printf("time_use is %f\n",time_use/1000);
/* アルゴリズム結果を画像上に描画して保存します */
// Draw Objects
char text[256];
for (int i = 0; i < detect_result_group.count; i++)
{
detect_result_t* det_result = &(detect_result_group.results[i]);
if( det_result->prop < 0.4)
{
continue;
}
sprintf(text, "%s %.1f%%", det_result->name, det_result->prop * 100);
printf("%s @ (%d %d %d %d) %f\n", det_result->name, det_result->box.left, det_result->box.top,
det_result->box.right, det_result->box.bottom, det_result->prop);
int x1 = det_result->box.left;
int y1 = det_result->box.top;
int x2 = det_result->box.right;
int y2 = det_result->box.bottom;
/*
rectangle(src, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(255, 0, 0, 255), 3);
putText(src, text, cv::Point(x1, y1 + 12), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
*/
plot_one_box(src, x1, x2, y1, y2, text, i%10);
}
cv::imwrite("result.jpg", src);
/* アルゴリズムモデルのリソースを解放します */
car_detect_release(ctx);
return 0;
}