-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpig_test.py
102 lines (75 loc) · 2.81 KB
/
pig_test.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
# coding:utf-8
# Bin GAO
import os
import cv2
import glob
import tensorflow as tf
import numpy as np
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--rgb',
type=str,
default='./new_pig1')
parser.add_argument('--model_dir',
type=str,
default='./model_base')
parser.add_argument('--pig_save_dir',
type=str,
default='./result')
parser.add_argument('--pig_save_name',
type=str,
default='333.jpg')
parser.add_argument('--gpu',
type=int,
default=1)
flags = parser.parse_args()
h=1024
w=1024
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
def load_model():
file_meta = os.path.join(flags.model_dir, 'model.ckpt.meta')
file_ckpt = os.path.join(flags.model_dir, 'model.ckpt')
saver = tf.train.import_meta_graph(file_meta)
# tf.GraphKeys.VARIABLES = tf.GraphKeys.GLOBAL_VARIABLES
sess = tf.InteractiveSession()
saver.restore(sess, file_ckpt)
# print(sess.run(tf.get_default_graph().get_tensor_by_name("v1:0")))
return sess
def read_image(image_path, gray=False):
if gray:
return cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
else:
image = cv2.imread(image_path)
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
def main(flags):
sess = load_model()
X, mode = tf.get_collection('inputs')
pred = tf.get_collection('upscore_fuse')[0]
img_list = os.listdir(flags.rgb)
for i in img_list:
if i != '.DS_Store':
img = os.listdir(flags.rgb+'/'+i)
for j in img:
if j != '.DS_Store':
#print(j)
image = read_image(flags.rgb + '/' + i + '/' + j)
origin_shape = image.shape
print(origin_shape[:2])
image=cv2.resize(image,(h,w))
# sess=tf.InteractiveSession()
#img = image.reshape((1, 352, 352, 3))
label_pred = sess.run(pred, feed_dict={X: np.expand_dims(image,0), mode: False})
#print(label_pred)
#result = np.reshape(img,(h/2,w/2,2))
#result = image[:,:,0]
merged = np.squeeze(label_pred)*255
#_,merged = cv2.threshold(merged,200,255,cv2.THRESH_BINARY)
try:
os.mkdir(flags.pig_save_dir + '/' + i)
except Exception as e:
print(e)
#save_name = os.path.join(flags.pig_save_dir + '/' + i, j)
save_name = os.path.join(flags.pig_save_dir + '/' + i,j)
cv2.imwrite(save_name,merged)
if __name__ == '__main__':
main(flags)