File indexing completed on 2025-01-05 03:58:07
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2010-06-16 0007 * Description : Face pre-processing CLI tool 0008 * 0009 * SPDX-FileCopyrightText: 2010 by Aditya Bhatt <adityabhatt1991 at gmail dot com> 0010 * 0011 * SPDX-License-Identifier: GPL-2.0-or-later 0012 * 0013 * ============================================================ */ 0014 0015 // Qt includes 0016 0017 #include <QApplication> 0018 #include <QDir> 0019 #include <QLabel> 0020 #include <QImage> 0021 #include <QElapsedTimer> 0022 0023 // Local includes 0024 0025 #include "digikam_debug.h" 0026 #include "qtopencv.h" 0027 #include "tantriggspreprocessor.h" 0028 0029 using namespace Digikam; 0030 0031 // -------------------------------------------------------------------------------------------------- 0032 0033 QStringList toPaths(char** const argv, int startIndex, int argc) 0034 { 0035 QStringList files; 0036 0037 for (int i = startIndex ; i < argc ; ++i) 0038 { 0039 files << QString::fromLocal8Bit(argv[i]); 0040 } 0041 0042 return files; 0043 } 0044 0045 QList<cv::Mat> toImages(const QStringList& paths) 0046 { 0047 QList<cv::Mat> images; 0048 0049 Q_FOREACH (const QString& path, paths) 0050 { 0051 QByteArray s = path.toLocal8Bit(); 0052 images << cv::imread(std::string(s.data()), 0053 0054 #if OPENCV_TEST_VERSION(3,99,0) 0055 0056 CV_LOAD_IMAGE_GRAYSCALE 0057 0058 #else 0059 0060 cv::IMREAD_GRAYSCALE 0061 0062 #endif 0063 0064 ); 0065 } 0066 0067 return images; 0068 } 0069 0070 // -------------------------------------------------------------------------------------------------- 0071 0072 class Q_DECL_HIDDEN OpenCVSideBySideDisplay 0073 { 0074 public: 0075 0076 explicit OpenCVSideBySideDisplay(int rows, int uiSize = 200) 0077 : bigImage(cv::Mat::zeros(uiSize*rows, 2*uiSize, CV_8UC3)), 0078 uiSize(uiSize), 0079 currentRow(0) 0080 { 0081 } 0082 0083 void add(const cv::Mat& left, const cv::Mat& right) 0084 { 0085 // Draw images side-by-side for later display 0086 0087 QSize size(left.cols, left.rows); 0088 size.scale(uiSize, uiSize, Qt::KeepAspectRatio); 0089 cv::Size scaleSize(size.width(), size.height()); 0090 0091 const int top = currentRow * uiSize; 0092 cv::Mat scaledLeft; 0093 cv::Mat scaledRight; 0094 cv::resize(left, scaledLeft, scaleSize); 0095 cv::resize(right, scaledRight, scaleSize); 0096 0097 if (scaledLeft.channels() == 1) 0098 { 0099 cv::cvtColor(scaledLeft, scaledLeft, CV_GRAY2BGR); 0100 } 0101 0102 if (scaledRight.channels() == 1) 0103 { 0104 cv::cvtColor(scaledRight, scaledRight, CV_GRAY2BGR); 0105 } 0106 0107 scaledLeft.copyTo(bigImage.colRange(0, scaledLeft.cols).rowRange(top, top + scaledLeft.rows)); 0108 scaledRight.copyTo(bigImage.colRange(uiSize, uiSize + scaledRight.cols).rowRange(top, top + scaledRight.rows)); 0109 0110 ++currentRow; 0111 } 0112 0113 void show() 0114 { 0115 QLabel label; 0116 label.setPixmap(QtOpenCV::cvMatToQPixmap(bigImage)); 0117 label.show(); 0118 } 0119 0120 public: 0121 0122 cv::Mat bigImage; 0123 const int uiSize; 0124 int currentRow; 0125 }; 0126 0127 // -------------------------------------------------------------------------------------------------- 0128 0129 int main(int argc, char** argv) 0130 { 0131 if (argc < 2) 0132 { 0133 qCDebug(DIGIKAM_TESTS_LOG) << "Bad Arguments!!!\nUsage: " << argv[0] << " preprocess <image1> <image2> ... "; 0134 return 0; 0135 } 0136 0137 QApplication app(argc, argv); 0138 0139 QStringList paths = toPaths(argv, 1, argc); 0140 QList<cv::Mat> images = toImages(paths); 0141 0142 QElapsedTimer timer; 0143 timer.start(); 0144 0145 TanTriggsPreprocessor preprocessor; 0146 OpenCVSideBySideDisplay display(images.size()); 0147 0148 Q_FOREACH (const cv::Mat& image, images) 0149 { 0150 qCDebug(DIGIKAM_TESTS_LOG) << "channels " << image.channels(); 0151 cv::Mat processed = preprocessor.preprocess(image); 0152 display.add(image, processed); 0153 } 0154 0155 int elapsed = timer.elapsed(); 0156 qCDebug(DIGIKAM_TESTS_LOG) << "Preprocessing took " << elapsed << " for " << images.size() << " , " 0157 << ((float)elapsed/images.size()) << " per image"; 0158 0159 display.show(); 0160 app.exec(); 0161 0162 return 0; 0163 }