From 1688582d119e21a4d852222100b42b9f650edb89 Mon Sep 17 00:00:00 2001 From: BohaoWu Date: Tue, 13 Oct 2020 11:03:28 +0000 Subject: [PATCH] Check for complier. --- core/preprocess/CMakeLists.txt | 1 + .../include/ExtractFrameJpeg.h | 2 +- .../pybind/pybind_frame_extract.cpp | 2 +- .../nvdec-extractframe/src/main.cpp | 88 ------------------- .../reader/test_preprocess.py | 26 +++++- 5 files changed, 28 insertions(+), 91 deletions(-) delete mode 100644 core/preprocess/nvdec-extractframe/src/main.cpp diff --git a/core/preprocess/CMakeLists.txt b/core/preprocess/CMakeLists.txt index 89a2502d..52738ad2 100644 --- a/core/preprocess/CMakeLists.txt +++ b/core/preprocess/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(hwvideoframe) +add_subdirectory(nvdec-extractframe) diff --git a/core/preprocess/nvdec-extractframe/include/ExtractFrameJpeg.h b/core/preprocess/nvdec-extractframe/include/ExtractFrameJpeg.h index 31c98444..6543bf09 100644 --- a/core/preprocess/nvdec-extractframe/include/ExtractFrameJpeg.h +++ b/core/preprocess/nvdec-extractframe/include/ExtractFrameJpeg.h @@ -82,7 +82,7 @@ class ExtractFrameJpeg : public ExtractFrameBase { int jpeg_encode(uint8_t* p_image, int width, int height, - const FrameResult& result); + FrameResult &result); nvjpegHandle_t _nv_jpeg_handler; nvjpegEncoderState_t _nv_enc_state; nvjpegEncoderParams_t _nv_enc_params; diff --git a/core/preprocess/nvdec-extractframe/pybind/pybind_frame_extract.cpp b/core/preprocess/nvdec-extractframe/pybind/pybind_frame_extract.cpp index 80bdd9e2..59555c40 100644 --- a/core/preprocess/nvdec-extractframe/pybind/pybind_frame_extract.cpp +++ b/core/preprocess/nvdec-extractframe/pybind/pybind_frame_extract.cpp @@ -20,7 +20,7 @@ simplelogger::Logger* logger = simplelogger::LoggerFactory::CreateConsoleLogger(); -PYBIND11_MODULE(hwextract, m) { +PYBIND11_MODULE(libhwextract, m) { pybind11::class_(m, "HwExtractFrameJpeg") .def(pybind11::init()) .def("init_handler", &baidu::xvision::ExtractFrameJpeg::init) diff --git a/core/preprocess/nvdec-extractframe/src/main.cpp b/core/preprocess/nvdec-extractframe/src/main.cpp deleted file mode 100644 index c960c9d5..00000000 --- a/core/preprocess/nvdec-extractframe/src/main.cpp +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include -#include -#include -#include -#include "ExtractFrameBGRARaw.h" -#include "ExtractFrameJpeg.h" - -simplelogger::Logger* g_logger = - simplelogger::LoggerFactory::CreateConsoleLogger(); -/** - * @Name: - * image_file_writer - * @Feature: - * write image data to file - * @params - * img_data: image_data - * file_path: image_file stored path - * prefix: image_name prefix - * extension: image_file extension name - * @returns - * void - **/ -void inline image_file_writer(const baidu::xvision::FrameResult& img_data, - std::string file_path, - std::string prefix, - std::string extension = "raw") { - std::ofstream f_out(file_path + "/" + prefix + "." + extension, - std::ios::binary | std::ios::out); - f_out.write(reinterpret_cast(img_data.get_frame()), img_data.len()); - f_out.close(); -} -bool parse_cmd_line(int argc, const char* const argv[]) { - if (argc <= 3) { - LOG(FATAL) << "params error, eg: ./hw_frame_extract /path/to/mp4.mp4 " - "/output/path bgra|jpeg"; - return false; - } - if (!strcmp(argv[3], "bgra") && !strcmp(argv[3], "jpeg")) { - LOG(FATAL) << "unsupported output file format"; - return false; - } - return true; -} -int main(int argc, char* argv[]) { - if (!parse_cmd_line(argc, argv)) { - return -1; - } - - baidu::xvision::ExtractFrameBase* extract_frame_handler(nullptr); - if (strcmp("bgra", argv[3]) == 0) { - extract_frame_handler = new baidu::xvision::ExtractFrameBGRARaw(); - } else { - extract_frame_handler = new baidu::xvision::ExtractFrameJpeg(); - } - auto init_result = extract_frame_handler->init(); - auto result = extract_frame_handler->extract_frame(argv[1], 1, 200); - int frame_index = 0; - std::stringstream ss; - for (auto result_iter = result.begin(); result_iter != result.end(); - result_iter++) { - ss << std::setw(5) << std::setfill('0') << frame_index; - image_file_writer(*result_iter, - argv[2], - "image_" + std::to_string(result_iter->width()) + "_" + - std::to_string(result_iter->height()) + "_" + - ss.str(), - argv[3]); - result_iter->free_memory(); - frame_index++; - ss.str(""); - } - return 0; -} diff --git a/python/paddle_serving_app/reader/test_preprocess.py b/python/paddle_serving_app/reader/test_preprocess.py index f407b586..a487cc05 100644 --- a/python/paddle_serving_app/reader/test_preprocess.py +++ b/python/paddle_serving_app/reader/test_preprocess.py @@ -16,9 +16,10 @@ import unittest import sys import numpy as np +import cv2 from paddle_serving_app.reader import Sequential, Resize, File2Image import libgpupreprocess as pp - +import libhwextract class TestOperators(unittest.TestCase): """ @@ -147,6 +148,29 @@ class TestOperators(unittest.TestCase): img_resize_diff = img_vis - img self.assertEqual(np.all(img_resize_diff == 0), True) + def test_extract_frame(self): + handler = libhwextract.HwExtractFrameJpeg(0) + # 0, gpu card index + # if you want BGRA Raw Data, plz use HwExtractBGRARaw + handler.init_handler() + # init once can decode many videos + video_file_name = sys.argv[1] + # for now just support h264 codec + frame_list = [] + try: + frame_list = handler.extract_frame(video_file_name, 1) + # specifiy file name and fps you want to extract, 0 for all frame + except Exception as e_frame: + print("Failed to cutframe, exception[%s]" % (e_frame)) + sys.exit(1) + for item in frame_list: + print "i am a item in frame_list" + # do something, for instance + jpeg_array = np.array(item, copy=False) + img = cv2.imdecode(jpeg_array, cv2.IMREAD_COLOR) + cv2.imwrite('1.jpg', img) + item.free_memory() + # have to release memor if __name__ == '__main__': unittest.main() -- GitLab