Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
🎉 Add: labelme to YOLOv5
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerohertz committed Mar 28, 2023
1 parent facdc54 commit 01c15ac
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 4 deletions.
52 changes: 48 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
Expand Down
99 changes: 99 additions & 0 deletions labelme2YOLOv5.py
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit 01c15ac

Please sign in to comment.