-
Notifications
You must be signed in to change notification settings - Fork 48
/
data_preprocess.py
135 lines (114 loc) · 3.61 KB
/
data_preprocess.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#-*- coding:utf-8 -*-
#author:zhangwei
import os
import sys
import numpy as np
import cv2
IMAGE_SIZE = 128
def resize_image(image , height=IMAGE_SIZE , width=IMAGE_SIZE):
top , bottom , left , right = (0 , 0 , 0 , 0)
h , w , channels = image.shape
# print(h , w)
longest_edge = max(h , w)
if h < longest_edge:
dh = longest_edge - h
top = dh // 2
bottom = dh - top
elif w < longest_edge:
dw = longest_edge - w
left = dw // 2
right = dw - left
# print(dw , left , right)
else:
pass
#print(top , bottom , left , right)
BLACK = [0 , 0 , 0]
constant = cv2.copyMakeBorder(image , top , bottom , left , right , cv2.BORDER_CONSTANT , value=BLACK)
return cv2.resize(constant , (height , width))
images = []
labels = []
def read_path(pathname):
for dir_item in os.listdir(pathname):
full_path = os.path.abspath(os.path.join(pathname , dir_item))
if os.path.isdir(full_path):
read_path(full_path)
else:
if dir_item.endswith('.bmp'):
image = cv2.imread(full_path)
image = resize_image(image , IMAGE_SIZE , IMAGE_SIZE)
# cv2.imwrite('/home/zhangwei/1.jpg' , image)
images.append(image)
# print(pathname)
labels.append(pathname)
return images , labels
def load_dataset(pathname):
images , labels = read_path(pathname)
images = np.array(images)
labels = np.array([0 if label.endswith('0') else 1 for label in labels])
return images , labels
if __name__ == '__main__':
pathname = '/home/zhangwei/data/ScanKnife/'
load_dataset(pathname)
'''
def resize_image(imagepath , height=64 , width=64):
# image = cv2.imread(imagepath)
top , bottom , left , right = (0 , 0 , 0 , 0)
h , w , channels = imagepath.shape
# print(image.shape)
# print(h)
longest_edge = max(h , w)
if h < longest_edge:
dh = longest_edge - h
top = dh // 2
bottom = dh - top
# print(top , bottom)
elif w < longest_edge:
dw = longest_edge - w
left = dw // 2
right = dw - left
# print(right)
else:
pass
BLACK = [0 , 0 , 0]
constant = cv2.copyMakeBorder(imagepath , top , bottom , left , right , cv2.BORDER_CONSTANT , value=BLACK)
return cv2.resize(constant , (height , width))
images = []
labels = []
def read_path(pathname):
for i in os.listdir(pathname):
j = os.path.abspath(os.path.join(pathname , i))
# print(pathname)
if os.path.isdir(j):
read_path(j)
else:
if i.endswith('.bmp'):
image = cv2.imread(j)
image = resize_image(image)
# cv2.imwrite('/home/zhangwei/1.bmp' , image)
images.append(image)
# print(pathname)
labels.append(pathname)
return images , labels
def load_data(pathname):
images , labels = read_path(pathname)
images = np.array(images , dtype='float32')
labels_re = []
for label in labels:
if label.endswith('1'):
label = '1'
labels_re.append(label)
elif label.endswith('0'):
label = '0'
labels_re.append(label)
else:
pass
labels_re = np.array(labels_re , dtype='int32')
return images , labels_re
if __name__ == '__main__':
pathname = '/home/zhangwei/data/ScanKnife/'
a , b = load_data(pathname)
# print(a[0] , b[0])
# imagepath = '/home/zhangwei/T005H1D1S002.bmp'
# image = resize_image(imagepath)
# cv2.imwite
'''