SOL9 Sample: DetectedObjectHTMLFileGenerator
|
1 Screenshot
2 Source code
/*
* DetectedObjectHTMLFileGenerator.cpp
* Copyright (c) 2019 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
//2019/06/18
#define _CONSOLE_
#define USE_STD
//#define OPENCV
#include <sol/yolo3/Detector3.h>
#include <sol/ModuleFileName.h>
#include <sol/Profile.h>
#include <sol/FileFinder.h>
#include <sol/FileFindData.h>
#include <imagehlp.h>
#pragma comment(lib, "imagehlp.lib")
namespace SOL {
class DetectedObjectHTMLFileGenerator {
std::string application;
SmartPtr<Detector3> detector;
std::string darknet_root;
std::string cfg_filename;
std::string coco_filename;
std::string weight_filename;
std::vector<std::string> class_names;
std::string inputImageFolder;
std::string fileType = "jpg";
std::string outputBaseFolder ="C:\\Apach24\\htdocs\\yolo\\"; //Default value. See a value in INI file.
const std::string outputSubFolder = "output\\";
const std::string csvFolder = "csv\\";
const std::string imageFolder = "image\\";
std::string outputCsvFolder = outputBaseFolder + outputSubFolder + csvFolder;
std::string outputImageFolder= outputBaseFolder + outputSubFolder + imageFolder;
std::string htmlTemplateFile = ".\\template.html"; //See a value of INI file
std::string htmlfilename = "detected.html"; //See a value of INI file
const std::string outputCsvFolder4Html = "./output/csv/"; //Used in a html file.
const std::string outputImageFolder4Html = "./output/image/"; //Used in a html file.
std::string outputHtmlFilepath = "C:\\Apach24\\htdocs\\yolo\\detected.html"; //Default value
void readIniFile(const char* inifile)
{
// Read some YOLO configuration file names from the ini file ".\\YoloObjectDetector.ini".
char buffer[256] = { 0 };
GetPrivateProfileString("DARKNET_ROOT","filename", "", buffer, sizeof(buffer), inifile);
darknet_root = buffer;
printf("DARKNET_ROOT:%s\n", buffer);
GetPrivateProfileString("CFG_FILE", "filename", "", buffer, sizeof(buffer), inifile);
cfg_filename = buffer;
printf("CFG_FILE:%s\n", buffer);
GetPrivateProfileString("WEIGHT_FILE", "filename", "", buffer, sizeof(buffer), inifile);
weight_filename = buffer;
printf("WEIGHT_FILE:%s\n", buffer);
GetPrivateProfileString("COCO_FILE", "filename", "", buffer, sizeof(buffer), inifile);
coco_filename = buffer;
printf("COCO_FILE:%s\n", buffer);
GetPrivateProfileString("INPUT", "foldername", "", buffer, sizeof(buffer), inifile);
inputImageFolder = buffer;
printf("INPUT:%s\n", buffer);
if (!PathFileExists( inputImageFolder.c_str()) ){
throw IException("Not found inputImageFolder %s", inputImageFolder.c_str());
}
GetPrivateProfileString("INPUT", "filetype", "", buffer, sizeof(buffer), inifile);
fileType = buffer;
printf("FileType:%s\n", buffer);
GetPrivateProfileString("TEMPLATE", "filename", "", buffer, sizeof(buffer), inifile);
htmlTemplateFile = buffer;
printf("htmlTemplate:%s\n", buffer);
//
GetPrivateProfileString("OUTPUT", "foldername", "", buffer, sizeof(buffer), inifile);
outputBaseFolder = buffer;
if (!PathFileExists( outputBaseFolder.c_str()) ){
if (!MakeSureDirectoryPathExists(outputBaseFolder.c_str() )) {
throw IException("Failed to create %s", outputBaseFolder.c_str());
} else {
printf("Created a folder %s\n", outputBaseFolder.c_str());
}
}
printf("OUTPUT: outputBaseFolder %s\n", buffer);
GetPrivateProfileString("OUTPUT", "htmlfilename", "", buffer, sizeof(buffer), inifile);
htmlfilename = buffer;
printf("OUTPUT: outputBaseFolder %s\n", buffer);
outputCsvFolder = outputBaseFolder + outputSubFolder + csvFolder;
outputImageFolder = outputBaseFolder + outputSubFolder + imageFolder;
if (!PathFileExists( outputCsvFolder.c_str()) ){
if (!MakeSureDirectoryPathExists(outputCsvFolder.c_str() )) {
throw IException("Failed to create %s", outputCsvFolder.c_str());
} else {
printf("Created a folder %s\n", outputCsvFolder.c_str());
}
}
if (!PathFileExists( outputImageFolder.c_str()) ){
if (!MakeSureDirectoryPathExists(outputImageFolder.c_str() )) {
throw IException("Failed to create %s", outputImageFolder.c_str());
} else {
printf("Created a folder %s\n", outputImageFolder.c_str());
}
}
outputHtmlFilepath = outputBaseFolder + htmlfilename;
printf("%s\n", outputHtmlFilepath.c_str());
}
void detectAll(std::vector<std::pair<std::string, std::string>>& paired_filenames)
{
const std::string sep = "\\";
const std::string csv_ext = ".csv";
const std::string jpg_ext = ".jpg";
std::string pattern = inputImageFolder + std::string("\\*.") + fileType; //jpg
printf("pattern %s\n", pattern.c_str());
FileFinder finder(pattern.c_str());
FindData findData = { 0 };
BOOL rc = finder.getFirst(&findData);
bool looping = true;
while (rc) {
try {
std::string imageFilename = findData.cFileName;
printf("imageFilename %s\n", imageFilename.c_str());
std::string inImageFilepath = inputImageFolder + sep + imageFilename;
auto dotpos = imageFilename.rfind(".");
std::string nameOnly = imageFilename.substr(0, dotpos);
printf("Real inImageFilepath:%s : nameOnly:%s\n", inImageFilepath.c_str(), nameOnly.c_str());
std::string fullOutImageFilepath = outputImageFolder + nameOnly;// + jpg_ext; //Output image is a jpeg file.
std::string fullOutCsvFilepath = outputCsvFolder + imageFilename + csv_ext;
printf("Real OutImageFullpath %s\n", fullOutImageFilepath.c_str());
printf("Real OutCsvFullpath %s\n", fullOutCsvFilepath.c_str());
image im = detector -> detect_image(inImageFilepath.c_str(), fullOutImageFilepath.c_str(), fullOutCsvFilepath.c_str());
free_image(im);
std::string imageFilepath4Html = outputImageFolder4Html + nameOnly + jpg_ext;
std::string csvFilepath4Html = outputCsvFolder4Html + imageFilename + csv_ext;
printf("Relative filepath %s %s in HtmlFile\n", imageFilepath4Html.c_str(), csvFilepath4Html.c_str());
std::pair<std::string, std::string> pair(imageFilepath4Html, csvFilepath4Html);
paired_filenames.push_back(pair);
rc = finder.getNext(&findData);
} catch (...) {
break;
}
}
}
const char** getListViewHeader(int& number)
{
static const char* LISTVIEW_HEADER[] = {"id ", "object ", "score", " x ", " y ", " w ", " h "};
number = CountOf(LISTVIEW_HEADER);
return LISTVIEW_HEADER;
}
void createHtmlFile(std::vector<std::pair<std::string, std::string>>& paired_filenames)
{
const char* format = "<tr>\n"
"<td valign=\"top\">%s<br>\n"
"<img src=\"%s\"></td>\n"
"<td>\n"
"<div style=\"height:500px; width:300px; overflow-y:scroll;\">\n"
"<div class=\"table_csv\" filename=\"%s\"></div>\n"
"</div>\n"
"</td>\n"
"</tr>\n";
std::ifstream ifs(htmlTemplateFile);
if (!ifs) {
throw IException("File not found %s", htmlTemplateFile.c_str());
}
std::ofstream ofs(outputHtmlFilepath);
if (!ofs) {
throw IException("File not found %s", outputHtmlFilepath.c_str());
}
while (!ifs.eof()) {
std::string line;
std::getline(ifs, line);
if (line.find("<!-- CODE_GENERATION -->") !=std::string::npos) {
char buffer[2000];
for (auto it = paired_filenames.begin(); it != paired_filenames.end(); it++) {
std::pair<std::string, std::string>& pair = *it;
std::string imageFilepath = pair.first;
std::string csvFilepath = pair.second;
sprintf(buffer, format, imageFilepath.c_str(), imageFilepath.c_str(), csvFilepath.c_str());
printf("%s\n", buffer);
ofs.write(buffer, strlen(buffer));
}
} else {
//printf("%s", line.c_str());
ofs<<line<<"\n";
}
}
ifs.close();
ofs.close();
}
public:
DetectedObjectHTMLFileGenerator(const char* name)
{
application = name;
printf("application %s\n", application.c_str());
}
void run()
{
try {
const char* inifile = "./DetectedObjectHTMLFileGenerator.ini";
readIniFile(inifile);
detector = new Detector3( darknet_root,
cfg_filename,
weight_filename,
coco_filename);
// pair (outImageFilepath4Html, ouputCsvFilepath4Html)
std::vector<std::pair<std::string, std::string>> paired_filenames;
detectAll(paired_filenames);
createHtmlFile(paired_filenames);
} catch (Exception& ex) {
caught(ex);
}
}
~DetectedObjectHTMLFileGenerator()
{
}
};
}
//
void main(int argc, char** argv)
{
try {
ModuleFileName module(argv[0]);
const char* name = module.getAppName();
DetectedObjectHTMLFileGenerator generator(name);
generator.run();
} catch (SOL::Exception& ex) {
caught(ex);
}
}
Last modified: 18 Jun. 2019
Copyright (c) 2019 Antillia.com ALL RIGHTS RESERVED.