From d2c22861731b49394bf72fa577b41b2876ea160a Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 21 Sep 2024 22:03:11 -0400 Subject: [PATCH] Things. --- .gitignore | 5 ++++- README.md | 6 ++++++ data.yaml | 5 +++++ src/include/main.hpp | 1 + src/include/util.hpp | 9 +++++++++ src/include/yolo.hpp | 4 ++-- src/main.cpp | 13 +++++++------ src/util.cpp | 8 ++++++++ src/yolo.cpp | 15 +++++++++++++-- 9 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 README.md create mode 100644 data.yaml create mode 100644 src/include/util.hpp create mode 100644 src/util.cpp diff --git a/.gitignore b/.gitignore index 391bf78..15a3d65 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,10 @@ *.o obj/ +runs/ *.out .cache/ -dataset/ +testdir/ +datasets/ testimgs/ +yolov8*.pt compile_commands.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..a76c64f --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# imgboxer + +Creates YOLOv8 image labels, based on paired data under normal and fluorescent +conditions. + +Depends on OpenCV. diff --git a/data.yaml b/data.yaml new file mode 100644 index 0000000..f1dc187 --- /dev/null +++ b/data.yaml @@ -0,0 +1,5 @@ +train: images/train +val: images/val + +nc: 1 +names: ['tooth'] diff --git a/src/include/main.hpp b/src/include/main.hpp index ef68e3c..c920afb 100644 --- a/src/include/main.hpp +++ b/src/include/main.hpp @@ -5,6 +5,7 @@ #include +#include "util.hpp" #include "img.hpp" #include "preprocess.hpp" #include "bound.hpp" diff --git a/src/include/util.hpp b/src/include/util.hpp new file mode 100644 index 0000000..8fb2558 --- /dev/null +++ b/src/include/util.hpp @@ -0,0 +1,9 @@ +#ifndef UTIL_H +#define UTIL_H + +#include + +// Re-extension a file name. +std::string reext(const std::string& fname); + +#endif diff --git a/src/include/yolo.hpp b/src/include/yolo.hpp index dc57323..ba295a8 100644 --- a/src/include/yolo.hpp +++ b/src/include/yolo.hpp @@ -3,8 +3,8 @@ // Create the YOLOv8 image labels. -#include #include +#include #include @@ -22,7 +22,7 @@ typedef struct Label { label_t* yolo_mklabel(cv::Rect box); // Write labels to file from cv::Rect. -void yolo_write_labels(char* fname, std::vector boxes); +void yolo_write_labels(std::string fname, std::vector boxes); #endif diff --git a/src/main.cpp b/src/main.cpp index 2b3550c..f4f5853 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ using namespace cv; int main(int argc, char** argv) { // Check for invalid invocation. if (argc != 2) { - printf("%s IMAGE", argv[0]); + printf("%s IMAGE\n", argv[0]); return -1; } @@ -27,14 +27,15 @@ int main(int argc, char** argv) { for (size_t i = 0; i < contours.size(); i++) { rectangle(img, boxes[i], Scalar(255, 255, 255), 1); - drawContours(img, contours, (int)i, Scalar(255, 255, 255), 1, LINE_8); - } + drawContours(img, contours, (int)i, Scalar(255, 255, 255), 1, LINE_8); } - yolo_write_labels("asdf", boxes); + std::string label_fname = reext(argv[1]); + + yolo_write_labels(label_fname, boxes); // Show image with bounding boxes. - imshow("Bounding Boxes", img); - waitKey(0); + // imshow("Bounding Boxes", img); + // waitKey(0); return 0; } diff --git a/src/util.cpp b/src/util.cpp new file mode 100644 index 0000000..323ee29 --- /dev/null +++ b/src/util.cpp @@ -0,0 +1,8 @@ +#include "include/util.hpp" + +std::string reext(const std::string& fname) { + size_t dot_pos = fname.rfind('.'); + if (dot_pos == std::string::npos) return fname + ".txt"; + + return fname.substr(0, dot_pos) + ".txt"; +} diff --git a/src/yolo.cpp b/src/yolo.cpp index 5c4e27e..867212f 100644 --- a/src/yolo.cpp +++ b/src/yolo.cpp @@ -13,9 +13,20 @@ label_t* yolo_mklabel(cv::Rect box) { return label; } -void yolo_write_labels(char* fname, std::vector boxes) { +void yolo_write_labels(std::string fname, std::vector boxes) { + std::ofstream f; + f.open(fname); + if (!f) { + std::cerr << "Could not open file for writing" << std::endl; + exit(1); + } + for (size_t i = 0; i < boxes.size(); i++) { auto box = boxes[i]; - printf("%s 1 %d %d %d %d\n", LABEL_NAME, box.x, box.y, box.x + box.width, box.y + box.height); + f << LABEL_NAME << " 1 " << box.x << " " << box.y << " " + << box.x + box.width << " " << box.y + box.height << std::endl; + std::cout << LABEL_NAME << " 1 " << box.x << " " << box.y << " " + << box.x + box.width << " " << box.y + box.height + << std::endl; } }