mirror of
https://github.com/davisking/dlib.git
synced 2024-11-01 10:14:53 +08:00
Added a converter for pascal v1.00 annotation files.
This commit is contained in:
parent
c98a4c4d7e
commit
dc3f420529
@ -20,6 +20,8 @@ ADD_EXECUTABLE(${target_name}
|
||||
src/metadata_editor.cpp
|
||||
src/convert_pascal_voc.h
|
||||
src/convert_pascal_voc.cpp
|
||||
src/convert_pascal_v1.h
|
||||
src/convert_pascal_v1.cpp
|
||||
src/common.h
|
||||
src/common.cpp
|
||||
)
|
||||
|
176
tools/imglab/src/convert_pascal_v1.cpp
Normal file
176
tools/imglab/src/convert_pascal_v1.cpp
Normal file
@ -0,0 +1,176 @@
|
||||
|
||||
#include "convert_pascal_v1.h"
|
||||
#include "image_dataset_metadata.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <dlib/dir_nav.h>
|
||||
#include <dlib/time_this.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace dlib;
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace dlib::image_dataset_metadata;
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
std::string pick_out_quoted_string (
|
||||
const std::string& str
|
||||
)
|
||||
{
|
||||
std::string temp;
|
||||
bool in_quotes = false;
|
||||
for (unsigned long i = 0; i < str.size(); ++i)
|
||||
{
|
||||
if (str[i] == '"')
|
||||
{
|
||||
in_quotes = !in_quotes;
|
||||
}
|
||||
else if (in_quotes)
|
||||
{
|
||||
temp += str[i];
|
||||
}
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
void parse_annotation_file(
|
||||
const std::string& file,
|
||||
dlib::image_dataset_metadata::image& img,
|
||||
std::string& dataset_name
|
||||
)
|
||||
{
|
||||
ifstream fin(file.c_str());
|
||||
if (!fin)
|
||||
throw dlib::error("Unable to open file " + file);
|
||||
|
||||
img = dlib::image_dataset_metadata::image();
|
||||
|
||||
string str, line;
|
||||
std::vector<string> words;
|
||||
while (fin.peek() != EOF)
|
||||
{
|
||||
getline(fin, line);
|
||||
words = split(line, " \r\n\t:(,-)\"");
|
||||
if (words.size() > 2)
|
||||
{
|
||||
if (words[0] == "#")
|
||||
continue;
|
||||
|
||||
if (words[0] == "Image" && words[1] == "filename")
|
||||
{
|
||||
img.filename = pick_out_quoted_string(line);
|
||||
}
|
||||
else if (words[0] == "Database")
|
||||
{
|
||||
dataset_name = pick_out_quoted_string(line);
|
||||
}
|
||||
else if (words[0] == "Objects" && words[1] == "with" && words.size() >= 5)
|
||||
{
|
||||
const int num = sa = words[4];
|
||||
img.boxes.resize(num);
|
||||
}
|
||||
else if (words.size() > 4 && words[2] == "for" && words[3] == "object")
|
||||
{
|
||||
int idx = sa = words[4];
|
||||
--idx;
|
||||
if (idx >= img.boxes.size())
|
||||
throw dlib::error("Invalid object id number of " + words[4]);
|
||||
|
||||
if (words[0] == "Center" && words[1] == "point" && words.size() > 9)
|
||||
{
|
||||
img.boxes[idx].head.x() = sa = words[8];
|
||||
img.boxes[idx].head.y() = sa = words[9];
|
||||
}
|
||||
else if (words[0] == "Bounding" && words[1] == "box" && words.size() > 13)
|
||||
{
|
||||
rectangle rect;
|
||||
img.boxes[idx].rect.left() = sa = words[10];
|
||||
img.boxes[idx].rect.top() = sa = words[11];
|
||||
img.boxes[idx].rect.right() = sa = words[12];
|
||||
img.boxes[idx].rect.bottom() = sa = words[13];
|
||||
}
|
||||
else if (words[0] == "Original" && words[1] == "label" && words.size() > 6)
|
||||
{
|
||||
img.boxes[idx].label = words[6];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
std::string figure_out_full_path_to_image (
|
||||
const std::string& annotation_file,
|
||||
const std::string& image_name
|
||||
)
|
||||
{
|
||||
directory parent = get_parent_directory(file(annotation_file));
|
||||
|
||||
|
||||
string temp;
|
||||
while (true)
|
||||
{
|
||||
if (parent.is_root())
|
||||
temp = parent.full_name() + image_name;
|
||||
else
|
||||
temp = parent.full_name() + directory::get_separator() + image_name;
|
||||
|
||||
if (file_exists(temp))
|
||||
return temp;
|
||||
|
||||
if (parent.is_root())
|
||||
throw dlib::error("Can't figure out where the file " + image_name + " is located.");
|
||||
parent = get_parent_directory(parent);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
||||
void convert_pascal_v1(
|
||||
const parser_type& parser
|
||||
)
|
||||
{
|
||||
cout << "Convert from PASCAL v1.00 annotation format..." << endl;
|
||||
|
||||
dlib::image_dataset_metadata::dataset dataset;
|
||||
|
||||
std::string name;
|
||||
dlib::image_dataset_metadata::image img;
|
||||
|
||||
const std::string filename = parser.option("c").argument();
|
||||
// make sure the file exists so we can use the get_parent_directory() command to
|
||||
// figure out it's parent directory.
|
||||
make_empty_file(filename);
|
||||
const std::string parent_dir = get_parent_directory(file(filename)).full_name();
|
||||
|
||||
for (unsigned long i = 0; i < parser.number_of_arguments(); ++i)
|
||||
{
|
||||
try
|
||||
{
|
||||
parse_annotation_file(parser[i], img, name);
|
||||
|
||||
dataset.name = name;
|
||||
img.filename = strip_path(figure_out_full_path_to_image(parser[i], img.filename), parent_dir);
|
||||
dataset.images.push_back(img);
|
||||
|
||||
}
|
||||
catch (exception& e)
|
||||
{
|
||||
cout << "Error while processing file " << parser[i] << endl << endl;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
save_image_dataset_metadata(dataset, filename);
|
||||
}
|
||||
|
||||
|
12
tools/imglab/src/convert_pascal_v1.h
Normal file
12
tools/imglab/src/convert_pascal_v1.h
Normal file
@ -0,0 +1,12 @@
|
||||
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
|
||||
// License: Boost Software License See LICENSE.txt for the full license.
|
||||
#ifndef DLIB_IMGLAB_CONVERT_PASCAl_V1_H__
|
||||
#define DLIB_IMGLAB_CONVERT_PASCAl_V1_H__
|
||||
|
||||
#include "common.h"
|
||||
|
||||
void convert_pascal_v1(const parser_type& parser);
|
||||
|
||||
#endif // DLIB_IMGLAB_CONVERT_PASCAl_V1_H__
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "image_dataset_metadata.h"
|
||||
#include "metadata_editor.h"
|
||||
#include "convert_pascal_voc.h"
|
||||
#include "convert_pascal_v1.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
@ -124,7 +125,7 @@ int main(int argc, char** argv)
|
||||
parser.add_option("rename", "Rename all labels of <arg1> to <arg2>.",2);
|
||||
parser.add_option("v","Display version.");
|
||||
parser.add_option("convert","Convert foreign image Annotations from <arg> format to the imglab format. "
|
||||
"Supported formats: pascal-voc",1);
|
||||
"Supported formats: pascal-voc, pascal-v1",1);
|
||||
|
||||
parser.parse(argc, argv);
|
||||
|
||||
@ -137,7 +138,7 @@ int main(int argc, char** argv)
|
||||
parser.check_incompatible_options("l", "rename");
|
||||
parser.check_incompatible_options("convert", "l");
|
||||
parser.check_incompatible_options("convert", "rename");
|
||||
const char* convert_args[] = {"pascal-voc"};
|
||||
const char* convert_args[] = {"pascal-voc","pascal-v1"};
|
||||
parser.check_option_arg_range("convert", convert_args);
|
||||
|
||||
if (parser.option("h"))
|
||||
@ -162,9 +163,9 @@ int main(int argc, char** argv)
|
||||
if (parser.option("convert"))
|
||||
{
|
||||
if (parser.option("convert").argument() == "pascal-voc")
|
||||
{
|
||||
convert_pascal_voc(parser);
|
||||
}
|
||||
else if (parser.option("convert").argument() == "pascal-v1")
|
||||
convert_pascal_v1(parser);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user