Removed IplImage completely

pull/3029/head
AlexeyAB 6 years ago
parent 8c970498a2
commit 099b71d1de
  1. 2
      README.md
  2. 29
      include/yolo_v2_class.hpp
  3. 49
      src/image_opencv.cpp

@ -44,7 +44,7 @@ More details: http://pjreddie.com/darknet/yolo/
* Windows or Linux * Windows or Linux
* **CMake >= 3.8** for modern CUDA support: https://cmake.org/download/ * **CMake >= 3.8** for modern CUDA support: https://cmake.org/download/
* **CUDA 10.0**: https://developer.nvidia.com/cuda-toolkit-archive (on Linux do [Post-installation Actions](https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#post-installation-actions)) * **CUDA 10.0**: https://developer.nvidia.com/cuda-toolkit-archive (on Linux do [Post-installation Actions](https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#post-installation-actions))
* **OpenCV > 2.4**: use your preferred package manager (brew, apt), build from source using [vcpkg](https://github.com/Microsoft/vcpkg) or download from [OpenCV official site](https://opencv.org/releases.html) (on Windows set system variable `OpenCV_DIR` = `C:\opencv\build` - where are the `include` and `x64` folders [image](https://user-images.githubusercontent.com/4096485/53249516-5130f480-36c9-11e9-8238-a6e82e48c6f2.png)) * **OpenCV >= 2.4**: use your preferred package manager (brew, apt), build from source using [vcpkg](https://github.com/Microsoft/vcpkg) or download from [OpenCV official site](https://opencv.org/releases.html) (on Windows set system variable `OpenCV_DIR` = `C:\opencv\build` - where are the `include` and `x64` folders [image](https://user-images.githubusercontent.com/4096485/53249516-5130f480-36c9-11e9-8238-a6e82e48c6f2.png))
* **cuDNN >= 7.0 for CUDA 10.0** https://developer.nvidia.com/rdp/cudnn-archive (on **Linux** copy `cudnn.h`,`libcudnn.so`... as desribed here https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installlinux-tar , on **Windows** copy `cudnn.h`,`cudnn64_7.dll`, `cudnn64_7.lib` as desribed here https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installwindows ) * **cuDNN >= 7.0 for CUDA 10.0** https://developer.nvidia.com/rdp/cudnn-archive (on **Linux** copy `cudnn.h`,`libcudnn.so`... as desribed here https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installlinux-tar , on **Windows** copy `cudnn.h`,`cudnn64_7.dll`, `cudnn64_7.lib` as desribed here https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installwindows )
* **GPU with CC >= 3.0**: https://en.wikipedia.org/wiki/CUDA#GPUs_supported * **GPU with CC >= 3.0**: https://en.wikipedia.org/wiki/CUDA#GPUs_supported
* on Linux **GCC or Clang**, on Windows **MSVC 2015/2017/2019** https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community * on Linux **GCC or Clang**, on Windows **MSVC 2015/2017/2019** https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community

@ -132,33 +132,28 @@ public:
else if (img_src.channels() == 1) cv::cvtColor(img_src, img, cv::COLOR_GRAY2BGR); else if (img_src.channels() == 1) cv::cvtColor(img_src, img, cv::COLOR_GRAY2BGR);
else std::cerr << " Warning: img_src.channels() is not 1, 3 or 4. It is = " << img_src.channels() << std::endl; else std::cerr << " Warning: img_src.channels() is not 1, 3 or 4. It is = " << img_src.channels() << std::endl;
std::shared_ptr<image_t> image_ptr(new image_t, [](image_t *img) { free_image(*img); delete img; }); std::shared_ptr<image_t> image_ptr(new image_t, [](image_t *img) { free_image(*img); delete img; });
std::shared_ptr<IplImage> ipl_small = std::make_shared<IplImage>(img); *image_ptr = mat_to_image_custom(img);
*image_ptr = ipl_to_image(ipl_small.get());
return image_ptr; return image_ptr;
} }
private: private:
static image_t ipl_to_image(IplImage* src) static image_t mat_to_image_custom(cv::Mat mat)
{ {
unsigned char *data = (unsigned char *)src->imageData; int w = mat.cols;
int h = src->height; int h = mat.rows;
int w = src->width; int c = mat.channels();
int c = src->nChannels; image_t im = make_image_custom(w, h, c);
int step = src->widthStep; unsigned char *data = (unsigned char *)mat.data;
image_t out = make_image_custom(w, h, c); int step = mat.step;
int count = 0; for (int y = 0; y < h; ++y) {
for (int k = 0; k < c; ++k) { for (int k = 0; k < c; ++k) {
for (int i = 0; i < h; ++i) { for (int x = 0; x < w; ++x) {
int i_step = i*step; im.data[k*w*h + y*w + x] = data[y*step + x*c + k] / 255.0f;
for (int j = 0; j < w; ++j) {
out.data[count++] = data[i_step + j*c + k] / 255.;
} }
} }
} }
return im;
return out;
} }
static image_t make_empty_image(int w, int h, int c) static image_t make_empty_image(int w, int h, int c)

@ -86,8 +86,8 @@ extern "C" {
// ==================================================================== // ====================================================================
image mat_to_image(cv::Mat mat); image mat_to_image(cv::Mat mat);
cv::Mat image_to_mat(image img); cv::Mat image_to_mat(image img);
image ipl_to_image(mat_cv* src); // image ipl_to_image(mat_cv* src);
mat_cv *image_to_ipl(image img); // mat_cv *image_to_ipl(image img);
// cv::Mat ipl_to_mat(IplImage *ipl); // cv::Mat ipl_to_mat(IplImage *ipl);
// IplImage *mat_to_ipl(cv::Mat mat); // IplImage *mat_to_ipl(cv::Mat mat);
@ -850,6 +850,7 @@ void save_cv_jpg(mat_cv *img_src, const char *name)
// ==================================================================== // ====================================================================
void draw_detections_cv_v3(mat_cv* mat, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output) void draw_detections_cv_v3(mat_cv* mat, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output)
{ {
try {
cv::Mat *show_img = mat; cv::Mat *show_img = mat;
int i, j; int i, j;
if (!show_img) return; if (!show_img) return;
@ -866,7 +867,7 @@ void draw_detections_cv_v3(mat_cv* mat, detection *dets, int num, float thresh,
strcat(labelstr, names[j]); strcat(labelstr, names[j]);
class_id = j; class_id = j;
char buff[10]; char buff[10];
sprintf(buff, " (%2.0f%%)", dets[i].prob[j]*100); sprintf(buff, " (%2.0f%%)", dets[i].prob[j] * 100);
strcat(labelstr, buff); strcat(labelstr, buff);
} }
else { else {
@ -933,7 +934,7 @@ void draw_detections_cv_v3(mat_cv* mat, detection *dets, int num, float thresh,
pt_text.x = left; pt_text.x = left;
pt_text.y = top - 4;// 12; pt_text.y = top - 4;// 12;
pt_text_bg1.x = left; pt_text_bg1.x = left;
pt_text_bg1.y = top - (1 + 18 * font_size); pt_text_bg1.y = top - (3 + 18 * font_size);
pt_text_bg2.x = right; pt_text_bg2.x = right;
if ((right - left) < text_size.width) pt_text_bg2.x = left + text_size.width; if ((right - left) < text_size.width) pt_text_bg2.x = left + text_size.width;
pt_text_bg2.y = top; pt_text_bg2.y = top;
@ -968,14 +969,18 @@ void draw_detections_cv_v3(mat_cv* mat, detection *dets, int num, float thresh,
cv::rectangle(*show_img, pt_text_bg1, pt_text_bg2, color, width, 8, 0); cv::rectangle(*show_img, pt_text_bg1, pt_text_bg2, color, width, 8, 0);
cv::rectangle(*show_img, pt_text_bg1, pt_text_bg2, color, CV_FILLED, 8, 0); // filled cv::rectangle(*show_img, pt_text_bg1, pt_text_bg2, color, CV_FILLED, 8, 0); // filled
cv::Scalar black_color = CV_RGB(0,0,0); cv::Scalar black_color = CV_RGB(0, 0, 0);
cv::putText(*show_img, labelstr, pt_text, cv::FONT_HERSHEY_COMPLEX_SMALL, font_size, black_color, 2*font_size, CV_AA); cv::putText(*show_img, labelstr, pt_text, cv::FONT_HERSHEY_COMPLEX_SMALL, font_size, black_color, 2 * font_size, CV_AA);
// cv::FONT_HERSHEY_COMPLEX_SMALL, cv::FONT_HERSHEY_SIMPLEX // cv::FONT_HERSHEY_COMPLEX_SMALL, cv::FONT_HERSHEY_SIMPLEX
} }
} }
if (ext_output) { if (ext_output) {
fflush(stdout); fflush(stdout);
} }
}
catch (...) {
cerr << "OpenCV exception: draw_detections_cv_v3() \n";
}
} }
// ---------------------------------------- // ----------------------------------------
@ -984,22 +989,23 @@ void draw_detections_cv_v3(mat_cv* mat, detection *dets, int num, float thresh,
// ==================================================================== // ====================================================================
mat_cv* draw_train_chart(float max_img_loss, int max_batches, int number_of_lines, int img_size, int dont_show) mat_cv* draw_train_chart(float max_img_loss, int max_batches, int number_of_lines, int img_size, int dont_show)
{ {
int img_offset = 50; int img_offset = 60;
int draw_size = img_size - img_offset; int draw_size = img_size - img_offset;
cv::Mat *img_ptr = new cv::Mat(img_size, img_size, CV_8UC3, CV_RGB(255, 255, 255)); cv::Mat *img_ptr = new cv::Mat(img_size, img_size, CV_8UC3, CV_RGB(255, 255, 255));
cv::Mat &img = *img_ptr; cv::Mat &img = *img_ptr;
cv::Point pt1, pt2, pt_text; cv::Point pt1, pt2, pt_text;
try {
char char_buff[100]; char char_buff[100];
int i; int i;
// vertical lines // vertical lines
pt1.x = img_offset; pt2.x = img_size, pt_text.x = 10; pt1.x = img_offset; pt2.x = img_size, pt_text.x = 30;
for (i = 1; i <= number_of_lines; ++i) { for (i = 1; i <= number_of_lines; ++i) {
pt1.y = pt2.y = (float)i * draw_size / number_of_lines; pt1.y = pt2.y = (float)i * draw_size / number_of_lines;
cv::line(img, pt1, pt2, CV_RGB(224, 224, 224), 1, 8, 0); cv::line(img, pt1, pt2, CV_RGB(224, 224, 224), 1, 8, 0);
if (i % 10 == 0) { if (i % 10 == 0) {
sprintf(char_buff, "%2.1f", max_img_loss*(number_of_lines - i) / number_of_lines); sprintf(char_buff, "%2.1f", max_img_loss*(number_of_lines - i) / number_of_lines);
pt_text.y = pt1.y + 5; pt_text.y = pt1.y + 3;
cv::putText(img, char_buff, pt_text, cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(0, 0, 0), 1, CV_AA); cv::putText(img, char_buff, pt_text, cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(0, 0, 0), 1, CV_AA);
cv::line(img, pt1, pt2, CV_RGB(128, 128, 128), 1, 8, 0); cv::line(img, pt1, pt2, CV_RGB(128, 128, 128), 1, 8, 0);
@ -1018,7 +1024,7 @@ mat_cv* draw_train_chart(float max_img_loss, int max_batches, int number_of_line
} }
} }
cv::putText(img, "Loss", cv::Point(0, 35), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(0, 0, 0), 1, CV_AA); cv::putText(img, "Loss", cv::Point(10, 55), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(0, 0, 255), 1, CV_AA);
cv::putText(img, "Iteration number", cv::Point(draw_size / 2, img_size - 10), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(0, 0, 0), 1, CV_AA); cv::putText(img, "Iteration number", cv::Point(draw_size / 2, img_size - 10), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(0, 0, 0), 1, CV_AA);
char max_batches_buff[100]; char max_batches_buff[100];
sprintf(max_batches_buff, "in cfg max_batches=%d", max_batches); sprintf(max_batches_buff, "in cfg max_batches=%d", max_batches);
@ -1032,6 +1038,10 @@ mat_cv* draw_train_chart(float max_img_loss, int max_batches, int number_of_line
cv::imshow("average loss", img); cv::imshow("average loss", img);
cv::waitKey(20); cv::waitKey(20);
} }
}
catch (...) {
cerr << "OpenCV exception: draw_train_chart() \n";
}
return (mat_cv*)img_ptr; return (mat_cv*)img_ptr;
} }
// ---------------------------------------- // ----------------------------------------
@ -1039,8 +1049,9 @@ mat_cv* draw_train_chart(float max_img_loss, int max_batches, int number_of_line
void draw_train_loss(mat_cv* img_src, int img_size, float avg_loss, float max_img_loss, int current_batch, int max_batches, void draw_train_loss(mat_cv* img_src, int img_size, float avg_loss, float max_img_loss, int current_batch, int max_batches,
float precision, int draw_precision, char *accuracy_name, int dont_show, int mjpeg_port) float precision, int draw_precision, char *accuracy_name, int dont_show, int mjpeg_port)
{ {
try {
cv::Mat &img = *(cv::Mat*)img_src; cv::Mat &img = *(cv::Mat*)img_src;
int img_offset = 50; int img_offset = 60;
int draw_size = img_size - img_offset; int draw_size = img_size - img_offset;
char char_buff[100]; char char_buff[100];
cv::Point pt1, pt2; cv::Point pt1, pt2;
@ -1055,18 +1066,20 @@ void draw_train_loss(mat_cv* img_src, int img_size, float avg_loss, float max_im
static int iteration_old = 0; static int iteration_old = 0;
static int text_iteration_old = 0; static int text_iteration_old = 0;
if (iteration_old == 0) if (iteration_old == 0)
cv::putText(img, accuracy_name, cv::Point(0, 12), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(255, 0, 0), 1, CV_AA); cv::putText(img, accuracy_name, cv::Point(10, 12), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(255, 0, 0), 1, CV_AA);
cv::line(img, cv::line(img,
cv::Point(img_offset + draw_size * (float)iteration_old / max_batches, draw_size * (1 - old_precision)), cv::Point(img_offset + draw_size * (float)iteration_old / max_batches, draw_size * (1 - old_precision)),
cv::Point(img_offset + draw_size * (float)current_batch / max_batches, draw_size * (1 - precision)), cv::Point(img_offset + draw_size * (float)current_batch / max_batches, draw_size * (1 - precision)),
CV_RGB(255, 0, 0), 1, 8, 0); CV_RGB(255, 0, 0), 1, 8, 0);
sprintf(char_buff, "%2.0f%% ", precision * 100);
cv::putText(img, char_buff, cv::Point(10, 28), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(255, 255, 255), 5, CV_AA);
cv::putText(img, char_buff, cv::Point(10, 28), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(200, 0, 0), 1, CV_AA);
if (((int)(old_precision * 10) != (int)(precision * 10)) || (current_batch - text_iteration_old) >= max_batches / 10) { if (((int)(old_precision * 10) != (int)(precision * 10)) || (current_batch - text_iteration_old) >= max_batches / 10) {
text_iteration_old = current_batch; text_iteration_old = current_batch;
sprintf(char_buff, "%2.0f%% ", precision * 100);
cv::putText(img, char_buff, cv::Point(pt1.x - 30, draw_size * (1 - precision) + 15), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(255, 255, 255), 5, CV_AA); cv::putText(img, char_buff, cv::Point(pt1.x - 30, draw_size * (1 - precision) + 15), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(255, 255, 255), 5, CV_AA);
cv::putText(img, char_buff, cv::Point(pt1.x - 30, draw_size * (1 - precision) + 15), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(200, 0, 0), 1, CV_AA); cv::putText(img, char_buff, cv::Point(pt1.x - 30, draw_size * (1 - precision) + 15), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(200, 0, 0), 1, CV_AA);
} }
old_precision = precision; old_precision = precision;
@ -1074,11 +1087,11 @@ void draw_train_loss(mat_cv* img_src, int img_size, float avg_loss, float max_im
} }
sprintf(char_buff, "current avg loss = %2.4f iteration = %d", avg_loss, current_batch); sprintf(char_buff, "current avg loss = %2.4f iteration = %d", avg_loss, current_batch);
pt1.x = 55, pt1.y = 10; pt1.x = 15, pt1.y = draw_size + 18;
pt2.x = pt1.x + 460, pt2.y = pt1.y + 20; pt2.x = pt1.x + 460, pt2.y = pt1.y + 20;
cv::rectangle(img, pt1, pt2, CV_RGB(255, 255, 255), CV_FILLED, 8, 0); cv::rectangle(img, pt1, pt2, CV_RGB(255, 255, 255), CV_FILLED, 8, 0);
pt1.y += 15; pt1.y += 15;
cv::putText(img, char_buff, pt1, cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(0, 0, 0), 1, CV_AA); cv::putText(img, char_buff, pt1, cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(0, 0, 100), 1, CV_AA);
int k = 0; int k = 0;
if (!dont_show) { if (!dont_show) {
@ -1093,6 +1106,10 @@ void draw_train_loss(mat_cv* img_src, int img_size, float avg_loss, float max_im
cv::putText(img, "- Saved", cv::Point(260, img_size - 10), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(255, 255, 255), 1, CV_AA); cv::putText(img, "- Saved", cv::Point(260, img_size - 10), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.7, CV_RGB(255, 255, 255), 1, CV_AA);
if (mjpeg_port > 0) send_mjpeg((mat_cv *)&img, mjpeg_port, 500000, 100); if (mjpeg_port > 0) send_mjpeg((mat_cv *)&img, mjpeg_port, 500000, 100);
}
catch (...) {
cerr << "OpenCV exception: draw_train_loss() \n";
}
} }
// ---------------------------------------- // ----------------------------------------

Loading…
Cancel
Save