Skip to content

Commit 9db1f21

Browse files
author
zhuchaojie
committed
add csv2labelme
1 parent 703a34c commit 9db1f21

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
万事开头难。之前写图像识别的博客教程,也是为了方便那些学了很多理论知识,却对实际项目无从下手的小伙伴,后来转到目标检测来了,师从烨兄、亚光兄,从他们那学了不少检测的知识和操作,今天也终于闲下了,准备写个检测系列的总结。一方面分享知识希望可以一起学习,另一方面让一部分人少走弯路,快速上路(入坑)。
44

55
此部分代码:[Github](https://github.com/spytensor/prepare_detection_dataset)
6+
博客地址: [目标检测系列一:如何制作数据集?](http://www.spytensor.com/index.php/archives/48/)
7+
8+
9+
**更新**
10+
11+
- (28/03/2019)
12+
- 新增 `csv2labelme`
13+
614

715
<h4 id="1">1. 内容介绍</h4>
816

@@ -214,6 +222,18 @@ saved_coco_path = "./" # path for saved coco dataset
214222
```
215223
然后运行 `python labelme2voc.py`,生成文件形式同`csv2voc`
216224

225+
<h5 id="3.5">3.5 csv2labelme</h5>
226+
227+
首先更改`csv2labelme.py`中以下几个配置
228+
229+
```
230+
image_path = "./images/" # path for images
231+
csv_file = "./" # path for csv annotations
232+
```
233+
然后运行 `python csv2labelme.py`,生成的`json`文件会保存在`image_path`下,切换路径过去,直接`labelme`便
234+
可以查看标签.
235+
236+
217237
<h4 id="4">4. 万能中介csv</h4>
218238

219239
从上面的转换格式中可以看出,并没有给出如何转到csv的,一是因为太过于简单,而是主流检测框架很少支持这种格式的数据输入。以下给出如何将标注信息写入`csv`

csv2labelme.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
import cv2
3+
import json
4+
import pandas as pd
5+
import numpy as np
6+
from glob import glob
7+
from tqdm import tqdm
8+
from IPython import embed
9+
import base64
10+
from labelme import utils
11+
image_path = "./images/"
12+
csv_file = "./train_labels.csv"
13+
annotations = pd.read_csv(csv_file,header=None).values
14+
total_csv_annotations = {}
15+
for annotation in annotations:
16+
key = annotation[0].split(os.sep)[-1]
17+
value = np.array([annotation[1:]])
18+
if key in total_csv_annotations.keys():
19+
total_csv_annotations[key] = np.concatenate((total_csv_annotations[key],value),axis=0)
20+
else:
21+
total_csv_annotations[key] = value
22+
for key,value in total_csv_annotations.items():
23+
height,width = cv2.imread(image_path+key).shape[:2]
24+
labelme_format = {
25+
"version":"3.6.16",
26+
"flags":{},
27+
"lineColor":[0,255,0,128],
28+
"fillColor":[255,0,0,128],
29+
"imagePath":key,
30+
"imageHeight":height,
31+
"imageWidth":width
32+
}
33+
with open(image_path+key,"rb") as f:
34+
imageData = f.read()
35+
imageData = base64.b64encode(imageData).decode('utf-8')
36+
#img = utils.img_b64_to_arr(imageData)
37+
labelme_format["imageData"] = imageData
38+
shapes = []
39+
for shape in value:
40+
label = shape[-1]
41+
s = {"label":label,"line_color":None,"fill_color":None,"shape_type":"rectangle"}
42+
points = [
43+
[shape[0],shape[1]],
44+
[shape[2],shape[3]]
45+
]
46+
s["points"] = points
47+
shapes.append(s)
48+
labelme_format["shapes"] = shapes
49+
json.dump(labelme_format,open("%s/%s/"%(image_path,key.replace(".jpg",".json")),"w"),ensure_ascii=False, indent=2)

0 commit comments

Comments
 (0)