亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 anaconda 提示符將批量 Labelme json 文件轉換為一組圖像和標簽

使用 anaconda 提示符將批量 Labelme json 文件轉換為一組圖像和標簽

慕容708150 2022-11-01 15:44:54
我有一個由 Labelme 工具生成為 JSON 文件的圖像掩碼數據集,在 Github 教程(https://github.com/wkentaro/labelme/tree/master/examples/tutorial)上它顯示更改 JSON 文件進入圖像文件,我們使用以下命令行代碼labelme_json_to_dataset apc2016_obj3.json -o apc2016_obj3_json但是這一次只適用于一個文件,所以我一直在嘗試找到一種使用一組代碼處理所有文件的方法,我嘗試了以下代碼setlocalset "yourDir=C:\Users\Acer\Desktop\datasets\combined masks\"set "yourExt=*.json"pushd %yourDir%for %%a in (*%yourExt%)do labelme_json_to_dataset %%a -o %%apopdendlocal該代碼現在通過讀取文件名并添加文件擴展名 .json 來工作,但是,將文件保存到具有相同名稱的目錄(包括 .json 擴展名)會給我以下錯誤Traceback (most recent call last):  File "c:\users\acer\.conda\envs\labelme\lib\runpy.py", line 193, in _run_module_as_main    "__main__", mod_spec)  File "c:\users\acer\.conda\envs\labelme\lib\runpy.py", line 85, in _run_code    exec(code, run_globals)  File "C:\Users\ACER\.conda\envs\labelme\Scripts\labelme_json_to_dataset.exe\__main__.py", line 7, in <module>  File "c:\users\acer\.conda\envs\labelme\lib\site-packages\labelme\cli\json_to_dataset.py", line 65, in main    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))  File "c:\users\acer\.conda\envs\labelme\lib\site-packages\PIL\Image.py", line 2131, in save    fp = builtins.open(filename, "w+b")FileNotFoundError: [Errno 2] No such file or directory: '000828.json\\img.png'我不熟悉 cmd,需要幫助將輸出保存到文件名不帶 .json 擴展名的目錄下面是一個單個文件示例,它顯示了成功運行應該是什么樣子 在此處輸入圖像描述(labelme) C:\Users\ACER\Desktop\datasets\combined masks>labelme_json_to_dataset 000814.json -o 000814[[1m[33mWARNING[0m] [36mjson_to_dataset[0m:[36mmain[0m:[36m15[0m - [1m[33mThis script is aimed to demonstrate how to convert the JSON file to a single image dataset.[0m[[1m[33mWARNING[0m] [36mjson_to_dataset[0m:[36mmain[0m:[36m17[0m - [1m[33mIt won't handle multiple JSON files to generate a real-use dataset.[0m[[1m[37mINFO   [0m] [36mjson_to_dataset[0m:[36mmain[0m:[36m73[0m - [1m[37mSaved to: 000814[0m(labelme) C:\Users\ACER\Desktop\datasets\combined masks>
查看完整描述

4 回答

?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

使用命令FOR /?閱讀substitution of FOR variable references幫助輸出的最后一頁。要獲取文件的基本名稱,%%~na可以使用。在沒有的情況下運行它,ECHO OFF以便您可以看到每個命令。


setlocal

set "yourDir=C:\Users\Acer\Desktop\datasets\combined masks\"

set "yourExt=*.json"

pushd %yourDir%

for %%a in (*%yourExt%) do (labelme_json_to_dataset %%a -o %%~na)

popd

endlocal


查看完整回答
反對 回復 2022-11-01
?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

import labelme

import os, sys

path="path/to/directory"

dirs = os.listdir(path)

i=0

for item in dirs:

   if item.endswith(".json"):

      if os.path.isfile(path+item):

         my_dest ="fin" + str(i)

         os.system("mkdir "+my_dest)

         os.system("labelme_json_to_dataset "+item+" -o "+my_dest)

         i=i+1


查看完整回答
反對 回復 2022-11-01
?
largeQ

TA貢獻2039條經驗 獲得超8個贊

for /l %n in (2,1,50) do 
    (labelme_json_to_dataset images%n.json -o images%n)

每個file.json空白文件夾(之前為保存數據集而準備的)都在同一個文件夾中(使用此代碼的當前目錄)。


查看完整回答
反對 回復 2022-11-01
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

這個解決方案并不完美,只是生成了遮罩和覆蓋 png 文件。它也只是保留文件名。


import argparse

import base64

import json

import os

import os.path as osp


import imgviz

import PIL.Image


from labelme.logger import logger

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


    if args.out is None:

        out_dir = osp.basename(json_file).replace(".", "_")

        out_dir = osp.join(osp.dirname(json_file), out_dir)

    else:

        out_dir = args.out

    if not osp.exists(out_dir):

        os.mkdir(out_dir)


    data = json.load(open(json_file))

    imageData = data.get("imageData")


    if not imageData:

        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])

        print(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

    lbl, _ = utils.labelme_shapes_to_label(img.shape, data["shapes"])


    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 = imgviz.label2rgb(

        lbl, imgviz.asgray(img), label_names=label_names, loc="rb"

    )


    filename = str(json_file).split('.')[1]

    utils.lblsave(osp.join(out_dir, f'.{filename}.png'), lbl)

    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, f'.{filename}_viz.png'))


    logger.info("Saved to: {}".format(out_dir))



if __name__ == "__main__":

    main()


查看完整回答
反對 回復 2022-11-01
  • 4 回答
  • 0 關注
  • 270 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號