我想使用
labelbox_json2yolo.py
(https://github.com/ultralytics/JSON2YOLO/blob/) 将 *json 中的标签文件转换为 YOLO * txt 并使用类 ('bsb','wsb') Jupyter Notebook 中的 master/labelbox_json2yolo.py)。
在我的数据(图像和标签文件)中,我有:
file = "https://raw.githubusercontent.com/Leprechault/trash/main/YT_EMBRAPA_002.zip" # Patch to zip file in GitHub
import json
import os
from pathlib import Path
import requests
import yaml
from PIL import Image
from tqdm import tqdm
from utils import make_dirs
def convert(file, zip=True):
# Convert Labelbox JSON labels to YOLO labels
names = ['bsb','wsb'] # class names
file = Path(file)
save_dir = make_dirs(file.stem)
with open(file) as f:
data = json.load(f) # load JSON
for img in tqdm(data, desc=f'Converting {file}'):
im_path = img['Labeled Data']
im = Image.open(requests.get(im_path, stream=True).raw if im_path.startswith('http') else im_path) # open
width, height = im.size # image size
label_path = save_dir / 'labels' / Path(img['External ID']).with_suffix('.txt').name
image_path = save_dir / 'images' / img['External ID']
im.save(image_path, quality=95, subsampling=0)
for label in img['Label']['objects']:
# box
top, left, h, w = label['bbox'].values() # top, left, height, width
xywh = [(left + w / 2) / width, (top + h / 2) / height, w / width, h / height] # xywh normalized
# class
cls = label['value'] # class name
if cls not in names:
names.append(cls)
line = names.index(cls), *xywh # YOLO format (class_index, xywh)
with open(label_path, 'a') as f:
f.write(('%g ' * len(line)).rstrip() % line + '\n')
# Save dataset.yaml
d = {'path': f"../datasets/{file.stem} # dataset root dir",
'train': "images/train # train images (relative to path) 128 images",
'val': "images/val # val images (relative to path) 128 images",
'test': " # test images (optional)",
'nc': len(names),
'names': names} # dictionary
with open(save_dir / file.with_suffix('.yaml').name, 'w') as f:
yaml.dump(d, f, sort_keys=False)
# Zip
if zip:
print(f'Zipping as {save_dir}.zip...')
os.system(f'zip -qr {save_dir}.zip {save_dir}')
print('Conversion completed successfully!')
if __name__ == '__main__':
convert('export-2021-06-29T15_25_41.934Z.json')
尽管正确安装了
utils
,但当我尝试运行脚本时,出现以下错误:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[6], line 10
7 from PIL import Image
8 from tqdm import tqdm
---> 10 from utils import make_dirs
ImportError: cannot import name 'make_dirs' from 'utils' (C:\Users\fores\anaconda3\lib\site-packages\utils\__init__.py)
是否可以为我的机器中的本地目录提供替代方法而不调用
make_dirs(file.stem)
?
请问有什么帮助吗?
我在
makedirs()
中找不到utils
。
不过我能够在
os
中找到它:
from os import makedirs
您是否试图在错误的包/模块中查找它?
大卫
您应该检查 repo 的其余部分,有一个 utils.py 是
from utils import make_dirs
所指的。