- addedd NCNN model support to rtsp_ai_player

- added printing of inference FPS
- simple AI test bench which can be used to compare models
This commit is contained in:
Tuomas Järvinen
2024-10-02 19:15:49 +02:00
parent ef137fbc4b
commit d4779b1bb0
12 changed files with 555 additions and 763 deletions
-74
View File
@@ -1,74 +0,0 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
CMakeLists.txt.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe
@@ -1,387 +0,0 @@
/**
* @file yolo_detector.cpp
* @brief Yolo Object Detection Sample
* @author OpenCV team
*/
//![includes]
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <fstream>
#include <sstream>
#include "iostream"
#include "common.hpp"
#include <opencv2/highgui.hpp>
//![includes]
using namespace cv;
using namespace cv::dnn;
void getClasses(std::string classesFile);
void drawPrediction(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);
void yoloPostProcessing(
std::vector<Mat>& outs,
std::vector<int>& keep_classIds,
std::vector<float>& keep_confidences,
std::vector<Rect2d>& keep_boxes,
float conf_threshold,
float iou_threshold,
const std::string& model_name,
const int nc
);
std::vector<std::string> classes;
std::string keys =
"{ help h | | Print help message. }"
"{ device | 0 | camera device number. }"
"{ model | onnx/models/yolox_s_inf_decoder.onnx | Default model. }"
"{ yolo | yolox | yolo model version. }"
"{ input i | | Path to input image or video file. Skip this argument to capture frames from a camera. }"
"{ classes | | Optional path to a text file with names of classes to label detected objects. }"
"{ nc | 80 | Number of classes. Default is 80 (coming from COCO dataset). }"
"{ thr | .5 | Confidence threshold. }"
"{ nms | .4 | Non-maximum suppression threshold. }"
"{ mean | 0.0 | Normalization constant. }"
"{ scale | 1.0 | Preprocess input image by multiplying on a scale factor. }"
"{ width | 640 | Preprocess input image by resizing to a specific width. }"
"{ height | 640 | Preprocess input image by resizing to a specific height. }"
"{ rgb | 1 | Indicate that model works with RGB input images instead BGR ones. }"
"{ padvalue | 114.0 | padding value. }"
"{ paddingmode | 2 | Choose one of computation backends: "
"0: resize to required input size without extra processing, "
"1: Image will be cropped after resize, "
"2: Resize image to the desired size while preserving the aspect ratio of original image }"
"{ backend | 0 | Choose one of computation backends: "
"0: automatically (by default), "
"1: Halide language (http://halide-lang.org/), "
"2: Intel's Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), "
"3: OpenCV implementation, "
"4: VKCOM, "
"5: CUDA }"
"{ target | 0 | Choose one of target computation devices: "
"0: CPU target (by default), "
"1: OpenCL, "
"2: OpenCL fp16 (half-float precision), "
"3: VPU, "
"4: Vulkan, "
"6: CUDA, "
"7: CUDA fp16 (half-float preprocess) }"
"{ async | 0 | Number of asynchronous forwards at the same time. "
"Choose 0 for synchronous mode }";
void getClasses(std::string classesFile)
{
std::ifstream ifs(classesFile.c_str());
if (!ifs.is_open())
CV_Error(Error::StsError, "File " + classesFile + " not found");
std::string line;
while (std::getline(ifs, line))
classes.push_back(line);
}
void drawPrediction(int classId, float conf, int left, int top, int right, int bottom, Mat& frame)
{
rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 255, 0));
std::string label = format("%.2f", conf);
if (!classes.empty())
{
CV_Assert(classId < (int)classes.size());
label = classes[classId] + ": " + label;
}
int baseLine;
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
top = max(top, labelSize.height);
rectangle(frame, Point(left, top - labelSize.height),
Point(left + labelSize.width, top + baseLine), Scalar::all(255), FILLED);
putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.5, Scalar());
}
void yoloPostProcessing(
std::vector<Mat>& outs,
std::vector<int>& keep_classIds,
std::vector<float>& keep_confidences,
std::vector<Rect2d>& keep_boxes,
float conf_threshold,
float iou_threshold,
const std::string& model_name,
const int nc=80)
{
// Retrieve
std::vector<int> classIds;
std::vector<float> confidences;
std::vector<Rect2d> boxes;
if (model_name == "yolov8" || model_name == "yolov10" ||
model_name == "yolov9")
{
cv::transposeND(outs[0], {0, 2, 1}, outs[0]);
}
if (model_name == "yolonas")
{
// outs contains 2 elemets of shape [1, 8400, 80] and [1, 8400, 4]. Concat them to get [1, 8400, 84]
Mat concat_out;
// squeeze the first dimension
outs[0] = outs[0].reshape(1, outs[0].size[1]);
outs[1] = outs[1].reshape(1, outs[1].size[1]);
cv::hconcat(outs[1], outs[0], concat_out);
outs[0] = concat_out;
// remove the second element
outs.pop_back();
// unsqueeze the first dimension
outs[0] = outs[0].reshape(0, std::vector<int>{1, 8400, nc + 4});
}
// assert if last dim is 85 or 84
CV_CheckEQ(outs[0].dims, 3, "Invalid output shape. The shape should be [1, #anchors, 85 or 84]");
CV_CheckEQ((outs[0].size[2] == nc + 5 || outs[0].size[2] == 80 + 4), true, "Invalid output shape: ");
for (auto preds : outs)
{
preds = preds.reshape(1, preds.size[1]); // [1, 8400, 85] -> [8400, 85]
for (int i = 0; i < preds.rows; ++i)
{
// filter out non object
float obj_conf = (model_name == "yolov8" || model_name == "yolonas" ||
model_name == "yolov9" || model_name == "yolov10") ? 1.0f : preds.at<float>(i, 4) ;
if (obj_conf < conf_threshold)
continue;
Mat scores = preds.row(i).colRange((model_name == "yolov8" || model_name == "yolonas" || model_name == "yolov9" || model_name == "yolov10") ? 4 : 5, preds.cols);
double conf;
Point maxLoc;
minMaxLoc(scores, 0, &conf, 0, &maxLoc);
conf = (model_name == "yolov8" || model_name == "yolonas" || model_name == "yolov9" || model_name == "yolov10") ? conf : conf * obj_conf;
if (conf < conf_threshold)
continue;
// get bbox coords
float* det = preds.ptr<float>(i);
double cx = det[0];
double cy = det[1];
double w = det[2];
double h = det[3];
// [x1, y1, x2, y2]
if (model_name == "yolonas" || model_name == "yolov10"){
boxes.push_back(Rect2d(cx, cy, w, h));
} else {
boxes.push_back(Rect2d(cx - 0.5 * w, cy - 0.5 * h,
cx + 0.5 * w, cy + 0.5 * h));
}
classIds.push_back(maxLoc.x);
confidences.push_back(static_cast<float>(conf));
}
}
// NMS
std::vector<int> keep_idx;
NMSBoxes(boxes, confidences, conf_threshold, iou_threshold, keep_idx);
for (auto i : keep_idx)
{
keep_classIds.push_back(classIds[i]);
keep_confidences.push_back(confidences[i]);
keep_boxes.push_back(boxes[i]);
}
}
/**
* @function main
* @brief Main function
*/
int main(int argc, char** argv)
{
CommandLineParser parser(argc, argv, keys);
parser.about("Use this script to run object detection deep learning networks using OpenCV.");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
CV_Assert(parser.has("model"));
CV_Assert(parser.has("yolo"));
// if model is default, use findFile to get the full path otherwise use the given path
std::string weightPath = "yolov8n.onnx";
std::string yolo_model = "yolov8";
int nc = parser.get<int>("nc");
float confThreshold = parser.get<float>("thr");
float nmsThreshold = parser.get<float>("nms");
//![preprocess_params]
float paddingValue = parser.get<float>("padvalue");
bool swapRB = parser.get<bool>("rgb");
int inpWidth = parser.get<int>("width");
int inpHeight = parser.get<int>("height");
Scalar scale = parser.get<float>("scale");
Scalar mean = parser.get<Scalar>("mean");
ImagePaddingMode paddingMode = static_cast<ImagePaddingMode>(parser.get<int>("paddingmode"));
//![preprocess_params]
// check if yolo model is valid
if (yolo_model != "yolov5" && yolo_model != "yolov6"
&& yolo_model != "yolov7" && yolo_model != "yolov8"
&& yolo_model != "yolov10" && yolo_model !="yolov9"
&& yolo_model != "yolox" && yolo_model != "yolonas")
CV_Error(Error::StsError, "Invalid yolo model: " + yolo_model);
// get classes
if (parser.has("classes"))
{
getClasses(findFile(parser.get<String>("classes")));
}
// load model
//![read_net]
Net net = readNet(weightPath);
int backend = parser.get<int>("backend");
net.setPreferableBackend(backend);
net.setPreferableTarget(parser.get<int>("target"));
//![read_net]
VideoCapture cap;
Mat img;
bool isImage = false;
bool isCamera = false;
isImage = true;
img = imread("bus.png");
/*
// Check if input is given
if (parser.has("input"))
{
String input = parser.get<String>("input");
// Check if the input is an image
if (input.find(".jpg") != String::npos || input.find(".png") != String::npos)
{
img = imread(findFile(input));
if (img.empty())
{
CV_Error(Error::StsError, "Cannot read image file: " + input);
}
isImage = true;
}
else
{
cap.open(input);
if (!cap.isOpened())
{
CV_Error(Error::StsError, "Cannot open video " + input);
}
isCamera = true;
}
}
else
{
int cameraIndex = parser.get<int>("device");
cap.open(cameraIndex);
if (!cap.isOpened())
{
CV_Error(Error::StsError, cv::format("Cannot open camera #%d", cameraIndex));
}
isCamera = true;
}
*/
// image pre-processing
//![preprocess_call]
Size size(inpWidth, inpHeight);
Image2BlobParams imgParams(
scale,
size,
mean,
swapRB,
CV_32F,
DNN_LAYOUT_NCHW,
paddingMode,
paddingValue);
// rescale boxes back to original image
Image2BlobParams paramNet;
paramNet.scalefactor = scale;
paramNet.size = size;
paramNet.mean = mean;
paramNet.swapRB = swapRB;
paramNet.paddingmode = paddingMode;
//![preprocess_call]
//![forward_buffers]
std::vector<Mat> outs;
std::vector<int> keep_classIds;
std::vector<float> keep_confidences;
std::vector<Rect2d> keep_boxes;
std::vector<Rect> boxes;
//![forward_buffers]
Mat inp;
while (waitKey(1) < 0)
{
if (isCamera)
cap >> img;
if (img.empty())
{
std::cout << "Empty frame" << std::endl;
waitKey();
break;
}
//![preprocess_call_func]
inp = blobFromImageWithParams(img, imgParams);
//![preprocess_call_func]
//![forward]
net.setInput(inp);
net.forward(outs, net.getUnconnectedOutLayersNames());
//![forward]
//![postprocess]
yoloPostProcessing(
outs, keep_classIds, keep_confidences, keep_boxes,
confThreshold, nmsThreshold,
yolo_model,
nc);
//![postprocess]
// covert Rect2d to Rect
//![draw_boxes]
for (auto box : keep_boxes)
{
boxes.push_back(Rect(cvFloor(box.x), cvFloor(box.y), cvFloor(box.width - box.x), cvFloor(box.height - box.y)));
}
paramNet.blobRectsToImageRects(boxes, boxes, img.size());
for (size_t idx = 0; idx < boxes.size(); ++idx)
{
Rect box = boxes[idx];
drawPrediction(keep_classIds[idx], keep_confidences[idx], box.x, box.y,
box.width + box.x, box.height + box.y, img);
}
const std::string kWinName = "Yolo Object Detector";
namedWindow(kWinName, WINDOW_NORMAL);
imshow(kWinName, img);
//![draw_boxes]
outs.clear();
keep_classIds.clear();
keep_confidences.clear();
keep_boxes.clear();
boxes.clear();
if (isImage)
{
waitKey();
break;
}
}
}
-262
View File
@@ -1,262 +0,0 @@
#include <QDebug>
#include <QElapsedTimer>
#include <opencv2/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#define CONFIDENCE_THRESHOLD 0.2
using namespace std;
using namespace cv;
// function to create vector of class names
std::vector<std::string> createClaseNames() {
std::vector<std::string> classNames;
classNames.push_back("background");
classNames.push_back("aeroplane");
classNames.push_back("bicycle");
classNames.push_back("bird");
classNames.push_back("boat");
classNames.push_back("bottle");
classNames.push_back("bus");
classNames.push_back("car");
classNames.push_back("cat");
classNames.push_back("chair");
classNames.push_back("cow");
classNames.push_back("diningtable");
classNames.push_back("dog");
classNames.push_back("horse");
classNames.push_back("motorbike");
classNames.push_back("person");
classNames.push_back("pottedplant");
classNames.push_back("sheep");
return classNames;
};
int main(int argc, char *argv[])
{
if (argc < 3) {
qDebug() << "Give ONNX model as first argument and image as second one.";
return 1;
}
std::vector<String> classNames = createClaseNames();
std::string model_filename = argv[1];
std::string image_filename = argv[2];
// Load the ONNX model
cv::dnn::Net net = cv::dnn::readNetFromONNX(model_filename);
// Set backend to OpenCL
net.setPreferableBackend(cv::dnn::DNN_BACKEND_DEFAULT);
//net.setPreferableTarget(cv::dnn::DNN_TARGET_OPENCL);
// Load an image and preprocess it
cv::Mat image = cv::imread(image_filename);
cv::Mat blob = cv::dnn::blobFromImage(image, 1/255.0, cv::Size(640, 640), cv::Scalar(), true, false);
cv::imwrite("tmpInput.png", image);
// Set the input for the network
net.setInput(blob);
std::vector<cv::Mat> outputs;
net.forward(outputs);
cv::Mat output = outputs[0];
std::vector<cv::String> layerNames = net.getLayerNames();
std::vector<int> outLayerIndices = net.getUnconnectedOutLayers();
for (int index : outLayerIndices) {
std::cout << layerNames[index - 1] << std::endl;
}
cv::Mat detections = net.forward("output0");
// print some information about detections
std::cout << "dims: " << outputs[0].dims << std::endl;
std::cout << "size: " << outputs[0].size << std::endl;
int num_detections0 = outputs.size();
int num_classes0 = outputs[0].size[1];
int rows = outputs[0].size[1];
qDebug() << "num_detections0:" << num_detections0 << "num_classes0:" << num_classes0 << "rows:" << rows;
int num_detections = detections.size[2]; // 8400
int num_attributes = detections.size[1]; // 14
qDebug() << "num_detections:" << num_detections << "num_attributes:" << num_attributes;
detections = detections.reshape(1, num_detections);
for (int i = 0; i < num_detections; i++) {
Mat row = detections.row(i);
float box_confidence = row.at<float>(4);
float cls_confidence = row.at<float>(5);
if (box_confidence > 0.5 && cls_confidence > 0.5) {
std::cout << "box_confidence " << box_confidence << " cls_confidence: " << cls_confidence << std::endl;
}
}
/*
// Output tensor processing
// Assuming output tensor has shape [1, 14, 8400]
int num_detections = detections.size[2]; // 8400
int num_attributes = detections.size[1]; // 14
qDebug() << "num_detections:" << num_detections << "num_attributes:" << num_attributes;
// Extract and print confidence for each detection
const float* data = (float*)output.data;
for (int i = 0; i < num_detections; i++) {
// Confidence value is at index 4 (based on YOLO-like model output format)
float confidence = data[i * num_attributes + 3];
// Print confidence value
if (confidence > 0.5f) {
std::cout << "Detection " << i << " confidence: " << confidence << std::endl;
}
}
*/
/*
// Assuming outputs is a vector of cv::Mat containing the model's output
for (int i = 0; i < outputs[0].size[0]; ++i) {
for (int j = 0; j < outputs[0].size[1]; ++j) {
float confidence = outputs[0].at<float>(i, j);
// Print or use the confidence value
std::cout << "Confidence: " << confidence << std::endl;
}
}
*/
/*
// Output processing variables
std::vector<int> class_ids;
std::vector<float> confidences;
std::vector<cv::Rect> boxes;
// Analyze each output
for (const auto& output : outputs) {
// Each row is a detection: [center_x, center_y, width, height, conf, class1_score, class2_score, ...]
const auto* data = (float*)output.data;
for (int i = 0; i < output.rows; i++, data += output.cols) {
float confidence = data[4]; // Objectness confidence
if (confidence >= 0.5) { // Apply a confidence threshold
cv::Mat scores = output.row(i).colRange(5, output.cols);
cv::Point class_id_point;
double max_class_score;
cv::minMaxLoc(scores, 0, &max_class_score, 0, &class_id_point);
int class_id = class_id_point.x;
if (max_class_score >= 0.5) { // Apply a class score threshold
// Get bounding box
int center_x = (int)(data[0] * image.cols);
int center_y = (int)(data[1] * image.rows);
int width = (int)(data[2] * image.cols);
int height = (int)(data[3] * image.rows);
int left = center_x - width / 2;
int top = center_y - height / 2;
// Store the results
class_ids.push_back(class_id);
confidences.push_back(confidence);
boxes.push_back(cv::Rect(left, top, width, height));
}
}
}
}
*/
/*
for (int i = 0; i < num_detections; ++i) {
for (int a = 0; a < 10; a++) {
qDebug() << "confidence:" << confidence << "class_id" << class_id;
}
//float confidence = outputs[0].at<float>(i, 4);
//int class_id = outputs[0].at<float>(i, 5);
qDebug() << "confidence:" << confidence << "class_id" << class_id;
}
*/
/*
std::vector<int> class_ids;
std::vector<float> confidences;
std::vector<cv::Rect> boxes;
// Output tensor processing
for (size_t i = 0; i < outputs.size(); i++) {
float* data = (float*)outputs[i].data;
for (int j = 0; j < outputs[i].rows; ++j) {
float confidence = data[4]; // Objectness confidence
qDebug() <<" Confidence: " << confidence;
if (confidence >= CONFIDENCE_THRESHOLD) {
// Get class with the highest score
cv::Mat scores = outputs[i].row(j).colRange(5, num_classes + 5);
cv::Point class_id_point;
double max_class_score;
cv::minMaxLoc(scores, 0, &max_class_score, 0, &class_id_point);
qDebug() << "Class ID: " << class_id_point.x << " Confidence: " << confidence;
if (max_class_score > CONFIDENCE_THRESHOLD) {
int center_x = (int)(data[0] * image.cols);
int center_y = (int)(data[1] * image.rows);
int width = (int)(data[2] * image.cols);
int height = (int)(data[3] * image.rows);
int left = center_x - width / 2;
int top = center_y - height / 2;
// Store the results
class_ids.push_back(class_id_point.x);
confidences.push_back((float)max_class_score);
boxes.push_back(cv::Rect(left, top, width, height));
}
}
data += num_classes + 5; // Move to the next detection
}
}
*/
/*
// Process the output
for (int i = 0; i < num_detections; ++i) {
float confidence = outputs[0].at<float>(i, 4);
int class_id = outputs[0].at<float>(i, 5);
// ... extract bounding box coordinates
float x_center = outputs[0].at<float>(i, 0);
float y_center = outputs[0].at<float>(i, 1);
float width = outputs[0].at<float>(i, 2);
float height = outputs[0].at<float>(i, 3);
// Calculate bounding box corners
int x = (int)(x_center - width / 2.0);
int y = (int)(y_center - height / 2.0);
int width_int = (int)width;
int height_int = (int)height;
// Print or use the detected information
qDebug() << "Class ID: " << class_id << " Confidence: " << confidence << " Bounding Box: (" << x << ", " << y << ", " << width_int << ", " << height_int << ")";
}
*/
/*
// Perform forward pass
for (int i = 0; i < 10; i++) {
std::vector<cv::Mat> outputs;
QElapsedTimer timer;
timer.start();
net.forward(outputs);
qDebug() << "Inference completed in" << timer.elapsed() << "ms.";
}
*/
// Process the output (YOLO-specific post-processing like NMS is needed)
// Outputs contain class scores, bounding boxes, etc.
return 0;
}
@@ -1,16 +0,0 @@
QT = core
QT -= gui
CONFIG += c++17 cmdline concurrent console
# link_pkgconfig
SOURCES += \
main-opencv-example.cpp
#main.cpp
HEADERS += \
common.hpp
INCLUDEPATH += /opt/opencv-4.10.0/include/opencv4/
LIBS += /opt/opencv-4.10.0/lib/libopencv_world.so
QMAKE_LFLAGS += -Wl,-rpath,/opt/opencv-4.10.0/lib
+13
View File
@@ -1,5 +1,6 @@
#include <QDebug> #include <QDebug>
#include <opencv2/highgui.hpp> #include <opencv2/highgui.hpp>
#include "aiengine.h" #include "aiengine.h"
#include "aiengineinference.h" #include "aiengineinference.h"
#include "aiengineimagesaver.h" #include "aiengineimagesaver.h"
@@ -8,10 +9,14 @@
#include "src-opi5/aiengineinferenceopi5.h" #include "src-opi5/aiengineinferenceopi5.h"
#elif defined(OPENCV_BUILD) #elif defined(OPENCV_BUILD)
#include "src-opencv-onnx/aiengineinferenceopencvonnx.h" #include "src-opencv-onnx/aiengineinferenceopencvonnx.h"
#elif defined(NCNN_BUILD)
#include "src-ncnn/aiengineinferencencnn.h"
#else #else
#include "src-onnx-runtime/aiengineinferenceonnxruntime.h" #include "src-onnx-runtime/aiengineinferenceonnxruntime.h"
#endif #endif
AiEngine::AiEngine(QString modelPath, QObject *parent) AiEngine::AiEngine(QString modelPath, QObject *parent)
: QObject{parent} : QObject{parent}
{ {
@@ -27,6 +32,8 @@ AiEngine::AiEngine(QString modelPath, QObject *parent)
mInference3->initialize(2); mInference3->initialize(2);
#elif defined(OPENCV_BUILD) #elif defined(OPENCV_BUILD)
mInference = new AiEngineInferenceOpencvOnnx(modelPath); mInference = new AiEngineInferenceOpencvOnnx(modelPath);
#elif defined(NCNN_BUILD)
mInference = new AiEngineInferencevNcnn(modelPath);
#else #else
mInference = new AiEngineInferencevOnnxRuntime(modelPath); mInference = new AiEngineInferencevOnnxRuntime(modelPath);
#endif #endif
@@ -76,6 +83,7 @@ void AiEngine::inferenceResultsReceivedSlot(AiEngineInferenceResult result)
{ {
mFrameCounter++; mFrameCounter++;
qDebug() << "FPS = " << (mFrameCounter / (mElapsedTimer.elapsed()/1000.0f)); qDebug() << "FPS = " << (mFrameCounter / (mElapsedTimer.elapsed()/1000.0f));
//qDebug() << "DEBUG. inference frame counter:" << mFrameCounter;
//qDebug() << "AiEngine got inference results in thread: " << QThread::currentThreadId(); //qDebug() << "AiEngine got inference results in thread: " << QThread::currentThreadId();
if (mGimbalClient != nullptr) { if (mGimbalClient != nullptr) {
@@ -97,19 +105,24 @@ void AiEngine::frameReceivedSlot(cv::Mat frame)
{ {
//qDebug() << "AiEngine got frame from RTSP listener in thread: " << QThread::currentThreadId(); //qDebug() << "AiEngine got frame from RTSP listener in thread: " << QThread::currentThreadId();
//cv::imshow("Received Frame", frame); //cv::imshow("Received Frame", frame);
static int framecounter = 0;
//qDebug() << "DEBUG. RTSP frame counter:" << framecounter;
if (mInference->isActive() == false) { if (mInference->isActive() == false) {
//qDebug() << "AiEngine. Inference thread is free. Sending frame to it."; //qDebug() << "AiEngine. Inference thread is free. Sending frame to it.";
emit inferenceFrame(frame); emit inferenceFrame(frame);
framecounter++;
} }
#ifdef OPI5_BUILD #ifdef OPI5_BUILD
else if (mInference2->isActive() == false) { else if (mInference2->isActive() == false) {
//qDebug() << "AiEngine. Inference thread is free. Sending frame to it."; //qDebug() << "AiEngine. Inference thread is free. Sending frame to it.";
emit inferenceFrame2(frame); emit inferenceFrame2(frame);
framecounter++;
} }
else if (mInference3->isActive() == false) { else if (mInference3->isActive() == false) {
//qDebug() << "AiEngine. Inference thread is free. Sending frame to it."; //qDebug() << "AiEngine. Inference thread is free. Sending frame to it.";
emit inferenceFrame3(frame); emit inferenceFrame3(frame);
framecounter++;
} }
#endif #endif
} }
+1 -3
View File
@@ -3,10 +3,8 @@
#include <QString> #include <QString>
#ifdef OPI5_BUILD #ifdef OPI5_BUILD
QString rtspVideoUrl = "rtsp://192.168.0.1:8554/live.stream"; QString rtspVideoUrl = "rtsp://192.168.168.91:8554/live.stream";
#else #else
// Video file from the local MTX RTSP server or gimbal camera.
QString rtspVideoUrl = "rtsp://localhost:8554/live.stream"; QString rtspVideoUrl = "rtsp://localhost:8554/live.stream";
//QString rtspVideoUrl = "rtsp://192.168.0.25:8554/main.264";
#endif #endif
@@ -1,5 +1,7 @@
#include <QDebug> #include <QDebug>
#include <QtConcurrent/QtConcurrent> #include <QtConcurrent/QtConcurrent>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include "aienginertsplistener.h" #include "aienginertsplistener.h"
#include "aiengineconfig.h" #include "aiengineconfig.h"
@@ -39,6 +41,45 @@ void AiEngineRtspListener::stopListening()
void AiEngineRtspListener::listenLoop(void) void AiEngineRtspListener::listenLoop(void)
{ {
#ifdef AI_BENCH
QThread::msleep(2000);
QString directoryPath = "/home/pama/tmp/photos";
QDir directory(directoryPath);
// Ensure the directory exists
if (!directory.exists()) {
qDebug() << "Directory does not exist!";
exit(1);
}
QStringList filters;
filters << "*.jpg" << "*.jpeg" << "*.png" << "*.bmp";
directory.setNameFilters(filters);
QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files, QDir::Name);
std::sort(fileInfoList.begin(), fileInfoList.end(), [](const QFileInfo &a, const QFileInfo &b) {
return a.fileName() < b.fileName();
});
qDebug() << "Images in folder:" << fileInfoList.size();
for (const QFileInfo &fileInfo : fileInfoList) {
QString filePath = fileInfo.absoluteFilePath();
cv::Mat image = cv::imread(filePath.toStdString());
if (image.empty()) {
qDebug() << "Failed to load image";
exit(1);
}
emit frameReceived(image.clone());
QThread::msleep(1500);
}
QThread::msleep(2000);
qDebug() << "Sleep 2000ms and exit";
exit(0);
#else
qDebug() << "AiEngineRtspListener loop running in thread: " << QThread::currentThreadId(); qDebug() << "AiEngineRtspListener loop running in thread: " << QThread::currentThreadId();
mCap.open(rtspVideoUrl.toStdString()); mCap.open(rtspVideoUrl.toStdString());
@@ -51,4 +92,5 @@ void AiEngineRtspListener::listenLoop(void)
emit frameReceived(frame.clone()); emit frameReceived(frame.clone());
} }
} }
#endif
} }
+16 -1
View File
@@ -1,12 +1,16 @@
QT += core network serialport QT += core network serialport
QT -= gui QT -= gui
CONFIG += c++11 concurrent console CONFIG += concurrent console c++17
MOC_DIR = moc MOC_DIR = moc
OBJECTS_DIR = obj OBJECTS_DIR = obj
SOURCES = $$PWD/*.cpp SOURCES = $$PWD/*.cpp
HEADERS = $$PWD/*.h HEADERS = $$PWD/*.h
ai_bench {
QMAKE_CXXFLAGS += -DAI_BENCH
}
gimbal { gimbal {
message("Using real gimbal camera.") message("Using real gimbal camera.")
QMAKE_CXXFLAGS += -DGIMBAL QMAKE_CXXFLAGS += -DGIMBAL
@@ -35,6 +39,17 @@ opi5 {
SOURCES += $$PWD/src-opi5/*.c $$PWD/src-opi5/*.cpp $$PWD/src-opi5/*.cc SOURCES += $$PWD/src-opi5/*.c $$PWD/src-opi5/*.cpp $$PWD/src-opi5/*.cc
HEADERS += $$PWD/src-opi5/*.h HEADERS += $$PWD/src-opi5/*.h
} }
else:ncnn {
message("NCNN build")
CONFIG += link_pkgconfig
PKGCONFIG += opencv4
QMAKE_CXXFLAGS += -DNCNN_BUILD -fopenmp
QMAKE_LFLAGS += -fopenmp
INCLUDEPATH += /opt/ncnn/include
LIBS += /opt/ncnn/lib/libncnn.a -lgomp
SOURCES += $$PWD/src-ncnn/*.cpp
HEADERS += $$PWD/src-ncnn/*.h
}
else:opencv { else:opencv {
message("OpenCV build") message("OpenCV build")
message("You must use YOLOv8 ONNX files. Azaion model does not work!") message("You must use YOLOv8 ONNX files. Azaion model does not work!")
@@ -0,0 +1,408 @@
#include <QDebug>
#include <QThread>
#include <iostream>
#include <vector>
#include "aiengineinferencencnn.h"
const int target_size = 640;
const float prob_threshold = 0.25f;
const float nms_threshold = 0.45f;
const int num_labels = 10; // 80 object labels in COCO, 10 in Azaion
const int MAX_STRIDE = 32;
char* getCharPointerCopy(const QString& modelPath) {
QByteArray byteArray = modelPath.toUtf8();
char* cString = new char[byteArray.size() + 1]; // Allocate memory
std::strcpy(cString, byteArray.constData()); // Copy the data
return cString; // Remember to delete[] this when done!
}
AiEngineInferencevNcnn::AiEngineInferencevNcnn(QString modelPath, QObject *parent) :
AiEngineInference{modelPath, parent}
{
qDebug() << "TUOMAS AiEngineInferencevNcnn() mModelPath=" << mModelPath;
yolov8.opt.num_threads = 4;
yolov8.opt.use_vulkan_compute = false;
QString paramPath = modelPath.chopped(3).append("param");
char *model = getCharPointerCopy(modelPath);
char *param = getCharPointerCopy(paramPath);
qDebug() << "model:" << model;
qDebug() << "param:" << param;
yolov8.load_param(param);
yolov8.load_model(model);
}
static inline float intersection_area(const Object& a, const Object& b)
{
cv::Rect_<float> inter = a.rect & b.rect;
return inter.area();
}
static void qsort_descent_inplace(std::vector<Object>& objects, int left, int right)
{
int i = left;
int j = right;
float p = objects[(left + right) / 2].prob;
while (i <= j)
{
while (objects[i].prob > p)
i++;
while (objects[j].prob < p)
j--;
if (i <= j)
{
// swap
std::swap(objects[i], objects[j]);
i++;
j--;
}
}
#pragma omp parallel sections
{
#pragma omp section
{
if (left < j) qsort_descent_inplace(objects, left, j);
}
#pragma omp section
{
if (i < right) qsort_descent_inplace(objects, i, right);
}
}
}
static void qsort_descent_inplace(std::vector<Object>& objects)
{
if (objects.empty())
return;
qsort_descent_inplace(objects, 0, objects.size() - 1);
}
static void nms_sorted_bboxes(const std::vector<Object>& faceobjects, std::vector<int>& picked, float nms_threshold, bool agnostic = false)
{
picked.clear();
const int n = faceobjects.size();
std::vector<float> areas(n);
for (int i = 0; i < n; i++)
{
areas[i] = faceobjects[i].rect.area();
}
for (int i = 0; i < n; i++)
{
const Object& a = faceobjects[i];
int keep = 1;
for (int j = 0; j < (int)picked.size(); j++)
{
const Object& b = faceobjects[picked[j]];
if (!agnostic && a.label != b.label)
continue;
// intersection over union
float inter_area = intersection_area(a, b);
float union_area = areas[i] + areas[picked[j]] - inter_area;
// float IoU = inter_area / union_area
if (inter_area / union_area > nms_threshold)
keep = 0;
}
if (keep)
picked.push_back(i);
}
}
static inline float sigmoid(float x)
{
return static_cast<float>(1.f / (1.f + exp(-x)));
}
static inline float clampf(float d, float min, float max)
{
const float t = d < min ? min : d;
return t > max ? max : t;
}
static void parse_yolov8_detections(
float* inputs, float confidence_threshold,
int num_channels, int num_anchors, int num_labels,
int infer_img_width, int infer_img_height,
std::vector<Object>& objects)
{
std::vector<Object> detections;
cv::Mat output = cv::Mat((int)num_channels, (int)num_anchors, CV_32F, inputs).t();
for (int i = 0; i < num_anchors; i++)
{
const float* row_ptr = output.row(i).ptr<float>();
const float* bboxes_ptr = row_ptr;
const float* scores_ptr = row_ptr + 4;
const float* max_s_ptr = std::max_element(scores_ptr, scores_ptr + num_labels);
float score = *max_s_ptr;
if (score > confidence_threshold)
{
float x = *bboxes_ptr++;
float y = *bboxes_ptr++;
float w = *bboxes_ptr++;
float h = *bboxes_ptr;
float x0 = clampf((x - 0.5f * w), 0.f, (float)infer_img_width);
float y0 = clampf((y - 0.5f * h), 0.f, (float)infer_img_height);
float x1 = clampf((x + 0.5f * w), 0.f, (float)infer_img_width);
float y1 = clampf((y + 0.5f * h), 0.f, (float)infer_img_height);
cv::Rect_<float> bbox;
bbox.x = x0;
bbox.y = y0;
bbox.width = x1 - x0;
bbox.height = y1 - y0;
Object object;
object.label = max_s_ptr - scores_ptr;
object.prob = score;
object.rect = bbox;
detections.push_back(object);
}
}
objects = detections;
}
int AiEngineInferencevNcnn::detect_yolov8(const cv::Mat& bgr, std::vector<Object>& objects)
{
/*
int img_w = bgr.cols;
int img_h = bgr.rows;
// letterbox pad to multiple of MAX_STRIDE
int w = img_w;
int h = img_h;
float scale = 1.f;
if (w > h)
{
scale = (float)target_size / w;
w = target_size;
h = h * scale;
}
else
{
scale = (float)target_size / h;
h = target_size;
w = w * scale;
}
*/
int w = 640;
int h = 640;
int img_w = 640;
int img_h = 640;
float scale = 1.f;
ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, img_w, img_h, w, h);
int wpad = (target_size + MAX_STRIDE - 1) / MAX_STRIDE * MAX_STRIDE - w;
int hpad = (target_size + MAX_STRIDE - 1) / MAX_STRIDE * MAX_STRIDE - h;
ncnn::Mat in_pad;
ncnn::copy_make_border(in, in_pad, hpad / 2, hpad - hpad / 2, wpad / 2, wpad - wpad / 2, ncnn::BORDER_CONSTANT, 114.f);
const float norm_vals[3] = {1 / 255.f, 1 / 255.f, 1 / 255.f};
in_pad.substract_mean_normalize(0, norm_vals);
auto start = std::chrono::high_resolution_clock::now();
ncnn::Extractor ex = yolov8.create_extractor();
ex.input("in0", in_pad);
std::vector<Object> proposals;
// stride 32
{
ncnn::Mat out;
ex.extract("out0", out);
std::vector<Object> objects32;
parse_yolov8_detections(
(float*)out.data, prob_threshold,
out.h, out.w, num_labels,
in_pad.w, in_pad.h,
objects32);
proposals.insert(proposals.end(), objects32.begin(), objects32.end());
}
// sort all proposals by score from highest to lowest
qsort_descent_inplace(proposals);
// apply nms with nms_threshold
std::vector<int> picked;
nms_sorted_bboxes(proposals, picked, nms_threshold);
int count = picked.size();
objects.resize(count);
for (int i = 0; i < count; i++)
{
objects[i] = proposals[picked[i]];
// adjust offset to original unpadded
float x0 = (objects[i].rect.x - (wpad / 2)) / scale;
float y0 = (objects[i].rect.y - (hpad / 2)) / scale;
float x1 = (objects[i].rect.x + objects[i].rect.width - (wpad / 2)) / scale;
float y1 = (objects[i].rect.y + objects[i].rect.height - (hpad / 2)) / scale;
// clip
x0 = std::max(std::min(x0, (float)(img_w - 1)), 0.f);
y0 = std::max(std::min(y0, (float)(img_h - 1)), 0.f);
x1 = std::max(std::min(x1, (float)(img_w - 1)), 0.f);
y1 = std::max(std::min(y1, (float)(img_h - 1)), 0.f);
objects[i].rect.x = x0;
objects[i].rect.y = y0;
objects[i].rect.width = x1 - x0;
objects[i].rect.height = y1 - y0;
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Time taken: " << duration.count() << " milliseconds" << std::endl;
return 0;
}
static cv::Mat draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
{
static const char* class_names[] = {
"Armoured vehicles",
"Truck",
"Passenger car",
"Artillery",
"Shadow of the vehicle",
"Trenches",
"Military",
"Ramps",
"Tank with additional protection",
"Smoke"
};
static const unsigned char colors[19][3] = {
{54, 67, 244},
{99, 30, 233},
{176, 39, 156},
{183, 58, 103},
{181, 81, 63},
{243, 150, 33},
{244, 169, 3},
{212, 188, 0},
{136, 150, 0},
{80, 175, 76},
{74, 195, 139},
{57, 220, 205},
{59, 235, 255},
{7, 193, 255},
{0, 152, 255},
{34, 87, 255},
{72, 85, 121},
{158, 158, 158},
{139, 125, 96}
};
int color_index = 0;
cv::Mat image = bgr.clone();
for (size_t i = 0; i < objects.size(); i++)
{
const Object& obj = objects[i];
const unsigned char* color = colors[color_index % 19];
color_index++;
cv::Scalar cc(color[0], color[1], color[2]);
fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
cv::rectangle(image, obj.rect, cc, 2);
char text[256];
sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
int baseLine = 0;
cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
int x = obj.rect.x;
int y = obj.rect.y - label_size.height - baseLine;
if (y < 0)
y = 0;
if (x + label_size.width > image.cols)
x = image.cols - label_size.width;
cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
cc, -1);
cv::putText(image, text, cv::Point(x, y + label_size.height),
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255, 255, 255));
}
return image;
}
void AiEngineInferencevNcnn::performInferenceSlot(cv::Mat frame)
{
mActive = true;
cv::Mat scaledImage = resizeAndPad(frame);
std::vector<Object> objects;
detect_yolov8(scaledImage, objects);
if (objects.empty() == false) {
AiEngineInferenceResult result;
result.frame = draw_objects(scaledImage, objects);
for (uint i = 0; i < objects.size(); i++) {
const Object &detection = objects[i];
AiEngineObject object;
object.classId = detection.label;
object.classStr = mClassNames[detection.label];
object.propability = detection.prob;
object.rectangle.top = detection.rect.y;
object.rectangle.left = detection.rect.x;
object.rectangle.bottom = detection.rect.y + detection.rect.height;
object.rectangle.right = detection.rect.x + detection.rect.width;
result.objects.append(object);
}
emit resultsReady(result);
}
mActive = false;
}
void AiEngineInferencevNcnn::initialize(int number)
{
(void)number;
}
@@ -0,0 +1,31 @@
#pragma once
#include <QObject>
#include <opencv2/core.hpp>
#include <ncnn/net.h>
#include "aiengineinference.h"
struct Object
{
cv::Rect_<float> rect;
int label;
float prob;
};
class AiEngineInferencevNcnn : public AiEngineInference
{
Q_OBJECT
public:
explicit AiEngineInferencevNcnn(QString modelPath, QObject *parent = nullptr);
void initialize(int number);
public slots:
void performInferenceSlot(cv::Mat frame) override;
private:
int detect_yolov8(const cv::Mat& bgr, std::vector<Object>& objects);
ncnn::Net yolov8;
};
@@ -4,9 +4,9 @@
#include "aiengineinferenceonnxruntime.h" #include "aiengineinferenceonnxruntime.h"
static const float confThreshold = 0.2f; static const float confThreshold = 0.25f;
static const float iouThreshold = 0.4f; static const float iouThreshold = 0.45f;
static const float maskThreshold = 0.5f; static const float maskThreshold = 0.45f;
AiEngineInferencevOnnxRuntime::AiEngineInferencevOnnxRuntime(QString modelPath, QObject *parent) : AiEngineInferencevOnnxRuntime::AiEngineInferencevOnnxRuntime(QString modelPath, QObject *parent) :
@@ -70,6 +70,7 @@ void AiEngineInferenceOpi5::freeImageBuffer(image_buffer_t& imgBuffer)
cv::Mat AiEngineInferenceOpi5::resizeToHalfAndAssigntoTopLeft640x640(const cv::Mat& inputFrame) cv::Mat AiEngineInferenceOpi5::resizeToHalfAndAssigntoTopLeft640x640(const cv::Mat& inputFrame)
{ {
/*
// Resize input frame to half size // Resize input frame to half size
cv::Mat resizedFrame; cv::Mat resizedFrame;
cv::resize(inputFrame, resizedFrame, cv::Size(), 0.5, 0.5); cv::resize(inputFrame, resizedFrame, cv::Size(), 0.5, 0.5);
@@ -81,6 +82,25 @@ cv::Mat AiEngineInferenceOpi5::resizeToHalfAndAssigntoTopLeft640x640(const cv::M
cv::Rect roi(0, 0, resizedFrame.cols, resizedFrame.rows); cv::Rect roi(0, 0, resizedFrame.cols, resizedFrame.rows);
resizedFrame.copyTo(outputFrame(roi)); resizedFrame.copyTo(outputFrame(roi));
return outputFrame;
*/
const int targetWidth = 640;
const int targetHeight = 640;
float aspectRatio = static_cast<float>(inputFrame.cols) / static_cast<float>(inputFrame.rows);
int newWidth = targetWidth;
int newHeight = static_cast<int>(targetWidth / aspectRatio);
if (newHeight > targetHeight) {
newHeight = targetHeight;
newWidth = static_cast<int>(targetHeight * aspectRatio);
}
cv::Mat resizedFrame;
cv::resize(inputFrame, resizedFrame, cv::Size(newWidth, newHeight));
cv::Mat outputFrame = cv::Mat::zeros(targetHeight, targetWidth, inputFrame.type());
cv::Rect roi(cv::Point(0, 0), resizedFrame.size());
resizedFrame.copyTo(outputFrame(roi));
return outputFrame; return outputFrame;
} }
@@ -91,7 +111,9 @@ void AiEngineInferenceOpi5::drawObjects(cv::Mat& image, const object_detect_resu
const object_detect_result& result = result_list.results[i]; const object_detect_result& result = result_list.results[i];
if (result.cls_id >= mClassNames.size()) { if (result.cls_id >= mClassNames.size()) {
continue; //result.cls_id = result.cls_id % mClassNames.size();
qDebug() << "Class id >= mClassNames.size() Reducing it.";
//continue;
} }
fprintf(stderr, "TUOMAS [%d] prop = %f\n", i, result.prop); fprintf(stderr, "TUOMAS [%d] prop = %f\n", i, result.prop);
@@ -106,7 +128,7 @@ void AiEngineInferenceOpi5::drawObjects(cv::Mat& image, const object_detect_resu
// Text // Text
char c_text[256]; char c_text[256];
//sprintf(c_text, "%s %d%%", coco_cls_to_name(result.cls_id), (int)(round(result.prop * 100))); //sprintf(c_text, "%s %d%%", coco_cls_to_name(result.cls_id), (int)(round(result.prop * 100)));
sprintf(c_text, "%s %d%%", mClassNames[result.cls_id].toStdString().c_str(), (int)(round(result.prop * 100))); sprintf(c_text, "%s %d%%", mClassNames[result.cls_id % mClassNames.size()].toStdString().c_str(), (int)(round(result.prop * 100)));
cv::Point textOrg(left, top - 5); cv::Point textOrg(left, top - 5);
cv::putText(image, std::string(c_text), textOrg, cv::FONT_HERSHEY_COMPLEX, result.prop, cv::Scalar(0, 0, 255), 1, cv::LINE_AA); cv::putText(image, std::string(c_text), textOrg, cv::FONT_HERSHEY_COMPLEX, result.prop, cv::Scalar(0, 0, 255), 1, cv::LINE_AA);
} }
@@ -131,23 +153,25 @@ void AiEngineInferenceOpi5::performInferenceSlot(cv::Mat frame)
return; return;
} }
AiEngineInferenceResult result; if (od_results.count > 0) {
for (int i = 0; i < od_results.count; i++) { AiEngineInferenceResult result;
object_detect_result *det_result = &(od_results.results[i]); for (int i = 0; i < od_results.count; i++) {
object_detect_result *det_result = &(od_results.results[i]);
qDebug() << "TUOMAS box:" << det_result->box.top << det_result->box.left << det_result->box.bottom << det_result->box.right;
AiEngineObject object;
object.classId = det_result->cls_id;
object.propability = det_result->prop;
object.rectangle.top = det_result->box.top;
object.rectangle.left = det_result->box.left;
object.rectangle.bottom = det_result->box.bottom;
object.rectangle.right = det_result->box.right;
result.objects.append(object);
}
AiEngineObject object; drawObjects(scaledFrame, od_results);
object.classId = det_result->cls_id; result.frame = scaledFrame.clone();
object.propability = det_result->prop; emit resultsReady(result);
object.rectangle.top = det_result->box.top;
object.rectangle.left = det_result->box.left;
object.rectangle.bottom = det_result->box.bottom;
object.rectangle.right = det_result->box.right;
result.objects.append(object);
} }
drawObjects(scaledFrame, od_results);
result.frame = scaledFrame.clone();
emit resultsReady(result);
mActive = false; mActive = false;
} }