labelme标注医学图像之后批量把json文件转化PNG

本文介绍了如何使用Python脚本批量将json文件归类到指定目录,并在LabelMe环境中执行shell脚本来转换json为图片标注。适合有Anaconda和LabelMe环境的开发者进行数据预处理工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、把json都放在同一目录下

这里写个py即可

#!/usr/bin/env python
# coding : utf-8

import os
import shutil

absolute_path = os.path.dirname(__file__) #获取本文件的绝对路径
absolute_path = absolute_path[:-len('/scripts')]
all_file_name = os.listdir(os.path.join(absolute_path,'image_dataset'))
json_file = [] #存储json文件名字
for i in all_file_name:
    if i[-4:] == 'json':
        json_file.append(i)
print(json_file)
for i in json_file:
    shutil.copy(os.path.join(absolute_path+'/image_dataset',i),os.path.join(absolute_path,'image_dataset_label'))

自己根据自己的路径做一些修改。

2、编写一个shell脚本

shell脚本的作用是利用labelme_json_to_dataset这个程序(它是anaconda创建labelme环境时自带的,所以后面需要在anaconda的labelme环境下执行shell)

#!/bin/bash
echo "Now begin to search json file..."
for file in ./*
do
    if [ "${file##*.}"x = "json"x ]
    then
    filename=`basename $file`
    temp_filename=`basename $file  .json`
    suf=_json
    new_filename=${temp_filename}${suf}
#    echo $new_filename
    cmd="labelme_json_to_dataset ${filename} -o ${new_filename}"
    eval $cmd
    fi
#    printf "no!\n "
done

这里参考了here
自己根据自己的路径做一些修改。

3、打开anaconda环境

在win下搜索anaconda Prompt,进入之后是这样
在这里插入图片描述

4、在labelme环境下执行shell

在这里插入图片描述
即输入 json2png.sh即可。

写在最后:以上基于安装好anaconda,并且创建了一个叫做labelme的环境,在这个环境下安装了labelme,如果环境名称不叫labelme,自行在命令中替换即可。

可以使用labelme的官方工具labelme2voc将json文件转化为VOC格式的标注文件,然后使用VOC格式的标注文件生成灰度的png图像。 具体步骤如下: 1. 安装labelme2voc 可以使用pip安装: ``` pip install labelme2voc ``` 2. 将json文件转化为VOC格式的标注文件 使用labelme2voc命令将json文件转化为VOC格式的标注文件: ``` labelme2voc input_json_dir output_voc_dir ``` 其中,input_json_dir是存放json文件的目录路径,output_voc_dir是输出VOC格式标注文件的目录路径。 例如,有一个json文件存放在/home/user/data/json目录下,要将其转化为VOC格式标注文件并存放在/home/user/data/voc目录下,可以执行以下命令: ``` labelme2voc /home/user/data/json /home/user/data/voc ``` 执行完毕后,会在输出目录下生成与json文件对应的VOC格式标注文件。 3. 将VOC格式标注文件生成灰度的png图像 可以使用VOC格式标注文件生成灰度的png图像的工具有很多,例如opencv、PIL等。 以opencv为例,可以使用以下代码将VOC格式标注文件生成灰度的png图像: ``` import cv2 import os input_voc_dir = "/home/user/data/voc" output_gray_dir = "/home/user/data/gray" if not os.path.exists(output_gray_dir): os.makedirs(output_gray_dir) for filename in os.listdir(input_voc_dir): if filename.endswith(".xml"): xml_file = os.path.join(input_voc_dir, filename) img_file = xml_file.replace(".xml", ".jpg") img = cv2.imread(img_file) h, w = img.shape[:2] gray = np.zeros((h, w), np.uint8) tree = ET.parse(xml_file) for obj in tree.findall("object"): name = obj.find("name").text bndbox = obj.find("bndbox") xmin = int(bndbox.find("xmin").text) ymin = int(bndbox.find("ymin").text) xmax = int(bndbox.find("xmax").text) ymax = int(bndbox.find("ymax").text) gray[ymin:ymax, xmin:xmax] = 255 if name == "target" else 128 gray_file = os.path.join(output_gray_dir, filename.replace(".xml", ".png")) cv2.imwrite(gray_file, gray) ``` 其中,input_voc_dir是存放VOC格式标注文件的目录路径,output_gray_dir是输出灰度png图像的目录路径。 该代码会遍历目录下所有的xml文件(即VOC格式标注文件),根据标注信息生成相应的灰度的png图像,并存放在输出目录下。其中,目标区域的像素值为255,非目标区域的像素值为128。 执行完毕后,会在输出目录下生成与VOC格式标注文件对应的灰度png图像。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千里飞刀客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值