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

Commit

Permalink
🎉 Update: Save Data for Recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerohertz committed Mar 24, 2023
1 parent f6b2b58 commit 410613a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 66 deletions.
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
```shell
Parent
└── datasets
├── FlickrLogos-v2
│ ├── classes
│ │ ├── jpg
│ │ ├── masks
│ │ └── thumbnails
│ └── scripts
├── FlickrLogos_47
│ ├── scripts
│ ├── test
│ │ ├── 000000
│ │ ├── 000001
│ │ └── 000002
│ └── train
│ ├── 000000
│ ├── 000001
│ ├── 000002
│ └── no-logo
└── MakeData
├── checkData.py
└── saveData.py
Expand All @@ -18,26 +23,26 @@ Parent

```shell
Paraent/datasets/MakeData$ python saveData.py
100%|████████████████████████████████████████████████████████████████| 33/33 [00:26<00:00, 1.27it/s]
100%|████████████████████████████████████████████████████████████████████████| 2235/2235 [00:45<00:00, 49.47it/s]
==============================
No. Total Data: 2240
No. Total Data: 2235
==============================
Training Data: No. Images 1866
Training Data: No. GT 1866
Training Data: No. Images 1861
Training Data: No. GT 1861
Validation Data: No. Images 187
Validation Data: No. GT 187
Test Data: No. Images 187
Test Data: No. GT 187
==============================
No. Total Image Data: 2240
No. Total GT Data: 2240
No. Total Image Data: 2235
No. Total GT Data: 2235
==============================
```

> Result
```shell
FlickrLogos-v2
LogoRec
├── images
│ ├── test
│ ├── train
Expand Down
116 changes: 63 additions & 53 deletions saveData.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,12 @@
import cv2


def initializeData():
def initializeData(DataStoreName):
tmp = os.getcwd()
try:
os.chdir('FlickrLogos-v2')
except Exception as e:
print('There is no FlickrLogos-v2')
print(e)
os.chdir(tmp)
return
try:
shutil.rmtree('images')
except:
pass
try:
shutil.rmtree('labels')
except:
pass
if DataStoreName in os.listdir():
shutil.rmtree(DataStoreName)
os.mkdir(DataStoreName)
os.chdir(DataStoreName)
os.mkdir('images')
os.chdir('images')
os.mkdir('train')
Expand All @@ -39,72 +28,93 @@ def initializeData():
os.mkdir('test')
os.chdir(tmp)


def saveData(target, brand, img):
targetDirImg = datasets + '/FlickrLogos-v2/images/' + target
targetDirLab = datasets + '/FlickrLogos-v2/labels/' + target
absDataDir = datasets + '/FlickrLogos-v2'
def makePoly(relMask):
tmp = cv2.imread(relMask)
mh, mw, _ = tmp.shape
tmp = cv2.cvtColor(tmp, cv2.COLOR_BGR2GRAY)
contours, _ = cv2.findContours(tmp, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
tmp = np.array(contours[0]/[mw, mh])
poly = tmp.reshape(len(contours[0])*2)
return poly

def saveData(target, relImg):
targetDirImg = DataStoreDir + '/images/' + target
targetDirLab = DataStoreDir + '/labels/' + target
imgName = relImg[-13:]
# Make Target Data: IMG
absImgDir = absDataDir + '/classes/jpg/' + brand + '/' + img
shutil.copy(absImgDir, targetDirImg + '/' + img)
# Make Target Data: Bbox (GT)
absBboxDir = absDataDir + '/classes/masks/' + brand.lower() + '/' + img + '.bboxes.txt'
mh, mw, _ = cv2.imread(absImgDir).shape
bbox = pd.read_csv(absBboxDir, sep=' ')
with open(targetDirLab + '/' + img[:-4] + '.txt', 'a', encoding='utf-8') as f:
shutil.copy(relImg, targetDirImg + '/' + imgName)
# Make Target Data: Poly (GT)
gt = relImg.replace('.png', '.gt_data.txt')
gt = pd.read_csv(gt, sep=' ', header=None)
with open(targetDirLab + '/' + imgName.replace('.png', '.txt'), 'a', encoding='utf-8') as f:
wr = csv.writer(f, delimiter=' ')
for i in range(len(bbox)):
x, y, width, height = bbox.iloc[i]
wr.writerow([0, (x+width/2)/mw, (y+height/2)/mh, width/mw, height/mh])
for i in range(len(gt)):
mask = gt.iloc[i, 6]
relMask = relImg.replace('.png', '.' + mask + '.png')
poly = makePoly(relMask)
wr.writerow([0, *poly])

if __name__ == "__main__":
DataStoreName = "LogoRec"
os.chdir('..')
initializeData()
initializeData(DataStoreName)

datasets = os.getcwd()

os.chdir(datasets + '/FlickrLogos-v2/classes/jpg')
brands = os.listdir()
DataStoreDir = datasets + '/' + DataStoreName

candidate = []

tmp = "train/filelist-logosonly"
file = open("FlickrLogos_47/" + tmp + ".txt", "r")
while True:
line = file.readline()
if not line:
break
candidate.append('./FlickrLogos_47/train' + line.strip()[1:])

tmp = "test/filelist"
file = open("FlickrLogos_47/" + tmp + ".txt", "r")
while True:
line = file.readline()
if not line:
break
candidate.append('./FlickrLogos_47/test' + line.strip()[1:])

cnt = 0
for brand in tqdm(brands):
if not '.' in brand and not brand == 'no-logo':
os.chdir(datasets + '/FlickrLogos-v2/classes/jpg/' + brand)
for img in os.listdir():
if '.jpg' in img:
if cnt % 12 == 0:
saveData('val', brand, img)
elif cnt % 12 == 1:
saveData('test', brand, img)
else:
saveData('train', brand, img)
cnt += 1
for c in tqdm(candidate):
if cnt % 12 == 0:
saveData('val', c)
elif cnt % 12 == 1:
saveData('test', c)
else:
saveData('train', c)
cnt += 1

print('=' * 30)
print('No. Total Data: ', cnt)
print('=' * 30)

os.chdir(datasets + '/FlickrLogos-v2/images/train')
os.chdir(DataStoreDir + '/images/train')
tin = len(os.listdir())
print('Training Data: No. Images', tin)

os.chdir(datasets + '/FlickrLogos-v2/labels/train')
os.chdir(DataStoreDir + '/labels/train')
ttn = len(os.listdir())
print('Training Data: No. GT', ttn)

os.chdir(datasets + '/FlickrLogos-v2/images/val')
os.chdir(DataStoreDir + '/images/val')
vin = len(os.listdir())
print('Validation Data: No. Images', vin)

os.chdir(datasets + '/FlickrLogos-v2/labels/val')
os.chdir(DataStoreDir + '/labels/val')
vtn = len(os.listdir())
print('Validation Data: No. GT', vtn)

os.chdir(datasets + '/FlickrLogos-v2/images/test')
os.chdir(DataStoreDir + '/images/test')
ttin = len(os.listdir())
print('Test Data: No. Images', ttin)

os.chdir(datasets + '/FlickrLogos-v2/labels/test')
os.chdir(DataStoreDir + '/labels/test')
tttn = len(os.listdir())
print('Test Data: No. GT', tttn)

Expand Down

0 comments on commit 410613a

Please sign in to comment.