Ubuntu18使用labelme制作多物体语义分割数据集并对其批量处理。

目的:为maskrcnn( TensorFlow版本)制作数据集。生成coco格式。

步骤1 使用realsense2相机拍摄物体图片

复制下面程序,按键“d”来快速拍摄物体。

import pyrealsense2 as rs
import numpy as np
import time
import cv2
import os
# 类似相机的拍摄加保存脚本 d:保存并显示保存后的图像;f:切换到下一帧
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_stream(rs.stream.infrared, 1, 640, 480, rs.format.y8, 30)
config.enable_stream(rs.stream.infrared, 2, 640, 480, rs.format.y8, 30)

# Start streaming
pipeline.start(config)
ROOT_DIR = os.getcwd()
IMAGE_DIR = os.path.join(ROOT_DIR, "images")
try:
    while True:
        # Wait for a coherent pair of frames: depth and color
        frames = pipeline.wait_for_frames()
        depth_frame = frames.get_depth_frame()
        color_frame = frames.get_color_frame()
        ir_frame_left = frames.get_infrared_frame(1)
        ir_frame_right = frames.get_infrared_frame(2)
        if not depth_frame or not color_frame:
            continue
        # Convert images to numpy arrays
        depth_image = np.asanyarray(depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())
        ir_left_image = np.asanyarray(ir_frame_left.get_data())
        ir_right_image = np.asanyarray(ir_frame_right.get_data())

        # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
        depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)

        # Stack both images horizontally
        #images1 = np.hstack((color_image, depth_colormap))
        #images2 = np.hstack((ir_left_image, ir_right_image))
        # Show images
        #cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
        #cv2.imshow('RealSense', images1)
        cv2.imshow('ori_img', color_image)
        #cv2.imshow("Display pic_irt", images2)

        key = cv2.waitKey(1)
        t = time.time()
        tname = str(t)[5:10]
        # Press d to display and save the image
        if not os.path.exists(IMAGE_DIR):
            os.mkdir(IMAGE_DIR)
        if key & 0xFF == ord('d'):
            cv2.imwrite(IMAGE_DIR + '/' + 'color' + str(tname) + '.png', color_image)
            print("success to save a image in " + str(IMAGE_DIR))
            cv2.imshow('display', color_image)
            time.sleep(1)
            # Press f to break
            while 1:
                key1 = cv2.waitKey(10000)
                if key1 & 0xFF == ord('f'):
                    break
        print("sampling")
        # Press esc or 'q' to close the image window
        if key & 0xFF == ord('q') or key == 27:
            cv2.destroyAllWindows()
            break


finally:
    # Stop streaming
    pipeline.stop()

图片存在当前.py目录下的images文件夹内。

步骤2 使用labelme对拍摄的照片制作标签

安装labelme 3.16.7版本。(本教程只适用于该版本,应为后面要对源码进行相应修改,不同版本源码会有略微不同。)
安装命令为:pip install labelme==3.16.7(最新版本不会产生info.yaml文件,该文件在程序中需要被用来读取类别信息。)
对每个图片中的物体进行标注标签。具体方法不做介绍。然后生成对应的.json文件。
终端运行

labelme_json_to_dataset 2.json

将.json文件转成coco格式,即.png的格式,

生成对应的文件夹。
在这里插入图片描述
每个文件夹里面包含:原始图片,标签图片等。
在这里插入图片描述

注意

我们们需要检查生成的标签文件(label.png文件),看每个文件夹内,物体对应的颜色标签是否相同。由于有的labelme版本原因,其生成的标签没有一一对应,即同一物体会在不同文件中显示不同颜色。我们的目标是不同物体的颜色应该具有相同的颜色标签。这时,找到文件json_to_dataset.py,我的在/home/wuxr/anaconda3/lib/python3.7/site-packages/labelme/cli/json_to_dataset.py

data = json.load(open(json_file))
    imageData = data.get('imageData')

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    # label_name_to_value = {'_background_': 0}
    # for shape in sorted(data['shapes'], key=lambda x: x['label']):
    #     label_name = shape['label']
    #     if label_name in label_name_to_value:
    #         label_value = label_name_to_value[label_name]
    #     else:
    #         label_value = len(label_name_to_value)
    #         label_name_to_value[label_name] = label_value
    label_name_to_value = {'_background_': 0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8}
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
    label_names = ['_background_', '1', '2', '3', '4', '5', '6', '7', '8']
    # label_names = [None] * (max(label_name_to_value.values()) + 1)
    # for name, value in label_name_to_value.items():
    #     label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

,然后修改对应代码

label_name_to_value = {'_background_': 0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8}
lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
label_names = ['_background_', '1', '2', '3', '4', '5', '6', '7', '8']

注意,我的这里为8个类。
修改完成后,再次执行

labelme_json_to_dataset 2.json

生成对应的标签文件夹。

注意:若是想批量生成,可参考下面代码:

import os
def json2data(path):
    filelist = os.listdir(path)
    order_model = 'labelme_json_to_dataset' +' '+path+'/'
    for i in filelist:
        order = order_model + i
        val = os.system(order)
        print(val)

path = '/home/wuxr/下载/my_datasets/label_item'
json2data(path)

来源

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值