Skip to content

Commit

Permalink
add csv2labelme
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuchaojie committed Mar 28, 2019
1 parent 703a34c commit 9db1f21
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
万事开头难。之前写图像识别的博客教程,也是为了方便那些学了很多理论知识,却对实际项目无从下手的小伙伴,后来转到目标检测来了,师从烨兄、亚光兄,从他们那学了不少检测的知识和操作,今天也终于闲下了,准备写个检测系列的总结。一方面分享知识希望可以一起学习,另一方面让一部分人少走弯路,快速上路(入坑)。

此部分代码:[Github](https://github.com/spytensor/prepare_detection_dataset)
博客地址: [目标检测系列一:如何制作数据集?](http://www.spytensor.com/index.php/archives/48/)


**更新**

- (28/03/2019)
- 新增 `csv2labelme`


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

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

<h5 id="3.5">3.5 csv2labelme</h5>

首先更改`csv2labelme.py`中以下几个配置

```
image_path = "./images/" # path for images
csv_file = "./" # path for csv annotations
```
然后运行 `python csv2labelme.py`,生成的`json`文件会保存在`image_path`下,切换路径过去,直接`labelme`便
可以查看标签.


<h4 id="4">4. 万能中介csv</h4>

从上面的转换格式中可以看出,并没有给出如何转到csv的,一是因为太过于简单,而是主流检测框架很少支持这种格式的数据输入。以下给出如何将标注信息写入`csv`
Expand Down
49 changes: 49 additions & 0 deletions csv2labelme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
import cv2
import json
import pandas as pd
import numpy as np
from glob import glob
from tqdm import tqdm
from IPython import embed
import base64
from labelme import utils
image_path = "./images/"
csv_file = "./train_labels.csv"
annotations = pd.read_csv(csv_file,header=None).values
total_csv_annotations = {}
for annotation in annotations:
key = annotation[0].split(os.sep)[-1]
value = np.array([annotation[1:]])
if key in total_csv_annotations.keys():
total_csv_annotations[key] = np.concatenate((total_csv_annotations[key],value),axis=0)
else:
total_csv_annotations[key] = value
for key,value in total_csv_annotations.items():
height,width = cv2.imread(image_path+key).shape[:2]
labelme_format = {
"version":"3.6.16",
"flags":{},
"lineColor":[0,255,0,128],
"fillColor":[255,0,0,128],
"imagePath":key,
"imageHeight":height,
"imageWidth":width
}
with open(image_path+key,"rb") as f:
imageData = f.read()
imageData = base64.b64encode(imageData).decode('utf-8')
#img = utils.img_b64_to_arr(imageData)
labelme_format["imageData"] = imageData
shapes = []
for shape in value:
label = shape[-1]
s = {"label":label,"line_color":None,"fill_color":None,"shape_type":"rectangle"}
points = [
[shape[0],shape[1]],
[shape[2],shape[3]]
]
s["points"] = points
shapes.append(s)
labelme_format["shapes"] = shapes
json.dump(labelme_format,open("%s/%s/"%(image_path,key.replace(".jpg",".json")),"w"),ensure_ascii=False, indent=2)

0 comments on commit 9db1f21

Please sign in to comment.