Jorge Lamperez
Published © Apache-2.0

Karp, Petalinux and Vitis-AI 2.0

This project explains how to install Vitis-AI 2.0 in Petalinux for Kria Autonomous Robotic Platform (Karp).

IntermediateProtip6 hours1,143
Karp, Petalinux and Vitis-AI 2.0

Things used in this project

Hardware components

Kria KV260 Vision AI Starter Kit
AMD Kria KV260 Vision AI Starter Kit
×1
11.6inch HDMI LCD Monitor
×1
Webcam, Logitech® HD Pro
Webcam, Logitech® HD Pro
×1

Software apps and online services

Vitis Unified Software Platform
AMD Vitis Unified Software Platform
PetaLinux
AMD PetaLinux

Story

Read more

Code

builddrm-sh

SH
Script for building the application
#
# 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.
#

result=0 && pkg-config --list-all | grep opencv4 && result=1
if [ $result -eq 1 ]; then
	OPENCV_FLAGS=$(pkg-config --cflags --libs-only-L opencv4)
else
	OPENCV_FLAGS=$(pkg-config --cflags --libs-only-L opencv)
fi

CXX=${CXX:-"g++ --sysroot=/"}
$CXX -std=c++17 -O2 -I=/usr/include/drm/ -o karp_yolov4_drm karp_yolov4.cpp -lglog -lvitis_ai_library-yolov3 -lvitis_ai_library-dpu_task -lvitis_ai_library-xnnpp -ldrm -lpthread -DUSE_DRM=1 ${OPENCV_FLAGS} -lopencv_core -lopencv_video -lopencv_videoio -lopencv_imgproc -lopencv_imgcodecs -lopencv_highgui

karp_yolov4.cpp

C/C++
Yolov4 DRM example
/*
 * Copyright 2022 Jorge Lamperez.
 *
 * 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 <glog/logging.h>

#include <eigen3/Eigen/Dense>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <vitis/ai/demo_video.hpp>
#include <vitis/ai/nnpp/yolov3.hpp>
#include <vitis/ai/yolov3.hpp>

static cv::Scalar getColor(int label) {
  int c[3];
  for (int i = 1, j = 0; i <= 9; i *= 3, j++) {
    c[j] = ((label / i) % 3) * 127;
  }
  return cv::Scalar(c[2], c[1], c[0]);
}

static cv::Mat process_result(
  cv::Mat &m1, const vitis::ai::YOLOv3Result &result, bool is_jpeg) {
  LOG_IF(INFO, true) << "process_result_yolo";
  cv::Mat image;
  cv::resize(m1, image, cv::Size{960, 1080});

  for (const auto bbox : result.bboxes) {
    int label = bbox.label;
    float xmin = bbox.x * image.cols ;
    float ymin = bbox.y * image.rows ;
    float xmax = xmin + bbox.width * image.cols;
    float ymax = ymin + bbox.height * image.rows;
    float confidence = bbox.score;
    if (xmax > image.cols) xmax = image.cols;
    if (ymax > image.rows) ymax = image.rows;
    LOG_IF(INFO, is_jpeg) << "RESULT: " << label << "\t" << xmin << "\t" << ymin
                          << "\t" << xmax << "\t" << ymax << "\t" << confidence
                          << "\n";
    cv::rectangle(image, cv::Point(xmin, ymin), cv::Point(xmax, ymax),
                  getColor(label), 3, 1, 0);
  }
  cv::Mat canvas(cv::Size(image.cols, image.rows), CV_8UC3);
  image.copyTo(canvas(cv::Rect(0, 0, image.cols, image.rows)));
  return canvas;
}

int main(int argc, char* argv[]) {
  gui_layout() = {{0, 0, 960, 1080}, {960, 0, 960, 1080}};
  gui_background() = cv::imread("/etc/alternatives/background.jpg");
  return vitis::ai::main_for_video_demo_multiple_channel(
      argc, argv,
      {[] {
         return vitis::ai::create_dpu_filter(
             [] { return vitis::ai::YOLOv3::create("yolov4_leaky_spp_m_pruned_0_36"); },
             process_result);
       },
       [] {
         return vitis::ai::create_dpu_filter(
             [] { return vitis::ai::YOLOv3::create("yolov4_leaky_spp_m"); },
             process_result);
       }
      });
}

Credits

Jorge Lamperez

Jorge Lamperez

7 projects • 24 followers

Comments