Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
dc3f4205
Commit
dc3f4205
authored
Jul 08, 2011
by
Davis King
Browse files
Added a converter for pascal v1.00 annotation files.
parent
c98a4c4d
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
195 additions
and
4 deletions
+195
-4
tools/imglab/CMakeLists.txt
tools/imglab/CMakeLists.txt
+2
-0
tools/imglab/src/convert_pascal_v1.cpp
tools/imglab/src/convert_pascal_v1.cpp
+176
-0
tools/imglab/src/convert_pascal_v1.h
tools/imglab/src/convert_pascal_v1.h
+12
-0
tools/imglab/src/main.cpp
tools/imglab/src/main.cpp
+5
-4
No files found.
tools/imglab/CMakeLists.txt
View file @
dc3f4205
...
...
@@ -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
)
...
...
tools/imglab/src/convert_pascal_v1.cpp
0 → 100644
View file @
dc3f4205
#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
);
}
tools/imglab/src/convert_pascal_v1.h
0 → 100644
View file @
dc3f4205
// 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__
tools/imglab/src/main.cpp
View file @
dc3f4205
...
...
@@ -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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment