Compare commits

...

2 Commits

Author SHA1 Message Date
triple-mu f8a01a5da9
update jetson det code 1 day ago
triplemu de9be938fa update jetson det code 1 day ago
  1. 30
      csrc/jetson/detect/include/common.hpp
  2. 6075
      csrc/jetson/detect/include/filesystem.hpp
  3. 21
      csrc/jetson/detect/include/yolov8.hpp
  4. 32
      csrc/jetson/detect/main.cpp

@ -5,9 +5,8 @@
#ifndef JETSON_DETECT_COMMON_HPP #ifndef JETSON_DETECT_COMMON_HPP
#define JETSON_DETECT_COMMON_HPP #define JETSON_DETECT_COMMON_HPP
#include "NvInfer.h" #include "NvInfer.h"
#include "filesystem.hpp"
#include "opencv2/opencv.hpp" #include "opencv2/opencv.hpp"
#include <sys/stat.h>
#include <unistd.h>
#define CHECK(call) \ #define CHECK(call) \
do { \ do { \
@ -89,33 +88,6 @@ inline static float clamp(float val, float min, float max)
return val > min ? (val < max ? val : max) : min; return val > min ? (val < max ? val : max) : min;
} }
inline bool IsPathExist(const std::string& path)
{
if (access(path.c_str(), 0) == F_OK) {
return true;
}
return false;
}
inline bool IsFile(const std::string& path)
{
if (!IsPathExist(path)) {
printf("%s:%d %s not exist\n", __FILE__, __LINE__, path.c_str());
return false;
}
struct stat buffer;
return (stat(path.c_str(), &buffer) == 0 && S_ISREG(buffer.st_mode));
}
inline bool IsFolder(const std::string& path)
{
if (!IsPathExist(path)) {
return false;
}
struct stat buffer;
return (stat(path.c_str(), &buffer) == 0 && S_ISDIR(buffer.st_mode));
}
namespace det { namespace det {
struct Binding { struct Binding {
size_t size = 1; size_t size = 1;

File diff suppressed because it is too large Load Diff

@ -172,7 +172,19 @@ void YOLOv8::letterbox(const cv::Mat& image, cv::Mat& out, cv::Size& size)
cv::copyMakeBorder(tmp, tmp, top, bottom, left, right, cv::BORDER_CONSTANT, {114, 114, 114}); cv::copyMakeBorder(tmp, tmp, top, bottom, left, right, cv::BORDER_CONSTANT, {114, 114, 114});
cv::dnn::blobFromImage(tmp, out, 1 / 255.f, cv::Size(), cv::Scalar(0, 0, 0), true, false, CV_32F); out.create({1, 3, (int)inp_h, (int)inp_w}, CV_32F);
std::vector<cv::Mat> channels;
cv::split(tmp, channels);
cv::Mat c0((int)inp_h, (int)inp_w, CV_32F, (float*)out.data);
cv::Mat c1((int)inp_h, (int)inp_w, CV_32F, (float*)out.data + (int)inp_h * (int)inp_w);
cv::Mat c2((int)inp_h, (int)inp_w, CV_32F, (float*)out.data + (int)inp_h * (int)inp_w * 2);
channels[0].convertTo(c2, CV_32F, 1 / 255.f);
channels[1].convertTo(c1, CV_32F, 1 / 255.f);
channels[2].convertTo(c0, CV_32F, 1 / 255.f);
this->pparam.ratio = 1 / r; this->pparam.ratio = 1 / r;
this->pparam.dw = dw; this->pparam.dw = dw;
this->pparam.dh = dh; this->pparam.dh = dh;
@ -185,8 +197,8 @@ void YOLOv8::copy_from_Mat(const cv::Mat& image)
{ {
cv::Mat nchw; cv::Mat nchw;
auto& in_binding = this->input_bindings[0]; auto& in_binding = this->input_bindings[0];
auto width = in_binding.dims.d[3]; int width = in_binding.dims.d[3];
auto height = in_binding.dims.d[2]; int height = in_binding.dims.d[2];
cv::Size size{width, height}; cv::Size size{width, height};
this->letterbox(image, nchw, size); this->letterbox(image, nchw, size);
@ -272,8 +284,9 @@ void YOLOv8::draw_objects(const cv::Mat& image,
int x = (int)obj.rect.x; int x = (int)obj.rect.x;
int y = (int)obj.rect.y + 1; int y = (int)obj.rect.y + 1;
if (y > res.rows) if (y > res.rows) {
y = res.rows; y = res.rows;
}
cv::rectangle(res, cv::Rect(x, y, label_size.width, label_size.height + baseLine), {0, 0, 255}, -1); cv::rectangle(res, cv::Rect(x, y, label_size.width, label_size.height + baseLine), {0, 0, 255}, -1);

@ -5,6 +5,8 @@
#include "yolov8.hpp" #include "yolov8.hpp"
#include <chrono> #include <chrono>
namespace fs = ghc::filesystem;
const std::vector<std::string> CLASS_NAMES = { const std::vector<std::string> CLASS_NAMES = {
"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train",
"truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench",
@ -37,24 +39,30 @@ const std::vector<std::vector<unsigned int>> COLORS = {
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (argc != 3) {
fprintf(stderr, "Usage: %s [engine_path] [image_path/image_dir/video_path]\n", argv[0]);
return -1;
}
// cuda:0
cudaSetDevice(0);
const std::string engine_file_path{argv[1]}; const std::string engine_file_path{argv[1]};
const std::string path{argv[2]}; const fs::path path{argv[2]};
std::vector<std::string> imagePathList; std::vector<std::string> imagePathList;
bool isVideo{false}; bool isVideo{false};
assert(argc == 3);
auto yolov8 = new YOLOv8(engine_file_path); auto yolov8 = new YOLOv8(engine_file_path);
yolov8->make_pipe(true); yolov8->make_pipe(true);
if (IsFile(path)) { if (fs::exists(path)) {
std::string suffix = path.substr(path.find_last_of('.') + 1); std::string suffix = path.extension();
if (suffix == "jpg" || suffix == "jpeg" || suffix == "png") { if (suffix == ".jpg" || suffix == ".jpeg" || suffix == ".png") {
imagePathList.push_back(path); imagePathList.push_back(path);
} }
else if (suffix == "mp4" || suffix == "avi" || suffix == "m4v" || suffix == "mpeg" || suffix == "mov" else if (suffix == ".mp4" || suffix == ".avi" || suffix == ".m4v" || suffix == ".mpeg" || suffix == ".mov"
|| suffix == "mkv") { || suffix == ".mkv") {
isVideo = true; isVideo = true;
} }
else { else {
@ -62,8 +70,8 @@ int main(int argc, char** argv)
std::abort(); std::abort();
} }
} }
else if (IsFolder(path)) { else if (fs::is_directory(path)) {
cv::glob(path + "/*.jpg", imagePathList); cv::glob(path.string() + "/*.jpg", imagePathList);
} }
cv::Mat res, image; cv::Mat res, image;
@ -96,9 +104,9 @@ int main(int argc, char** argv)
} }
} }
else { else {
for (auto& path : imagePathList) { for (auto& p : imagePathList) {
objs.clear(); objs.clear();
image = cv::imread(path); image = cv::imread(p);
yolov8->copy_from_Mat(image, size); yolov8->copy_from_Mat(image, size);
auto start = std::chrono::system_clock::now(); auto start = std::chrono::system_clock::now();
yolov8->infer(); yolov8->infer();

Loading…
Cancel
Save