Skip to content

Commit

Permalink
[Streamer] Build python wheel and python API (#1702)
Browse files Browse the repository at this point in the history
* add setup.py

* fix setup.py

* fix setup.py

* fix setup.py

* fix setup.py

* fix setup.py

* fix setup.py

* fix setup.py

* fix setup.py

* fix setup.py

* add scripts

* fix setup.py

* fix setup.py

* fix setup.py

* fix setup.py

* add pybind interface

* update CMakeLists

* fix CMakeLists

* fix setup.py

* fix setup.py

* fix setup

* fix CMakeLists

* fix CMakeLists

* fix CMakeLists

* fix pybind

* fix pybind

* fix pybind

* fix pybind

* fix CMakeLists

* fix CMakeLists

* fix CMakeLists

* fix CMakeLists

* fix CMakeLists

* fix CMakeLists

* fix CMakeLists

* fix CMakeLists.txt

* fix CMakeLists.txt

* fix CMakeLists

* fix CMakeLists

* fix CMakeLists

* fix process_libraries

* test setup

* fix setup

* fix py wheel build

* add ppyoloe py streamer

* delete annotation, fix form

---------

Co-authored-by: Wang Xinyu <[email protected]>
  • Loading branch information
CoolKbh and wang-xinyu authored Mar 30, 2023
1 parent c04a3a4 commit f4a7008
Show file tree
Hide file tree
Showing 13 changed files with 632 additions and 10 deletions.
51 changes: 50 additions & 1 deletion streamer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
PROJECT(fd_streamer C CXX)
CMAKE_MINIMUM_REQUIRED (VERSION 3.10)

if(NOT PY_LIBRARY_NAME)
set(PY_LIBRARY_NAME "fastdeploy_streamer_main")
endif()

option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
option(ENABLE_DEEPSTREAM "Enable NVIDIA DeepStream SDK" ON)

file(GLOB_RECURSE DEPLOY_PYBIND_SRCS ${PROJECT_SOURCE_DIR}/src/pybind/*.cc ${PROJECT_SOURCE_DIR}/src/*_pybind.cc)
file(GLOB_RECURSE ALL_STREAMER_SRCS ${PROJECT_SOURCE_DIR}/src/*.cc)
file(GLOB_RECURSE DEEPSTREAM_SRCS ${PROJECT_SOURCE_DIR}/src/deepstream/*.cc)
file(GLOB_RECURSE PLUGIN_SRCS ${PROJECT_SOURCE_DIR}/src/gstreamer/plugin/*.cc)
list(REMOVE_ITEM ALL_STREAMER_SRCS ${DEEPSTREAM_SRCS})
list(REMOVE_ITEM ALL_STREAMER_SRCS ${DEPLOY_PYBIND_SRCS})
list(REMOVE_ITEM ALL_STREAMER_SRCS ${PLUGIN_SRCS})

set(DEPEND_LIBS "")
Expand All @@ -34,6 +40,7 @@ list(APPEND DEPEND_LIBS ${GST_LIBRARIES})
include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)
include_directories(${FASTDEPLOY_INCS})
include_directories(${PROJECT_SOURCE_DIR}/src/)
#include_directories(${PROJECT_SOURCE_DIR}/../third_party/pybind11/include/)

if(ENABLE_DEEPSTREAM)
add_definitions(-DENABLE_DEEPSTREAM)
Expand All @@ -48,9 +55,51 @@ endif()
# If we link multiple yaml-cpp libs, strange error will occur.
list(APPEND DEPEND_LIBS yaml-cpp)

add_library(fd_streamer SHARED ${ALL_STREAMER_SRCS})
add_library(fd_streamer SHARED ${ALL_STREAMER_SRCS} ${ALL_DEPLOY_SRCS})
target_link_libraries(fd_streamer ${FASTDEPLOY_LIBS} ${DEPEND_LIBS})

############################### Building: FastDeploy Python Wheel #############################
if(BUILD_FDSTREAMER_PYTHON)
add_definitions(-DBUILD_FDSTREAMER_PYTHON)
if("${PY_EXT_SUFFIX}" STREQUAL "")
set(PY_EXT_SUFFIX ".so")
endif()

# find_package Python has replaced PythonInterp and PythonLibs since cmake 3.12
# Use the following command in the future; now this is only compatible with the latest pybind11
# find_package(Python ${PY_VERSION} COMPONENTS Interpreter Development REQUIRED)
find_package(PythonInterp ${PY_VERSION} REQUIRED)
find_package(PythonLibs ${PY_VERSION})
if(CMAKE_SYSTEM_NAME STREQUAL "AIX")
set(CMAKE_NO_SYSTEM_FROM_IMPORTED 1)
endif()

message(STATUS "pybind srcs: ${DEPLOY_PYBIND_SRCS}")

add_library(${PY_LIBRARY_NAME} MODULE ${DEPLOY_PYBIND_SRCS})
redefine_file_macro(${PY_LIBRARY_NAME})
set_target_properties(${PY_LIBRARY_NAME} PROPERTIES PREFIX "")
set_target_properties(${PY_LIBRARY_NAME}
PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
set_target_properties(${PY_LIBRARY_NAME} PROPERTIES SUFFIX ${PY_EXT_SUFFIX})
set_target_properties(${PY_LIBRARY_NAME}
PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
target_include_directories(${PY_LIBRARY_NAME} PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>
${PYTHON_INCLUDE_DIR})

target_include_directories(${PY_LIBRARY_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/../third_party/pybind11/include)
# target_include_directories(${PY_LIBRARY_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/../third_party/dlpack/include)

target_link_libraries(${PY_LIBRARY_NAME} PUBLIC fd_streamer)

file(REMOVE_RECURSE ${PROJECT_SOURCE_DIR}/python/streamer/libs)
file(MAKE_DIRECTORY ${PROJECT_SOURCE_DIR}/python/streamer/libs)

add_custom_target(copy_fd_libraries ALL COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/*.so* ${PROJECT_SOURCE_DIR}/python/streamer/libs/ DEPENDS ${PY_LIBRARY_NAME})
endif(BUILD_FDSTREAMER_PYTHON)

add_subdirectory(src/gstreamer/meta)
add_subdirectory(src/gstreamer/plugin/fdinfer)
add_subdirectory(src/gstreamer/plugin/fdtracker)
6 changes: 6 additions & 0 deletions streamer/examples/ppyoloe/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from streamer.fd_streamer import FDStreamer

st = FDStreamer('streamer_cfg.yml')

st.Run()

Empty file.
72 changes: 72 additions & 0 deletions streamer/python/scripts/process_libraries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# 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.

import os
import sys
import shutil
import subprocess
import platform

user_specified_dirs = ['@OPENCV_DIRECTORY@', '@ORT_DIRECTORY@', ]

def process_on_linux(current_dir):
rpaths = ["$ORIGIN:$ORIGIN/libs"]
fd_libs = list()
libs_path = os.path.join(current_dir, "streamer", "libs")
for f in os.listdir(libs_path):
filename = os.path.join(libs_path, f)
if not os.path.isfile(filename):
continue
if f.count("fastdeploy") and f.count(".so") > 0:
fd_libs.append(filename)

cmake_build_dir = os.path.join(current_dir, ".setuptools-cmake-build")

for lib in fd_libs:
command = "{} --set-rpath '{}' {}".format(patchelf_bin_path, ":".join(rpaths), lib)
if platform.machine() != 'sw_64' and platform.machine() != 'mips64':
assert subprocess.Popen(
command,
shell=True) != 0, "patchelf {} failed, the command: {}".format(
command, lib)


def get_all_files(dirname):
files = list()
for root, dirs, filenames in os.walk(dirname):
for f in filenames:
fullname = os.path.join(root, f)
files.append(fullname)
return files


def process_libraries(current_dir):
if platform.system().lower() == "linux":
process_on_linux(current_dir)
elif platform.system().lower() == "darwin":
process_on_mac(current_dir)
elif platform.system().lower() == "windows":
process_on_windows(current_dir)

all_files = get_all_files(os.path.join(current_dir, "streamer", "libs"))
package_data = list()

filters = [".vcxproj", ".png", ".java", ".h", ".cc", ".cpp", ".hpp"]
for f in all_files:
remain = True

if remain:
package_data.append(
os.path.relpath(f, os.path.join(current_dir, "streamer")))
return package_data
Loading

0 comments on commit f4a7008

Please sign in to comment.