-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdataprocess.py
45 lines (40 loc) · 1.37 KB
/
dataprocess.py
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
# Preprocess the data, split the images(in "images" folder) into training dataset and test dataset
# accoring to the tag in "train_test_split.txt"
import pandas
import numpy as np
import os
import sys
import glob
import argparse
import matplotlib.pyplot as plt
from PIL import Image
IM_SIZE = (299, 299)
data = np.loadtxt("train_test_split.txt", delimiter=' ')
data = np.array(data, dtype=int)
imglist = np.genfromtxt('images.txt',delimiter=' ',dtype='str')
X_train = []
y_train = []
X_test=[]
y_test=[]
for imgid, flag in data:
if flag == 0:
X_train.append('images/'+ imglist[imgid-1, 1])
y_train.append(imglist[imgid-1, 1].split("/",1)[0] )
else:
X_test.append('images/' + imglist[imgid-1, 1])
y_test.append(imglist[imgid-1, 1].split("/",1)[0] )
# save the images to train dataset and test dataset
for imgpath in X_train:
im = Image.open(imgpath)
#im.thumbnail(IM_SIZE)
savepath = 'train_images/'+ imgpath.split("/",1)[1].split("/",1)[0]
if not os.path.exists(savepath):
os.makedirs(savepath)
im.save(savepath + '/'+ imgpath.split("/",1)[1].split("/",1)[1])
for imgpath in X_test:
im = Image.open(imgpath)
#im.thumbnail(IM_SIZE)
savepath = 'test_images/'+ imgpath.split("/",1)[1].split("/",1)[0]
if not os.path.exists(savepath):
os.makedirs(savepath)
im.save(savepath + '/'+ imgpath.split("/",1)[1].split("/",1)[1])