From 01c15ac2b9008c8a68c7fb0431d360ccdfe2a6f8 Mon Sep 17 00:00:00 2001 From: Zerohertz Date: Tue, 28 Mar 2023 21:48:14 +0900 Subject: [PATCH] :tada: Add: labelme to YOLOv5 --- README.md | 52 +++++++++++++++++++++++-- labelme2YOLOv5.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 labelme2YOLOv5.py diff --git a/README.md b/README.md index 67cd1b5..b603901 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,51 @@ +# How to use? + +### labelme2YOLOv5.py + +```shell +Parent +└── datasets + ├── MakeData + │   ├── README.md + │   ├── checkData.py + │   ├── labelme2YOLOv5.py + │   └── saveData.py + └── tmp + ├── data1.jpeg + ├── data1.json + ├── ... +``` + +```python +Parent/datasets/MakeData$ python labelme2YOLOv5.py +100%|█████████████████████████████████████████████| 5/5 [00:00<00:00, 9.03it/s] +============================== +No. Total Data: 5 +============================== +Training Data: No. Images 3 +Training Data: No. GT 3 +Validation Data: No. Images 2 +Validation Data: No. GT 2 +============================== +No. Total Image Data: 5 +No. Total GT Data: 5 +============================== +``` + +> Result + +```shell +labelme +├── images +│   ├── train +│   └── val +└── labels + ├── train + └── val +``` + +### saveData.py + ```shell Parent └── datasets @@ -17,10 +65,6 @@ Parent └── saveData.py ``` -# How to use? - -### saveData.py - ```shell Parent/datasets/MakeData$ python saveData.py 100%|████████████████████████████████████████████████████████████████████████| 2235/2235 [00:45<00:00, 49.47it/s] diff --git a/labelme2YOLOv5.py b/labelme2YOLOv5.py new file mode 100644 index 0000000..5b7f1d6 --- /dev/null +++ b/labelme2YOLOv5.py @@ -0,0 +1,99 @@ +import os +import shutil + +import csv +import json + +from tqdm import tqdm +import cv2 + + +def initializeData(DataStoreName): + tmp = os.getcwd() + if DataStoreName in os.listdir(): + shutil.rmtree(DataStoreName) + os.mkdir(DataStoreName) + os.chdir(DataStoreName) + os.mkdir('images') + os.chdir('images') + os.mkdir('train') + os.mkdir('val') + os.chdir('..') + os.mkdir('labels') + os.chdir('labels') + os.mkdir('train') + os.mkdir('val') + os.chdir(tmp) + +def labelme2YOLOv5(target, relJson): + targetDirImg = DataStoreDir + '/images/' + target + targetDirLab = DataStoreDir + '/labels/' + target + relImage = relJson.replace('.json', '.jpeg') + # Make Target Data: IMG + shutil.copy(relImage, targetDirImg + '/' + relImage) + # Make Target Data: Poly (GT) + h, w, _ = cv2.imread(relImage).shape + with open(relJson) as f: + data = json.load(f) + tmp = data['shapes'][0]['points'] + tar = [] + cnt = 0 + for j in tmp: + for k in j: + if cnt % 2 == 0: + tar.append(k/w) + else: + tar.append(k/h) + with open(targetDirLab + '/' + relJson.replace('.json', '.txt'), 'a', encoding='utf-8') as f: + wr = csv.writer(f, delimiter=' ') + wr.writerow([0, *tar]) + +if __name__ == "__main__": + DataStoreName = "labelme" + os.chdir('..') + initializeData(DataStoreName) + + datasets = os.getcwd() + DataStoreDir = datasets + '/' + DataStoreName + + os.chdir('tmp') + + candidate = [] + for i in os.listdir(): + if '.json' in i: + candidate.append(i) + + cnt = 0 + for c in tqdm(candidate): + if cnt % 3 == 0: + labelme2YOLOv5('val', c) + else: + labelme2YOLOv5('train', c) + cnt += 1 + + print('=' * 30) + print('No. Total Data: ', cnt) + print('=' * 30) + + os.chdir(DataStoreDir + '/images/train') + tin = len(os.listdir()) + print('Training Data: No. Images', tin) + + os.chdir(DataStoreDir + '/labels/train') + ttn = len(os.listdir()) + print('Training Data: No. GT', ttn) + + os.chdir(DataStoreDir + '/images/val') + vin = len(os.listdir()) + print('Validation Data: No. Images', vin) + + os.chdir(DataStoreDir + '/labels/val') + vtn = len(os.listdir()) + print('Validation Data: No. GT', vtn) + + print('=' * 30) + print('No. Total Image Data: ', tin + vin) + print('No. Total GT Data: ', ttn + vtn) + print('=' * 30) + + os.chdir(datasets + '/MakeData') \ No newline at end of file