Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

Commit

Permalink
Use Message system instead of std::cout, it cleans the code a lot
Browse files Browse the repository at this point in the history
  • Loading branch information
brian8544 committed Jun 9, 2023
1 parent 71b61b3 commit 1d06e89
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
1 change: 1 addition & 0 deletions RealTimeFacialRecognition.vcxproj
Expand Up @@ -172,6 +172,7 @@
</Xml>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Console\ConsoleOutput.cpp" />
<ClCompile Include="src\Main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
50 changes: 50 additions & 0 deletions src/Console/ConsoleOutput.cpp
@@ -0,0 +1,50 @@
#include <iostream>

// Color constants
const std::string RESET_COLOR = "\033[0m";
const std::string BLACK = "\033[30m";
const std::string RED = "\033[31m";
const std::string GREEN = "\033[32m";
const std::string YELLOW = "\033[33m";
const std::string BLUE = "\033[34m";
const std::string MAGENTA = "\033[35m";
const std::string CYAN = "\033[36m";
const std::string WHITE = "\033[37m";
const std::string BRIGHT_BLACK = "\033[90m";
const std::string BRIGHT_RED = "\033[91m";
const std::string BRIGHT_GREEN = "\033[92m";
const std::string BRIGHT_YELLOW = "\033[93m";
const std::string BRIGHT_BLUE = "\033[94m";
const std::string BRIGHT_MAGENTA = "\033[95m";
const std::string BRIGHT_CYAN = "\033[96m";
const std::string BRIGHT_WHITE = "\033[97m";

namespace Messages {

// Function to print the current time inline with std::cout
static void printCurrentTime()
{
time_t now = time(0);
struct tm timeInfo;
localtime_s(&timeInfo, &now);
char buffer[80];
strftime(buffer, sizeof(buffer), "[%d/%m/%Y %H:%M]", &timeInfo);
std::cout << buffer << " ";
}

static void Info(const std::string& text) {
printCurrentTime();
std::cout << GREEN << "[INFO]: " << text << RESET_COLOR << std::endl;
}

static void Notice(const std::string& text) {
printCurrentTime();
std::cout << YELLOW << "[NOTICE]: " << text << RESET_COLOR << std::endl;
}

static void Error(const std::string& text) {
printCurrentTime();
std::cerr << RED << "[ERROR]: " << text << RESET_COLOR << std::endl;
}

}
34 changes: 9 additions & 25 deletions src/Main.cpp
Expand Up @@ -3,6 +3,8 @@
#include <fstream>
#include <ctime>

#include "Console/ConsoleOutput.cpp"

// OpenCV 4
#include <opencv2/objdetect.hpp>
#include <opencv2/highgui.hpp>
Expand All @@ -12,17 +14,6 @@ std::string CASCADE_FILE_MAIN;
std::string IMAGE_DIR;
std::string LOGGING_DIR;

// Function to print the current time inline with std::cout
void printCurrentTime()
{
time_t now = time(0);
struct tm timeInfo;
localtime_s(&timeInfo, &now);
char buffer[80];
strftime(buffer, sizeof(buffer), "[%d/%m/%Y %H:%M]", &timeInfo);
std::cout << buffer << " ";
}

// Function to check if a file has a valid image extension
bool hasValidImageExtension(const std::filesystem::path& path) {
std::string extension = path.extension().string();
Expand All @@ -48,8 +39,7 @@ void readSettings()
{
if (settingsFile.peek() == std::ifstream::traits_type::eof())
{
printCurrentTime();
std::cout << "settings.conf is empty or corrupt." << std::endl;
Messages::Info("settings.conf is empty or corrupt.");
settingsFile.close();
}
std::string line;
Expand All @@ -72,8 +62,7 @@ void readSettings()
}
else
{
printCurrentTime();
std::cout << "Failed to open settings.conf file." << std::endl;
Messages::Info("Failed to open settings.conf file.");
system("pause");
exit(1); // Use exit(1) to indicate an error
}
Expand Down Expand Up @@ -136,8 +125,7 @@ void detectAndDraw(cv::Mat& img, cv::CascadeClassifier& cascade, double scale)
cv::Mat compareImg = cv::imread(entry.path().string());
if (compareImg.empty())
{
printCurrentTime();
std::cout << "Failed to read image: " << entry.path().filename().string() << std::endl;
Messages::Error("Failed to read image: " + entry.path().filename().string());
continue; // Skip to the next image
}

Expand All @@ -155,8 +143,7 @@ void detectAndDraw(cv::Mat& img, cv::CascadeClassifier& cascade, double scale)
}
catch (const cv::Exception& e)
{
printCurrentTime();
std::cout << "Error occurred during template matching: " << e.what() << std::endl;
Messages::Error("Error occurred during template matching: " + std::string(e.what()));
continue; // Skip to the next image
}

Expand All @@ -173,9 +160,8 @@ void detectAndDraw(cv::Mat& img, cv::CascadeClassifier& cascade, double scale)
}
else if (maxVal > 0.1 && entry.path().filename().string() != bestMatchName)
{
printCurrentTime();
// Print other matches to the console
std::cout << "Other match: " << entry.path().filename().string() << std::endl;
Messages::Info("Other match: " + entry.path().filename().string());
log("Hello World");
}
}
Expand All @@ -201,8 +187,7 @@ int main(int argc, char** argv)
{
// Face cascade could not be found
std::system("cls");
printCurrentTime();
std::cout << "Could not find the face cascade." << std::endl;
Messages::Error("Could not find the face cascade.");
std::system("pause");
return -1;
}
Expand All @@ -214,8 +199,7 @@ int main(int argc, char** argv)
if (!capture.isOpened())
{
std::system("cls");
printCurrentTime();
std::cout << "Error opening camera." << std::endl;
Messages::Error("Error opening camera.");
std::system("pause");
return -1;
}
Expand Down

0 comments on commit 1d06e89

Please sign in to comment.