├── README.md └── modules ├── av_stream ├── CMakeLists.txt ├── FindFFmpeg.cmake ├── include │ └── opencv2 │ │ └── av_stream.hpp ├── samples │ └── test.py └── src │ ├── av_stream.cpp │ ├── config.cpp │ ├── config.hpp │ ├── precomp.hpp │ ├── stream_recv.cpp │ └── stream_recv.hpp └── dnn_inference ├── CMakeLists.txt ├── include └── opencv2 │ └── dnn_inference.hpp ├── samples └── test.py └── src ├── benchmark_timer.hpp ├── config.cpp ├── config.hpp ├── dnn_inference.cpp └── precomp.hpp /README.md: -------------------------------------------------------------------------------- 1 | # pybindcpp 2 | >cd opencv \ 3 | >git checkout 3.4 \ 4 | >mkdir build && cd build \ 5 | >cmake 6 | -DCMAKE_BUILD_TYPE=Release \ 7 | -DCMAKE_INSTALL_PREFIX=/your/path/to/opencv-compile \ 8 | -DBUILD_LIST=core,imgcodecs,python_bindings_generator,python3,av_stream,dnn_innference \ 9 | -DOPENCV_EXTRA_MODULES_PATH=/your/path/to/pybindcpp/modules/ \ 10 | -DBUILD_opencv_python3=ON \ 11 | -DSOFTFP=ON .. 12 | -------------------------------------------------------------------------------- /modules/av_stream/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(the_description "Video stream processing using ffmpeg") 2 | 3 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}) 4 | SET(CMAKE_CXX_FLAGS "-O2 -g -Wall -Wno-unused-function -std=c++11 -ldl -lm -lpthread -fpermissive") 5 | 6 | 7 | find_package(FFmpeg) 8 | if(FFmpeg_FOUND) 9 | message(STATUS "FFmpeg: YES") 10 | set(HAVE_FFmpeg 1) 11 | else() 12 | message(STATUS "FFmpeg: NO") 13 | endif() 14 | 15 | set(AV_SRCS 16 | config.cpp 17 | stream_recv.cpp 18 | ) 19 | 20 | include_directories(${CMAKE_CURRENT_BINARY_DIR}) 21 | include_directories(${FFMPEG_INCLUDE_DIRS}) 22 | 23 | ocv_glob_module_sources(${AV_SRCS}) 24 | ocv_define_module(av_stream opencv_imgproc ${FFMPEG_LIBRARIES} WRAP python) -------------------------------------------------------------------------------- /modules/av_stream/FindFFmpeg.cmake: -------------------------------------------------------------------------------- 1 | # - Try to find the required ffmpeg components(default: AVFORMAT, AVUTIL, AVCODEC) 2 | # 3 | # Once done this will define 4 | # FFMPEG_FOUND - System has the all required components. 5 | # FFMPEG_INCLUDE_DIRS - Include directory necessary for using the required components headers. 6 | # FFMPEG_LIBRARIES - Link these to use the required ffmpeg components. 7 | # FFMPEG_DEFINITIONS - Compiler switches required for using the required ffmpeg components. 8 | # 9 | # For each of the components it will additionally set. 10 | # - AVCODEC 11 | # - AVDEVICE 12 | # - AVFORMAT 13 | # - AVFILTER 14 | # - AVUTIL 15 | # - POSTPROC 16 | # - SWSCALE 17 | # - SWRESAMPLE 18 | # the following variables will be defined 19 | # _FOUND - System has 20 | # _INCLUDE_DIRS - Include directory necessary for using the headers 21 | # _LIBRARIES - Link these to use 22 | # _DEFINITIONS - Compiler switches required for using 23 | # _VERSION - The components version 24 | # 25 | # Copyright (c) 2006, Matthias Kretz, 26 | # Copyright (c) 2008, Alexander Neundorf, 27 | # Copyright (c) 2011, Michael Jansen, 28 | # 29 | # Redistribution and use is allowed according to the terms of the BSD license. 30 | # For details see the accompanying COPYING-CMAKE-SCRIPTS file. 31 | 32 | include(FindPackageHandleStandardArgs) 33 | 34 | # The default components were taken from a survey over other FindFFMPEG.cmake files 35 | if (NOT FFmpeg_FIND_COMPONENTS) 36 | set(FFmpeg_FIND_COMPONENTS AVFORMAT AVCODEC AVUTIL SWRESAMPLE SWSCALE) 37 | endif () 38 | 39 | # 40 | ### Macro: set_component_found 41 | # 42 | # Marks the given component as found if both *_LIBRARIES AND *_INCLUDE_DIRS is present. 43 | # 44 | macro(set_component_found _component ) 45 | if (${_component}_LIBRARIES AND ${_component}_INCLUDE_DIRS) 46 | # message(STATUS " - ${_component} found.") 47 | set(${_component}_FOUND TRUE) 48 | else () 49 | # message(STATUS " - ${_component} not found.") 50 | endif () 51 | endmacro() 52 | 53 | # 54 | ### Macro: find_component 55 | # 56 | # Checks for the given component by invoking pkgconfig and then looking up the libraries and 57 | # include directories. 58 | # 59 | macro(find_component _component _pkgconfig _library _header) 60 | 61 | if (NOT WIN32) 62 | # use pkg-config to get the directories and then use these values 63 | # in the FIND_PATH() and FIND_LIBRARY() calls 64 | find_package(PkgConfig) 65 | if (PKG_CONFIG_FOUND) 66 | pkg_check_modules(PC_${_component} ${_pkgconfig}) 67 | endif () 68 | endif (NOT WIN32) 69 | 70 | find_path(${_component}_INCLUDE_DIRS ${_header} 71 | HINTS 72 | ${PC_LIB${_component}_INCLUDEDIR} 73 | ${PC_LIB${_component}_INCLUDE_DIRS} 74 | PATH_SUFFIXES 75 | ffmpeg 76 | ) 77 | 78 | find_library(${_component}_LIBRARIES NAMES ${_library} 79 | HINTS 80 | ${PC_LIB${_component}_LIBDIR} 81 | ${PC_LIB${_component}_LIBRARY_DIRS} 82 | ) 83 | 84 | set(${_component}_DEFINITIONS ${PC_${_component}_CFLAGS_OTHER} CACHE STRING "The ${_component} CFLAGS.") 85 | set(${_component}_VERSION ${PC_${_component}_VERSION} CACHE STRING "The ${_component} version number.") 86 | 87 | set_component_found(${_component}) 88 | 89 | mark_as_advanced( 90 | ${_component}_INCLUDE_DIRS 91 | ${_component}_LIBRARIES 92 | ${_component}_DEFINITIONS 93 | ${_component}_VERSION) 94 | 95 | endmacro() 96 | 97 | 98 | # Check for cached results. If there are skip the costly part. 99 | if (NOT FFMPEG_LIBRARIES) 100 | 101 | # Check for all possible component. 102 | find_component(AVCODEC libavcodec avcodec libavcodec/avcodec.h) 103 | find_component(AVFORMAT libavformat avformat libavformat/avformat.h) 104 | find_component(AVDEVICE libavdevice avdevice libavdevice/avdevice.h) 105 | find_component(AVUTIL libavutil avutil libavutil/avutil.h) 106 | find_component(AVFILTER libavfilter avfilter libavfilter/avfilter.h) 107 | find_component(SWSCALE libswscale swscale libswscale/swscale.h) 108 | find_component(POSTPROC libpostproc postproc libpostproc/postprocess.h) 109 | find_component(SWRESAMPLE libswresample swresample libswresample/swresample.h) 110 | 111 | # Check if the required components were found and add their stuff to the FFMPEG_* vars. 112 | foreach (_component ${FFmpeg_FIND_COMPONENTS}) 113 | if (${_component}_FOUND) 114 | # message(STATUS "Required component ${_component} present.") 115 | set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${${_component}_LIBRARIES}) 116 | set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} ${${_component}_DEFINITIONS}) 117 | list(APPEND FFMPEG_INCLUDE_DIRS ${${_component}_INCLUDE_DIRS}) 118 | else () 119 | # message(STATUS "Required component ${_component} missing.") 120 | endif () 121 | endforeach () 122 | 123 | # Build the include path with duplicates removed. 124 | if (FFMPEG_INCLUDE_DIRS) 125 | list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS) 126 | endif () 127 | 128 | # cache the vars. 129 | set(FFMPEG_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS} CACHE STRING "The FFmpeg include directories." FORCE) 130 | set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} CACHE STRING "The FFmpeg libraries." FORCE) 131 | set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} CACHE STRING "The FFmpeg cflags." FORCE) 132 | 133 | mark_as_advanced(FFMPEG_INCLUDE_DIRS 134 | FFMPEG_LIBRARIES 135 | FFMPEG_DEFINITIONS) 136 | 137 | endif () 138 | 139 | # Now set the noncached _FOUND vars for the components. 140 | foreach (_component AVCODEC AVDEVICE AVFORMAT AVUTIL POSTPROCESS SWSCALE) 141 | set_component_found(${_component}) 142 | endforeach () 143 | 144 | # Compile the list of required vars 145 | set(_FFmpeg_REQUIRED_VARS FFMPEG_LIBRARIES FFMPEG_INCLUDE_DIRS) 146 | foreach (_component ${FFmpeg_FIND_COMPONENTS}) 147 | list(APPEND _FFmpeg_REQUIRED_VARS ${_component}_LIBRARIES ${_component}_INCLUDE_DIRS) 148 | endforeach () 149 | 150 | # Give a nice error message if some of the required vars are missing. 151 | find_package_handle_standard_args(FFmpeg DEFAULT_MSG ${_FFmpeg_REQUIRED_VARS}) -------------------------------------------------------------------------------- /modules/av_stream/include/opencv2/av_stream.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __OPENCV_AV_STREAM_HPP__ 2 | #define __OPENCV_AV_STREAM_HPP__ 3 | 4 | #include "opencv2/core.hpp" 5 | 6 | typedef struct AVCapture AVCapture; 7 | // typedef struct MediaReader MediaReader; 8 | 9 | namespace cv { 10 | namespace av_stream { 11 | 12 | class CV_EXPORTS_W VideoCapture { 13 | public: 14 | CV_WRAP VideoCapture() {} 15 | CV_WRAP VideoCapture(const String& filename); 16 | virtual ~VideoCapture() {} 17 | 18 | CV_WRAP virtual bool open(const String& filename); 19 | CV_WRAP virtual bool read(OutputArray image); 20 | CV_WRAP virtual bool grab(); 21 | CV_WRAP virtual bool retrieve(OutputArray image, int flag = 0); 22 | 23 | private: 24 | Ptr cap; 25 | // Ptr reader; 26 | }; 27 | 28 | } //av_stream 29 | } //cv 30 | 31 | #endif -------------------------------------------------------------------------------- /modules/av_stream/samples/test.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.insert(0, '/your/opencv-compile/lib/python3.7/site-packages') 3 | import cv2 4 | cap = cv2.av_stream_VideoCapture() 5 | cap.open("video rtsp/rtmp url") 6 | 7 | cnt = 0 8 | while True: 9 | ret = cap.grab() 10 | # ret, frame = cap.read() 11 | if not ret: 12 | continue 13 | ret, frame = cap.retrieve() 14 | print (frame.shape) -------------------------------------------------------------------------------- /modules/av_stream/src/av_stream.cpp: -------------------------------------------------------------------------------- 1 | #include "precomp.hpp" 2 | #include "stream_recv.hpp" 3 | #include "config.hpp" 4 | 5 | #include "opencv2/core/utility.hpp" 6 | #include "opencv2/core/private.hpp" 7 | 8 | namespace cv { 9 | namespace av_stream { 10 | 11 | VideoCapture::VideoCapture(const String& filename) { 12 | CV_TRACE_FUNCTION(); 13 | open(filename); 14 | } 15 | 16 | bool VideoCapture::open(const String& filename) { 17 | CV_TRACE_FUNCTION(); 18 | 19 | cap.reset(new StreamRecv()); 20 | return cap->open(filename); 21 | } 22 | 23 | bool VideoCapture::read(OutputArray image) 24 | { 25 | CV_INSTRUMENT_REGION() 26 | 27 | if(cap->grabFrame()) { 28 | IplImage* _img = cap->retrieveFrame(0); 29 | if(!_img) { 30 | image.release(); 31 | return false; 32 | } 33 | if(_img->origin == IPL_ORIGIN_TL) { 34 | cv::cvarrToMat(_img).copyTo(image); 35 | } else { 36 | Mat temp = cv::cvarrToMat(_img); 37 | flip(temp, image, 0); 38 | } 39 | } else { 40 | image.release(); 41 | } 42 | return !image.empty(); 43 | } 44 | 45 | bool VideoCapture::grab() { 46 | CV_INSTRUMENT_REGION() 47 | 48 | return cap->grabFrame(); 49 | } 50 | 51 | bool VideoCapture::retrieve(OutputArray image, int flag) { 52 | CV_INSTRUMENT_REGION() 53 | 54 | IplImage* _img = cap->retrieveFrame(0); 55 | if(!_img) { 56 | image.release(); 57 | return false; 58 | } 59 | if(_img->origin == IPL_ORIGIN_TL) { 60 | cv::cvarrToMat(_img).copyTo(image); 61 | } else { 62 | Mat temp = cv::cvarrToMat(_img); 63 | flip(temp, image, 0); 64 | } 65 | return true; 66 | } 67 | 68 | } // av_stream 69 | } // cv 70 | -------------------------------------------------------------------------------- /modules/av_stream/src/config.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "config.hpp" 5 | 6 | char * timeString() { 7 | struct timespec ts; 8 | clock_gettime(CLOCK_REALTIME, &ts); 9 | struct tm * timeinfo = localtime(&ts.tv_sec); 10 | static char timeStr[40]; 11 | sprintf(timeStr, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%.3ld+08:00", 12 | timeinfo->tm_year + 1900, 13 | timeinfo->tm_mon + 1, 14 | timeinfo->tm_mday, 15 | timeinfo->tm_hour, 16 | timeinfo->tm_min, 17 | timeinfo->tm_sec, 18 | ts.tv_nsec / 1000000); 19 | return timeStr; 20 | } 21 | -------------------------------------------------------------------------------- /modules/av_stream/src/config.hpp: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __CONFIG_HPP__ 3 | #define __CONFIG_HPP__ 4 | 5 | #include 6 | 7 | #define INFO_LOG(format, ...) \ 8 | do { \ 9 | fprintf(stderr, "[%s] " format "\n", \ 10 | timeString(), \ 11 | ##__VA_ARGS__); \ 12 | } while(0) 13 | 14 | char * timeString(); 15 | 16 | #endif // __CONFIG_HPP__ -------------------------------------------------------------------------------- /modules/av_stream/src/precomp.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __OPENCV_AV_STREAM_PRECOMP_HPP__ 2 | #define __OPENCV_AV_STREAM_PRECOMP_HPP__ 3 | 4 | #include "../include/opencv2/av_stream.hpp" 5 | #include "opencv2/imgproc/imgproc_c.h" 6 | 7 | #include 8 | 9 | struct AVCapture 10 | { 11 | virtual ~AVCapture() {} 12 | virtual bool open(std::string) { return true; } 13 | virtual bool grabFrame() { return true; } 14 | virtual IplImage* retrieveFrame(int) { return 0; } 15 | }; 16 | 17 | #endif -------------------------------------------------------------------------------- /modules/av_stream/src/stream_recv.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "config.hpp" 4 | #include "stream_recv.hpp" 5 | 6 | StreamRecv::~StreamRecv() { 7 | cleanup(); 8 | } 9 | 10 | void StreamRecv::cleanup() { 11 | avformat_close_input(&pFormatCtx); 12 | avformat_free_context(pFormatCtx); 13 | av_dict_free(&format_opts); 14 | sws_freeContext(img_convert_ctx); 15 | } 16 | 17 | bool StreamRecv::open(std::string url) { 18 | int ret, got_picture; 19 | 20 | pFormatCtx = NULL; 21 | pCodecCtx = NULL; 22 | video_stream = NULL; 23 | format_opts = NULL; 24 | video_decoder = NULL; 25 | memset( &rgb_picture, 0, sizeof(rgb_picture) ); 26 | 27 | stream_url = url; 28 | av_register_all(); 29 | avformat_network_init(); 30 | av_log_set_level(AV_LOG_FATAL); 31 | av_log_set_flags(AV_LOG_PRINT_LEVEL); 32 | 33 | reopen_rtsp: 34 | if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) { 35 | av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE); 36 | } 37 | av_dict_set_int(&format_opts, "stimeout", 5 * 1000 * 1000, 0); // 5 second 38 | av_dict_set(&format_opts, "rtsp_transport", "tcp", 0); 39 | do { 40 | INFO_LOG("opening a connection."); 41 | ret = avformat_open_input(&pFormatCtx, stream_url.c_str(), NULL, &format_opts); 42 | if (ret < 0) { 43 | INFO_LOG("open connection %s failed, will try open later.", stream_url.c_str()); 44 | return false; 45 | } else { 46 | INFO_LOG("open connection %s success.", stream_url.c_str()); 47 | break; 48 | } 49 | } while(1); 50 | 51 | ret = avformat_find_stream_info(pFormatCtx, NULL); 52 | if (ret < 0) { 53 | INFO_LOG("call avformat_find_stream_info failed"); 54 | return false; 55 | } 56 | 57 | stream_index = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &video_decoder, 0); 58 | if (stream_index < 0) { 59 | INFO_LOG("find video stream failed"); 60 | return false; 61 | } 62 | 63 | av_dump_format(pFormatCtx, stream_index, stream_url.c_str(), 0); 64 | // video 65 | video_stream = pFormatCtx->streams[stream_index]; 66 | pCodecCtx = video_stream->codec; 67 | video_decoder = avcodec_find_decoder(pCodecCtx->codec_id); 68 | if(video_decoder == NULL){ 69 | INFO_LOG("Codec not found"); 70 | return false; 71 | } 72 | if(avcodec_open2(pCodecCtx, video_decoder, NULL)<0){ 73 | INFO_LOG("Could not open codec"); 74 | return false; 75 | } 76 | 77 | stream_fps = av_q2d(video_stream->avg_frame_rate); 78 | if (stream_fps == 0.0f) { 79 | stream_fps = av_q2d(video_stream->r_frame_rate); 80 | } 81 | 82 | if (stream_fps == 0.0f) { 83 | INFO_LOG("unable to get fps, use fps = 25"); 84 | stream_fps = 25.0f; 85 | } else { 86 | INFO_LOG("get fps = %f", stream_fps); 87 | } 88 | 89 | img_convert_ctx = sws_getContext( 90 | pCodecCtx->width, pCodecCtx->height, 91 | pCodecCtx->pix_fmt, 92 | pCodecCtx->width, pCodecCtx->height, 93 | AV_PIX_FMT_BGR24, 94 | SWS_BICUBIC, 95 | NULL, NULL, NULL); 96 | 97 | pFrame = av_frame_alloc(); 98 | pic_width = video_stream->codecpar->width; 99 | pic_height = video_stream->codecpar->height; 100 | INFO_LOG("picture w = %d, h = %d", pic_width, pic_height); 101 | 102 | int buffer_width = pic_width; 103 | int buffer_height = pic_height; 104 | int aligns[AV_NUM_DATA_POINTERS]; 105 | avcodec_align_dimensions2(video_stream->codec, &buffer_width, &buffer_height, aligns); 106 | INFO_LOG("buffer w = %d, h = %d", buffer_width, buffer_height); 107 | rgb_picture.data[0] = (uint8_t*)realloc(rgb_picture.data[0], 108 | av_image_get_buffer_size(AV_PIX_FMT_BGR24, 109 | buffer_width, 110 | buffer_height, 111 | 1)); 112 | av_image_fill_arrays( 113 | rgb_picture.data, 114 | rgb_picture.linesize, 115 | rgb_picture.data[0], 116 | AV_PIX_FMT_BGR24, 117 | buffer_width, 118 | buffer_height, 1); 119 | return true; 120 | } 121 | 122 | bool StreamRecv::grabFrame() { 123 | bool valid = false; 124 | auto pkt = ReadPacketFromSource(); 125 | if (!pkt) { 126 | return false; 127 | } 128 | p_pkt = pkt.get(); 129 | if (pkt.get()->stream_index == stream_index) { 130 | int got_picture = 0; 131 | avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, pkt.get()); 132 | if(got_picture) { 133 | valid = true; 134 | } else { 135 | valid = false; 136 | } 137 | } 138 | return valid; 139 | } 140 | 141 | IplImage* StreamRecv::retrieveFrame(int) { 142 | sws_scale( 143 | img_convert_ctx, 144 | pFrame->data, 145 | pFrame->linesize, 146 | 0, pCodecCtx->height, 147 | rgb_picture.data, 148 | rgb_picture.linesize 149 | ); 150 | cvInitImageHeader(&frame, cvSize(pic_width, pic_height), 8, 3); 151 | cvSetData(&frame, rgb_picture.data[0], rgb_picture.linesize[0]); 152 | return &frame; 153 | } 154 | 155 | std::shared_ptr StreamRecv::ReadPacketFromSource() { 156 | std::shared_ptr packet(static_cast(av_malloc(sizeof(AVPacket))), [&](AVPacket *p) { av_free_packet(p); av_freep(&p);}); 157 | av_init_packet(packet.get()); 158 | int ret = av_read_frame(pFormatCtx, packet.get()); 159 | if(ret >= 0) { 160 | return packet; 161 | } else { 162 | INFO_LOG("av_read_frame failed, exit!"); 163 | exit(0); 164 | // return NULL; 165 | } 166 | } -------------------------------------------------------------------------------- /modules/av_stream/src/stream_recv.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __STREAM_RECV_HPP__ 2 | #define __STREAM_RECV_HPP__ 3 | 4 | #include "precomp.hpp" 5 | #include 6 | #include 7 | 8 | extern "C" 9 | { 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | } 16 | 17 | class StreamRecv : public AVCapture { 18 | public: 19 | StreamRecv() {} 20 | virtual ~StreamRecv(); 21 | 22 | virtual bool open(std::string url); 23 | virtual bool grabFrame(); 24 | virtual IplImage* retrieveFrame(int); 25 | 26 | private: 27 | void cleanup(); 28 | std::shared_ptr ReadPacketFromSource(); 29 | 30 | AVFormatContext *pFormatCtx; 31 | AVCodecContext *pCodecCtx; 32 | AVStream *video_stream; 33 | AVDictionary *format_opts; 34 | AVCodec *video_decoder; 35 | AVFrame *pFrame; 36 | AVFrame rgb_picture; 37 | AVPacket *p_pkt; 38 | SwsContext *img_convert_ctx; 39 | int stream_index; 40 | 41 | float stream_fps; 42 | std::string stream_url; 43 | int pic_width; 44 | int pic_height; 45 | IplImage frame; 46 | }; 47 | 48 | #endif // __STREAM_RECV_HPP__ -------------------------------------------------------------------------------- /modules/dnn_inference/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(the_description "dnn inference module") 2 | 3 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}) 4 | SET(CMAKE_CXX_FLAGS "-O3 -g -Wall -Wno-unused-function -std=c++11") 5 | 6 | include_directories(${CMAKE_CURRENT_BINARY_DIR}) 7 | 8 | set(MODEL_SRCS config.cpp) 9 | ocv_glob_module_sources(${MODEL_SRCS}) 10 | ocv_define_module(dnn_innference opencv_imgproc WRAP python) -------------------------------------------------------------------------------- /modules/dnn_inference/include/opencv2/dnn_inference.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __OPENCV_DNN_INFERENCE_HPP__ 2 | #define __OPENCV_DNN_INFERENCE_HPP__ 3 | 4 | #include "opencv2/core.hpp" 5 | 6 | typedef struct DLContext DLContext; 7 | 8 | namespace cv { 9 | namespace dnn_inference { 10 | 11 | class CV_EXPORTS_W Model { 12 | public: 13 | CV_WRAP Model() {} 14 | virtual ~Model() {} 15 | 16 | CV_WRAP virtual bool open(const String& model_file); 17 | CV_WRAP void face_detect(InputArray src, CV_OUT std::vector& objects); 18 | CV_WRAP virtual bool landmark( 19 | InputArray src, std::vector& objects, OutputArray points); 20 | CV_WRAP virtual bool feature(InputArray src, OutputArray feature); 21 | }; 22 | 23 | } // dnn_inference 24 | } // cv 25 | 26 | #endif -------------------------------------------------------------------------------- /modules/dnn_inference/samples/test.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.insert(0, '/your/opencv-compile/lib/python3.7/site-packages') 3 | import cv2 4 | 5 | img = cv2.imread('test.jpg') 6 | model = cv2.dnn_inference_Model() 7 | model.open('test.model') 8 | rects = model.face_detect(img) 9 | ret, pts = model.landmark(img, rects.tolist()) -------------------------------------------------------------------------------- /modules/dnn_inference/src/benchmark_timer.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __BENCHMARK_TIMER_HPP__ 2 | #define __BENCHMARK_TIMER_HPP__ 3 | 4 | #include 5 | #include 6 | #include "config.hpp" 7 | 8 | enum class TimeUnit { 9 | milliseconds, microseconds 10 | }; 11 | 12 | template 13 | class BenchmarkTimer { 14 | public: 15 | explicit BenchmarkTimer(std::string tag) : begin{ 16 | std::chrono::steady_clock::now()}, mTag{std::move(tag)} { 17 | } 18 | 19 | explicit BenchmarkTimer() : 20 | begin{std::chrono::steady_clock::now()}, 21 | mTag{"Timer"} {} 22 | 23 | ~BenchmarkTimer() { 24 | if (!isStop) { 25 | stop(); 26 | } 27 | } 28 | 29 | void stop() { 30 | stopImpl(std::integral_constant{}); 31 | isStop = true; 32 | } 33 | 34 | private: 35 | 36 | void stopImpl(std::integral_constant) { 37 | std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); 38 | INFO_LOG("[ %s ] cost time = %lld ms", mTag.c_str(), 39 | std::chrono::duration_cast(end - begin).count()); 40 | } 41 | 42 | void stopImpl(std::integral_constant) { 43 | std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); 44 | INFO_LOG("[ %s ] cost time = %lld us", mTag.c_str(), 45 | std::chrono::duration_cast(end - begin).count()); 46 | } 47 | 48 | private: 49 | std::chrono::steady_clock::time_point begin; 50 | std::string mTag; 51 | bool isStop = false; 52 | }; 53 | 54 | #endif // __BENCHMARK_TIMER_HPP__ -------------------------------------------------------------------------------- /modules/dnn_inference/src/config.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "config.hpp" 5 | 6 | char * timeString() { 7 | struct timespec ts; 8 | clock_gettime(CLOCK_REALTIME, &ts); 9 | struct tm * timeinfo = localtime(&ts.tv_sec); 10 | static char timeStr[40]; 11 | sprintf(timeStr, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%.3ld+08:00", 12 | timeinfo->tm_year + 1900, 13 | timeinfo->tm_mon + 1, 14 | timeinfo->tm_mday, 15 | timeinfo->tm_hour, 16 | timeinfo->tm_min, 17 | timeinfo->tm_sec, 18 | ts.tv_nsec / 1000000); 19 | return timeStr; 20 | } 21 | -------------------------------------------------------------------------------- /modules/dnn_inference/src/config.hpp: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __CONFIG_HPP__ 3 | #define __CONFIG_HPP__ 4 | 5 | #include 6 | 7 | #define INFO_LOG(format, ...) \ 8 | do { \ 9 | fprintf(stderr, "[%s] " format "\n", \ 10 | timeString(), \ 11 | ##__VA_ARGS__); \ 12 | } while(0) 13 | 14 | char * timeString(); 15 | 16 | #endif // __CONFIG_HPP__ -------------------------------------------------------------------------------- /modules/dnn_inference/src/dnn_inference.cpp: -------------------------------------------------------------------------------- 1 | #include "precomp.hpp" 2 | #include "config.hpp" 3 | 4 | // #include 5 | 6 | namespace cv { 7 | namespace dnn_inference { 8 | 9 | bool Model::open(const String& model_file) { 10 | INFO_LOG("model file: %s", model_file.c_str()); 11 | return true; 12 | } 13 | 14 | bool Model::landmark(InputArray src, std::vector& objects, 15 | OutputArray points) { 16 | 17 | std::vector points_dim = {static_cast(objects.size()), 68, 3}; 18 | cv::Mat landmark_pts(points_dim, CV_32F); 19 | cv::Mat _src = src.getMat(); 20 | int batch_size = objects.size(); 21 | for (int i = 0; i < batch_size; i++) { 22 | int length = 68; 23 | for (int c = 0; c < length; ++c) { 24 | landmark_pts.at(i, c, 0) = 1.0f; 25 | landmark_pts.at(i, c, 1) = 2.0f; 26 | landmark_pts.at(i, c, 2) = 3.0f; 27 | } 28 | } 29 | landmark_pts.copyTo(points); 30 | return true; 31 | } 32 | 33 | void Model::face_detect(InputArray src, CV_OUT std::vector& objects) { 34 | int ret; 35 | cv::Mat _src = src.getMat(); 36 | int image_width = _src.cols; 37 | int image_height = _src.rows; 38 | 39 | objects.clear(); 40 | for (int i = 0; i < 2; i++) { 41 | objects.push_back({ 42 | static_cast(1), 43 | static_cast(2), 44 | static_cast(3), 45 | static_cast(4), 46 | }); 47 | } 48 | } 49 | 50 | } // dnn_inference 51 | } // cv -------------------------------------------------------------------------------- /modules/dnn_inference/src/precomp.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __OPENCV_DNN_INFERENCE_PRECOMP_HPP__ 2 | #define __OPENCV_DNN_INFERENCE_PRECOMP_HPP__ 3 | 4 | #include "../include/opencv2/dnn_inference.hpp" 5 | 6 | #include 7 | #include 8 | 9 | #endif --------------------------------------------------------------------------------