Commit 17a25564 authored by lijian6's avatar lijian6
Browse files

First commit


Signed-off-by: lijian6's avatarlijian <lijian6@sugon.com>
parents
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
build/*
depend/*
.vscode/*
#core.*
# qtcreator generated files
*.pro.user*
# VS
*.sdf
*.opensdf
*.ilk
*.pdb
*.exp
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
#*.so
*.dylib
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
#*.la
#*.a
# Executables
*.exe
*.out
*.app
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# others
#*.avi
#*.mp4
*.pyc
*.egg
cmake_minimum_required(VERSION 3.16)
project(Video_Onnx)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_COMPILER g++)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pthread")
add_definitions(-DUNICODE -D_UNICODE)
add_definitions("-Wall")
# project include
set(INCLUDE_PATH /usr/local/include
${CMAKE_CURRENT_SOURCE_DIR}/include
$ENV{ROCM_PATH}/include)
include_directories(${INCLUDE_PATH})
# project lib path
set(LIBRARY_PATH /usr/local/lib
${CMAKE_CURRENT_SOURCE_DIR}/lib
$ENV{ROCM_PATH}/lib)
link_directories(${LIBRARY_PATH})
# project lib
set(LIBRARY avcodec avdevice avfilter avformat avutil swresample swscale postproc
opencv_core opencv_imgproc opencv_imgcodecs opencv_dnn opencv_highgui opencv_videoio
onnxruntime)
link_libraries(${LIBRARY})
# project source files
file(GLOB _SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
set(_COMPILE_CODE ${_SRC})
add_executable(Video_Onnx ${_COMPILE_CODE})
target_link_libraries(Video_Onnx decode Queue)
# Video_Ort
## 目录
- [目录结构](#目录结构)
- [项目介绍](#项目介绍)
- [环境配置](#环境配置)
- [编译运行](#编译运行)
- [参考文档](#参考文档)
- [历史版本](#历史版本)
## 目录结构
```
├── CMakeLists.txt
├── include
├── lib
│   ├── libdecode.so
│   └── libQueue.so
├── README.md
├── Resource
│   ├── Images
│   └── Models
└── src
├── CommonUtils.cpp
├── DetectorYOLOV3.cpp
├── DetectorYOLOV5.cpp
├── DetectorYOLOV7.cpp
├── main.cpp
├── yolov3-tiny.cpp
├── yolov5s.cpp
└── yolov7-tiny.cpp
```
## 项目介绍
基于CPU解码,onnxruntime框架推理的视频推理范例
## 环境配置
推荐使用docker方式运行,提供[光源](https://www.sourcefind.cn/#/service-list)拉取的docker镜像
```
docker pull image.sourcefind.cn:5000/dcu/admin/base/custom:tvm0.10_ort1.14.0_migraphx3.0.0-dtk23.04
```
## 编译运行
### 编译
```
git clone https://developer.hpccube.com/codes/modelzoo/video_ort
cd video_ort
mkdir build
cd build
cmake ../
make
```
### 运行
```
./Video_Ort
```
根据提示选择要运行的示例程序,比如执行:
```
./Video_Ort 0
```
运行CPU解码并运行YOLOV3推理示例程序
## 参考文档
文档参考Doc目录下说明文档.
## 源码仓库及问题反馈
https://developer.hpccube.com/codes/modelzoo/video_ort
person
bicycle
car
motorbike
aeroplane
bus
train
truck
boat
traffic light
fire hydrant
stop sign
parking meter
bench
bird
cat
dog
horse
sheep
cow
elephant
bear
zebra
giraffe
backpack
umbrella
handbag
tie
suitcase
frisbee
skis
snowboard
sports ball
kite
baseball bat
baseball glove
skateboard
surfboard
tennis racket
bottle
wine glass
cup
fork
knife
spoon
bowl
banana
apple
sandwich
orange
broccoli
carrot
hot dog
pizza
donut
cake
chair
sofa
pottedplant
bed
diningtable
toilet
tvmonitor
laptop
mouse
remote
keyboard
cell phone
microwave
oven
toaster
sink
refrigerator
book
clock
vase
scissors
teddy bear
hair drier
toothbrush
person
bicycle
car
motorbike
aeroplane
bus
train
truck
boat
traffic light
fire hydrant
stop sign
parking meter
bench
bird
cat
dog
horse
sheep
cow
elephant
bear
zebra
giraffe
backpack
umbrella
handbag
tie
suitcase
frisbee
skis
snowboard
sports ball
kite
baseball bat
baseball glove
skateboard
surfboard
tennis racket
bottle
wine glass
cup
fork
knife
spoon
bowl
banana
apple
sandwich
orange
broccoli
carrot
hot dog
pizza
donut
cake
chair
sofa
pottedplant
bed
diningtable
toilet
tvmonitor
laptop
mouse
remote
keyboard
cell phone
microwave
oven
toaster
sink
refrigerator
book
clock
vase
scissors
teddy bear
hair drier
toothbrush
person
bicycle
car
motorbike
aeroplane
bus
train
truck
boat
traffic light
fire hydrant
stop sign
parking meter
bench
bird
cat
dog
horse
sheep
cow
elephant
bear
zebra
giraffe
backpack
umbrella
handbag
tie
suitcase
frisbee
skis
snowboard
sports ball
kite
baseball bat
baseball glove
skateboard
surfboard
tennis racket
bottle
wine glass
cup
fork
knife
spoon
bowl
banana
apple
sandwich
orange
broccoli
carrot
hot dog
pizza
donut
cake
chair
sofa
pottedplant
bed
diningtable
toilet
tvmonitor
laptop
mouse
remote
keyboard
cell phone
microwave
oven
toaster
sink
refrigerator
book
clock
vase
scissors
teddy bear
hair drier
toothbrush
#ifndef __COMMON_UTILS_H__
#define __COMMON_UTILS_H__
#include <opencv2/core.hpp>
#include <onnxruntime/core/session/onnxruntime_cxx_api.h>
#include <numeric>
#include <sys/stat.h>
std::vector<float> substractMeanNormalize(cv::Mat &src, const float *meanVals, const float *normVals);
std::vector<Ort::AllocatedStringPtr> getInputNames(Ort::Session *session);
std::vector<int64_t> getInputDim(Ort::Session *session);
std::vector<Ort::AllocatedStringPtr> getOutputNames(Ort::Session *session);
std::string getSrcImgFilePath(const char *path, const char *imgName);
#endif
/*
* Decoder
* 2022.09.23 sugon
* */
#ifndef __DECODER_H__
#define __DECODER_H__
extern "C"
{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libavcodec/avcodec.h>
#include <libavcodec/packet.h>
#include <libavcodec/codec.h>
#include <libavutil/imgutils.h>
#include <libavutil/samplefmt.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include <libavutil/time.h>
}
class Decoder
{
public:
Decoder();
virtual ~Decoder();
virtual int DecoderInit();
AVFormatContext *fmt_ctx = NULL;
AVCodecContext *video_dec_ctx = NULL;
AVStream *video_stream = NULL;
AVFrame *frame = NULL;
AVPacket *pkt = NULL;
const AVCodec *dec = NULL;
int video_stream_idx = -1;
const char *src_filename = "../Resource/Images/Mean.mp4";
};
#endif
// YOLOV3检测器
#ifndef __DETECTOR_YOLOV3_H__
#define __DETECTOR_YOLOV3_H__
#include <string>
#include <fstream>
#include <onnxruntime/core/session/onnxruntime_cxx_api.h>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include "CommonUtils.h"
#include "Queuethread.h"
#include "Decoder.h"
using namespace std;
using namespace cv;
typedef struct _ResultOfDetection
{
Rect boundingBox;
float confidence;
int classID;
string className;
bool exist;
_ResultOfDetection():confidence(0.0f),classID(0),exist(true){}
}ResultOfDetection;
class DetectorYOLOV3
{
public:
~DetectorYOLOV3();
void setNumThread(int numOfThread);
void setGpuIndex(int gpuIndex);
void initModel(const std::string &pathStr, const std::string &pathOfClassNameFile);
void Detect(cv::Mat srcImage);
private:
Ort::Session *session;
Ort::Env env = Ort::Env(ORT_LOGGING_LEVEL_ERROR, "Yolov3");
Ort::SessionOptions sessionOptions = Ort::SessionOptions();
int numThread = 0;
vector<string> classNames;
std::vector<int64_t> inputDim;
std::vector<Ort::AllocatedStringPtr> inputNamesPtr;
std::vector<Ort::AllocatedStringPtr> outputNamesPtr;
const float meanValues[3] = {0, 0, 0};
const float normValues[3] = {1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0};
};
#endif
// YOLOV5检测器
#ifndef __DETECTOR_YOLOV5_H__
#define __DETECTOR_YOLOV5_H__
#include <string>
#include <fstream>
#include <onnxruntime/core/session/onnxruntime_cxx_api.h>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include "CommonUtils.h"
#include "Queuethread.h"
#include "Decoder.h"
using namespace std;
using namespace cv;
typedef struct _ResultOfDetection
{
Rect boundingBox;
float confidence;
int classID;
string className;
bool exist;
_ResultOfDetection():confidence(0.0f),classID(0),exist(true){}
}ResultOfDetection;
class DetectorYOLOV5
{
public:
~DetectorYOLOV5();
void setNumThread(int numOfThread);
void setGpuIndex(int gpuIndex);
void initModel(const std::string &pathStr, const std::string &pathOfClassNameFile);
void Detect(cv::Mat originSrc);
private:
Ort::Session *session;
Ort::Env env = Ort::Env(ORT_LOGGING_LEVEL_ERROR, "Yolov5");
Ort::SessionOptions sessionOptions = Ort::SessionOptions();
int numThread = 0;
vector<string> classNames;
std::vector<int64_t> inputDim;
std::vector<Ort::AllocatedStringPtr> inputNamesPtr;
std::vector<Ort::AllocatedStringPtr> outputNamesPtr;
const float meanValues[3] = {0, 0, 0};
const float normValues[3] = {1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0};
};
#endif
// YOLOV7检测器
#ifndef __DETECTOR_YOLOV7_H__
#define __DETECTOR_YOLOV7_H__
#include <string>
#include <fstream>
#include <onnxruntime/core/session/onnxruntime_cxx_api.h>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include "CommonUtils.h"
#include "Queuethread.h"
#include "Decoder.h"
using namespace std;
using namespace cv;
typedef struct _ResultOfDetection
{
Rect boundingBox;
float confidence;
int classID;
string className;
bool exist;
_ResultOfDetection():confidence(0.0f),classID(0),exist(true){}
}ResultOfDetection;
class DetectorYOLOV7
{
public:
~DetectorYOLOV7();
void setNumThread(int numOfThread);
void setGpuIndex(int gpuIndex);
void initModel(const std::string &pathStr, const std::string &pathOfClassNameFile);
//void Detect(const char *path, const char *imgName);
void Detect(cv::Mat srcImage);
private:
Ort::Session *session;
Ort::Env env = Ort::Env(ORT_LOGGING_LEVEL_ERROR, "Yolov7");
Ort::SessionOptions sessionOptions = Ort::SessionOptions();
int numThread = 0;
vector<string> classNames;
std::vector<int64_t> inputDim;
std::vector<Ort::AllocatedStringPtr> inputNamesPtr;
std::vector<Ort::AllocatedStringPtr> outputNamesPtr;
const float meanValues[3] = {0, 0, 0};
const float normValues[3] = {1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0};
};
#endif
/*
* queue for Decoder and Detector
* 2023.03.04 sugon
* */
#ifndef __QUEUE_H__
#define __QUEUE_H__
#include <pthread.h>
#include <thread>
#include <queue>
#include <condition_variable>
#include <mutex>
#include <sys/time.h>
#include <opencv2/opencv.hpp>
using std::queue;
#define CAPACITY 1
class Queue
{
public:
Queue(size_t Capacity = CAPACITY);
~Queue();
void enQueue(cv::Mat& Image);
void deQueue(cv::Mat* Image);
void finish();
bool Isempty();
bool DecodeEnd;
private:
bool IsFull();
queue<cv::Mat> _Queue;
size_t _Capacity;
pthread_mutex_t Lock;
pthread_cond_t ConsumeCond;
pthread_cond_t ProductCond;
};
#endif
// 示例程序
#ifndef __MAIN_H__
#define __MAIN_H__
// YOLOV3 sample
void Ort_DetectorYOLOV3();
// YOLOV5 sample
void Ort_DetectorYOLOV5();
// YOLOV7 sample
void Ort_DetectorYOLOV7();
#endif
File added
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment