-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
executable file
·52 lines (43 loc) · 1.31 KB
/
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
import os
import tensorflow as tf
import numpy as np
import classes
import glob
import argparse
import sys
import alexnet
import cv2
dropoutPro = 1
classNum = 1000
skip = []
# Get test images from directory
testPath = "TestModel/"
testImg = []
# Function to read hidden files
def listdir_files(path):
return glob.glob(os.path.join(path, '*'))
for f in listdir_files(testPath):
testImg.append(cv2.imread(f))
imgMean = np.array([104, 117, 124], np.float)
x = tf.placeholder("float", [1, 227, 227, 3])
model = alexnet.alexNet(x, dropoutPro, classNum, skip)
score = model.fc3
softmax = tf.nn.softmax(score)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# Loading the model
model.loadModel(sess)
result = []
for i, img in enumerate(testImg):
# image preprocessing
test = cv2.resize(img.astype(float), (227, 227))
test -= imgMean
test = test.reshape((1, 227, 227, 3))
maxx = np.argmax(sess.run(softmax, feed_dict = {x: test}))
res = classes.names[maxx]
print(res)
result.append(res)
font = cv2.FONT_HERSHEY_SCRIPT_SIMPLEX
cv2.putText(img, res, (int(img.shape[0]/3), int(img.shape[1]/3)), font, 1, (0, 0, 255), 2) #putting on the labels
cv2.imshow("demo"+str(i), img)
cv2.waitKey(5000)