Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RAVEN2YOLO
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stephane Chavin
RAVEN2YOLO
Commits
c5d50a5f
Commit
c5d50a5f
authored
2 years ago
by
Stephane Chavin
Browse files
Options
Downloads
Patches
Plain Diff
suppression du ratio aléatoire
parent
57008fbe
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
labelme2yolo.py
+12
-56
12 additions, 56 deletions
labelme2yolo.py
with
12 additions
and
56 deletions
labelme2yolo.py
+
12
−
56
View file @
c5d50a5f
...
...
@@ -15,7 +15,7 @@ import shutil
import
argparse
from
pathlib
import
Path
from
glob
import
glob
#import ipdb;ipdb.set_trace()
# Parse arguments
def
arg_directory
(
path
):
...
...
@@ -25,67 +25,36 @@ def arg_directory(path):
raise
argparse
.
ArgumentTypeError
(
f
'
`
{
path
}
` is not a valid path
'
)
parser
=
argparse
.
ArgumentParser
(
description
=
'
Convert LabelMe annotations to YOLO compatible
'
)
parser
.
add_argument
(
'
--input
'
,
type
=
arg_directory
,
help
=
'
Directory to LabelMe annotations
'
,
default
=
os
.
path
.
join
(
str
(
Path
.
home
()),
'
Pictures
'
,
'
Webcam
'
)
)
parser
.
add_argument
(
'
--output
'
,
type
=
arg_directory
,
help
=
'
Directory to which YOLO annotations will be stored
'
,
default
=
'
./yolo
'
)
parser
.
add_argument
(
'
--ratio
'
,
type
=
float
,
help
=
'
Training ratio
'
,
default
=
0.9
)
parser
=
argparse
.
ArgumentParser
(
description
=
'
Convert LabelMe annotations to YOLO compatible
'
)
parser
.
add_argument
(
'
-p
'
,
'
--path_to_data
'
,
type
=
arg_directory
,
help
=
'
Path to LabelMe annotations
'
)
parser
.
add_argument
(
'
-d
'
,
'
--direction
'
,
type
=
arg_directory
,
help
=
'
Directory to which YOLO annotations will be stored
'
)
args
=
parser
.
parse_args
()
# YOLO metadata and files
yolo_names
=
[]
yolo_train_dir_path
=
os
.
path
.
join
(
args
.
output
,
'
train
'
)
yolo_valid_dir_path
=
os
.
path
.
join
(
args
.
output
,
'
valid
'
)
yolo_backup_dir_path
=
os
.
path
.
join
(
args
.
output
,
'
backup
'
)
yolo_list_file_train_path
=
os
.
path
.
join
(
args
.
output
,
'
train.txt
'
)
yolo_list_file_valid_path
=
os
.
path
.
join
(
args
.
output
,
'
valid.txt
'
)
yolo_data_path
=
os
.
path
.
join
(
args
.
output
,
'
custom.data
'
)
yolo_names_path
=
os
.
path
.
join
(
args
.
output
,
'
custom.names
'
)
yolo_names_path
=
os
.
path
.
join
(
args
.
direction
,
'
custom.names
'
)
# Prepare output directory
shutil
.
rmtree
(
yolo_train_dir_path
)
os
.
mkdir
(
yolo_train_dir_path
)
shutil
.
rmtree
(
yolo_valid_dir_path
)
os
.
mkdir
(
yolo_valid_dir_path
)
if
not
os
.
path
.
isdir
(
yolo_backup_dir_path
):
os
.
mkdir
(
yolo_backup_dir_path
)
os
.
mkdir
(
os
.
path
.
join
(
args
.
direction
,
'
labels
'
))
# Convert image annotations
yolo_list_file
=
{
'
train
'
:
open
(
yolo_list_file_train_path
,
'
w
'
),
'
valid
'
:
open
(
yolo_list_file_valid_path
,
'
w
'
)
}
for
index
,
labelme_annotation_path
in
enumerate
(
glob
(
f
'
{
args
.
input
}
/*.json
'
)):
for
index
,
labelme_annotation_path
in
enumerate
(
glob
(
f
'
{
args
.
path_to_data
}
/*.json
'
)):
image_id
=
os
.
path
.
basename
(
labelme_annotation_path
).
rstrip
(
'
.json
'
)
train_or_valid
=
'
train
'
if
random
.
random
()
<
args
.
ratio
else
'
valid
'
labelme_annotation_file
=
open
(
labelme_annotation_path
,
'
r
'
)
labelme_annotation
=
json
.
load
(
labelme_annotation_file
)
yolo_annotation_path
=
os
.
path
.
join
(
args
.
output
,
train_or_valid
,
image_id
+
'
.txt
'
)
yolo_annotation_path
=
os
.
path
.
join
(
args
.
direction
,
'
labels
'
,
image_id
+
'
.txt
'
)
yolo_annotation_file
=
open
(
yolo_annotation_path
,
'
w
'
)
yolo_image
=
base64
.
decodebytes
(
labelme_annotation
[
'
imageData
'
].
encode
())
yolo_image_path
=
os
.
path
.
join
(
args
.
output
,
train_or_valid
,
image_id
+
'
.jpg
'
)
yolo_image_path
=
os
.
path
.
join
(
args
.
direction
,
'
labels
'
,
image_id
+
'
.jpg
'
)
# Write YOLO image (and it to the list)
yolo_image_file
=
open
(
yolo_image_path
,
'
wb
'
)
yolo_image_file
.
write
(
yolo_image
)
yolo_image_file
.
close
()
yolo_list_file
[
train_or_valid
].
write
(
f
'
{
yolo_image_path
}{
os
.
linesep
}
'
)
# Write YOLO image annotation
for
shape
in
labelme_annotation
[
'
shapes
'
]:
...
...
@@ -103,25 +72,12 @@ for index, labelme_annotation_path in enumerate(glob(f'{args.input}/*.json')):
height
=
abs
(
points
[
1
][
1
]
-
points
[
0
][
1
])
*
scale_height
x
=
(
abs
(
points
[
1
][
0
]
+
points
[
0
][
0
])
/
2
)
*
scale_width
y
=
(
abs
(
points
[
1
][
1
]
+
points
[
0
][
1
])
/
2
)
*
scale_height
# object_class = yolo_names.index(shape['label'])
object_class
=
shape
[
'
label
'
]
yolo_annotation_file
.
write
(
f
'
{
object_class
}
{
x
}
{
y
}
{
width
}
{
height
}
\n
'
)
yolo_annotation_file
.
close
()
yolo_list_file
[
'
train
'
].
close
()
yolo_list_file
[
'
valid
'
].
close
()
# Write YOLO names
yolo_names_file
=
open
(
yolo_names_path
,
'
w
'
)
yolo_names_file
.
write
(
os
.
linesep
.
join
(
yolo_names
))
yolo_names_file
.
close
()
# Write YOLO data configuration
yolo_data_file
=
open
(
yolo_data_path
,
'
w
'
)
yolo_data_file
.
write
(
f
'
classes =
{
len
(
yolo_names
)
}{
os
.
linesep
}
'
)
yolo_data_file
.
write
(
f
'
train =
{
yolo_list_file_train_path
}{
os
.
linesep
}
'
)
yolo_data_file
.
write
(
f
'
valid =
{
yolo_list_file_valid_path
}{
os
.
linesep
}
'
)
yolo_data_file
.
write
(
f
'
names =
{
yolo_names_path
}{
os
.
linesep
}
'
)
yolo_data_file
.
write
(
f
'
backup =
{
yolo_backup_dir_path
}{
os
.
linesep
}
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment