From 410613aab64bb9f9fb7edb110165298d5ddff637 Mon Sep 17 00:00:00 2001 From: zerohertz Date: Fri, 24 Mar 2023 11:11:08 +0900 Subject: [PATCH 1/2] :tada: Update: Save Data for Recognition --- README.md | 31 ++++++++------ saveData.py | 116 ++++++++++++++++++++++++++++------------------------ 2 files changed, 81 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index de7dac3..60d0427 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/saveData.py b/saveData.py index 8bcbfad..674f856 100644 --- a/saveData.py +++ b/saveData.py @@ -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') @@ -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) From 8b5de40ce17f411ca5c06ee62f555f5ffc0d3c53 Mon Sep 17 00:00:00 2001 From: zerohertz Date: Fri, 24 Mar 2023 11:12:13 +0900 Subject: [PATCH 2/2] :bento: Update: Check Data for Recognition --- README.md | 4 ++-- checkData.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 60d0427..22a5b63 100644 --- a/README.md +++ b/README.md @@ -58,8 +58,8 @@ LogoRec ```shell Paraent/datasets/MakeData$ python checkData.py ==================== train ==================== -Images: 1866 -Ground Truth: 1866 +Images: 1861 +Ground Truth: 1861 ==================== train ==================== ==================== val ==================== Images: 187 diff --git a/checkData.py b/checkData.py index 14ce083..9985cc0 100644 --- a/checkData.py +++ b/checkData.py @@ -2,7 +2,7 @@ def checkData(target): - os.chdir(init + '/FlickrLogos-v2') + os.chdir(init + '/LogoRec') try: os.chdir('images/' + target) except: @@ -39,7 +39,7 @@ def checkData(target): os.chdir('..') init = os.getcwd() try: - os.chdir(init + '/FlickrLogos-v2') + os.chdir(init + '/LogoRec') except: print('경로가 없어용 ~')