labelme的安裝及生成json后批處理


一、 labelme的安裝

1、 打開anaconda prompt,先在Anaconda中新建了一個虛擬環境,作為我的一個標注環境。

conda create -n my_labelme python==3.7

image

2、 安裝labelme

首先激活環境

activate my-labelme

然后先安裝pyqt5

pip install pyqt5

安裝好后安裝labelme

pip install labelme
image

二、使用labelme

直接在激活后的環境內輸入labelme可以彈出窗口進行操作,具體的使用教程有很多不錯的資料,不畫蛇添足了。

image

三、label生成的json文件轉png

我們標注完成后會生成相應的json文件,文件里有label的詳細信息。生成的json文件並不能直接用,我們需要對他進行處理才能變為需要形式。

1、單個json文件的轉換,這里我生成的json文件名為1-1.json,位於C:\Users\Harry\Desktop\label\GT12目錄下,在激活my_labelme環境的anaconda prompt中,直接輸入

labelme_json_to_dataset C:\Users\Harry\Desktop\label\GT12\1-1.json

image

運行后可以在json文件所在文件夾下,生成一個文件夾,文件夾中有對應的4個生成文件。

image

2、labelme標注工具再轉化.json文件有一個缺陷,一次只能轉換一個.json文件,我們通常需要大量的數據,可以對labelme做出了改進,可以實現批量轉換.json文件。

①找到虛擬環境的安裝位置,具體的位置和系統有一些關系,以我電腦為例,一般在C:\Users\Harry\.conda\envs\****或者 C:\ProgramData\Anaconda3\envs\**** 中。

②按照自己路徑進入cli文件夾,我的是 C:\ProgramData\Anaconda3\envs\labelme\Lib\site-packages\labelme\cli

image

③共有四個.py 文件,我們備份一下,然后更改json_to_dataset.py中的文件,文件中內容如下:

import argparse import json import os import os.path as osp import warnings import numpy as np import PIL.Image import yaml from labelme import utils def main(): parser = argparse.ArgumentParser() parser.add_argument('json_file') parser.add_argument('-o', '--out', default=None) args = parser.parse_args() json_file = args.json_file list = os.listdir(json_file) for i in range(0, len(list)): path = os.path.join(json_file, list[i]) if os.path.isfile(path): data = json.load(open(path)) img = utils.img_b64_to_arr(data['imageData']) lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes']) captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)] lbl_viz = utils.draw_label(lbl, img, captions) out_dir = osp.basename(list[i]).replace('.', '_') out_dir = osp.join(osp.dirname(list[i]), out_dir) if not osp.exists(out_dir): os.mkdir(out_dir) PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png')) PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png')) PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png')) with open(osp.join(out_dir, 'label_names.txt'), 'w') as f: for lbl_name in lbl_names: f.write(lbl_name + '\n') warnings.warn('info.yaml is being replaced by label_names.txt') info = dict(label_names=lbl_names) with open(osp.join(out_dir, 'info.yaml'), 'w') as f: yaml.safe_dump(info, f, default_flow_style=False) print('Saved to: %s' % out_dir) if __name__ == '__main__': main()

④假設我的json文件放在d:\my_jsondata中,生成的文件我想放到d:\my_data中

這個時候就可以使用,就可以看到生成的文件里

#先進入存儲生成文件的文件夾
cd D:\my_jsondata
#運行
labelme_json_to_dataset d:\my_data

但是這個時候運行,一般會出現這個錯誤,顯示缺少draw_label,這個應該是版本的原因,最新版本缺少了這個文件。

這個坑我填了好久…

image

⑤在envs\my_labelme\Lib\site-packages\labelme\utils中添加draw.py,內容如下:

import io import os.path as osp import numpy as np import PIL.Image import PIL.ImageDraw import PIL.ImageFont def label_colormap(N=256): def bitget(byteval, idx): return ((byteval & (1 << idx)) != 0) cmap = np.zeros((N, 3)) for i in range(0, N): id = i r, g, b = 0, 0, 0 for j in range(0, 8): r = np.bitwise_or(r, (bitget(id, 0) << 7 - j)) g = np.bitwise_or(g, (bitget(id, 1) << 7 - j)) b = np.bitwise_or(b, (bitget(id, 2) << 7 - j)) id = (id >> 3) cmap[i, 0] = r cmap[i, 1] = g cmap[i, 2] = b cmap = cmap.astype(np.float32) / 255 return cmap def _validate_colormap(colormap, n_labels): if colormap is None: colormap = label_colormap(n_labels) else: assert colormap.shape == (colormap.shape[0], 3), \ 'colormap must be sequence of RGB values' assert 0 <= colormap.min() and colormap.max() <= 1, \ 'colormap must ranges 0 to 1' return colormap # similar function as skimage.color.label2rgb def label2rgb( lbl, img=None, n_labels=None, alpha=0.5, thresh_suppress=0, colormap=None, ): if n_labels is None: n_labels = len(np.unique(lbl)) colormap = _validate_colormap(colormap, n_labels) colormap = (colormap * 255).astype(np.uint8) lbl_viz = colormap[lbl] lbl_viz[lbl == -1] = (0, 0, 0) # unlabeled if img is not None: img_gray = PIL.Image.fromarray(img).convert('LA') img_gray = np.asarray(img_gray.convert('RGB')) # img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # img_gray = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2RGB) lbl_viz = alpha * lbl_viz + (1 - alpha) * img_gray lbl_viz = lbl_viz.astype(np.uint8) return lbl_viz def draw_label(label, img=None, label_names=None, colormap=None, **kwargs): """Draw pixel-wise label with colorization and label names.  label: ndarray, (H, W) Pixel-wise labels to colorize. img: ndarray, (H, W, 3), optional Image on which the colorized label will be drawn. label_names: iterable List of label names. """  import matplotlib.pyplot as plt backend_org = plt.rcParams['backend'] plt.switch_backend('agg') plt.subplots_adjust(left=0, right=1, top=1, bottom=0, wspace=0, hspace=0) plt.margins(0, 0) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) if label_names is None: label_names = [str(l) for l in range(label.max() + 1)] colormap = _validate_colormap(colormap, len(label_names)) label_viz = label2rgb( label, img, n_labels=len(label_names), colormap=colormap, **kwargs ) plt.imshow(label_viz) plt.axis('off') plt_handlers = [] plt_titles = [] for label_value, label_name in enumerate(label_names): if label_value not in label: continue fc = colormap[label_value] p = plt.Rectangle((0, 0), 1, 1, fc=fc) plt_handlers.append(p) plt_titles.append('{value}: {name}' .format(value=label_value, name=label_name)) plt.legend(plt_handlers, plt_titles, loc='lower right', framealpha=.5) f = io.BytesIO() plt.savefig(f, bbox_inches='tight', pad_inches=0) plt.cla() plt.close() plt.switch_backend(backend_org) out_size = (label_viz.shape[1], label_viz.shape[0]) out = PIL.Image.open(f).resize(out_size, PIL.Image.BILINEAR).convert('RGB') out = np.asarray(out) return out def draw_instances( image=None, bboxes=None, labels=None, masks=None, captions=None, ): import matplotlib # TODO(wkentaro) assert image is not None assert bboxes is not None assert labels is not None assert masks is None assert captions is not None viz = PIL.Image.fromarray(image) draw = PIL.ImageDraw.ImageDraw(viz) font_path = osp.join( osp.dirname(matplotlib.__file__), 'mpl-data/fonts/ttf/DejaVuSans.ttf' ) font = PIL.ImageFont.truetype(font_path) colormap = label_colormap(255) for bbox, label, caption in zip(bboxes, labels, captions): color = colormap[label] color = tuple((color * 255).astype(np.uint8).tolist()) xmin, ymin, xmax, ymax = bbox draw.rectangle((xmin, ymin, xmax, ymax), outline=color) draw.text((xmin, ymin), caption, font=font) return np.asarray(viz)


⑥修改__init__.py中的內容

添加幾行

from .draw import label_colormap
from .draw import _validate_colormap
from .draw import label2rgb
from .draw import draw_label
from .draw import draw_instances

這個時候基本就能夠完成想要的功能了,但是總感覺缺點什么,因為修改的是原本的文件,這個時候如果是單個的json文件輸入會出現錯誤,這個需要再考慮!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM