-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetData.py
More file actions
110 lines (88 loc) · 2.58 KB
/
setData.py
File metadata and controls
110 lines (88 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import random
import os
import shutil
import tqdm
import cv2
import numpy as np
import matplotlib.pyplot as plt
images_path = 'dataset/JPEGImages/'
annotations_path = 'dataset/Annotations_txt/'
path = 'dataset/'
# 만약 폴더가 없으면 주석 풀고 실행.
os.mkdir('train')
os.mkdir('train/images')
os.mkdir('train/labels')
os.mkdir('test')
os.mkdir('test/images')
os.mkdir('test/labels')
def convert(size, x, y, w, h):
box = np.zeros(4)
dw = 1./size[0]
dh = 1./size[1]
x = x/dw
w = w/dw
y = y/dh
h = h/dh
box[0] = x-(w/2.0)
box[1] = x+(w/2.0)
box[2] = y-(h/2.0)
box[3] = y+(h/2.0)
return (box)
def plot_annotations(img, filename):
fig, ax = plt.subplots()
with open(annotations_path+filename, 'r') as f:
for line in f:
value = line.split()
cls = int(value[0])
x = float(value[1])
y = float(value[2])
w = float(value[3])
h = float(value[4])
img_h, img_w = img.shape[:2]
bb = convert((img_w, img_h), x, y, w, h)
cv2.rectangle(img, (int(round(bb[0])), int(round(bb[2]))), (int(round(bb[1])), int(round(bb[3]))), (255, 0, 0), 2)
plt.imshow(img)
ax.axis('off')
plt.show()
plt.figure(figsize=(20,12))
ls = os.listdir(images_path)
c = 1
for i in random.sample(ls, 10):
img = plt.imread(images_path+i)
i = i.rstrip('.jpg') + '.txt'
plt.subplot(2, 5, c)
plot_annotations(img, i)
c += 1
train = []
with open(path+'ImageSets/Main/trainval.txt', 'r') as f:
for line in f.readlines():
if line[-1] == '\n':
line = line[:-1]
train.append(line)
test = []
with open(path+'ImageSets/Main/test.txt', 'r') as f:
for line in f.readlines():
if line[-1] == '\n':
line = line[:-1]
test.append(line)
print(len(train))
print(len(test))
train_path = 'train/'
test_path = 'test/'
print('Copying Train Data..!!')
for i in tqdm.tqdm(train):
a = shutil.copyfile(images_path+i+'.jpg', train_path+'images/'+i+'.jpg')
a = shutil.copyfile(annotations_path+i+'.txt', train_path+'labels/'+i+'.txt')
print('Copying Test Data..!!')
for i in tqdm.tqdm(test):
a = shutil.copyfile(images_path+i+'.jpg', test_path+'images/'+i+'.jpg')
a = shutil.copyfile(annotations_path+i+'.txt', test_path+'labels/'+i+'.txt')
text = """
train: /kaggle/working/train
val: /kaggle/working/test
# number of classes
nc: 10
# class names
names: ['sunglass','hat','jacket','shirt','pants','shorts','skirt','dress','bag','shoe']"""
with open("data.yaml", 'w') as file:
file.write(text)