Skip to content

Commit

Permalink
Merge pull request #31 from jasvinderkhurana/main
Browse files Browse the repository at this point in the history
Adding Vitis accelerated resize example
  • Loading branch information
vmayoral authored Dec 13, 2024
2 parents b63b1d8 + 1ad52af commit cb7c556
Show file tree
Hide file tree
Showing 9 changed files with 716 additions and 0 deletions.
80 changes: 80 additions & 0 deletions vitis_accelerated_examples/vitis_accelerated_resize/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
cmake_minimum_required(VERSION 3.5)
project(vitis_accelerated_resize)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic -Wno-unknown-pragmas)
endif()

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(std_msgs REQUIRED)
find_package(cv_bridge REQUIRED)
find_package(image_transport REQUIRED)
find_package(Threads REQUIRED)
find_package(ament_vitis)

# add_executable(accelerated_vadd_new
# src/host_new.cpp)
# target_include_directories(accelerated_vadd_new PUBLIC include)
# target_link_libraries(accelerated_vadd_new
# ${OpenCL_LIBRARY}
# pthread)

find_package(vitis_common REQUIRED)
find_package(OpenCL REQUIRED)

# accelerated_vadd
add_executable(vitis_accelerated_resize
src/resize_fpga.cpp
src/main.cpp
)
target_include_directories(vitis_accelerated_resize PUBLIC
include
$ENV{XILINX_HLS}/common/technology/autopilot
$ENV{XILINX_HLS}/include
)

target_link_libraries(vitis_accelerated_resize
${OpenCL_LIBRARY}
pthread
)
ament_target_dependencies(vitis_accelerated_resize
rclcpp
rclcpp_components
std_msgs
cv_bridge
image_transport
vitis_common )

if (ROS_VITIS)
# resize kernel
vitis_acceleration_kernel(
NAME resize_accel
FILE src/xf_resize_accel.cpp
CONFIG cfg/kr260.cfg
INCLUDE
include
${CMAKE_INSTALL_PREFIX}/include
TYPE
# sw_emu
# hw_emu
hw
LINK
PACKAGE
)

endif() # ROS_VITIS

install(TARGETS
vitis_accelerated_resize
# accelerated_vadd_new
DESTINATION lib/${PROJECT_NAME}
)

ament_package()
13 changes: 13 additions & 0 deletions vitis_accelerated_examples/vitis_accelerated_resize/cfg/kr260.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
platform=kv260_custom_platform
save-temps=1
debug=1

[advanced]
param=compiler.skipTimingCheckAndFrequencyScaling=1

# Enable profiling of data ports
[profile]
data=all:all:all

# [hls]
# clock=300000000:vadd_accelerated
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
____ ____
/ /\/ /
/___/ \ / Copyright (c) 2024, AMD®.
\ \ \/ Author: Jasvinder Khurana <[email protected]>
\ \
/ / 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.
*/


#ifndef MINIMAL_IMAGE_PUBLISHER_HPP_
#define MINIMAL_IMAGE_PUBLISHER_HPP_



#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/image.hpp"
#include "std_msgs/msg/header.hpp"
#include <chrono>
#include <cv_bridge/cv_bridge.h> // cv_bridge converts between ROS 2 image messages and OpenCV image representations.
#include <image_transport/image_transport.hpp> // Using image_transport allows us to publish and subscribe to compressed image streams in ROS2
#include <opencv2/opencv.hpp> // We include everything about OpenCV as we don't care much about compilation time at the moment.

using namespace std::chrono_literals;

class MinimalImagePublisher : public rclcpp::Node {
public:
MinimalImagePublisher() : Node("opencv_image_publisher"), count_(0) {
publisher_ =
this->create_publisher<sensor_msgs::msg::Image>("random_image", 10);
timer_ = this->create_wall_timer(
500ms, std::bind(&MinimalImagePublisher::timer_callback, this));
}

private:
void timer_callback() {
// Create a new 640x480 image
cv::Mat my_image(cv::Size(640, 480), CV_8UC3);

// Generate an image where each pixel is a random color
cv::randu(my_image, cv::Scalar(0, 0, 0), cv::Scalar(255, 255, 255));

// Write message to be sent. Member function toImageMsg() converts a CvImage
// into a ROS image message
msg_ = cv_bridge::CvImage(std_msgs::msg::Header(), "bgr8", my_image)
.toImageMsg();

// Publish the image to the topic defined in the publisher
publisher_->publish(*msg_.get());
RCLCPP_INFO(this->get_logger(), "Image %ld published", count_);
count_++;
}
rclcpp::TimerBase::SharedPtr timer_;
sensor_msgs::msg::Image::SharedPtr msg_;
rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr publisher_;
size_t count_;
};

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
____ ____
/ /\/ /
/___/ \ / Copyright (c) 2024, AMD®.
\ \ \/ Author: Jasvinder Khurana <[email protected]>
\ \
/ / 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.
*/

#ifndef IMAGE_PROC_RESIZE_FPGA_HPP_
#define IMAGE_PROC_RESIZE_FPGA_HPP_

#include <rclcpp/rclcpp.hpp>
#include <image_transport/image_transport.hpp>
#include <ament_index_cpp/get_resource.hpp>
#include <cv_bridge/cv_bridge.h>

#include <cstring>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#include <thread>

#include <vitis_common/common/ros_opencl_120.hpp>


class AcceleratedResize : public rclcpp::Node
{
public:
AcceleratedResize(const rclcpp::NodeOptions& options);

protected:

int interpolation_;
bool use_scale_;
bool profile_;
double scale_height_;
double scale_width_;
int height_;
int width_;

cl::Kernel* krnl_;
cl::Context* context_;
cl::CommandQueue* queue_;

std::mutex connect_mutex_;

rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr publisher_;
rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr subscriber_;
// image_transport::CameraPublisher pub_image_;
// image_transport::CameraSubscriber sub_image_;

void connectCb();
size_t get_msg_size(sensor_msgs::msg::Image::ConstSharedPtr image_msg);
size_t get_msg_size(sensor_msgs::msg::CameraInfo::ConstSharedPtr info_msg);


void imageCb(const sensor_msgs::msg::Image::ConstSharedPtr & image_msg);

// sensor_msgs::msg::Image::ConstSharedPtr image_msg,
// sensor_msgs::msg::CameraInfo::ConstSharedPtr info_msg);

private:

cv::Mat result_hls;
cv_bridge::CvImage output_image;
cv_bridge::CvImagePtr cv_ptr;
void InitKernel();
void ExecuteKernel(bool gray, int src_width, int src_height, int dst_width, int dst_height);
};


#endif // IMAGE_PROC_RESIZE_FPGA_HPP_
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2019 Xilinx, Inc.
*
* 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.
*/

#ifndef _XF_RESIZE_CONFIG_
#define _XF_RESIZE_CONFIG_

#include "hls_stream.h"
#include "ap_int.h"
#include <vitis_common/common/xf_common.hpp>
#include <vitis_common/imgproc/xf_resize.hpp>

/* resize kernel configuration */

#define RO 0 // Resource Optimized (8-pixel implementation)
#define NO 1 // Normal Operation (1-pixel implementation)

// port widths
#define INPUT_PTR_WIDTH 128
#define OUTPUT_PTR_WIDTH 128

// For Nearest Neighbor & Bilinear Interpolation, max down scale factor 2 for all 1-pixel modes, and for upscale in x
// direction
#define MAXDOWNSCALE 2

#define RGB 1
#define GRAY 0
/* Interpolation type*/
#define INTERPOLATION 1
// 0 - Nearest Neighbor Interpolation
// 1 - Bilinear Interpolation
// 2 - AREA Interpolation

/* Input image Dimensions */
#define WIDTH 640 // Maximum Input image width
#define HEIGHT 480 // Maximum Input image height

/* Output image Dimensions */
#define NEWWIDTH 1280 // Maximum output image width
#define NEWHEIGHT 960 // Maximum output image height

/* Interface types */
#if RO

#if RGB
#define NPC_T XF_NPPC4
#else
#define NPC_T XF_NPPC8
#endif

#else
#define NPC_T XF_NPPC1
#endif

#if RGB
#define TYPE XF_8UC3
#define CH_TYPE XF_RGB
#else
#define TYPE XF_8UC1
#define CH_TYPE XF_GRAY
#endif

#endif
45 changes: 45 additions & 0 deletions vitis_accelerated_examples/vitis_accelerated_resize/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="2">
<name>vitis_accelerated_resize</name>
<version>0.1.0</version>
<description>An example to showcase how to integrate Vitis Vision Library with KRS.
Here in this example we are showcasing the integration of resize functions.
</description>

<maintainer email="[email protected]">Jasvinder Khurana</maintainer>
<license>Apache License 2.0</license>
<author email="[email protected]">Jasvinder Khurana</author>

<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>ament_vitis</buildtool_depend>

<build_depend>acceleration_firmware_kv260</build_depend>
<build_depend>vitis_common</build_depend>
<build_depend>rclcpp</build_depend>

<buildtool_depend>ament_cmake_auto</buildtool_depend>
<buildtool_depend>ament_vitis</buildtool_depend>
<buildtool_depend>ament_cuda</buildtool_depend>

<depend>cv_bridge</depend>
<depend>image_geometry</depend>
<depend>image_transport</depend>
<depend>rclcpp_components</depend>
<depend>rcutils</depend>
<depend>sensor_msgs</depend>
<depend>tracetools_image_pipeline</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>


<exec_depend>acceleration_firmware_kv260</exec_depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Loading

0 comments on commit cb7c556

Please sign in to comment.