Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2021-1-capstone-design1
/
HCS_Project2
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
정우진
2021-06-20 00:36:36 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
4252eb7a8b3b8e9eb869f31ec84abf340eb0f2f9
4252eb7a
1 parent
a4ed6069
Upload new file
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
73 additions
and
0 deletions
csv2coco.py
csv2coco.py
0 → 100644
View file @
4252eb7
import
numpy
as
np
import
json
import
pandas
as
pd
path
=
'../dataset/test/test1/annotations.csv'
classpath
=
'../dataset/e_label2.csv'
save_json_path
=
'testcoco.json'
data
=
pd
.
read_csv
(
path
,
names
=
[
'filename'
,
'width'
,
'height'
,
'xmin'
,
'ymin'
,
'xmax'
,
'ymax'
,
'class'
])
images
=
[]
categories
=
[]
annotations
=
[]
category
=
{}
category
[
"supercategory"
]
=
'none'
category
[
"id"
]
=
0
category
[
"name"
]
=
'None'
categories
.
append
(
category
)
data
[
'fileid'
]
=
data
[
'filename'
]
.
astype
(
'category'
)
.
cat
.
codes
data
[
'categoryid'
]
=
pd
.
Categorical
(
data
[
'class'
],
ordered
=
True
)
.
codes
data
[
'categoryid'
]
=
data
[
'categoryid'
]
+
1
data
[
'annid'
]
=
data
.
index
def
image
(
row
):
image
=
{}
image
[
"height"
]
=
row
.
height
image
[
"width"
]
=
row
.
width
image
[
"id"
]
=
row
.
fileid
image
[
"file_name"
]
=
row
.
filename
return
image
def
category
(
row
):
category
=
{}
category
[
"supercategory"
]
=
'None'
category
[
"id"
]
=
row
.
categoryid
category
[
"name"
]
=
row
[
2
]
return
category
def
annotation
(
row
):
annotation
=
{}
area
=
(
row
.
xmax
-
row
.
xmin
)
*
(
row
.
ymax
-
row
.
ymin
)
annotation
[
"segmentation"
]
=
[]
annotation
[
"iscrowd"
]
=
0
annotation
[
"area"
]
=
area
annotation
[
"image_id"
]
=
row
.
fileid
annotation
[
"bbox"
]
=
[
row
.
xmin
,
row
.
ymin
,
row
.
xmax
-
row
.
xmin
,
row
.
ymax
-
row
.
ymin
]
annotation
[
"category_id"
]
=
row
.
categoryid
annotation
[
"id"
]
=
row
.
annid
return
annotation
for
row
in
data
.
itertuples
():
annotations
.
append
(
annotation
(
row
))
imagedf
=
data
.
drop_duplicates
(
subset
=
[
'fileid'
])
.
sort_values
(
by
=
'fileid'
)
for
row
in
imagedf
.
itertuples
():
images
.
append
(
image
(
row
))
catdf
=
data
.
drop_duplicates
(
subset
=
[
'categoryid'
])
.
sort_values
(
by
=
'categoryid'
)
for
row
in
catdf
.
itertuples
():
categories
.
append
(
category
(
row
))
data_coco
=
{}
data_coco
[
"images"
]
=
images
data_coco
[
"categories"
]
=
categories
data_coco
[
"annotations"
]
=
annotations
json
.
dump
(
data_coco
,
open
(
save_json_path
,
"w"
),
indent
=
4
)
\ No newline at end of file
Please
register
or
login
to post a comment