Skip to content

Commit

Permalink
Remove OpenCV as a hard dependency for scannerpy
Browse files Browse the repository at this point in the history
  • Loading branch information
fpoms committed Apr 16, 2019
1 parent 1d94850 commit 537dfac
Show file tree
Hide file tree
Showing 12 changed files with 8,131 additions and 485 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ add_library(scanner SHARED
$<TARGET_OBJECTS:util>
scanner/util/halide_context.cpp
${PROTO_SRCS}
${GRPC_PROTO_SRCS}
${STRUCK_SOURCES}
${OFDIS_SOURCES})
${GRPC_PROTO_SRCS})

target_link_libraries(scanner PUBLIC
${SCANNER_LIBRARIES}
Expand All @@ -158,10 +156,12 @@ endif()

if (BUILD_DOCS)
set(DOXYGEN_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/doxygen)
set(DOXYGEN_EXCLUDE_PATTERNS json.hpp concurrentqueue.h nvcuvid.h cuviddec.h tinyformat.h blockingconcurrentqueue.h thread_pool.h)
set(DOXYGEN_EXCLUDE_PATTERNS json.hpp concurrentqueue.h nvcuvid.h cuviddec.h
tinyformat.h blockingconcurrentqueue.h thread_pool.h)
find_package(Doxygen REQUIRED)
set(DOXYGEN_PROJECT_NAME "Scanner C++ API")
doxygen_add_docs(doxygen ${PROJECT_SOURCE_DIR}/scanner)
endif()

message("${GRPC_VERSION}")
file(WRITE ${CMAKE_BINARY_DIR}/grpc_version.txt "${GRPC_VERSION}")
12 changes: 0 additions & 12 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ if (BUILD_CUDA)
add_library(scanner_halide scanner/util/halide_context.cpp)
endif()

set(OPENCV_DESIRED_COMPONENTS core highgui imgproc)
if (BUILD_CUDA)
list(APPEND OPENCV_DESIRED_COMPONENTS cudaimgproc cudaarithm)
endif()

if (NO_FFMPEG STREQUAL "false")
find_package(FFmpeg REQUIRED)
include_directories("${FFMPEG_INCLUDE_DIR}")
Expand Down Expand Up @@ -71,7 +66,6 @@ find_package(Storehouse REQUIRED CONFIG
PATHS "${CMAKE_SOURCE_DIR}/thirdparty/install"
"${STOREHOUSE_DIR}")
find_package(TinyToml REQUIRED)
find_package(OpenCV COMPONENTS ${OPENCV_DESIRED_COMPONENTS})

set(PYBIND11_PYTHON_VERSION 3)
find_package(pybind11 REQUIRED)
Expand Down Expand Up @@ -118,12 +112,6 @@ include_directories(
"${PYTHON_INCLUDE_DIRS}"
"${pybind11_INCLUDE_DIR}")

if (OpenCV_FOUND)
list(APPEND SCANNER_LIBRARIES ${OpenCV_LIBRARIES})
include_directories(${OpenCV_INCLUDE_DIRS})
add_definitions(-DHAVE_OPENCV)
endif()

if (BUILD_TESTS)
include_directories("${GTEST_INCLUDE_DIRS}")
endif()
Expand Down
7 changes: 5 additions & 2 deletions deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ echo "(customized by specifying (--prefix <dir>)."

set -- "${POSITIONAL[@]}" # restore positional parameters

if command -v brew info >/dev/null 2>&1; then
if command -v brew >/dev/null 2>&1; then
HAS_BREW=true
else
HAS_BREW=false
Expand Down Expand Up @@ -553,6 +553,9 @@ if [[ $INSTALL_OPENCV == true ]] && [[ ! -f $BUILD_DIR/opencv.done ]]; then
-D BUILD_opencv_cnn_3dobj=OFF \
-D BUILD_opencv_cudacodec=OFF \
-D BUILD_opencv_xfeatures2d=OFF \
-D WITH_PROTOBUF=OFF \
-D BUILD_PROTOBUF=OFF \
-D BUILD_opencv_dnn=OFF \
-D OPENCV_EXTRA_MODULES_PATH=$BUILD_DIR/opencv_contrib/modules \
$(echo $CMDS) -DCMAKE_PREFIX_PATH=$(echo $PY_EXTRA_CMDS) \
.. && \
Expand Down Expand Up @@ -641,7 +644,7 @@ if [[ $INSTALL_PYBIND == true ]] && [[ ! -f $BUILD_DIR/pybind.done ]] ; then
echo "Installing pybind..."
cd $BUILD_DIR
rm -fr pybind11
git clone -b v2.2.2 https://github.com/pybind/pybind11 --depth 1 && \
git clone -b v2.2.4 https://github.com/pybind/pybind11 --depth 1 && \
cd pybind11 && \
mkdir build && cd build && \
cmake .. -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX -DPYBIND11_TEST=Off -DCMAKE_BUILD_TYPE=RelWithDebInfo && \
Expand Down
6 changes: 3 additions & 3 deletions python/scannerpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ def compatible_version(v1, v2):
if has_brew and has_grpc_brew and has_grpc_python:
data = json.loads(subprocess.check_output('brew info --json grpc', shell=True))
brew_version = data[0]['linked_keg']
output = subprocess.check_output('pip3 list | grep grpcio', shell=True)
python_version = [x for x in output.decode('utf-8').split(' ') if len(x) > 0][1]
if not compatible_version(built_grpc_version, brew_version):
if brew_version and not compatible_version(built_grpc_version, brew_version):
print(('Warning: Scanner was built with GRPC version {:s}, '
'but the version installed via brew is {:s}. '
'Please reinstall Scanner to fix this issue.').format(
built_grpc_version, brew_version))
output = subprocess.check_output('pip3 list | grep grpcio', shell=True)
python_version = [x for x in output.decode('utf-8').split(' ') if len(x) > 0][1]
if not compatible_version(built_grpc_version, python_version):
print(('Warning: Scanner was built with GRPC version {:s}, '
'but the version installed via python is {:s}. '
Expand Down
5 changes: 1 addition & 4 deletions scanner/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@ set(SOURCE_FILES
fs.cpp
bbox.cpp
glog.cpp
lodepng.cpp
image_encoder.cpp)

if (NO_FFMPEG STREQUAL "false")
list(APPEND SOURCE_FILES
ffmpeg.cpp)
endif()

if (OpenCV_FOUND)
list(APPEND SOURCE_FILES opencv.cpp)
endif()

add_library(util OBJECT
${SOURCE_FILES})

Expand Down
91 changes: 70 additions & 21 deletions scanner/util/image_encoder.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
#include "scanner/api/kernel.h"
#include "scanner/api/op.h"
#include "scanner/util/memory.h"
#include "scanner/util/opencv.h"
#include "scanner/types.pb.h"
#include "scanner/util/lodepng.h"

namespace scanner {
namespace {
int frame_info_to_lct(const FrameInfo& info, LodePNGColorType& type) {
if (info.channels() == 1) {
type = LCT_GREY;
} else if (info.channels() == 2) {
type = LCT_GREY_ALPHA;
} else if (info.channels() == 3) {
type = LCT_RGB;
} else if (info.channels() == 4) {
type = LCT_RGBA;
} else {
return 1;
}
return 0;
}
int frame_info_to_bitdepth(const FrameInfo& info, unsigned& bitdepth) {
switch (info.type) {
case FrameType::U8: {
bitdepth = 8;
break;
}
case FrameType::U16: {
bitdepth = 16;
break;
}
default: { return 1; }
}
return 0;
}

int encode_png(const Frame* frame, std::vector<unsigned char>& encoded_image) {
unsigned char* png;
LodePNGColorType type;
FrameInfo info = frame->as_frame_info();
if (frame_info_to_lct(info, type) != 0) {
return 1;
}
unsigned bitdepth;
if (frame_info_to_bitdepth(info, bitdepth) != 0) {
return 2;
}
{
u32 error = lodepng::encode(encoded_image, frame->data, frame->width(),
frame->height(), type, bitdepth);
if (error != 0) {
return 3;
}
}
return 0;
}
}

class ImageEncoderKernel : public BatchedKernel, public VideoKernel {
public:
Expand All @@ -16,6 +67,12 @@ class ImageEncoderKernel : public BatchedKernel, public VideoKernel {
return;
}
image_type_ = args.format() == "" ? "png" : args.format();
if (!(image_type_ == "png" || image_type_ == "PNG")) {
RESULT_ERROR(&valid_,
"Invalid format type specified to "
"ImageEncoder: %s. Valid types are: png.",
image_type_.c_str());
}
valid_.set_success(true);
}

Expand All @@ -29,29 +86,21 @@ class ImageEncoderKernel : public BatchedKernel, public VideoKernel {
auto& frame_col = input_columns[0];
check_frame(CPU_DEVICE, frame_col[0]);

std::vector<i32> encode_params;
if (image_type_ == "jpg" || image_type_ == "jpeg" || image_type_ == "JPG" ||
image_type_ == "jpeg") {
encode_params.push_back(cv::IMWRITE_JPEG_QUALITY);
encode_params.push_back(100);
}

i32 input_count = num_rows(frame_col);
for (i32 i = 0; i < input_count; ++i) {
cv::Mat img = frame_to_mat(frame_col[i].as_const_frame());
std::vector<u8> buf;
cv::Mat recolored;
if (img.channels() == 3) {
cv::cvtColor(img, recolored, cv::COLOR_RGB2BGR);
} else {
recolored = img;
const Frame* frame = frame_col[i].as_const_frame();
std::vector<unsigned char> encoded_image;
int error = encode_png(frame, encoded_image);
if (error == 1) {
RESULT_ERROR(&valid_, "Invalid frame type for ImageEncoder.");
} else if (error == 2) {
RESULT_ERROR(&valid_, "Invalid frame bitdepth for ImageEncoder.");
} else if (error != 0) {
RESULT_ERROR(&valid_, "Failed to encode image to PNG in ImageEncoder.");
}
bool success =
cv::imencode("." + image_type_, recolored, buf, encode_params);
LOG_IF(FATAL, !success) << "Failed to encode image";
u8* output_buf = new_buffer(CPU_DEVICE, buf.size());
std::memcpy(output_buf, buf.data(), buf.size());
insert_element(output_columns[0], output_buf, buf.size());
u8* output_buf = new_buffer(CPU_DEVICE, encoded_image.size());
std::memcpy(output_buf, encoded_image.data(), encoded_image.size());
insert_element(output_columns[0], output_buf, encoded_image.size());
}
}

Expand Down
Loading

0 comments on commit 537dfac

Please sign in to comment.